hightlightKinginCheck changes

Change-Id: I7ffb96e6d228a72a198559cc649367b97b968675
This commit is contained in:
charles.duteil 2025-05-18 14:49:59 +02:00
commit c084635905
1 changed files with 23 additions and 7 deletions

View File

@ -500,25 +500,41 @@ public class Board implements Cloneable {
}
public void highlightKingInCheck() {
boolean isWhiteTurn = isTurnWhite();
// Determine the color of the current player
boolean isWhiteTurn = isTurnWhite();
// Find the current player's king piece
Piece king = null;
boolean isTurnWhite = isTurnWhite();
Piece king = null;
for (Piece piece : pieces) {
if (piece.getType() == PieceType.King && piece.isWhite() == isWhiteTurn) {
king = piece;
break;
}
}
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);
ArrayList<Move> opponentMoves = getAllLegalMoves(!isWhiteTurn);
for (Move move : opponentMoves) {
if (move.getToCol() == king.getX() && move.getToRow() == king.getY()) {
highlightedPositions.add(new int[]{king.getX(), king.getY()});
return;
}
}
// Check if any opponent move targets the king's position
for (Move move : opponentMoves) {
if (move.getToCol() == king.getX() && move.getToRow() == king.getY()) {