diff --git a/OOP_3B6_Project/src/backend/Board.java b/OOP_3B6_Project/src/backend/Board.java index 8a8ca0d..51f5839 100644 --- a/OOP_3B6_Project/src/backend/Board.java +++ b/OOP_3B6_Project/src/backend/Board.java @@ -352,188 +352,3 @@ public class Board { } - -//Add these methods to your Board class - -/** -* Checks if the king of the specified color is in check -* @param isWhiteKing true if checking for white king, false for black king -* @return true if the king is in check, false otherwise -*/ -public boolean isKingInCheck(boolean isWhiteKing) { - // First, find the king - Piece king = null; - for (Piece p : pieces) { - if (p.getType() == PieceType.King && p.isWhite() == isWhiteKing) { - king = p; - break; - } - } - - if (king == null) { - return false; // No king found (shouldn't happen in a valid game) - } - - // Check if any opponent piece can capture the king - for (Piece p : pieces) { - if (p.isWhite() != isWhiteKing) { // Opponent's piece - ArrayList moves = computeLegalMoves(p); - for (int[] move : moves) { - if (move[0] == king.getX() && move[1] == king.getY()) { - return true; // King is in check - } - } - } - } - - return false; -} - -public boolean isCheckmate() { - boolean currentPlayerIsWhite = isTurnWhite(); - - // First, check if the king is in check - if (!isKingInCheck(currentPlayerIsWhite)) { - return false; // Not in check, so definitely not in checkmate - } - - // Try every possible move for the current player - for (Piece p : pieces) { - if (p.isWhite() == currentPlayerIsWhite) { - ArrayList moves = computeLegalMoves(p); - for (int[] move : moves) { - // Create a copy of the board and try the move - Board tempBoard = new Board(this); - - // Create and play the move - Piece capturedPiece = null; - for (Piece target : tempBoard.getPieces()) { - if (target.getX() == move[0] && target.getY() == move[1]) { - capturedPiece = target; - break; - } - } - - Move testMove = new Move( - p.getX(), p.getY(), - move[0], move[1], - p.isWhite(), p.getType(), - capturedPiece - ); - - tempBoard.playMove(testMove); - - // Check if the king is still in check after the move - if (!tempBoard.isKingInCheck(currentPlayerIsWhite)) { - return false; // Found a move that escapes check - } - } - } - } - - // If we've tried all moves and none escape check, it's checkmate - return true; -} - -/** -* Modified userTouch method to check for checkmate after each move -*/ -public void userTouch(int x, int y) { - Piece clickedPiece = getPieceAt(x, y); - // No selection yet - if (selectedX == null || selectedY == null) { - if (clickedPiece != null && clickedPiece.isWhite() == isWhiteTurn) { - selectedX = x; - selectedY = y; - highlightedSquares = computeLegalMoves(clickedPiece); - } - return; - } - // Click again to unselect - if (selectedX == x && selectedY == y) { - selectedX = null; - selectedY = null; - highlightedSquares.clear(); - return; - } - // Try to move - for (int[] move : highlightedSquares) { - if (move[0] == x && move[1] == y) { - Piece selectedPiece = getPieceAt(selectedX, selectedY); - Piece captured = getPieceAt(x, y); - // Store move for undo - Move m = new Move( - selectedX, selectedY, x, y, - selectedPiece.isWhite(), selectedPiece.getType(), captured - ); - moveHistory.push(m); - removePieceAt(x, y); // capture - removePieceAt(selectedX, selectedY); - pieces.add(new Piece(x, y, selectedPiece.isWhite(), selectedPiece.getType())); - turnNumber++; - isWhiteTurn = !isWhiteTurn; - - // Check for checkmate after the move - if (isCheckmate()) { - // Use a JOptionPane to show a popup message - javax.swing.JOptionPane.showMessageDialog( - null, - (isWhiteTurn ? "Black" : "White") + " wins by checkmate!", - "Checkmate", - javax.swing.JOptionPane.INFORMATION_MESSAGE - ); - } - // Also check if the opponent's king is in check (but not checkmate) - else if (isKingInCheck(isWhiteTurn)) { - javax.swing.JOptionPane.showMessageDialog( - null, - "Check!", - "Check", - javax.swing.JOptionPane.INFORMATION_MESSAGE - ); - } - - break; - } - } - selectedX = null; - selectedY = null; - highlightedSquares.clear(); -} - -public void playMove(Move move) { - if (move == null) return; - // Store move for undo - moveHistory.push(move); - // Remove captured piece if any - if (move.getCapturedPiece() != null) { - removePieceAt(move.getToX(), move.getToY()); - } - // Remove piece from original position - removePieceAt(move.getFromX(), move.getFromY()); - // Add piece to new position - pieces.add(new Piece(move.getToX(), move.getToY(), move.isWhite(), move.getType())); - // Update turn - turnNumber++; - isWhiteTurn = !isWhiteTurn; - - // Check for checkmate after the move - if (isCheckmate()) { - // Use a JOptionPane to show a popup message - javax.swing.JOptionPane.showMessageDialog( - null, - (isWhiteTurn ? "Black" : "White") + " wins by checkmate!", - "Checkmate", - javax.swing.JOptionPane.INFORMATION_MESSAGE - ); - } - // Also check if the opponent's king is in check (but not checkmate) - else if (isKingInCheck(isWhiteTurn)) { - javax.swing.JOptionPane.showMessageDialog( - null, - "Check!", - "Check", - javax.swing.JOptionPane.INFORMATION_MESSAGE - ); - } -} \ No newline at end of file