Actualiser src/backend/MoveHistory.java

This commit is contained in:
Guillaume VALLENET 2025-04-25 16:35:28 +02:00
parent 2c09b153ac
commit eae26d68ab
1 changed files with 20 additions and 10 deletions

View File

@ -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<Move> 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();
}
}