player command

This commit is contained in:
2025-04-04 14:46:58 +02:00
parent 55774b4605
commit 873ffc05d3
6 changed files with 51 additions and 10 deletions

View File

@@ -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();
}
}

View File

@@ -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);
}

View File

@@ -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'");
}
}

View File

@@ -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'");
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}