better undo

This commit is contained in:
2025-04-04 20:06:32 +02:00
parent 48a215eae5
commit 810934aea1
8 changed files with 48 additions and 56 deletions

View File

@@ -10,10 +10,13 @@ import chess.model.Piece;
import chess.model.visitor.PiecePathChecker;
public class MoveCommand extends PlayerCommand {
private final Move move;
private Piece deadPiece;
public MoveCommand(Move move) {
this.move = move;
this.deadPiece = null;
}
public Move getMove() {
@@ -39,6 +42,7 @@ public class MoveCommand extends PlayerCommand {
if (!valid)
return CommandResult.NotAllowed;
this.deadPiece = board.pieceAt(move.getFinish());
board.applyMove(move);
if (board.isKingInCheck(game.getPlayerTurn())) {
@@ -51,7 +55,9 @@ public class MoveCommand extends PlayerCommand {
@Override
public void undo(Game game, OutputSystem outputSystem) {
game.getBoard().undoLastMove();
final ChessBoard board = game.getBoard();
board.undoMove(move, deadPiece);
}
}