This commit is contained in:
Yash Shah 2025-05-07 15:42:28 +02:00
parent 2e04e59529
commit 0d64a41fa1
2 changed files with 18 additions and 2 deletions

View File

@ -20,3 +20,6 @@ public class Main {
} }
} }

View File

@ -217,11 +217,24 @@ public class Board {
switch (piece.getType()) { switch (piece.getType()) {
case Pawn: case Pawn:
int forwardY = y + dir; int forwardY = y + dir;
if (getPieceAt(x, forwardY) == null)
// Move one square forward if it's empty
if (isInBounds(x, forwardY) && getPieceAt(x, forwardY) == null) {
moves.add(new int[] {x, forwardY}); moves.add(new int[] {x, forwardY});
// Move two squares forward if at starting position and both squares are empty
int twoForwardY = y + 2 * dir;
boolean atStartingRank = (piece.isWhite() && y == 1) || (!piece.isWhite() && y == 6);
if (atStartingRank && isInBounds(x, twoForwardY) && getPieceAt(x, twoForwardY) == null) {
moves.add(new int[] {x, twoForwardY});
}
}
// Capture diagonally
Piece diagLeft = getPieceAt(x - 1, forwardY); Piece diagLeft = getPieceAt(x - 1, forwardY);
if (diagLeft != null && diagLeft.isWhite() != piece.isWhite()) if (diagLeft != null && diagLeft.isWhite() != piece.isWhite())
moves.add(new int[] {x - 1, forwardY}); moves.add(new int[] {x - 1, forwardY});
Piece diagRight = getPieceAt(x + 1, forwardY); Piece diagRight = getPieceAt(x + 1, forwardY);
if (diagRight != null && diagRight.isWhite() != piece.isWhite()) if (diagRight != null && diagRight.isWhite() != piece.isWhite())
moves.add(new int[] {x + 1, forwardY}); moves.add(new int[] {x + 1, forwardY});