Merge pull request 'castling-saved' (#3) from castling-saved into master

Reviewed-on: #3
This commit is contained in:
Bastien DRUILLETTE 2025-05-15 23:40:51 +02:00
commit 8062a2ce28
1 changed files with 27 additions and 1 deletions

View File

@ -165,12 +165,14 @@ public class Board {
board[x][y] == null;
// Move piece
previousBoard = cloneBoard(board);
board[x][y] = selectedPiece;
board[selectedX][selectedY] = null;
selectedPiece.setX(x);
selectedPiece.setY(y);
selectedPiece.setMoved(true); // Marque la pièce comme ayant bougé
// Déplacement de la tour si roque
if (selectedPiece.getType() == PieceType.King && Math.abs(x - selectedX) == 2) {
@ -306,11 +308,35 @@ public class Board {
}
return false;
}
private Piece[][] previousBoard = null;
private Piece[][] cloneBoard(Piece[][] original) {
Piece[][] copy = new Piece[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (original[i][j] != null) {
Piece p = original[i][j];
copy[i][j] = new Piece(p.getX(), p.getY(), p.isWhite(), p.getType());
copy[i][j].setMoved(p.hasMoved());
}
}
}
return copy;
}
public void undoLastMove() {
// TODO
if (previousBoard != null) {
board = cloneBoard(previousBoard); // Restore the board
previousBoard = null; // Clear saved state
hasSelectedPiece = false; // Deselect piece
highlightedSquares.clear(); // Remove highlights
turnWhite = !turnWhite; // Go back to previous player
turnNumber--; // Go back a turn
}
}
public Board(Board board) {
// TODO
}