From 27b23a3bca388bd86562d51120ae9d554351d614 Mon Sep 17 00:00:00 2001 From: Yash Shah Date: Sat, 17 May 2025 13:43:20 +0200 Subject: [PATCH] En Passant logic --- src/backend/Board.java | 132 +++++++++++++++++++++++++---------------- src/backend/Move.java | 29 +++++---- 2 files changed, 99 insertions(+), 62 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 0659b05..bb2a29a 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -12,12 +12,13 @@ public class Board { private int[] selected = null; //for user touch private ArrayList highlighted = new ArrayList<>(); private ArrayList moveHistory = new ArrayList<>(); + private int[] enPassantTarget = null; public Board(int colNum, int lineNum) { this.width = colNum; this.height = lineNum; this.pieces = new ArrayList<>(); - //TODO + this.enPassantTarget = null; } public Board(Board other) { @@ -26,6 +27,10 @@ public class Board { this.turnNumber = other.turnNumber; this.turnWhite = other.turnWhite; this.selected = other.selected != null ? new int[]{other.selected[0], other.selected[1]} : null; + this.enPassantTarget = other.enPassantTarget != null + ? new int[]{ other.enPassantTarget[0], other.enPassantTarget[1] } + : null; + this.pieces = new ArrayList<>(); for (Piece p : other.pieces) { @@ -44,29 +49,24 @@ public class Board { public int getWidth() { - //TODO return width; } public int getHeight() { - //TODO return height; } public int getTurnNumber() { - //TODO return turnNumber; } public boolean isTurnWhite() { - //TODO return turnWhite; } public void setPiece(boolean isWhite, PieceType type, int x, int y) { Piece piece = new Piece(isWhite, type, x, y); pieces.add(piece); - //TODO } public void populateBoard() { @@ -97,12 +97,11 @@ public class Board { setPiece(false, PieceType.Bishop, 5, 7); setPiece(false, PieceType.Knight, 6, 7); setPiece(false, PieceType.Rook, 7, 7); - //TODO } public void cleanBoard() { pieces.clear(); - //TODO + enPassantTarget = null; } public String toString() { @@ -267,6 +266,12 @@ public class Board { Piece diagRight = getPieceAt(x + 1, forwardY); if (diagRight != null && diagRight.isWhite() != piece.isWhite()) moves.add(new int[]{x + 1, forwardY}); + if (enPassantTarget != null) { + int tx = enPassantTarget[0], ty = enPassantTarget[1]; + if (ty == forwardY && Math.abs(tx - x) == 1) { + moves.add(new int[]{ tx, ty }); + } + } break; case Rook: @@ -490,53 +495,80 @@ public void undoLastMove() { highlighted.clear(); } - public void playMove(Move move) { - Piece piece = move.getPieceMoved(); - if (piece.getType() == PieceType.King && Math.abs(move.getToX() - move.getFromX()) == 2) { - int row = piece.isWhite() ? 0 : 7; - // Kingside castling - if (move.getToX() == 6) { - Piece rook = getPieceAt(7, row); - if (rook != null) { - rook.setPosition(5, row); // rook moves to f1/f8 - } - } - // Queenside castling - else if (move.getToX() == 2) { - Piece rook = getPieceAt(0, row); - if (rook != null) { - rook.setPosition(3, row); // rook moves to d1/d8 - } - } - } +public void playMove(Move move) { + Piece piece = move.getPieceMoved(); + int fromX = move.getFromX(), fromY = move.getFromY(); + int toX = move.getToX(), toY = move.getToY(); - // If a piece is captured, remove it from the board - if (move.getPieceCaptured() != null) { - pieces.remove(move.getPieceCaptured()); - } + // ─── castling ───────────────────────────────────────────────────────── + if (piece.getType() == PieceType.King + && Math.abs(toX - fromX) == 2) { + int row = piece.isWhite() ? 0 : 7; + // kingside + if (toX == 6) { + Piece rook = getPieceAt(7, row); + if (rook != null) rook.setPosition(5, row); + } + // queenside + else { + Piece rook = getPieceAt(0, row); + if (rook != null) rook.setPosition(3, row); + } + } + // ───────────────────────────────────────────────────────────────────── - // Update the moved piece's position - piece.setPosition(move.getToX(), move.getToY()); - - if (piece.getType() == PieceType.Pawn) { - // white promotes on y == height‐1, black on y == 0 - int promotionRank = piece.isWhite() ? height - 1 : 0; - if (piece.getY() == promotionRank) { - piece.setType(PieceType.Queen); - } - } + // ─── en passant capture ───────────────────────────────────────────── + if (piece.getType() == PieceType.Pawn + && enPassantTarget != null + && toX == enPassantTarget[0] + && toY == enPassantTarget[1]) { + // the pawn being captured sits on (toX, fromY) + Piece captured = getPieceAt(toX, fromY); + if (captured != null && captured.getType() == PieceType.Pawn) { + pieces.remove(captured); + move.setPieceCaptured(captured); // so undo can restore it + } + } + // ───────────────────────────────────────────────────────────────────── - // Save move in history for undo - moveHistory.add(move); + // ─── normal capture ───────────────────────────────────────────────── + else if (move.getPieceCaptured() != null) { + pieces.remove(move.getPieceCaptured()); + } + // ───────────────────────────────────────────────────────────────────── - // Switch turn - turnWhite = !turnWhite; - turnNumber++; + // move the piece + piece.setPosition(toX, toY); + + // ─── pawn promotion ────────────────────────────────────────────────── + if (piece.getType() == PieceType.Pawn) { + int promotionRank = piece.isWhite() ? height - 1 : 0; + if (toY == promotionRank) { + piece.setType(PieceType.Queen); + } + } + // ───────────────────────────────────────────────────────────────────── + + // ─── update enPassantTarget ──────────────────────────────────────── + if (piece.getType() == PieceType.Pawn + && Math.abs(toY - fromY) == 2) { + // record the square it jumped over + enPassantTarget = new int[]{ fromX, (fromY + toY) / 2 }; + } else { + enPassantTarget = null; + } + // ───────────────────────────────────────────────────────────────────── + + // record history & advance turn + moveHistory.add(move); + turnWhite = !turnWhite; + turnNumber++; + + // clear selection/highlight + selected = null; + highlighted.clear(); +} - // Clear selection/highlight - selected = null; - highlighted.clear(); - } public int evaluateBoard() { int score = 0; for (Piece piece : pieces) { diff --git a/src/backend/Move.java b/src/backend/Move.java index e79ee04..5a7c431 100644 --- a/src/backend/Move.java +++ b/src/backend/Move.java @@ -4,21 +4,26 @@ public class Move { private final Piece pieceMoved; private final int fromX, fromY; private final int toX, toY; - private final Piece pieceCaptured; + private Piece pieceCaptured; // ← removed 'final' here public Move(Piece pieceMoved, int fromX, int fromY, int toX, int toY, Piece pieceCaptured) { - this.pieceMoved = pieceMoved; - this.fromX = fromX; - this.fromY = fromY; - this.toX = toX; - this.toY = toY; - this.pieceCaptured = pieceCaptured; + this.pieceMoved = pieceMoved; + this.fromX = fromX; + this.fromY = fromY; + this.toX = toX; + this.toY = toY; + this.pieceCaptured = pieceCaptured; } - public Piece getPieceMoved() { return pieceMoved; } - public int getFromX() { return fromX; } - public int getFromY() { return fromY; } - public int getToX() { return toX; } - public int getToY() { return toY; } + public Piece getPieceMoved() { return pieceMoved; } + public int getFromX() { return fromX; } + public int getFromY() { return fromY; } + public int getToX() { return toX; } + public int getToY() { return toY; } public Piece getPieceCaptured() { return pieceCaptured; } + + + public void setPieceCaptured(Piece captured) { + this.pieceCaptured = captured; + } }