undo last move
This commit is contained in:
parent
c68c72757c
commit
6f8846e3ea
|
|
@ -1,6 +1,7 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Board {
|
||||
private Piece[][] board;
|
||||
|
|
@ -10,6 +11,7 @@ public class Board {
|
|||
private int selectedX = -1;
|
||||
private int selectedY = -1;
|
||||
private boolean[][] highlightedSquares;
|
||||
private Stack<String[]> undoStack = new Stack<>();
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
this.width = colNum; // col move x
|
||||
|
|
@ -140,25 +142,17 @@ public class Board {
|
|||
|
||||
} else if (highlightedSquares[x][y]) {
|
||||
// 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
|
||||
board[selectedX][selectedY] = null; // Clear old square
|
||||
selectedX = -1;
|
||||
selectedY = -1; // Unselect
|
||||
|
||||
selectedPiece.x = x;
|
||||
selectedPiece.y = y;
|
||||
|
||||
turn++; // Next player's turn
|
||||
|
||||
selectedX = -1;
|
||||
selectedY = -1; // Unselect
|
||||
|
||||
clearHighlights();
|
||||
highlightKingInCheck();
|
||||
System.out.println(toString()); // Work but not sure ?
|
||||
clearHighlights();
|
||||
highlightKingInCheck();
|
||||
System.out.println(toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSelected(int x, int y) {
|
||||
|
|
@ -248,7 +242,7 @@ public class Board {
|
|||
Move move = legalMoves.get(i);
|
||||
highlightedSquares[move.getToX()][move.getToY()] = true; // assuming Move has toX and toY fields
|
||||
}
|
||||
} // KDO FLO
|
||||
}
|
||||
|
||||
private ArrayList<Move> getLegalMoves(Piece piece) {
|
||||
ArrayList<Move> moves = new ArrayList<>();
|
||||
|
|
@ -380,17 +374,43 @@ public class Board {
|
|||
|
||||
|
||||
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) {
|
||||
//TODO
|
||||
|
||||
}
|
||||
|
||||
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.getFromX()][move.getFromY()] = null;
|
||||
piece.x = move.getToX();
|
||||
|
|
|
|||
Loading…
Reference in New Issue