Java 棋局中的棋子运动-爪哇
我正在创建的国际象棋应用程序中的移动有问题。以下是检查移动是否有效的方法:Java 棋局中的棋子运动-爪哇,java,android,logic,chess,Java,Android,Logic,Chess,我正在创建的国际象棋应用程序中的移动有问题。以下是检查移动是否有效的方法: public boolean isMove(int row, int col, Pawn[][] board){ Pawn p = board[row][col]; int direction = 1; if (this.color=='w') { direction = -1; } if (p == null && this.col ==
public boolean isMove(int row, int col, Pawn[][] board){
Pawn p = board[row][col];
int direction = 1;
if (this.color=='w') {
direction = -1;
}
if (p == null && this.col == col && ((this.row + direction) == row) || (this.row + 2 * direction) == row && ! this.hasMoved) { //can move
return true;
}
else if (p != null && p.color != this.color && row == (this.row + direction) && (col == (this.col - 1) || col == (this.col + 1))) { // can capture
return true;
}
return false;
}
以下是我得到的一些结果:
这个移动应该是无效的,但是它允许移动到那个正方形。我认为我上面发布的方法存在问题。我认为您的
&&
和|
在优先级/顺序上存在冲突
也许:
if (p == null && this.col == col && (this.row + direction) == row || this.row + 2 * direction == row && ! this.hasMoved)
应该是:
if (p == null && this.col == col && ((this.row + direction) == row || this.row + 2 * direction == row) && ! this.hasMoved)
我没有游戏,所以无法尝试,但…您需要确保
这一点。hasmove
仅与+2方向测试挂钩。否则,除初始移动之外的所有操作都将无效。这应该适用于您的第一个if语句:
if (p == null && (this.col == col && (((this.row + direction) == row) ||((this.row + (2 * direction)) == row && !this.hasMoved))))
您需要自己更正工件捕获声明。确保在条件之间使用正确的括号。调试过吗?