diff --git a/app/src/main/java/chess/io/CommandExecutor.java b/app/src/main/java/chess/io/CommandExecutor.java index 4462bbe..53b039a 100644 --- a/app/src/main/java/chess/io/CommandExecutor.java +++ b/app/src/main/java/chess/io/CommandExecutor.java @@ -19,6 +19,10 @@ public class CommandExecutor { assert this.outputSystem != null : "No output system specified !"; CommandResult result = command.execute(this.game, this.outputSystem); + + // non player commands are not supposed to return move result + assert result != CommandResult.Moved || command instanceof PlayerCommand; + processResult(command, result); return result; } @@ -34,7 +38,7 @@ public class CommandExecutor { if (!needsPromote) this.game.switchPlayerTurn(); - } else if (command instanceof PromoteCommand) { + } else if (command instanceof PlayerCommand) { this.game.switchPlayerTurn(); } } diff --git a/app/src/main/java/chess/io/PlayerCommand.java b/app/src/main/java/chess/io/PlayerCommand.java new file mode 100644 index 0000000..3b3ad28 --- /dev/null +++ b/app/src/main/java/chess/io/PlayerCommand.java @@ -0,0 +1,7 @@ +package chess.io; + +import chess.model.Game; + +public abstract class PlayerCommand extends Command{ + public abstract void undo(Game game, OutputSystem outputSystem); +} diff --git a/app/src/main/java/chess/io/commands/CastlingCommand.java b/app/src/main/java/chess/io/commands/CastlingCommand.java index 38ce968..9dcfc71 100644 --- a/app/src/main/java/chess/io/commands/CastlingCommand.java +++ b/app/src/main/java/chess/io/commands/CastlingCommand.java @@ -1,15 +1,21 @@ package chess.io.commands; -import chess.io.Command; import chess.io.CommandResult; import chess.io.OutputSystem; +import chess.io.PlayerCommand; import chess.model.Game; -public class CastlingCommand extends Command{ +public class CastlingCommand extends PlayerCommand{ @Override public CommandResult execute(Game game, OutputSystem outputSystem) { return CommandResult.NotAllowed; } + + @Override + public void undo(Game game, OutputSystem outputSystem) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'undo'"); + } } diff --git a/app/src/main/java/chess/io/commands/GrandCastlingCommand.java b/app/src/main/java/chess/io/commands/GrandCastlingCommand.java index f02eb44..5953918 100644 --- a/app/src/main/java/chess/io/commands/GrandCastlingCommand.java +++ b/app/src/main/java/chess/io/commands/GrandCastlingCommand.java @@ -1,15 +1,21 @@ package chess.io.commands; -import chess.io.Command; import chess.io.CommandResult; import chess.io.OutputSystem; +import chess.io.PlayerCommand; import chess.model.Game; -public class GrandCastlingCommand extends Command{ +public class GrandCastlingCommand extends PlayerCommand { @Override public CommandResult execute(Game game, OutputSystem outputSystem) { return CommandResult.NotAllowed; } - + + @Override + public void undo(Game game, OutputSystem outputSystem) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'undo'"); + } + } diff --git a/app/src/main/java/chess/io/commands/MoveCommand.java b/app/src/main/java/chess/io/commands/MoveCommand.java index c51901d..0dec394 100644 --- a/app/src/main/java/chess/io/commands/MoveCommand.java +++ b/app/src/main/java/chess/io/commands/MoveCommand.java @@ -1,15 +1,15 @@ package chess.io.commands; -import chess.io.Command; import chess.io.CommandResult; import chess.io.OutputSystem; +import chess.io.PlayerCommand; import chess.model.ChessBoard; import chess.model.Game; import chess.model.Move; import chess.model.Piece; import chess.model.visitor.PiecePathChecker; -public class MoveCommand extends Command { +public class MoveCommand extends PlayerCommand { private final Move move; public MoveCommand(Move move) { @@ -49,4 +49,9 @@ public class MoveCommand extends Command { return CommandResult.Moved; } + @Override + public void undo(Game game, OutputSystem outputSystem) { + game.getBoard().undoLastMove(); + } + } diff --git a/app/src/main/java/chess/io/commands/PromoteCommand.java b/app/src/main/java/chess/io/commands/PromoteCommand.java index 018852b..a7a7bef 100644 --- a/app/src/main/java/chess/io/commands/PromoteCommand.java +++ b/app/src/main/java/chess/io/commands/PromoteCommand.java @@ -1,8 +1,8 @@ package chess.io.commands; -import chess.io.Command; import chess.io.CommandResult; import chess.io.OutputSystem; +import chess.io.PlayerCommand; import chess.model.ChessBoard; import chess.model.Color; import chess.model.Coordinate; @@ -14,7 +14,7 @@ import chess.model.pieces.Pawn; import chess.model.pieces.Queen; import chess.model.pieces.Rook; -public class PromoteCommand extends Command { +public class PromoteCommand extends PlayerCommand { public enum PromoteType { Queen, @@ -71,4 +71,17 @@ public class PromoteCommand extends Command { } } + @Override + public void undo(Game game, OutputSystem outputSystem) { + final ChessBoard board = game.getBoard(); + + Piece promoted = board.pieceAt(this.pieceCoords); + + assert promoted != null; + + Color player = promoted.getColor(); + board.pieceLeaves(this.pieceCoords); + board.pieceComes(new Pawn(player), this.pieceCoords); + } + }