post exec

This commit is contained in:
2025-04-05 10:12:25 +02:00
parent 63a1e261e8
commit a0af8caf57
3 changed files with 11 additions and 4 deletions

View File

@@ -16,4 +16,6 @@ public abstract class Command {
} }
public abstract CommandResult execute(Game game, OutputSystem outputSystem); public abstract CommandResult execute(Game game, OutputSystem outputSystem);
public void postExec(Game game, OutputSystem outputSystem) {}
} }

View File

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

View File

@@ -50,7 +50,7 @@ public class MoveCommand extends PlayerCommand {
return CommandResult.NotAllowed; return CommandResult.NotAllowed;
} }
if (shouldPromote(game, outputSystem)) if (game.pawnShouldBePromoted())
return CommandResult.ActionNeeded; return CommandResult.ActionNeeded;
return CommandResult.Moved; return CommandResult.Moved;
@@ -63,14 +63,18 @@ public class MoveCommand extends PlayerCommand {
board.undoMove(move, deadPiece); board.undoMove(move, deadPiece);
} }
private boolean shouldPromote(Game game, OutputSystem outputSystem) { @Override
public void postExec(Game game, OutputSystem outputSystem) {
tryPromote(game, outputSystem);
}
private void tryPromote(Game game, OutputSystem outputSystem) {
Coordinate pawnPos = game.pawnPromotePosition(); Coordinate pawnPos = game.pawnPromotePosition();
if (pawnPos == null) if (pawnPos == null)
return false; return;
outputSystem.promotePawn(pawnPos); outputSystem.promotePawn(pawnPos);
return true;
} }
} }