Actualiser src/backend/MoveHistory.java
This commit is contained in:
parent
2c09b153ac
commit
eae26d68ab
|
|
@ -3,53 +3,63 @@ package backend;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to maintain a history of moves made during the game
|
* This class keeps track of all moves made during a chess game
|
||||||
* and enable undo functionality
|
* and allows players to undo moves.
|
||||||
*/
|
*/
|
||||||
public class MoveHistory {
|
public class MoveHistory {
|
||||||
|
// Stack data structure to store moves in order (last in, first out)
|
||||||
private Stack<Move> moveStack;
|
private Stack<Move> moveStack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for MoveHistory
|
* Create a new empty move history
|
||||||
*/
|
*/
|
||||||
public MoveHistory() {
|
public MoveHistory() {
|
||||||
|
// Initialize an empty stack to store moves
|
||||||
moveStack = new Stack<>();
|
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) {
|
public void recordMove(Move move) {
|
||||||
|
// Add the move to the top of the stack
|
||||||
moveStack.push(move);
|
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() {
|
public Move getLastMove() {
|
||||||
|
// Check if there are any moves in the history
|
||||||
if (moveStack.isEmpty()) {
|
if (moveStack.isEmpty()) {
|
||||||
|
// If no moves exist, return null
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
// Remove and return the most recent move
|
||||||
return moveStack.pop();
|
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() {
|
public boolean canUndo() {
|
||||||
|
// Return true if the stack has moves, false if it's empty
|
||||||
return !moveStack.isEmpty();
|
return !moveStack.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears the move history
|
* Remove all moves from the history
|
||||||
|
* Used when starting a new game
|
||||||
*/
|
*/
|
||||||
public void clear() {
|
public void clear() {
|
||||||
|
// Delete all moves from the stack
|
||||||
moveStack.clear();
|
moveStack.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue