Final edit

This commit is contained in:
yohanmontagne 2025-05-22 10:18:59 +02:00
parent dd8b55e2a3
commit 421d7f49bd
2 changed files with 21 additions and 25 deletions

Binary file not shown.

View File

@ -362,46 +362,42 @@ public class Board implements Cloneable {
boolean wasThisMoveAnEnPassantCapture = false;
// --- Capture Logic ---
// 1. Check if THIS move is an en passant capture
if (pieceToMove.getType() == PieceType.Pawn &&
move.getToCol() != move.getFromCol() && // Moving diagonally
getPieceAt(move.getToCol(), move.getToRow()) == null && // Destination is empty
move.getCapturedPiece() != null && // Move object has a "capturedPiece"
move.getToCol() != move.getFromCol() &&
getPieceAt(move.getToCol(), move.getToRow()) == null &&
move.getCapturedPiece() != null &&
move.getCapturedPiece().getType() == PieceType.Pawn &&
move.getCapturedPiece() == this.enPassantVulnerablePawn && // It IS the vulnerable pawn
this.enPassantVulnerablePawn.getX() == move.getToCol() && // Vuln. pawn X matches target X
this.enPassantVulnerablePawn.getY() == move.getFromRow()) { // Vuln. pawn Y matches attacking pawn's original Y
move.getCapturedPiece() == this.enPassantVulnerablePawn &&
this.enPassantVulnerablePawn.getX() == move.getToCol() &&
this.enPassantVulnerablePawn.getY() == move.getFromRow()) {
wasThisMoveAnEnPassantCapture = true;
Piece actualPawnToCaptureForEP = move.getCapturedPiece(); // This is enPassantVulnerablePawn
Piece actualPawnToCaptureForEP = move.getCapturedPiece();
if (pieces.contains(actualPawnToCaptureForEP)) {
pieces.remove(actualPawnToCaptureForEP);
}
} else {
// 2. Not an en passant, check for a normal capture at the destination square
Piece pieceAtDestination = getPieceAt(move.getToCol(), move.getToRow());
if (pieceAtDestination != null) { // If there's any piece at the destination
if (pieceAtDestination.isWhite() != pieceToMove.isWhite()) { // It's an opponent's piece
// Optional: Consistency check with move.getCapturedPiece()
if (pieceAtDestination != null) {
if (pieceAtDestination.isWhite() != pieceToMove.isWhite()) {
if (move.getCapturedPiece() != null && move.getCapturedPiece() != pieceAtDestination) {
System.err.println("Warning: Normal capture - Move.capturedPiece mismatch with piece at destination.");
}
pieces.remove(pieceAtDestination);
} else {
// Trying to move onto your own piece (should be caught by getLegalMoves ideally)
System.err.println("Invalid move: cannot move onto or capture own piece.");
return;
}
} else if (move.getCapturedPiece() != null) {
// Destination is empty, but Move object has a capturedPiece (and it wasn't an EP).
// This signals an issue in how the Move object was created by getLegalMoves for a non-capturing move.
System.err.println("Warning: Move object suggests capture to an empty square (not EP).");
}
}
// --- End of Capture Logic ---
// Remove the piece from its original location
pieces.remove(pieceToMove);
@ -419,20 +415,20 @@ public class Board implements Cloneable {
}
}
// Add the moved piece (or unpromoted pawn) to its new position
Piece newPositionedPiece = makeNewPiece(pieceToMove.getType(), pieceToMove.isWhite(), move.getToCol(), move.getToRow());
pieces.add(newPositionedPiece);
// Update turn information
turnIsWhite = !turnIsWhite;
turnNumber++;
// this.lastMoveSourceSquare = new int[]{move.getFromCol(), move.getFromRow()}; // For graying out feature
// Update enPassantVulnerablePawn state for the NEXT player's turn
if (wasThisMoveAnEnPassantCapture) {
this.enPassantVulnerablePawn = null; // EP capture consumes the vulnerability
this.enPassantVulnerablePawn = null;
} else {
// Check if the move just made (newPositionedPiece) creates a new EP vulnerability
updateEnPassantStateAfterMove(move, newPositionedPiece);
}