From db431b8d0b25277b5d5c2bc09787bc1e4f9a121b Mon Sep 17 00:00:00 2001 From: User Date: Tue, 13 May 2025 15:39:16 +0200 Subject: [PATCH] en passant method --- src/backend/Board.java | 112 ++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 74 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 43e9f8d..d5e853a 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -13,6 +13,7 @@ public class Board { private int turnNumber = 0; private boolean turnWhite = true; private ArrayList highlightedSquares = new ArrayList<>(); + private int[] enPassantTarget = null; // [x,y] coordinates of en passant target square public Board(int colNum, int lineNum) { this.width = colNum; @@ -138,94 +139,37 @@ public class Board { int dir = piece.isWhite() ? 1 : -1; int startRow = piece.isWhite() ? 1 : 6; + // Normal forward moves int oneStep = y + dir; if (inBounds(x, oneStep) && board[x][oneStep] == null) { moves.add(new int[]{x, oneStep}); - // If on start row, try two steps forward + // Two-step move from starting position int twoStep = y + 2 * dir; - if (y == startRow && inBounds(x, twoStep) && board[x][twoStep] == null) { + if (y == startRow && inBounds(x, twoStep) && board[x][twoStep] == null && board[x][oneStep] == null) { moves.add(new int[]{x, twoStep}); } } - // Capture diagonals + // Capture diagonals (including en passant) for (int dx : new int[]{-1, 1}) { int nx = x + dx; int ny = y + dir; - if (inBounds(nx, ny) && board[nx][ny] != null && - board[nx][ny].isWhite() != piece.isWhite()) { - moves.add(new int[]{nx, ny}); - } - } - } - else if (type == PieceType.Rook) { - int[][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}}; - for (int[] d : dirs) { - int nx = x + d[0], ny = y + d[1]; - while (inBounds(nx, ny)) { - if (board[nx][ny] == null) { + if (inBounds(nx, ny)) { + // Normal capture + if (board[nx][ny] != null && board[nx][ny].isWhite() != piece.isWhite()) { moves.add(new int[]{nx, ny}); - } else { - if (board[nx][ny].isWhite() != piece.isWhite()) { + } + // En passant capture + else if (board[nx][ny] == null && enPassantTarget != null && + nx == enPassantTarget[0] && ny == enPassantTarget[1]) { + // Verify there's an opponent pawn beside us + int pawnY = y; // The pawn to capture is beside us, not in front + if (inBounds(nx, pawnY) && board[nx][pawnY] != null && + board[nx][pawnY].getType() == PieceType.Pawn && + board[nx][pawnY].isWhite() != piece.isWhite()) { moves.add(new int[]{nx, ny}); } - break; - } - nx += d[0]; ny += d[1]; - } - } - } - - else if (type == PieceType.Bishop) { - int[][] dirs = {{1,1}, {-1,1}, {-1,-1}, {1,-1}}; - for (int[] d : dirs) { - int nx = x + d[0], ny = y + d[1]; - while (inBounds(nx, ny)) { - if (board[nx][ny] == null) { - moves.add(new int[]{nx, ny}); - } else { - if (board[nx][ny].isWhite() != piece.isWhite()) { - moves.add(new int[]{nx, ny}); - } - break; - } - nx += d[0]; ny += d[1]; - } - } - } - - else if (type == PieceType.Queen) { - // Queen = Rook + Bishop - moves.addAll(getValidMoves(new Piece(x, y, piece.isWhite(), PieceType.Rook))); - moves.addAll(getValidMoves(new Piece(x, y, piece.isWhite(), PieceType.Bishop))); - } - - else if (type == PieceType.Knight) { - int[][] jumps = { - {2,1},{1,2},{-1,2},{-2,1}, - {-2,-1},{-1,-2},{1,-2},{2,-1} - }; - for (int[] j : jumps) { - int nx = x + j[0], ny = y + j[1]; - if (inBounds(nx, ny)) { - if (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite()) { - moves.add(new int[]{nx, ny}); - } - } - } - } - - else if (type == PieceType.King) { - int[][] dirs = { - {1,0}, {-1,0}, {0,1}, {0,-1}, - {1,1}, {-1,1}, {-1,-1}, {1,-1} - }; - for (int[] d : dirs) { - int nx = x + d[0], ny = y + d[1]; - if (inBounds(nx, ny)) { - if (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite()) { - moves.add(new int[]{nx, ny}); } } } @@ -248,7 +192,7 @@ public class Board { selectedX = x; selectedY = y; hasSelectedPiece = true; - highlightedSquares = getValidMoves(clicked); // ✅ Highlight legal moves + highlightedSquares = getValidMoves(clicked); } } else { // Check if clicked again on the same square to unselect @@ -260,12 +204,32 @@ public class Board { else if (isHighlighted(x, y)) { Piece selectedPiece = board[selectedX][selectedY]; + // Check if this is an en passant capture + boolean isEnPassant = selectedPiece.getType() == PieceType.Pawn && + enPassantTarget != null && + x == enPassantTarget[0] && + y == enPassantTarget[1] && + board[x][y] == null; + // Move piece board[x][y] = selectedPiece; board[selectedX][selectedY] = null; selectedPiece.setX(x); selectedPiece.setY(y); + // If en passant, remove the captured pawn + if (isEnPassant) { + int capturedPawnY = selectedY; // The pawn is beside, not in front + board[x][capturedPawnY] = null; + } + + // Set en passant target if pawn moved two squares + enPassantTarget = null; + if (selectedPiece.getType() == PieceType.Pawn && + Math.abs(y - selectedY) == 2) { + enPassantTarget = new int[]{x, (selectedY + y) / 2}; + } + // Update turn turnWhite = !turnWhite; turnNumber++;