From f14bb1d522e14bd4f312362a400e9591ebf34fcc Mon Sep 17 00:00:00 2001 From: "louise.berteloot" Date: Mon, 19 May 2025 20:44:08 +0200 Subject: [PATCH] added isGameOver to stop movement if game is over --- src/backend/Board.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index dd07f37..3e764a3 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -15,6 +15,7 @@ public class Board { private boolean pawnDoubleStep; private int xCoordinatePawn; private int yCoordinatePawn; + private boolean isGameOver = false;//flag for when a king is checkmate and no moves can be made anymore private ArrayList previousStates; @@ -119,6 +120,7 @@ public class Board { public void cleanBoard() { pieces.clear(); + isGameOver=false;//when board reset, moves can be made again bc not game over anymore } public String toString() { @@ -149,6 +151,10 @@ public class Board { } public void userTouch(int x, int y) { + if (isGameOver==true) {//if game is over, any attempt to select/move a piece should be ignored + System.out.println("Checkmate! Can't move pieces anymore.");//says game over + return; + } System.out.println("userTouch triggered at: " + x + ", " + y); Piece selectedPiece = getPieceAt(selectedX, selectedY); @@ -215,7 +221,8 @@ public class Board { rook.setX(5);//moves the rook to the square to the left of the king rook.setMoved(true);//the rook has been moved once so wont be used for castling anymore } - } else {//king moves to the left this time (queen side castling) + } + else {//king moves to the left this time (queen side castling) Piece rook = getPieceAt(0, row);//selects the rook on the left if (rook != null) { rook.setX(3);//moves rook to the right of the king @@ -249,6 +256,7 @@ public class Board { if (isCheckmate(isWhite)) { System.out.println((isWhite ? "White" : "Black") + " is in checkmate!"); + isGameOver=true;//if checkmate, flags the game as over by setting boolean on true } } }