highlightKingInCheck

This commit is contained in:
charles.duteil 2025-05-15 18:56:01 +02:00
parent 3f5a33dace
commit 8d5e97c5f5
1 changed files with 30 additions and 0 deletions

View File

@ -468,6 +468,36 @@ public class Board implements Cloneable {
this.enPassantVulnerablePawn = null;
}
}
public void highlightKingInCheck() {
// Determine the color of the current player
boolean isWhiteTurn = isTurnWhite();
// Find the current player's king piece
Piece king = null;
for (Piece piece : pieces) {
if (piece.getType() == PieceType.King && piece.isWhite() == isWhiteTurn) {
king = piece;
break;
}
}
if (king == null) {
throw new IllegalStateException("No king found for the current player!");
}
// Get all legal moves of the opponent's pieces
ArrayList<Move> opponentMoves = getAllLegalMoves(!isWhiteTurn);
// Check if any opponent move targets the king's position
for (Move move : opponentMoves) {
if (move.getToCol() == king.getX() && move.getToRow() == king.getY()) {
// If the king is in check, highlight its position
highlightedPositions.add(new int[]{king.getX(), king.getY()});
return; // No need to continue after finding a check
}
}
}
}