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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Stack;
|
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
private int width;
|
private int width;
|
||||||
|
|
@ -12,8 +11,8 @@ public class Board {
|
||||||
private ArrayList<Piece> pieces;
|
private ArrayList<Piece> pieces;
|
||||||
private List<Move.Position> highlightedPositions = new ArrayList<>();
|
private List<Move.Position> highlightedPositions = new ArrayList<>();
|
||||||
|
|
||||||
// Add this for undo functionality
|
// Changed from Stack to ArrayList for undo functionality
|
||||||
private Stack<Board> boardHistory = new Stack<>();
|
private ArrayList<Board> boardHistory = new ArrayList<>();
|
||||||
|
|
||||||
private Integer selectedX = null;
|
private Integer selectedX = null;
|
||||||
private Integer selectedY = null;
|
private Integer selectedY = null;
|
||||||
|
|
@ -25,7 +24,7 @@ public class Board {
|
||||||
this.isWhiteTurn = true;
|
this.isWhiteTurn = true;
|
||||||
this.pieces = new ArrayList<>();
|
this.pieces = new ArrayList<>();
|
||||||
this.highlightedPositions = new ArrayList<>();
|
this.highlightedPositions = new ArrayList<>();
|
||||||
this.boardHistory = new Stack<>();
|
this.boardHistory = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
|
|
@ -234,7 +233,7 @@ public class Board {
|
||||||
this.width = 8;
|
this.width = 8;
|
||||||
this.height = 8;
|
this.height = 8;
|
||||||
this.pieces = new ArrayList<>();
|
this.pieces = new ArrayList<>();
|
||||||
this.boardHistory = new Stack<>();
|
this.boardHistory = new ArrayList<>();
|
||||||
|
|
||||||
for (int y = 0; y < height; y++) {
|
for (int y = 0; y < height; y++) {
|
||||||
if (y >= array.length) {
|
if (y >= array.length) {
|
||||||
|
|
@ -314,7 +313,7 @@ public class Board {
|
||||||
this.highlightedPositions.addAll(board.highlightedPositions);
|
this.highlightedPositions.addAll(board.highlightedPositions);
|
||||||
|
|
||||||
// Initialize board history for copy constructor
|
// Initialize board history for copy constructor
|
||||||
this.boardHistory = new Stack<>();
|
this.boardHistory = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clearHighlights() {
|
private void clearHighlights() {
|
||||||
|
|
@ -352,21 +351,32 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UNDO METHODS USING ARRAYLIST
|
||||||
private void saveCurrentState() {
|
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() {
|
public void undoLastMove() {
|
||||||
if (!boardHistory.isEmpty()) {
|
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.width = previousBoard.width;
|
||||||
this.height = previousBoard.height;
|
this.height = previousBoard.height;
|
||||||
this.turnNumber = previousBoard.turnNumber;
|
this.turnNumber = previousBoard.turnNumber;
|
||||||
this.isWhiteTurn = previousBoard.isWhiteTurn;
|
this.isWhiteTurn = previousBoard.isWhiteTurn;
|
||||||
|
|
||||||
|
// Use existing getPieces() method to restore pieces
|
||||||
this.pieces.clear();
|
this.pieces.clear();
|
||||||
for (Piece p : previousBoard.getPieces()) {
|
for (Piece p : previousBoard.getPieces()) {
|
||||||
this.pieces.add(new Piece(p.getX(), p.getY(), p.getType(), p.isWhite()));
|
this.pieces.add(new Piece(p.getX(), p.getY(), p.getType(), p.isWhite()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear selection and highlights
|
||||||
selectedX = null;
|
selectedX = null;
|
||||||
selectedY = null;
|
selectedY = null;
|
||||||
clearHighlights();
|
clearHighlights();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue