Command refactor

This commit is contained in:
2025-04-13 12:29:47 +02:00
parent 9f44548843
commit a23c334994
11 changed files with 207 additions and 100 deletions

View File

@@ -25,6 +25,26 @@ public class MoveCommand extends PlayerCommand {
@Override
public CommandResult execute(Game game, GameListener outputSystem) {
CommandResult result = processMove(game, outputSystem);
switch (result) {
case NotAllowed:
outputSystem.onMoveNotAllowed(this.move);
return result;
case Moved:
outputSystem.onMove(this.move);
return result;
case ActionNeeded:
case NotMoved:
return result;
}
return null;
}
private CommandResult processMove(Game game, GameListener outputSystem) {
final ChessBoard board = game.getBoard();
// we must promote the pending pawn before
@@ -50,10 +70,12 @@ public class MoveCommand extends PlayerCommand {
return CommandResult.NotAllowed;
}
if (board.pawnShouldBePromoted())
if (tryPromote(game, outputSystem)) {
return CommandResult.ActionNeeded;
}
board.setLastMove(this.move);
return CommandResult.Moved;
}
@@ -65,18 +87,14 @@ public class MoveCommand extends PlayerCommand {
return CommandResult.Moved;
}
@Override
public void postExec(Game game, GameListener outputSystem) {
tryPromote(game, outputSystem);
}
private void tryPromote(Game game, GameListener outputSystem) {
private boolean tryPromote(Game game, GameListener outputSystem) {
Coordinate pawnPos = game.getBoard().pawnPromotePosition();
if (pawnPos == null)
return;
return false;
outputSystem.promotePawn(pawnPos);
outputSystem.onPromotePawn(pawnPos);
return true;
}
}