Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 棋局中的棋子运动-爪哇_Java_Android_Logic_Chess - Fatal编程技术网

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))))

您需要自己更正工件捕获声明。确保在条件之间使用正确的括号。

调试过吗?