undo method + new class recording moves
This commit is contained in:
parent
da9d8b1a8b
commit
5af0f8487d
|
|
@ -9,7 +9,8 @@ public class Board {
|
|||
private int [] selectedPosition = null; // No piece selected initially
|
||||
private int turnNumber=0; // Tracks the number of turns elapsed
|
||||
private boolean turnWhite=true; // True if it's White's turn, False if it's Black's turn
|
||||
private ArrayList<int[]> highlightedPositions = new ArrayList<>();// List containing all board positions
|
||||
private ArrayList<int[]> highlightedPositions = new ArrayList<>(); // List containing all board positions
|
||||
private ArrayList<MoveRecord> moveHistory = new ArrayList<>(); // Stack to store move history
|
||||
|
||||
private boolean inBounds(int x, int y) {
|
||||
// Verify the bounds of the board
|
||||
|
|
@ -272,26 +273,31 @@ public class Board {
|
|||
|
||||
public void movePiece(int fromX, int fromY, int toX, int toY) {
|
||||
// Additional method: moves a piece from one position to another on the board
|
||||
Piece movingPiece = board[fromY][fromX];
|
||||
Piece capturedPiece = board[toY][toX];
|
||||
|
||||
// Get the piece at the source position
|
||||
Piece piece = board[fromY][fromX];
|
||||
|
||||
if (piece != null) {
|
||||
// Update the piece's internal position
|
||||
piece.setX(toX);
|
||||
piece.setY(toY);
|
||||
// Record the move before making it
|
||||
moveHistory.add(new MoveRecord(
|
||||
movingPiece, capturedPiece,
|
||||
fromX, fromY, toX, toY,
|
||||
turnNumber, turnWhite));
|
||||
|
||||
// Move the piece on the board
|
||||
board[toY][toX] = piece; // Place piece at the new position
|
||||
board[toY][toX] = movingPiece; // Place piece at the new position
|
||||
board[fromY][fromX] = null; // Clear the old position
|
||||
}
|
||||
|
||||
if (movingPiece != null) {
|
||||
// Update the piece's internal position
|
||||
movingPiece.setX(toX);
|
||||
movingPiece.setY(toY);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSelected(int x, int y) {
|
||||
return this.selectedPosition!=null && this.selectedPosition[0] == x && this.selectedPosition[1] == y;
|
||||
}
|
||||
|
||||
|
||||
/* saving-loading feature :*/
|
||||
public String[] toFileRep() {
|
||||
ArrayList<String> fileLines = new ArrayList<>();
|
||||
|
|
@ -362,8 +368,35 @@ public class Board {
|
|||
}
|
||||
|
||||
public void undoLastMove() {
|
||||
//TODO
|
||||
// Check if there are moves to undo
|
||||
if (moveHistory.isEmpty()) {
|
||||
return; // Nothing to undo
|
||||
}
|
||||
|
||||
// Get the last move
|
||||
MoveRecord lastMove = moveHistory.remove(moveHistory.size() - 1);
|
||||
|
||||
// Restore the moved piece to its original position
|
||||
board[lastMove.fromY][lastMove.fromX] = lastMove.movedPiece;
|
||||
if (lastMove.movedPiece != null) {
|
||||
lastMove.movedPiece.setX(lastMove.fromX);
|
||||
lastMove.movedPiece.setY(lastMove.fromY);
|
||||
}
|
||||
|
||||
// Restore the captured piece if any
|
||||
board[lastMove.toY][lastMove.toX] = lastMove.capturedPiece;
|
||||
if (lastMove.capturedPiece != null) {
|
||||
lastMove.capturedPiece.setX(lastMove.toX);
|
||||
lastMove.capturedPiece.setY(lastMove.toY);
|
||||
}
|
||||
|
||||
// Restore game state
|
||||
turnNumber = lastMove.turnNumberAtMove;
|
||||
turnWhite = lastMove.turnWhiteAtMove;
|
||||
|
||||
// Clear any selections or highlights
|
||||
selectedPosition = null;
|
||||
highlightedPositions.clear();
|
||||
}
|
||||
|
||||
public Board(Board board) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
package backend;
|
||||
|
||||
public class MoveRecord {
|
||||
Piece movedPiece;
|
||||
Piece capturedPiece;
|
||||
int fromX, fromY, toX, toY;
|
||||
int turnNumberAtMove;
|
||||
boolean turnWhiteAtMove;
|
||||
|
||||
public MoveRecord(Piece moved, Piece captured, int fx, int fy, int tx, int ty,
|
||||
int turnNum, boolean whiteTurn) {
|
||||
this.movedPiece = moved;
|
||||
this.capturedPiece = captured;
|
||||
this.fromX = fx;
|
||||
this.fromY = fy;
|
||||
this.toX = tx;
|
||||
this.toY = ty;
|
||||
this.turnNumberAtMove = turnNum;
|
||||
this.turnWhiteAtMove = whiteTurn;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue