Files
3DChess/app/src/main/java/chess/io/CommandExecutor.java
2025-04-03 21:02:04 +02:00

66 lines
1.8 KiB
Java

package chess.io;
import chess.io.commands.MoveCommand;
import chess.model.Game;
public class CommandExecutor {
private Game game;
private OutputSystem outputSystem;
public CommandExecutor() {
this.game = null;
this.outputSystem = null;
}
public CommandResult executeCommand(Command command) {
assert this.game != null : "No input game specified !";
assert this.outputSystem != null : "No output system specified !";
CommandResult result = command.execute(this.game, this.outputSystem);
if (result == CommandResult.Moved) {
this.game.checkPromotion(((MoveCommand) command).getMove().getFinish());
this.game.checkGameStatus();
this.game.switchPlayerTurn();
}
return result;
}
public void setOutputSystem(OutputSystem outputSystem) {
unbindListeners();
this.outputSystem = outputSystem;
bindListeners();
}
public void setGame(Game game) {
unbindListeners();
this.game = game;
bindListeners();
}
private void unbindListeners() {
if (this.game == null || this.outputSystem == null)
return;
this.game.OnCheck.disconnect(outputSystem::kingIsInCheck);
this.game.OnMat.disconnect(outputSystem::kingIsInMat);
this.game.OnPat.disconnect(outputSystem::patSituation);
this.game.OnPromote.disconnect(outputSystem::promotePawn);
this.game.OnWin.disconnect(outputSystem::winnerIs);
this.game.OnPlayerTurn.disconnect(outputSystem::playerTurn);
}
private void bindListeners() {
if (this.game == null || this.outputSystem == null)
return;
this.game.OnCheck.connect(outputSystem::kingIsInCheck);
this.game.OnMat.connect(outputSystem::kingIsInMat);
this.game.OnPat.connect(outputSystem::patSituation);
this.game.OnPromote.connect(outputSystem::promotePawn);
this.game.OnWin.connect(outputSystem::winnerIs);
this.game.OnPlayerTurn.connect(outputSystem::playerTurn);
}
}