2015-05-06 27 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)))) 

您將需要自行更正片段捕獲語句。確保在條件之間使用正確的包圍。