【问题标题】:Pawn Movement in Chess Game - Java国际象棋游戏中的棋子移动 - Java
【发布时间】:2015-07-16 16:50:41
【问题描述】:

我正在创建的国际象棋应用程序中的移动存在问题。这是检查移动是否有效的方法:

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

这是我得到的一些输出:

这个移动应该是无效的,但它允许移动到那个方格。我认为我上面发布的方法有问题。

【问题讨论】:

  • 你调试了吗?

标签: java android logic chess


【解决方案1】:

我认为您的 &&|| 在优先级/顺序上存在冲突。

也许:

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)

我没有游戏,所以无法尝试,但是...

【讨论】:

    【解决方案2】:

    您需要确保 this.hasMoved 仅与 +2 方向测试相关联。否则,除了初始移动之外的所有内容都将无效。这应该适用于您的第一个 if 语句:

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

    您需要自行更正计件说明。确保在条件句之间使用正确的括号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多