From 661d5980c79de492fbe56e2c6bfed74c3223f2a3 Mon Sep 17 00:00:00 2001 From: Jerome Bedier Date: Wed, 21 May 2025 21:33:30 +0200 Subject: [PATCH] handling the new class special moves with the new parameters --- src/backend/Board.java | 72 +++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 1b13401..38e23c2 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -447,7 +447,7 @@ public class Board { return x >= 0 && x < width && y >= 0 && y < height; } - private boolean isEmpty(int x, int y) { + public boolean isEmpty(int x, int y) { return isValidPosition(x, y) && board[x][y] == null; } @@ -458,6 +458,32 @@ public class Board { private boolean isAlly(int x, int y, boolean isWhite) { return isValidPosition(x, y) && board[x][y] != null && board[x][y].isWhite() == isWhite; } + + public Piece getPieceAt(int x, int y) { + if (!isValidPosition(x, y)) return null; + return board[x][y]; + } + + public void removePieceAt(int x, int y) { + if (isValidPosition(x, y)) board[x][y] = null; + } + + public void setEnPassantCol(int col) { + this.enPassantCol = col; + } + + public void setEnPassantRow(int row) { + this.enPassantRow = row; + } + + public int getEnPassantCol() { + return this.enPassantCol; + } + + public int getEnPassantRow() { + return this.enPassantRow; + } + public void undoLastMove() { @@ -496,46 +522,34 @@ public class Board { public void playMove(Move move) { // Save current state before move for undo undoStack.push(toFileRep()); - - // existing move logic + Piece piece = board[move.getFromX()][move.getFromY()]; + + // Handle en passant capture + if (SpecialMoves.isEnPassant(this, move)) { + Piece capturedPawn = board[move.getToX()][move.getFromY()]; + move.setCapturedPiece(capturedPawn); + SpecialMoves.handleEnPassant(this, move); + } + + // Move the piece board[move.getToX()][move.getToY()] = piece; board[move.getFromX()][move.getFromY()] = null; piece.x = move.getToX(); piece.y = move.getToY(); - // Reset en passant tracking at the start of each move - enPassantCol = -1; - enPassantRow = -1; + // Update en passant tracking + SpecialMoves.updateEnPassantTracking(this, move, piece); - // Check if this is a pawn making a double move + // Handle pawn promotion if (piece.getType() == PieceType.Pawn && - Math.abs(move.getFromY() - move.getToY()) == 2) { - enPassantCol = move.getToX(); - enPassantRow = (move.getFromY() + move.getToY()) / 2; // Middle square + (piece.getY() == 0 || piece.getY() == height - 1)) { + board[piece.getX()][piece.getY()] = SpecialMoves.promotePawn(piece.isWhite(), piece.getX(), piece.getY()); } - 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; // not working - } - turn++; } + //Finds the position of the king of a given color. public Piece findKing(boolean isWhite) {