undo last move
This commit is contained in:
parent
c68c72757c
commit
6f8846e3ea
|
|
@ -1,6 +1,7 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
private Piece[][] board;
|
private Piece[][] board;
|
||||||
|
|
@ -10,6 +11,7 @@ public class Board {
|
||||||
private int selectedX = -1;
|
private int selectedX = -1;
|
||||||
private int selectedY = -1;
|
private int selectedY = -1;
|
||||||
private boolean[][] highlightedSquares;
|
private boolean[][] highlightedSquares;
|
||||||
|
private Stack<String[]> undoStack = new Stack<>();
|
||||||
|
|
||||||
public Board(int colNum, int lineNum) {
|
public Board(int colNum, int lineNum) {
|
||||||
this.width = colNum; // col move x
|
this.width = colNum; // col move x
|
||||||
|
|
@ -140,25 +142,17 @@ public class Board {
|
||||||
|
|
||||||
} else if (highlightedSquares[x][y]) {
|
} else if (highlightedSquares[x][y]) {
|
||||||
// Move the piece
|
// Move the piece
|
||||||
Piece selectedPiece = board[selectedX][selectedY];
|
Move move = new Move(selectedX, selectedY, x, y);
|
||||||
|
playMove(move); // This handles moving the piece AND saving the undo state.
|
||||||
|
|
||||||
board[x][y] = selectedPiece; // Move to new square
|
selectedX = -1;
|
||||||
board[selectedX][selectedY] = null; // Clear old square
|
selectedY = -1; // Unselect
|
||||||
|
|
||||||
selectedPiece.x = x;
|
clearHighlights();
|
||||||
selectedPiece.y = y;
|
highlightKingInCheck();
|
||||||
|
System.out.println(toString());
|
||||||
turn++; // Next player's turn
|
|
||||||
|
|
||||||
selectedX = -1;
|
|
||||||
selectedY = -1; // Unselect
|
|
||||||
|
|
||||||
clearHighlights();
|
|
||||||
highlightKingInCheck();
|
|
||||||
System.out.println(toString()); // Work but not sure ?
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSelected(int x, int y) {
|
public boolean isSelected(int x, int y) {
|
||||||
|
|
@ -248,7 +242,7 @@ public class Board {
|
||||||
Move move = legalMoves.get(i);
|
Move move = legalMoves.get(i);
|
||||||
highlightedSquares[move.getToX()][move.getToY()] = true; // assuming Move has toX and toY fields
|
highlightedSquares[move.getToX()][move.getToY()] = true; // assuming Move has toX and toY fields
|
||||||
}
|
}
|
||||||
} // KDO FLO
|
}
|
||||||
|
|
||||||
private ArrayList<Move> getLegalMoves(Piece piece) {
|
private ArrayList<Move> getLegalMoves(Piece piece) {
|
||||||
ArrayList<Move> moves = new ArrayList<>();
|
ArrayList<Move> moves = new ArrayList<>();
|
||||||
|
|
@ -380,17 +374,43 @@ public class Board {
|
||||||
|
|
||||||
|
|
||||||
public void undoLastMove() {
|
public void undoLastMove() {
|
||||||
//TODO
|
if (!undoStack.isEmpty()) {
|
||||||
|
String[] lastState = undoStack.pop();
|
||||||
|
|
||||||
|
// Load the board from the saved state
|
||||||
|
Board previousBoard = new Board(lastState);
|
||||||
|
|
||||||
|
// Copy the previousBoard's fields into current board instance
|
||||||
|
this.board = previousBoard.board;
|
||||||
|
this.turn = previousBoard.turn;
|
||||||
|
this.width = previousBoard.width;
|
||||||
|
this.height = previousBoard.height;
|
||||||
|
this.highlightedSquares = previousBoard.highlightedSquares;
|
||||||
|
|
||||||
|
// Clear selection and highlights as well (optional)
|
||||||
|
this.selectedX = -1;
|
||||||
|
this.selectedY = -1;
|
||||||
|
clearHighlights();
|
||||||
|
|
||||||
|
System.out.println("Undo performed.");
|
||||||
|
System.out.println(this.toString());
|
||||||
|
} else {
|
||||||
|
System.out.println("No moves to undo.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Board(Board board) {
|
public Board(Board board) {
|
||||||
//TODO
|
//TODO
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playMove(Move move) {
|
public void playMove(Move move) {
|
||||||
Piece piece = board[move.getFromX()][move.getFromY()];
|
// Save current state before move for undo
|
||||||
|
undoStack.push(toFileRep());
|
||||||
|
|
||||||
|
// existing move logic
|
||||||
|
Piece piece = board[move.getFromX()][move.getFromY()];
|
||||||
board[move.getToX()][move.getToY()] = piece;
|
board[move.getToX()][move.getToY()] = piece;
|
||||||
board[move.getFromX()][move.getFromY()] = null;
|
board[move.getFromX()][move.getFromY()] = null;
|
||||||
piece.x = move.getToX();
|
piece.x = move.getToX();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue