-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.java
More file actions
21 lines (19 loc) · 1.1 KB
/
Copy pathPawn.java
File metadata and controls
21 lines (19 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Pawn extends Piece {
public Pawn(PieceColor color, Position position) {
super(color, position);
}
@Override
public boolean isValidMove(Position newPosition, Piece[][] board) {
int forwardDirection = (color.equals("WHITE")) ? -1 : 1; // White moves up, Black moves down
int rowDiff = newPosition.getRow() - position.getRow() * forwardDirection;
int colDiff = newPosition.getCol() - position.getCol() * forwardDirection;
if(colDiff ==0 && rowDiff == 1 && board[newPosition.getRow()][newPosition.getCol()] == null) {
return true; // Move forward by one square
} else if(colDiff == 0 && rowDiff == 2 && position.getRow() == (color.equals("WHITE") ? 1 : 6) && board[newPosition.getRow()][newPosition.getCol()] == null) {
return true; // Move forward by two squares from starting position
} else if(Math.abs(colDiff) == 1 && rowDiff == 1 && board[newPosition.getRow()][newPosition.getCol()] != null) {
return true; // Capture diagonally
}
return false; // Invalid move
}
}