feat: add undo
This commit is contained in:
@@ -24,6 +24,10 @@ public class CommandExecutor {
|
|||||||
assert result != CommandResult.Moved || command instanceof PlayerCommand;
|
assert result != CommandResult.Moved || command instanceof PlayerCommand;
|
||||||
|
|
||||||
processResult(command, result);
|
processResult(command, result);
|
||||||
|
|
||||||
|
if (command instanceof PlayerCommand playerCommand)
|
||||||
|
this.game.addAction(playerCommand);
|
||||||
|
|
||||||
command.postExec(game, outputSystem);
|
command.postExec(game, outputSystem);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,5 +3,5 @@ package chess.controller;
|
|||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
public abstract class PlayerCommand extends Command{
|
public abstract class PlayerCommand extends Command{
|
||||||
public abstract void undo(Game game, OutputSystem outputSystem);
|
public abstract CommandResult undo(Game game, OutputSystem outputSystem);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class CastlingCommand extends PlayerCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
public CommandResult undo(Game game, OutputSystem outputSystem) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class GrandCastlingCommand extends PlayerCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
public CommandResult undo(Game game, OutputSystem outputSystem) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,10 +57,11 @@ public class MoveCommand extends PlayerCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
public CommandResult undo(Game game, OutputSystem outputSystem) {
|
||||||
final ChessBoard board = game.getBoard();
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
board.undoMove(move, deadPiece);
|
board.undoMove(move, deadPiece);
|
||||||
|
return CommandResult.Moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public class PromoteCommand extends PlayerCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
public CommandResult undo(Game game, OutputSystem outputSystem) {
|
||||||
final ChessBoard board = game.getBoard();
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
Piece promoted = board.pieceAt(this.pieceCoords);
|
Piece promoted = board.pieceAt(this.pieceCoords);
|
||||||
@@ -84,6 +84,8 @@ public class PromoteCommand extends PlayerCommand {
|
|||||||
|
|
||||||
Color player = promoted.getColor();
|
Color player = promoted.getColor();
|
||||||
board.pieceComes(new Pawn(player), this.pieceCoords);
|
board.pieceComes(new Pawn(player), this.pieceCoords);
|
||||||
|
|
||||||
|
return CommandResult.ActionNeeded;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,18 @@ package chess.controller.commands;
|
|||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
import chess.controller.OutputSystem;
|
import chess.controller.OutputSystem;
|
||||||
|
import chess.controller.PlayerCommand;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
public class UndoCommand extends Command{
|
public class UndoCommand extends Command{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||||
//TODO
|
PlayerCommand lastAction = game.getLastAction();
|
||||||
return CommandResult.Moved;
|
if (lastAction == null)
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
return lastAction.undo(game, outputSystem);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
package chess.model;
|
package chess.model;
|
||||||
|
|
||||||
|
import java.util.EmptyStackException;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
import chess.controller.PlayerCommand;
|
||||||
import chess.model.visitor.PawnIdentifier;
|
import chess.model.visitor.PawnIdentifier;
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
private final ChessBoard board;
|
private final ChessBoard board;
|
||||||
private Color playerTurn;
|
private Color playerTurn;
|
||||||
|
private final Stack<PlayerCommand> movesHistory;
|
||||||
|
|
||||||
public enum GameStatus {
|
public enum GameStatus {
|
||||||
Check, CheckMate, OnGoing, Pat;
|
Check, CheckMate, OnGoing, Pat;
|
||||||
@@ -12,6 +17,7 @@ public class Game {
|
|||||||
|
|
||||||
public Game(ChessBoard board) {
|
public Game(ChessBoard board) {
|
||||||
this.board = board;
|
this.board = board;
|
||||||
|
this.movesHistory = new Stack<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChessBoard getBoard() {
|
public ChessBoard getBoard() {
|
||||||
@@ -80,4 +86,17 @@ public class Game {
|
|||||||
return null;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.awt.event.MouseAdapter;
|
|||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
@@ -13,31 +14,38 @@ import javax.swing.JPanel;
|
|||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
|
import chess.controller.Command.CommandResult;
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.OutputSystem;
|
import chess.controller.OutputSystem;
|
||||||
import chess.controller.Command.CommandResult;
|
import chess.controller.commands.CastlingCommand;
|
||||||
import chess.controller.commands.GetAllowedMovesCommand;
|
import chess.controller.commands.GetAllowedMovesCommand;
|
||||||
import chess.controller.commands.GetPieceAtCommand;
|
import chess.controller.commands.GetPieceAtCommand;
|
||||||
|
import chess.controller.commands.GrandCastlingCommand;
|
||||||
import chess.controller.commands.MoveCommand;
|
import chess.controller.commands.MoveCommand;
|
||||||
import chess.controller.commands.PromoteCommand;
|
import chess.controller.commands.PromoteCommand;
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
|
import chess.controller.commands.UndoCommand;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
|
|
||||||
public class Window extends JFrame implements OutputSystem {
|
public class Window extends JFrame implements OutputSystem {
|
||||||
|
|
||||||
private final JLabel cells[][];
|
|
||||||
private final JLabel displayText;
|
|
||||||
private final CommandExecutor commandExecutor;
|
private final CommandExecutor commandExecutor;
|
||||||
|
|
||||||
private Coordinate lastClick = null;
|
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) {
|
public Window(CommandExecutor commandExecutor) {
|
||||||
this.cells = new JLabel[8][8];
|
this.cells = new JLabel[8][8];
|
||||||
this.displayText = new JLabel();
|
this.displayText = new JLabel();
|
||||||
this.commandExecutor = commandExecutor;
|
this.commandExecutor = commandExecutor;
|
||||||
setSize(800, 870);
|
setSize(800, 910);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||||
@@ -51,12 +59,34 @@ public class Window extends JFrame implements OutputSystem {
|
|||||||
return ((x + y) % 2 == 1) ? Color.BLACK : Color.WHITE;
|
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() {
|
private void buildBoard() {
|
||||||
JPanel content = new JPanel();
|
JPanel content = new JPanel();
|
||||||
JPanel grid = new JPanel(new GridLayout(8, 8));
|
JPanel grid = new JPanel(new GridLayout(8, 8));
|
||||||
|
JPanel bottom = new JPanel();
|
||||||
|
|
||||||
|
buildButtons(bottom);
|
||||||
|
|
||||||
content.add(this.displayText);
|
content.add(this.displayText);
|
||||||
content.add(grid);
|
content.add(grid);
|
||||||
|
content.add(bottom);
|
||||||
|
|
||||||
setContentPane(content);
|
setContentPane(content);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user