undo last move

This commit is contained in:
flori 2025-05-16 09:52:38 +02:00
parent c68c72757c
commit 6f8846e3ea
1 changed files with 40 additions and 20 deletions

View File

@ -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
board[selectedX][selectedY] = null; // Clear old square
selectedPiece.x = x;
selectedPiece.y = y;
turn++; // Next player's turn
selectedX = -1; selectedX = -1;
selectedY = -1; // Unselect selectedY = -1; // Unselect
clearHighlights(); clearHighlights();
highlightKingInCheck(); highlightKingInCheck();
System.out.println(toString()); // Work but not sure ? System.out.println(toString());
} }
} }
} }
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,9 +374,31 @@ 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
@ -390,6 +406,10 @@ public class Board {
} }
public void playMove(Move move) { public void playMove(Move move) {
// Save current state before move for undo
undoStack.push(toFileRep());
// existing move logic
Piece piece = board[move.getFromX()][move.getFromY()]; 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;