handling the new class special moves with the new parameters

This commit is contained in:
Jérôme BEDIER 2025-05-21 21:33:30 +02:00
parent 07a9040077
commit 661d5980c7
1 changed files with 43 additions and 29 deletions

View File

@ -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) {