undo and enpassant undo
This commit is contained in:
parent
9e1e303d6c
commit
f53bc258b4
|
|
@ -3,12 +3,14 @@ package backend;
|
|||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Board {
|
||||
|
||||
private int colNum;
|
||||
private int lineNum;
|
||||
private Piece[][] board;
|
||||
private Stack<MoveRecord> moveHistory = new Stack<>();
|
||||
private EnPassant enPassant = new EnPassant();
|
||||
private int turnNumber = 0;
|
||||
private boolean isWhiteTurn = true;
|
||||
|
|
@ -138,7 +140,23 @@ public class Board {
|
|||
|
||||
void movePiece(int fromX, int fromY, int toX, int toY) { //from private to not
|
||||
Piece moving = board[fromY][fromX];
|
||||
Piece captured = board[toY][toX];
|
||||
|
||||
// For en passant
|
||||
if (enPassant.isEnPassantCapture(moving, fromX, toX, toY, board)) {
|
||||
int capturedY = moving.isWhite() ? toY + 1 : toY - 1;
|
||||
captured = board[capturedY][toX];
|
||||
board[capturedY][toX] = null;
|
||||
}
|
||||
|
||||
// Save the move
|
||||
moveHistory.push(new MoveRecord(
|
||||
new Piece(moving.getX(), moving.getY(), moving.getType(), moving.isWhite()),
|
||||
captured == null ? null : new Piece(captured.getX(), captured.getY(), captured.getType(), captured.isWhite()),
|
||||
fromX, fromY, toX, toY,
|
||||
turnNumber, isWhiteTurn,
|
||||
enPassant.getTarget()
|
||||
));
|
||||
// En passant capture
|
||||
if (enPassant.isEnPassantCapture(moving, fromX, toX, toY, board)) {
|
||||
int capturedY = moving.isWhite() ? toY + 1 : toY - 1;
|
||||
|
|
@ -330,8 +348,35 @@ public class Board {
|
|||
|
||||
|
||||
public void undoLastMove() {
|
||||
//TODO
|
||||
if (moveHistory.isEmpty()) return;
|
||||
|
||||
MoveRecord last = moveHistory.pop();
|
||||
|
||||
this.turnNumber = last.turnNumber;
|
||||
this.isWhiteTurn = last.wasWhiteTurn;
|
||||
enPassant.setTarget(last.enPassantTarget);
|
||||
|
||||
board[last.fromY][last.fromX] = new Piece(
|
||||
last.fromX, last.fromY, last.movedPiece.getType(), last.movedPiece.isWhite()
|
||||
);
|
||||
board[last.toY][last.toX] = null;
|
||||
|
||||
// Restore captured piece
|
||||
if (last.capturedPiece != null) {
|
||||
int cx = last.capturedPiece.getX();
|
||||
int cy = last.capturedPiece.getY();
|
||||
|
||||
// Check for en passant capture
|
||||
if (last.movedPiece.getType() == PieceType.Pawn && cy != last.toY) {
|
||||
// En passant capture happened — captured piece is not at (toX, toY)
|
||||
board[cy][cx] = new Piece(cx, cy, last.capturedPiece.getType(), last.capturedPiece.isWhite());
|
||||
} else {
|
||||
board[cy][cx] = new Piece(cx, cy, last.capturedPiece.getType(), last.capturedPiece.isWhite());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Board(Board other) {
|
||||
this.colNum = other.colNum;
|
||||
this.lineNum = other.lineNum;
|
||||
|
|
@ -354,5 +399,27 @@ public class Board {
|
|||
public void playMove(Move move) {
|
||||
//TODO
|
||||
}
|
||||
private class MoveRecord {
|
||||
final Piece movedPiece;
|
||||
final Piece capturedPiece;
|
||||
final int fromX, fromY, toX, toY;
|
||||
final int turnNumber;
|
||||
final boolean wasWhiteTurn;
|
||||
final int[] enPassantTarget;
|
||||
|
||||
MoveRecord(Piece movedPiece, Piece capturedPiece, int fromX, int fromY, int toX, int toY,
|
||||
int turnNumber, boolean wasWhiteTurn, int[] enPassantTarget) {
|
||||
this.movedPiece = movedPiece;
|
||||
this.capturedPiece = capturedPiece;
|
||||
this.fromX = fromX;
|
||||
this.fromY = fromY;
|
||||
this.toX = toX;
|
||||
this.toY = toY;
|
||||
this.turnNumber = turnNumber;
|
||||
this.wasWhiteTurn = wasWhiteTurn;
|
||||
this.enPassantTarget = enPassantTarget == null ? null : new int[]{enPassantTarget[0], enPassantTarget[1]};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue