refactor OutputSystem

This commit is contained in:
2025-04-12 11:12:36 +02:00
parent 0d3d77781f
commit 8190090adc
19 changed files with 176 additions and 49 deletions

View File

@@ -2,24 +2,25 @@ package chess.controller;
import chess.controller.Command.CommandResult;
import chess.controller.commands.UndoCommand;
import chess.controller.event.GameDispatcher;
import chess.controller.event.GameListener;
import chess.model.Game;
import chess.model.Game.GameStatus;
public class CommandExecutor {
private Game game;
private OutputSystem outputSystem;
private final GameDispatcher dispatcher;
public CommandExecutor() {
this.game = null;
this.outputSystem = null;
this.dispatcher = new GameDispatcher();
}
public synchronized 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);
CommandResult result = command.execute(this.game, this.dispatcher);
// non player commands are not supposed to return move result
assert result != CommandResult.Moved || command instanceof PlayerCommand || command instanceof UndoCommand;
@@ -29,7 +30,7 @@ public class CommandExecutor {
if (command instanceof PlayerCommand playerCommand && result != CommandResult.NotAllowed)
this.game.addAction(playerCommand);
command.postExec(game, outputSystem);
command.postExec(game, dispatcher);
return result;
}
@@ -40,21 +41,21 @@ public class CommandExecutor {
return;
case ActionNeeded:
this.outputSystem.updateDisplay();
this.dispatcher.updateDisplay();
return;
case Moved:
if (checkGameStatus())
return;
switchPlayerTurn();
this.outputSystem.updateDisplay();
this.dispatcher.updateDisplay();
return;
}
}
private void switchPlayerTurn() {
this.game.switchPlayerTurn();
this.outputSystem.playerTurn(this.game.getPlayerTurn());
this.dispatcher.playerTurn(this.game.getPlayerTurn());
}
/**
@@ -66,27 +67,27 @@ public class CommandExecutor {
switch (gameStatus) {
case Check:
this.outputSystem.kingIsInCheck();
this.dispatcher.kingIsInCheck();
return false;
case CheckMate:
this.outputSystem.kingIsInMat();
this.outputSystem.winnerIs(this.game.getPlayerTurn());
this.dispatcher.kingIsInMat();
this.dispatcher.winnerIs(this.game.getPlayerTurn());
return true;
case OnGoing:
return false;
case Pat:
this.outputSystem.patSituation();
this.dispatcher.patSituation();
return true;
}
return false;
}
public void setOutputSystem(OutputSystem outputSystem) {
this.outputSystem = outputSystem;
public void addListener(GameListener listener) {
this.dispatcher.addListener(listener);
}
public void setGame(Game game) {