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