undo
This commit is contained in:
parent
210a81cba1
commit
fdc7af9940
Binary file not shown.
|
|
@ -2,7 +2,6 @@ package backend;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Board {
|
||||
private int width;
|
||||
|
|
@ -12,8 +11,8 @@ public class Board {
|
|||
private ArrayList<Piece> pieces;
|
||||
private List<Move.Position> highlightedPositions = new ArrayList<>();
|
||||
|
||||
// Add this for undo functionality
|
||||
private Stack<Board> boardHistory = new Stack<>();
|
||||
// Changed from Stack to ArrayList for undo functionality
|
||||
private ArrayList<Board> boardHistory = new ArrayList<>();
|
||||
|
||||
private Integer selectedX = null;
|
||||
private Integer selectedY = null;
|
||||
|
|
@ -25,7 +24,7 @@ public class Board {
|
|||
this.isWhiteTurn = true;
|
||||
this.pieces = new ArrayList<>();
|
||||
this.highlightedPositions = new ArrayList<>();
|
||||
this.boardHistory = new Stack<>();
|
||||
this.boardHistory = new ArrayList<>();
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
|
|
@ -234,7 +233,7 @@ public class Board {
|
|||
this.width = 8;
|
||||
this.height = 8;
|
||||
this.pieces = new ArrayList<>();
|
||||
this.boardHistory = new Stack<>();
|
||||
this.boardHistory = new ArrayList<>();
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
if (y >= array.length) {
|
||||
|
|
@ -314,7 +313,7 @@ public class Board {
|
|||
this.highlightedPositions.addAll(board.highlightedPositions);
|
||||
|
||||
// Initialize board history for copy constructor
|
||||
this.boardHistory = new Stack<>();
|
||||
this.boardHistory = new ArrayList<>();
|
||||
}
|
||||
|
||||
private void clearHighlights() {
|
||||
|
|
@ -352,21 +351,32 @@ public class Board {
|
|||
}
|
||||
}
|
||||
|
||||
// UNDO METHODS USING ARRAYLIST
|
||||
private void saveCurrentState() {
|
||||
boardHistory.push(new Board(this));
|
||||
// Use existing copy constructor to save current board state
|
||||
boardHistory.add(new Board(this));
|
||||
}
|
||||
|
||||
public void undoLastMove() {
|
||||
if (!boardHistory.isEmpty()) {
|
||||
Board previousBoard = boardHistory.pop();
|
||||
// Get the last saved state (most recent)
|
||||
Board previousBoard = boardHistory.get(boardHistory.size() - 1);
|
||||
// Remove it from history
|
||||
boardHistory.remove(boardHistory.size() - 1);
|
||||
|
||||
// Use existing methods to restore state
|
||||
this.width = previousBoard.width;
|
||||
this.height = previousBoard.height;
|
||||
this.turnNumber = previousBoard.turnNumber;
|
||||
this.isWhiteTurn = previousBoard.isWhiteTurn;
|
||||
|
||||
// Use existing getPieces() method to restore pieces
|
||||
this.pieces.clear();
|
||||
for (Piece p : previousBoard.getPieces()) {
|
||||
this.pieces.add(new Piece(p.getX(), p.getY(), p.getType(), p.isWhite()));
|
||||
}
|
||||
|
||||
// Clear selection and highlights
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
clearHighlights();
|
||||
|
|
|
|||
Loading…
Reference in New Issue