From 228227f07354aff29c68ca5cf5a50156c679477f Mon Sep 17 00:00:00 2001 From: USer Date: Fri, 16 May 2025 10:27:25 +0200 Subject: [PATCH] In class JPanelChessBoard is change the colour of the frame and the colour of the posible move is also changed, now when the king is death the game is over by adding method isGameover --- OOP_group1A1_project/src/backend/Board.java | 28 +++++++++++++++++-- OOP_group1A1_project/src/backend/Move.java | 3 +- .../src/windowInterface/JPanelChessBoard.java | 6 ++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/OOP_group1A1_project/src/backend/Board.java b/OOP_group1A1_project/src/backend/Board.java index d4bb5d3..5c19c77 100644 --- a/OOP_group1A1_project/src/backend/Board.java +++ b/OOP_group1A1_project/src/backend/Board.java @@ -134,7 +134,11 @@ public class Board { public void userTouch(int x, int y) { // 1) Find if you clicked on a piece at (x,y) - Piece clicked = null; + if (isGameOver()) { + System.out.println("Game Over!"); + return; + } + Piece clicked = null; for (Piece p : Pieces) { if (p.getX() == x && p.getY() == y) { clicked = p; @@ -198,6 +202,7 @@ public class Board { Move move = new Move(toMove, x, y, captured); // captured can be null if no piece was taken moveHistory.push(move); System.out.println(this); + if (isGameOver()) return; } @@ -429,7 +434,11 @@ public class Board { public void playMove(Move move) { - // Remove the piece from the destination (if a capture) + 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()); // Remove the moved piece from its old position @@ -461,5 +470,20 @@ public class Board { selectedX = x; selectedY = y; } + public boolean isGameOver() { + 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); + } + + } diff --git a/OOP_group1A1_project/src/backend/Move.java b/OOP_group1A1_project/src/backend/Move.java index 816fb26..97bc729 100644 --- a/OOP_group1A1_project/src/backend/Move.java +++ b/OOP_group1A1_project/src/backend/Move.java @@ -2,7 +2,7 @@ package backend; public class Move { - private final int fromX; + private final int fromX; private final int fromY; private final int toX; private final int toY; @@ -10,6 +10,7 @@ public class Move { private final Piece capturedPiece; // Constructor for a move (with or without capture) + public Move(Piece movedPiece, int toX, int toY, Piece capturedPiece) { this.fromX = movedPiece.getX(); this.fromY = movedPiece.getY(); diff --git a/OOP_group1A1_project/src/windowInterface/JPanelChessBoard.java b/OOP_group1A1_project/src/windowInterface/JPanelChessBoard.java index ad260b1..85bb87a 100644 --- a/OOP_group1A1_project/src/windowInterface/JPanelChessBoard.java +++ b/OOP_group1A1_project/src/windowInterface/JPanelChessBoard.java @@ -103,10 +103,10 @@ public class JPanelChessBoard extends JPanel { boolean isSelect = myGame.isSelected(x,y); boolean isHighlight = myGame.isHighlighted(x,y); if(isSelect) { - g.setColor(Color.blue); + g.setColor(Color.cyan); } if(isHighlight) { - g.setColor(Color.yellow); + g.setColor(Color.lightGray); } if((x+y)%2==1 || isSelect || isHighlight) { g.fillRect( @@ -123,7 +123,7 @@ public class JPanelChessBoard extends JPanel { } } - g.setColor(Color.gray); + g.setColor(Color.blue); for(int x=0; x