From a81da804f006a59d5c2fff70613349ab75956b77 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Thu, 3 Apr 2025 21:02:04 +0200 Subject: [PATCH] refactor game --- .../main/java/chess/io/CommandExecutor.java | 70 ++++++++----------- app/src/main/java/chess/io/OutputSystem.java | 2 - .../java/chess/io/commands/MoveCommand.java | 1 - app/src/main/java/chess/model/Game.java | 47 +++++++++++++ 4 files changed, 76 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/chess/io/CommandExecutor.java b/app/src/main/java/chess/io/CommandExecutor.java index 677d09a..b452b8a 100644 --- a/app/src/main/java/chess/io/CommandExecutor.java +++ b/app/src/main/java/chess/io/CommandExecutor.java @@ -1,12 +1,7 @@ package chess.io; import chess.io.commands.MoveCommand; -import chess.model.ChessBoard; -import chess.model.Color; -import chess.model.Coordinate; import chess.model.Game; -import chess.model.Piece; -import chess.model.pieces.Pawn; public class CommandExecutor { @@ -19,59 +14,52 @@ public class CommandExecutor { } 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) { - checkPromotion(((MoveCommand) command).getMove().getFinish()); - checkGameStatus(); - alternatePlayers(); + this.game.checkPromotion(((MoveCommand) command).getMove().getFinish()); + this.game.checkGameStatus(); + this.game.switchPlayerTurn(); } return result; } - private void alternatePlayers() { - this.game.switchPlayerTurn(); - this.outputSystem.playerTurn(this.game.getPlayerTurn()); - } - public void setOutputSystem(OutputSystem outputSystem) { + unbindListeners(); this.outputSystem = outputSystem; + bindListeners(); } public void setGame(Game game) { + unbindListeners(); this.game = game; + bindListeners(); } - private void checkPromotion(Coordinate pieceCoords) { - - Piece piece = this.game.getBoard().pieceAt(pieceCoords); - - if (piece == null || !(piece instanceof Pawn)) + private void unbindListeners() { + if (this.game == null || this.outputSystem == null) return; - - int destY = pieceCoords.getY(); - - int enemyLine = piece.getColor() == Color.White ? 0 : 7; - - if (destY == enemyLine) { - outputSystem.promotePawn(pieceCoords); - } + 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 checkGameStatus() { - final ChessBoard board = game.getBoard(); - - final Color enemy = Color.getEnemy(game.getPlayerTurn()); - - if (board.isKingInCheck(enemy)) { - if (board.hasAllowedMoves(enemy)) { - outputSystem.kingIsInCheck(); - } else { - outputSystem.kingIsInMat(); - outputSystem.winnerIs(game.getPlayerTurn()); - } - } else if (!board.hasAllowedMoves(enemy)) { - outputSystem.patSituation(); - } + 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); } + + } diff --git a/app/src/main/java/chess/io/OutputSystem.java b/app/src/main/java/chess/io/OutputSystem.java index dd488c2..521ef8b 100644 --- a/app/src/main/java/chess/io/OutputSystem.java +++ b/app/src/main/java/chess/io/OutputSystem.java @@ -1,7 +1,5 @@ package chess.io; -import chess.io.commands.MoveCommand; -import chess.io.commands.PromoteCommand; import chess.model.Color; import chess.model.Coordinate; diff --git a/app/src/main/java/chess/io/commands/MoveCommand.java b/app/src/main/java/chess/io/commands/MoveCommand.java index bb3e59c..0a23565 100644 --- a/app/src/main/java/chess/io/commands/MoveCommand.java +++ b/app/src/main/java/chess/io/commands/MoveCommand.java @@ -3,7 +3,6 @@ package chess.io.commands; import chess.io.Command; import chess.io.CommandResult; import chess.io.OutputSystem; -import chess.io.commands.PromoteCommand.PromoteType; import chess.model.ChessBoard; import chess.model.Game; import chess.model.Move; diff --git a/app/src/main/java/chess/model/Game.java b/app/src/main/java/chess/model/Game.java index 65e7582..c7506ef 100644 --- a/app/src/main/java/chess/model/Game.java +++ b/app/src/main/java/chess/model/Game.java @@ -1,9 +1,22 @@ package chess.model; +import chess.model.pieces.Pawn; +import common.Signal0; +import common.Signal1; + public class Game { private final ChessBoard board; private Color playerTurn; + public final Signal1 OnPromote = new Signal1<>(); + public final Signal1 OnWin = new Signal1<>(); + public final Signal1 OnPlayerTurn = new Signal1<>(); + + public final Signal0 OnCheck = new Signal0(); + public final Signal0 OnMat = new Signal0(); + public final Signal0 OnPat = new Signal0(); + + public Game(ChessBoard board) { this.board = board; } @@ -22,6 +35,40 @@ public class Game { public void switchPlayerTurn() { playerTurn = Color.getEnemy(playerTurn); + this.OnPlayerTurn.emit(playerTurn); + } + + public void checkPromotion(Coordinate pieceCoords) { + + Piece piece = getBoard().pieceAt(pieceCoords); + + if (piece == null || !(piece instanceof Pawn)) + return; + + int destY = pieceCoords.getY(); + + int enemyLine = piece.getColor() == Color.White ? 0 : 7; + + if (destY == enemyLine) { + OnPromote.emit(pieceCoords); + } + } + + public void checkGameStatus() { + final ChessBoard board = getBoard(); + + final Color enemy = Color.getEnemy(getPlayerTurn()); + + if (board.isKingInCheck(enemy)) { + if (board.hasAllowedMoves(enemy)) { + OnCheck.emit(); + } else { + OnMat.emit(); + OnWin.emit(getPlayerTurn()); + } + } else if (!board.hasAllowedMoves(enemy)) { + OnPat.emit(); + } } }