diff --git a/OOP_2B1_Project/src/backend/Board.java b/OOP_2B1_Project/src/backend/Board.java index 39e604c..a2ddf1d 100644 --- a/OOP_2B1_Project/src/backend/Board.java +++ b/OOP_2B1_Project/src/backend/Board.java @@ -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 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 + } + } + } }