diff --git a/src/backend/Board.java b/src/backend/Board.java index 70a0317..b2f1384 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -12,6 +12,8 @@ public class Board { private int selectedY = -1; private boolean[][] highlightedSquares; private Stack undoStack = new Stack<>(); + private int enPassantCol = -1; + private int enPassantRow = -1; public Board(int colNum, int lineNum) { this.width = colNum; // col move x @@ -289,6 +291,21 @@ public class Board { // Captures checkPawnCapture(moves, pawn, x, y, direction, -1); checkPawnCapture(moves, pawn, x, y, direction, 1); + + // En passant captures + if ((pawn.isWhite() && y == 3) || (!pawn.isWhite() && y == 4)) { + // Check both sides + for (int dx : new int[]{-1, 1}) { + int captureX = x + dx; + // If there's a valid en passant opportunity + if (captureX == enPassantCol && y + direction == enPassantRow) { + Move enPassantMove = new Move(x, y, captureX, y + direction); + // Set the captured piece (the pawn that just moved) + enPassantMove.setCapturedPiece(board[captureX][y]); + moves.add(enPassantMove); + } + } + } } private void checkPawnCapture(ArrayList moves, Piece pawn, int x, int y, int dir, int dx) { @@ -403,7 +420,7 @@ public class Board { public Board(Board board) { //TODO - }// test + } public void playMove(Move move) { // Save current state before move for undo @@ -416,11 +433,36 @@ public class Board { piece.x = move.getToX(); piece.y = move.getToY(); + // Reset en passant tracking at the start of each move + enPassantCol = -1; + enPassantRow = -1; + + // Check if this is a pawn making a double move + if (piece.getType() == PieceType.Pawn && + Math.abs(move.getFromY() - move.getToY()) == 2) { + enPassantCol = move.getToX(); + enPassantRow = (move.getFromY() + move.getToY()) / 2; // Middle square + } + if (piece.getType() == PieceType.Pawn && (piece.getY() == 0 || piece.getY() == height - 1)) { board[piece.getX()][piece.getY()] = promotePawn(piece.isWhite(), piece.getX(), piece.getY()); } + // Handle en passant capture + if (piece.getType() == PieceType.Pawn && + move.getToX() != move.getFromX() && + board[move.getToX()][move.getToY()] == null) { + // This is a diagonal pawn move to an empty square - must be en passant + + // Capture the pawn + Piece capturedPawn = board[move.getToX()][move.getFromY()]; + move.setCapturedPiece(capturedPawn); + + // Remove the captured pawn + board[move.getToX()][move.getFromY()] = null; + } + turn++; }