From a6dab9e366130fa693d68301d27cddadb07eb007 Mon Sep 17 00:00:00 2001 From: Yash Shah Date: Wed, 7 May 2025 16:13:40 +0200 Subject: [PATCH] undo --- src/backend/Board.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index cf864b0..79e3975 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -352,7 +352,26 @@ public class Board { } public void undoLastMove() { - + if (moveHistory.isEmpty()) return; + + Move lastMove = moveHistory.remove(moveHistory.size() - 1); + Piece movedPiece = lastMove.getPieceMoved(); + + // Move the piece back to its original position + movedPiece.setPosition(lastMove.getFromX(), lastMove.getFromY()); + + // Restore the captured piece if any + if (lastMove.getPieceCaptured() != null) { + pieces.add(lastMove.getPieceCaptured()); + } + + // Switch turn and decrease turn count + turnWhite = !turnWhite; + turnNumber--; + + // Clear selection and highlights + selected = null; + highlighted.clear(); }