OOP_G01_Project_EENG4/src/backend/MoveHistory.java

124 lines
5.3 KiB
Java

package backend;
import java.util.ArrayList;
import java.util.List;
public class MoveHistory {
private final List<MoveRecord> moveRecords;
private final Board board;
// Inner class to store a move along with board state (like en passant target)
private static class MoveRecord {
private final Move move;
private final int enPassantTargetX;
private final int enPassantTargetY;
public MoveRecord(Move move, int enPassantTargetX, int enPassantTargetY) {
this.move = move;
this.enPassantTargetX = enPassantTargetX;
this.enPassantTargetY = enPassantTargetY;
}
public Move getMove() {
return move;
}
public int getEnPassantTargetX() {
return enPassantTargetX;
}
public int getEnPassantTargetY() {
return enPassantTargetY;
}
}
public MoveHistory(Board board) {
this.board = board;
this.moveRecords = new ArrayList<>();
}
// Add a move to the history, capturing the board's state at this point
public void addMove(Move move) {
MoveRecord record = new MoveRecord(
move,
board.getEnPassantTargetX(),
board.getEnPassantTargetY()
);
moveRecords.add(record);
System.out.println("MoveHistory: Added move from (" + move.getFromX() + "," + move.getFromY() +
") to (" + move.getToX() + "," + move.getToY() + ")");
}
// Clear the move history (e.g., when resetting the board)
public void clear() {
moveRecords.clear();
System.out.println("MoveHistory: Cleared all moves");
}
// Undo the last move and update the board state
public boolean undoLastMove() {
if (moveRecords.isEmpty()) {
System.out.println("MoveHistory: No moves to undo");
return false;
}
MoveRecord lastRecord = moveRecords.remove(moveRecords.size() - 1);
Move lastMove = lastRecord.getMove();
System.out.println("MoveHistory: Undoing move from (" + lastMove.getFromX() + "," + lastMove.getFromY() +
") to (" + lastMove.getToX() + "," + lastMove.getToY() + ")");
// Restore the piece to its original position
Piece pieceMoved = board.getPieceAt(lastMove.getToX(), lastMove.getToY());
if (pieceMoved == null || pieceMoved.getType() != lastMove.getPieceMoved().getType() || pieceMoved.isWhite() != lastMove.getPieceMoved().isWhite()) {
System.err.println("MoveHistory: Piece mismatch during undo at (" + lastMove.getToX() + "," + lastMove.getToY() + ")");
return false;
}
pieceMoved.setPosition(lastMove.getFromX(), lastMove.getFromY());
// Restore any captured piece (normal capture or en passant)
Piece pieceCaptured = lastMove.getPieceCaptured();
boolean wasEnPassantCapture = false;
if (pieceCaptured != null) {
// Check if this was an en passant capture
if (lastMove.getToX() == lastRecord.getEnPassantTargetX() &&
lastMove.getToY() == lastRecord.getEnPassantTargetY() &&
lastMove.getPieceCaptured() == null) {
// For en passant, the captured pawn is at (toX, fromY)
int capturedPawnY = lastMove.getFromY();
board.setPiece(pieceCaptured.isWhite(), pieceCaptured.getType(), lastMove.getToX(), capturedPawnY);
System.out.println("MoveHistory: Restored en passant captured pawn at (" + lastMove.getToX() + "," + capturedPawnY + ")");
wasEnPassantCapture = true;
} else {
// Normal capture: restore the captured piece at the destination
board.setPiece(pieceCaptured.isWhite(), pieceCaptured.getType(), lastMove.getToX(), lastMove.getToY());
System.out.println("MoveHistory: Restored captured piece at (" + lastMove.getToX() + "," + lastMove.getToY() + ")");
}
}
// If this wasn't an en passant capture but the move captured a piece, remove the piece we just moved back
if (!wasEnPassantCapture && pieceCaptured != null) {
board.getPieces().removeIf(p -> p.getX() == lastMove.getToX() && p.getY() == lastMove.getToY() && p != pieceMoved);
}
// Restore the en passant target state
board.enPassantTargetX = lastRecord.getEnPassantTargetX();
board.enPassantTargetY = lastRecord.getEnPassantTargetY();
System.out.println("MoveHistory: Restored en passant target to (" + board.enPassantTargetX + "," + board.enPassantTargetY + ")");
// Update turn number and player turn
board.turnNumber--;
board.isTurnWhite = !board.isTurnWhite;
// Clear selection and highlights
board.selectedX = -1;
board.selectedY = -1;
board.clearHighlightedPositions();
System.out.println("MoveHistory: After undo - Pieces: " + board.getPieces().size());
for (Piece piece : board.getPieces()) {
System.out.println("Piece at (" + piece.getX() + "," + piece.getY() + "): " + piece.getType() + ", isWhite=" + piece.isWhite());
}
return true;
}
}