remove game signals
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package chess.io;
|
||||
|
||||
import chess.io.commands.MoveCommand;
|
||||
import chess.io.commands.PromoteCommand;
|
||||
import chess.model.Game;
|
||||
import chess.model.Game.GameStatus;
|
||||
|
||||
public class CommandExecutor {
|
||||
|
||||
@@ -33,48 +33,58 @@ public class CommandExecutor {
|
||||
|
||||
if (command instanceof MoveCommand move) {
|
||||
boolean needsPromote = this.game.checkPromotion(move.getMove().getFinish());
|
||||
if (this.game.checkGameStatus())
|
||||
if (needsPromote)
|
||||
this.outputSystem.promotePawn(move.getMove().getFinish());
|
||||
|
||||
if (checkGameStatus())
|
||||
return;
|
||||
|
||||
if (!needsPromote)
|
||||
this.game.switchPlayerTurn();
|
||||
if(!needsPromote)
|
||||
switchPlayerTurn();
|
||||
} else if (command instanceof PlayerCommand) {
|
||||
this.game.switchPlayerTurn();
|
||||
switchPlayerTurn();
|
||||
}
|
||||
}
|
||||
|
||||
private void switchPlayerTurn() {
|
||||
this.game.switchPlayerTurn();
|
||||
this.outputSystem.playerTurn(this.game.getPlayerTurn());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return True if the game is over
|
||||
*/
|
||||
private boolean checkGameStatus() {
|
||||
GameStatus gameStatus = this.game.checkGameStatus();
|
||||
|
||||
switch (gameStatus) {
|
||||
case Check:
|
||||
this.outputSystem.kingIsInCheck();
|
||||
return false;
|
||||
|
||||
case CheckMate:
|
||||
this.outputSystem.kingIsInMat();
|
||||
this.outputSystem.winnerIs(this.game.getPlayerTurn());
|
||||
return true;
|
||||
|
||||
case OnGoing:
|
||||
return false;
|
||||
|
||||
case Pat:
|
||||
this.outputSystem.patSituation();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user