undo
This commit is contained in:
parent
2036a3d034
commit
5461e96cd4
|
|
@ -265,12 +265,29 @@ public class Board {
|
||||||
public void getLastMove (int x, int y) {// it saves the current state before making a move
|
public void getLastMove (int x, int y) {// it saves the current state before making a move
|
||||||
|
|
||||||
previousStates.add(new Board(this)); // Use the existing constructor to create a copy
|
previousStates.add(new Board(this)); // Use the existing constructor to create a copy
|
||||||
|
getLastMove(selectedX, selectedY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undoLastMove() {
|
public void undoLastMove() {
|
||||||
|
|
||||||
|
if (!previousStates.isEmpty()) {
|
||||||
|
Board previousState = previousStates.remove(previousStates.size() - 1); // Get the last state
|
||||||
|
this.colNum = previousState.colNum;
|
||||||
|
this.lineNum = previousState.lineNum;
|
||||||
|
this.turnNumber = previousState.turnNumber;
|
||||||
|
this.isWhiteTurn = previousState.isWhiteTurn;
|
||||||
|
this.pieces = new ArrayList<>(previousState.pieces); // Restore pieces
|
||||||
|
// Reset selected positions and highlighted squares
|
||||||
|
this.selectedX = previousState.selectedX;
|
||||||
|
this.selectedY = previousState.selectedY;
|
||||||
|
this.highlightedSquares = new ArrayList<>(previousState.highlightedSquares);
|
||||||
|
} else {
|
||||||
|
System.out.println("There are no moves to undo.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Board(Board board) {
|
public Board(Board board) {
|
||||||
this.colNum = board.colNum;
|
this.colNum = board.colNum;
|
||||||
|
|
|
||||||
|
|
@ -107,6 +107,7 @@ public class Game extends Thread {
|
||||||
board.undoLastMove();
|
board.undoLastMove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void toggleAI(boolean isWhite) {
|
public void toggleAI(boolean isWhite) {
|
||||||
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue