From b5aa58a2a60971f3d273f12c8c221d48592e7740 Mon Sep 17 00:00:00 2001 From: USer Date: Fri, 16 May 2025 12:27:50 +0200 Subject: [PATCH] New method is added in JPanelChessBoard, so now when the black chess win it shows W in chess board --- OOP_group1A1_project/src/backend/Board.java | 68 +++++++++++++++++-- .../src/windowInterface/JPanelChessBoard.java | 23 +++++++ 2 files changed, 87 insertions(+), 4 deletions(-) diff --git a/OOP_group1A1_project/src/backend/Board.java b/OOP_group1A1_project/src/backend/Board.java index 5c19c77..5f123aa 100644 --- a/OOP_group1A1_project/src/backend/Board.java +++ b/OOP_group1A1_project/src/backend/Board.java @@ -133,7 +133,11 @@ public class Board { } public void userTouch(int x, int y) { - // 1) Find if you clicked on a piece at (x,y) + if (isGameOver()) { + endGame(); // <— this MUST be called + return; + } + // 1) Find if you clicked on a piece at (x,y) if (isGameOver()) { System.out.println("Game Over!"); return; @@ -144,6 +148,10 @@ public class Board { clicked = p; break; } + if (isGameOver()) { + endGame(); // <— also check again after making a move + return; + } } // 2) If nothing is selected, select a piece of the correct color @@ -203,6 +211,10 @@ public class Board { moveHistory.push(move); System.out.println(this); if (isGameOver()) return; + if (isGameOver()) { + endGame(); + return; + } } @@ -434,9 +446,13 @@ public class Board { public void playMove(Move move) { - if (isGameOver()) { - System.out.println("Game Over!"); - return; + if (isGameOver()) { + endGame(); // Make sure this is here + return; + } + if (isGameOver()) { + System.out.println("Game Over!"); + return; } // Remove the piece from the destination (if a capture) Pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY()); @@ -457,6 +473,10 @@ public class Board { // Clear selection hasSelection = false; + if (isGameOver()) { + endGame(); // check again after applying move + return; + } } Piece getPieceAt(int x, int y) { @@ -483,7 +503,47 @@ public class Board { return !(whiteKingAlive && blackKingAlive); } + + + private void endGame() { + boolean whiteAlive = false; + boolean blackAlive = false; + + for (Piece p : Pieces) { + if (p.getType() == PieceType.King) { + if (p.isWhite()) whiteAlive = true; + else blackAlive = true; + } + } + + Pieces.clear(); // Remove all remaining pieces + hasSelection = false; + + if (whiteAlive && !blackAlive) { + showWinText(true); // White wins + } else if (!whiteAlive && blackAlive) { + showWinText(false); // Black wins + } + + System.out.println("Game Over!"); + } + + private void showWinText(boolean isWhite) { + // W + Pieces.add(new Piece(1, 2, isWhite, PieceType.Queen)); + Pieces.add(new Piece(1, 3, isWhite, PieceType.Queen)); + Pieces.add(new Piece(2, 4, isWhite, PieceType.Queen)); + Pieces.add(new Piece(3, 2, isWhite, PieceType.Queen)); + Pieces.add(new Piece(3, 3, isWhite, PieceType.Queen)); + Pieces.add(new Piece(4, 4, isWhite, PieceType.Queen)); + Pieces.add(new Piece(5, 2, isWhite, PieceType.Queen)); + Pieces.add(new Piece(5, 3, isWhite, PieceType.Queen)); + + } + + + } diff --git a/OOP_group1A1_project/src/windowInterface/JPanelChessBoard.java b/OOP_group1A1_project/src/windowInterface/JPanelChessBoard.java index 85bb87a..26cc32d 100644 --- a/OOP_group1A1_project/src/windowInterface/JPanelChessBoard.java +++ b/OOP_group1A1_project/src/windowInterface/JPanelChessBoard.java @@ -192,5 +192,28 @@ public class JPanelChessBoard extends JPanel { public boolean isPieceAdderMode() { return pieceAdderMode; } + + private void drawWinMessage(Graphics g, boolean isWhite) { + PieceType type = PieceType.Queen; + Color color = isWhite ? Color.white : Color.black; + + // Draw 'W' + drawSinglePiece(g, 0, 2, type, isWhite); + drawSinglePiece(g, 0, 3, type, isWhite); + drawSinglePiece(g, 1, 4, type, isWhite); + drawSinglePiece(g, 2, 3, type, isWhite); + drawSinglePiece(g, 2, 2, type, isWhite); + + + } + + private void drawSinglePiece(Graphics g, int boardX, int boardY, PieceType type, boolean isWhite) { + g.drawImage( + getChessPieceImageFromType(type, isWhite), + MARGIN + xCoordFromGame(boardX), + MARGIN + yCoordFromGame(boardY), + null + ); + } }