highlightKingInCheck
This commit is contained in:
parent
3f5a33dace
commit
8d5e97c5f5
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue