所以,我正在开发一个用于编程的大型项目1.我无法弄清楚如何使用网站上提供的任何其他方法来停止无限循环。以下是涉及移动玩家的所有方法:
public boolean movement(Move m, int moved){
int rowM, colM, row, col;
Player p = m.getPlayer();
if (moved == 0 || moved == 1 || moved == 7)
rowM = -1;
if (moved == 2 || moved == 6)
rowM = 0;
if (moved == 3 || moved == 4 || moved == 5)
rowM = 1;
else
rowM = 0;
if (moved == 5 || moved == 6 || moved == 7)
colM = -1;
if (moved == 0 || moved == 4)
colM = 0;
if (moved == 1 || moved == 2 || moved == 3)
colM = 1;
else
colM = 0;
row = rowM + m.getRow();
col = colM + m.getColumn();
if (!onBoard(row, col) || !getPiece(row, col).opposite(p.getPiece()))
return false;
while ((onBoard(row,col))&&(getPiece(row,col).opposite(p.getPiece()))){
row += rowM;
col += colM;
}
if (onBoard(row, col) && getPiece(row, col).opposite(p.getPiece()))
return true;
return false;
}
private void change(Move m, int moved){
int rowM, colM, row, col;
Player p = m.getPlayer();
if (moved == 0 || moved == 1 || moved == 7)
rowM = -1;
if (moved == 2 || moved == 6)
rowM = 0;
if (moved == 3 || moved == 4 || moved == 5)
rowM = 1;
else
rowM = 0;
if (moved == 5 || moved == 6 || moved == 7)
colM = -1;
if (moved == 0 || moved == 4)
colM = 0;
if (moved == 1 || moved == 2 || moved == 3)
colM = 1;
else
colM = 0;
row = rowM + m.getRow();
col = colM + m.getColumn();
while (getPiece(row, col).opposite(p.getPiece())){
setPiece(p.getPiece(), row, col);
row += rowM;
col += colM;
}
}
public void makeMove(Move move){
Player p = move.getPlayer();
for (int c = 0; c < 8; c++){
if (movement(move, c))
change(move, c);
}
setPiece(p.getPiece(), move.getRow(), move.getColumn());
}
public int handleTurn(Display display){
Move m = display.getMove(this.current);
if (!moveLegal(m)){
display. displayMessage("Illegal Move!");
return 1;
}
makeMove(m);
Player p = getNextPlayer();
if (p == current)
display.displayMessage("Skipped! " + other.getName().toString());
if (gameOver())
return -1;
else {
other = current;
current = p;
}
display.displayStatus(current, "Your Move!");
return 0;
}
答案 0 :(得分:1)
我需要更多的源代码来为您提供准确的答案。但是,我相信我可以帮助你做一些观察。
moved
超出了0
到7
的范围。if
条件永远无法评估
至true
,因为需要评估相同的条件
在循环退出之前false
。基于这些观察,我建议你试试这个版本的运动方法:
public boolean movement(Move m, int moved)
{
int rowM, colM, row, col;
Player p = m.getPlayer();
if (moved == 0 || moved == 1 || moved == 7)
rowM = -1;
else if (moved == 3 || moved == 4 || moved == 5)
rowM = 1;
else // moved == 2 || moved == 6
rowM = 0;
if (moved == 5 || moved == 6 || moved == 7)
colM = -1;
else if (moved == 1 || moved == 2 || moved == 3)
colM = 1;
else // moved == 0 || moved == 4
colM = 0;
row = rowM + m.getRow();
col = colM + m.getColumn();
return onBoard(row, col) && getPiece(row, col).opposite(p.getPiece());
}