diff --git a/src/backend/Board.java b/src/backend/Board.java index 60b4055..f88aed7 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -12,6 +12,10 @@ private int width; private int height; private ArrayList Pieces; private Stack moveHistory = new Stack<>(); +private boolean gameOver = false; +private String winnerText = null; +public boolean isGameOver() { return gameOver; } +public String getWinnerText() { return winnerText; } // ── NEW FIELDS FOR userTouch ─────────────────────────────────────── private boolean hasSelection = false; // did we already click to pick up a piece? @@ -315,26 +319,52 @@ public void userTouch(int x, int y) { turnWhite = !turnWhite; hasSelection = false; System.out.println(this); - // 🧠 CHECK & CHECKMATE logic + + // Reset previous check flags checkedKingX = -1; checkedKingY = -1; - checkmateFlag = false; - boolean opponentInCheck = isInCheck(turnWhite); - if (opponentInCheck) { + // 🧠 CHECK detection + boolean opponentColor = !turnWhite; + if (isInCheck(opponentColor)) { + Piece king = getKing(opponentColor); + if (king != null) { + checkedKingX = king.getX(); + checkedKingY = king.getY(); + System.out.println("⚠️ " + (opponentColor ? "White" : "Black") + " king is in CHECK!"); + } + } else { + checkedKingX = -1; + checkedKingY = -1; + } + if (isInCheck(turnWhite)) { Piece king = getKing(turnWhite); if (king != null) { checkedKingX = king.getX(); checkedKingY = king.getY(); - System.out.println("⚠️ " + (turnWhite ? "White" : "Black") + " king is in check!"); + System.out.println("⚠️ " + (turnWhite ? "White" : "Black") + " king is in CHECK!"); } } - if (isCheckmate(turnWhite)) { + // ♟️ CHECKMATE detection + if (captured != null && captured.getType() == PieceType.King) { + winnerText = (captured.isWhite() ? "Black" : "White") + " wins by king capture!"; + gameOver = true; + System.out.println("♟️ " + winnerText); + return; + }if (isCheckmate(turnWhite)) { checkmateFlag = true; - System.out.println("♟️ " + (!turnWhite ? "White" : "Black") + " wins by checkmate!"); + winnerText = (!turnWhite ? "White" : "Black") + " wins by checkmate!"; + gameOver = true; + System.out.println("♟️ " + winnerText); + return; } +else { + checkmateFlag = false; + } + + } } @@ -736,11 +766,45 @@ public boolean isCheckmate(boolean white) { } return true; } -public Piece getKing(boolean white) { +public Piece getKing(boolean isWhite) { for (Piece p : Pieces) { - if (p.getType() == PieceType.King && p.isWhite() == white) return p; + if (p.getType() == PieceType.King && p.isWhite() == isWhite) { + return p; + } } return null; } +public boolean isGameOver() { + // Checkmate + if (isCheckmate(true) || isCheckmate(false)) return true; + + // King capture + boolean whiteKingAlive = false; + boolean blackKingAlive = false; + for (Piece p : Pieces) { + if (p.getType() == PieceType.King) { + if (p.isWhite()) whiteKingAlive = true; + else blackKingAlive = true; + } + } + return !(whiteKingAlive && blackKingAlive); // game over if any king is missing +} + +public String getWinnerText() { + boolean whiteKingAlive = false; + boolean blackKingAlive = false; + for (Piece p : Pieces) { + if (p.getType() == PieceType.King) { + if (p.isWhite()) whiteKingAlive = true; + else blackKingAlive = true; + } + } + if (!whiteKingAlive && !blackKingAlive) return "Both Kings Captured!"; + if (!whiteKingAlive) return "Black Wins!"; + if (!blackKingAlive) return "White Wins!"; + if (isCheckmate(true)) return "Black Wins by Checkmate!"; + if (isCheckmate(false)) return "White Wins by Checkmate!"; + return ""; +} } \ No newline at end of file diff --git a/src/backend/Game.java b/src/backend/Game.java index 89e8621..4439dd2 100644 --- a/src/backend/Game.java +++ b/src/backend/Game.java @@ -41,6 +41,10 @@ public class Game extends Thread { public void run() { while(true) { + if (board.isGameOver()) { + mjf.update(board.getTurnNumber(), board.isTurnWhite()); // update GUI one last time + break; // 💣 exit game loop + } aiPlayerTurn(); mjf.update(board.getTurnNumber(), board.isTurnWhite()); try { @@ -56,12 +60,14 @@ public class Game extends Thread { } private void aiPlayerTurn() { + if (board.isGameOver()) return; if(isAITurn()) { board.playMove(aiPlayer.computeBestMove(new Board(board))); } } public void clickCoords(int x, int y) { + if (board.isGameOver()) return; int width = this.getWidth(); int height = this.getHeight(); if(0>x || 0>y || x>width || y>height) { diff --git a/src/windowInterface/MyInterface.java b/src/windowInterface/MyInterface.java index 6328f0a..8468ac3 100644 --- a/src/windowInterface/MyInterface.java +++ b/src/windowInterface/MyInterface.java @@ -257,7 +257,12 @@ public class MyInterface extends JFrame { } public void update(int turnCount, boolean turnIsWhite) { - turnLabel.setText("Turn : "+turnCount+", "+ (turnIsWhite?"White":"Black")); + if (game.getBoard().isGameOver()) { + actionLabel.setText(game.getBoard().getWinnerText()); // 🏁 Show who won + turnLabel.setText("Game Over"); + } else { + turnLabel.setText("Turn : "+turnCount+", "+ (turnIsWhite?"White":"Black")); + } actionLabel.setText(panelDraw.isPieceAdderMode()?"Adding Piece": (panelDraw.isPieceSelectorMode()?"Selecting Piece to Add": "Playing"));