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 [] selectedPosition = null; // No piece selected initially
|
||||||
private int turnNumber=0; // Tracks the number of turns elapsed
|
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 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) {
|
private boolean inBounds(int x, int y) {
|
||||||
// Verify the bounds of the board
|
// Verify the bounds of the board
|
||||||
|
|
@ -272,26 +273,31 @@ public class Board {
|
||||||
|
|
||||||
public void movePiece(int fromX, int fromY, int toX, int toY) {
|
public void movePiece(int fromX, int fromY, int toX, int toY) {
|
||||||
// Additional method: moves a piece from one position to another on the board
|
// 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
|
// Record the move before making it
|
||||||
Piece piece = board[fromY][fromX];
|
moveHistory.add(new MoveRecord(
|
||||||
|
movingPiece, capturedPiece,
|
||||||
|
fromX, fromY, toX, toY,
|
||||||
|
turnNumber, turnWhite));
|
||||||
|
|
||||||
if (piece != null) {
|
|
||||||
// Update the piece's internal position
|
|
||||||
piece.setX(toX);
|
|
||||||
piece.setY(toY);
|
|
||||||
|
|
||||||
// Move the piece on the board
|
// 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
|
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) {
|
public boolean isSelected(int x, int y) {
|
||||||
return this.selectedPosition!=null && this.selectedPosition[0] == x && this.selectedPosition[1] == y;
|
return this.selectedPosition!=null && this.selectedPosition[0] == x && this.selectedPosition[1] == y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* saving-loading feature :*/
|
/* saving-loading feature :*/
|
||||||
public String[] toFileRep() {
|
public String[] toFileRep() {
|
||||||
ArrayList<String> fileLines = new ArrayList<>();
|
ArrayList<String> fileLines = new ArrayList<>();
|
||||||
|
|
@ -362,8 +368,35 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undoLastMove() {
|
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) {
|
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