国际象棋游戏中的典当运动 - Java(Pawn Movement in Chess Game - Java)
我正在创建的国际象棋应用程序中的运动存在问题。 以下是检查移动是否有效的方法:
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; }以下是我得到的一些输出:
此举不应该有效,但它允许移动到那个方块。 我想我上面发布的方法存在问题。
There is an issue with the movement in the chess app I am creating. Here is the method that checks if a move is valid:
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; }Here are some outputs that I am getting:
This move should not be valid, but yet it allows to move to that square. I am thinking there is an issue with the method I posted above.
最满意答案
我想你的&&和|| 在优先级/顺序方面存在冲突。
也许:
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)我没有游戏所以我不能尝试但是......
I think your && and || are conflicting in priorities/order.
Maybe:
if (p == null && this.col == col && (this.row + direction) == row || this.row + 2 * direction == row && ! this.hasMoved)Should be:
if (p == null && this.col == col && ((this.row + direction) == row || this.row + 2 * direction == row) && ! this.hasMoved)I don't have a game so I cannot try it but...
更多推荐
发布评论