package backend; import java.util.Stack; /** * 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; /** * Create a new empty move history */ public MoveHistory() { // Initialize an empty stack to store moves moveStack = new Stack<>(); } /** * Add a new move to the history * * @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); } /** * 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 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(); } /** * Check if there are any moves that can be undone * * @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(); } /** * Remove all moves from the history * Used when starting a new game */ public void clear() { // Delete all moves from the stack moveStack.clear(); } }