From eae26d68ab4686bf2ce4fa1022f47d3b5263720a Mon Sep 17 00:00:00 2001 From: Guillaume VALLENET Date: Fri, 25 Apr 2025 16:35:28 +0200 Subject: [PATCH] Actualiser src/backend/MoveHistory.java --- src/backend/MoveHistory.java | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/backend/MoveHistory.java b/src/backend/MoveHistory.java index c90b527..38b8ffc 100644 --- a/src/backend/MoveHistory.java +++ b/src/backend/MoveHistory.java @@ -3,53 +3,63 @@ package backend; import java.util.Stack; /** - * Class to maintain a history of moves made during the game - * and enable undo functionality + * This class keeps track of all moves made during a chess game + * and allows players to undo moves. */ public class MoveHistory { + // Stack data structure to store moves in order (last in, first out) private Stack moveStack; /** - * Constructor for MoveHistory + * Create a new empty move history */ public MoveHistory() { + // Initialize an empty stack to store moves moveStack = new Stack<>(); } /** - * Records a move in the history + * Add a new move to the history * - * @param move The move to record + * @param move The chess move to add to history */ public void recordMove(Move move) { + // Add the move to the top of the stack moveStack.push(move); } /** - * Gets the last move made and removes it from history + * Get the most recent move and remove it from history + * Used when undoing a move * - * @return The last move made, or null if no moves have been made + * @return The last move made, or null if no moves exist */ public Move getLastMove() { + // Check if there are any moves in the history if (moveStack.isEmpty()) { + // If no moves exist, return null return null; } + // Remove and return the most recent move return moveStack.pop(); } /** - * Checks if there are moves to undo + * Check if there are any moves that can be undone * - * @return True if there are moves in the history, false otherwise + * @return True if there are moves in history, false if history is empty */ public boolean canUndo() { + // Return true if the stack has moves, false if it's empty return !moveStack.isEmpty(); } /** - * Clears the move history + * Remove all moves from the history + * Used when starting a new game */ public void clear() { + // Delete all moves from the stack moveStack.clear(); } } \ No newline at end of file