feat: add undo

This commit is contained in:
2025-04-05 10:53:41 +02:00
parent 8c2c6946d7
commit 2ec7be27ca
9 changed files with 71 additions and 11 deletions

View File

@@ -24,6 +24,10 @@ public class CommandExecutor {
assert result != CommandResult.Moved || command instanceof PlayerCommand;
processResult(command, result);
if (command instanceof PlayerCommand playerCommand)
this.game.addAction(playerCommand);
command.postExec(game, outputSystem);
return result;
}

View File

@@ -3,5 +3,5 @@ package chess.controller;
import chess.model.Game;
public abstract class PlayerCommand extends Command{
public abstract void undo(Game game, OutputSystem outputSystem);
public abstract CommandResult undo(Game game, OutputSystem outputSystem);
}

View File

@@ -16,7 +16,7 @@ public class CastlingCommand extends PlayerCommand {
}
@Override
public void undo(Game game, OutputSystem outputSystem) {
public CommandResult undo(Game game, OutputSystem outputSystem) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'undo'");
}

View File

@@ -16,7 +16,7 @@ public class GrandCastlingCommand extends PlayerCommand {
}
@Override
public void undo(Game game, OutputSystem outputSystem) {
public CommandResult undo(Game game, OutputSystem outputSystem) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'undo'");
}

View File

@@ -57,10 +57,11 @@ public class MoveCommand extends PlayerCommand {
}
@Override
public void undo(Game game, OutputSystem outputSystem) {
public CommandResult undo(Game game, OutputSystem outputSystem) {
final ChessBoard board = game.getBoard();
board.undoMove(move, deadPiece);
return CommandResult.Moved;
}
@Override

View File

@@ -75,7 +75,7 @@ public class PromoteCommand extends PlayerCommand {
}
@Override
public void undo(Game game, OutputSystem outputSystem) {
public CommandResult undo(Game game, OutputSystem outputSystem) {
final ChessBoard board = game.getBoard();
Piece promoted = board.pieceAt(this.pieceCoords);
@@ -84,6 +84,8 @@ public class PromoteCommand extends PlayerCommand {
Color player = promoted.getColor();
board.pieceComes(new Pawn(player), this.pieceCoords);
return CommandResult.ActionNeeded;
}
}

View File

@@ -2,14 +2,18 @@ package chess.controller.commands;
import chess.controller.Command;
import chess.controller.OutputSystem;
import chess.controller.PlayerCommand;
import chess.model.Game;
public class UndoCommand extends Command{
@Override
public CommandResult execute(Game game, OutputSystem outputSystem) {
//TODO
return CommandResult.Moved;
PlayerCommand lastAction = game.getLastAction();
if (lastAction == null)
return CommandResult.NotAllowed;
return lastAction.undo(game, outputSystem);
}
}

View File

@@ -1,10 +1,15 @@
package chess.model;
import java.util.EmptyStackException;
import java.util.Stack;
import chess.controller.PlayerCommand;
import chess.model.visitor.PawnIdentifier;
public class Game {
private final ChessBoard board;
private Color playerTurn;
private final Stack<PlayerCommand> movesHistory;
public enum GameStatus {
Check, CheckMate, OnGoing, Pat;
@@ -12,6 +17,7 @@ public class Game {
public Game(ChessBoard board) {
this.board = board;
this.movesHistory = new Stack<>();
}
public ChessBoard getBoard() {
@@ -80,4 +86,17 @@ public class Game {
return null;
}
public void addAction(PlayerCommand command) {
this.movesHistory.add(command);
}
public PlayerCommand getLastAction() {
try {
PlayerCommand last = this.movesHistory.pop();
return last;
} catch (EmptyStackException e) {
return null;
}
}
}

View File

@@ -6,6 +6,7 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
@@ -13,31 +14,38 @@ import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import chess.controller.Command;
import chess.controller.Command.CommandResult;
import chess.controller.CommandExecutor;
import chess.controller.OutputSystem;
import chess.controller.Command.CommandResult;
import chess.controller.commands.CastlingCommand;
import chess.controller.commands.GetAllowedMovesCommand;
import chess.controller.commands.GetPieceAtCommand;
import chess.controller.commands.GrandCastlingCommand;
import chess.controller.commands.MoveCommand;
import chess.controller.commands.PromoteCommand;
import chess.controller.commands.PromoteCommand.PromoteType;
import chess.controller.commands.UndoCommand;
import chess.model.Coordinate;
import chess.model.Move;
import chess.model.Piece;
public class Window extends JFrame implements OutputSystem {
private final JLabel cells[][];
private final JLabel displayText;
private final CommandExecutor commandExecutor;
private Coordinate lastClick = null;
private final JLabel cells[][];
private final JLabel displayText;
private final JButton castlingButton = new JButton("Roque");
private final JButton bigCastlingButton = new JButton("Grand Roque");
private final JButton undoButton = new JButton("Annuler le coup précédent");
public Window(CommandExecutor commandExecutor) {
this.cells = new JLabel[8][8];
this.displayText = new JLabel();
this.commandExecutor = commandExecutor;
setSize(800, 870);
setSize(800, 910);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
@@ -51,12 +59,34 @@ public class Window extends JFrame implements OutputSystem {
return ((x + y) % 2 == 1) ? Color.BLACK : Color.WHITE;
}
private void buildButtons(JPanel bottom) {
castlingButton.addActionListener((event) -> {
sendCommand(new CastlingCommand());
});
bigCastlingButton.addActionListener((event) -> {
sendCommand(new GrandCastlingCommand());
});
undoButton.addActionListener((event) -> {
sendCommand(new UndoCommand());
});
bottom.add(castlingButton);
bottom.add(bigCastlingButton);
bottom.add(undoButton);
}
private void buildBoard() {
JPanel content = new JPanel();
JPanel grid = new JPanel(new GridLayout(8, 8));
JPanel bottom = new JPanel();
buildButtons(bottom);
content.add(this.displayText);
content.add(grid);
content.add(bottom);
setContentPane(content);