2015-05-06 25 views
1

我正在创建的国际象棋中的运动有一个问题。下面是一个检查,如果一招是有效的方法:国际象棋比赛中的典当运动 - 爪哇

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; 
} 

这里有一些产出,我越来越:

enter image description here

此举不应该是有效的,但还没有它允许移动到那个广场。我在考虑上面发布的方法存在问题。

+0

你有调试过吗? –

回答

0

我认为你的&&||在优先次序/顺序上是冲突的。

可能:

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) 

我没有比赛,所以我不能尝试,但...

0

您需要确保this.hasMoved只仅限于+2方向测试。否则,任何行动将是无效的。这应该适用于您的第一条if语句:

if (p == null && (this.col == col && (((this.row + direction) == row) ||((this.row + (2 * direction)) == row && !this.hasMoved)))) 

您将需要自行更正片段捕获语句。确保在条件之间使用正确的包围。