2023-03-20 17:41:13 -04:00
|
|
|
package laboratoire4;
|
|
|
|
|
|
|
|
public class Pushed extends Pawn {
|
2023-03-21 18:06:17 -04:00
|
|
|
public Pushed(Player player, int row, int col) {
|
|
|
|
super(player, row, col);
|
2023-03-20 17:41:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-03-21 18:06:17 -04:00
|
|
|
public boolean isMoveValid(Pawn[][] board, PawnMovement movement) {
|
2023-03-20 17:41:13 -04:00
|
|
|
Pawn pusher = null;
|
2023-03-21 18:06:17 -04:00
|
|
|
Pawn to = board[row + direction][col + movement.getMove()];
|
2023-03-20 17:41:13 -04:00
|
|
|
|
|
|
|
if (col > 0 && movement == PawnMovement.RIGHT_DIAGONAL) {
|
2023-03-21 18:06:17 -04:00
|
|
|
pusher = board[row - direction][col - 1];
|
2023-03-20 17:41:13 -04:00
|
|
|
} else if (col < board.length - 1 && movement == PawnMovement.LEFT_DIAGONAL) {
|
2023-03-21 18:06:17 -04:00
|
|
|
pusher = board[row - direction][col + 1];
|
2023-03-20 17:41:13 -04:00
|
|
|
} else if (movement == PawnMovement.STRAIGHT) {
|
2023-03-21 18:06:17 -04:00
|
|
|
pusher = board[row - direction][col];
|
2023-03-20 17:41:13 -04:00
|
|
|
}
|
|
|
|
|
2023-03-22 09:01:39 -04:00
|
|
|
boolean pusherValid = pusher instanceof Pusher && pusher.player == player;
|
|
|
|
boolean destinationValid = to == null || (movement != PawnMovement.STRAIGHT && to.player != this.player);
|
2023-03-20 17:41:13 -04:00
|
|
|
return pusherValid && destinationValid;
|
|
|
|
}
|
2023-03-21 18:06:17 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "Pushed{" +
|
|
|
|
player +
|
|
|
|
", " + col +
|
|
|
|
", " + row +
|
|
|
|
"} ";
|
|
|
|
}
|
2023-03-20 17:41:13 -04:00
|
|
|
}
|