From 48a215eae5d900cd56a192344f2662bf290c8a78 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Fri, 4 Apr 2025 19:42:23 +0200 Subject: [PATCH] remove game signals --- .../main/java/chess/io/CommandExecutor.java | 72 +++++++++++-------- .../chess/io/commands/NewGameCommand.java | 1 + app/src/main/java/chess/model/ChessBoard.java | 29 ++++---- app/src/main/java/chess/model/Game.java | 43 ++++------- 4 files changed, 68 insertions(+), 77 deletions(-) diff --git a/app/src/main/java/chess/io/CommandExecutor.java b/app/src/main/java/chess/io/CommandExecutor.java index 53b039a..d2dcb84 100644 --- a/app/src/main/java/chess/io/CommandExecutor.java +++ b/app/src/main/java/chess/io/CommandExecutor.java @@ -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); } } diff --git a/app/src/main/java/chess/io/commands/NewGameCommand.java b/app/src/main/java/chess/io/commands/NewGameCommand.java index 116b214..4d6f465 100644 --- a/app/src/main/java/chess/io/commands/NewGameCommand.java +++ b/app/src/main/java/chess/io/commands/NewGameCommand.java @@ -50,6 +50,7 @@ public class NewGameCommand extends Command { game.resetPlayerTurn(); outputSystem.gameStarted(); + outputSystem.playerTurn(game.getPlayerTurn()); return CommandResult.NotMoved; } diff --git a/app/src/main/java/chess/model/ChessBoard.java b/app/src/main/java/chess/model/ChessBoard.java index 4355f93..ef380b8 100644 --- a/app/src/main/java/chess/model/ChessBoard.java +++ b/app/src/main/java/chess/model/ChessBoard.java @@ -2,7 +2,6 @@ package chess.model; import java.util.ArrayList; import java.util.List; -import java.util.Stack; import chess.model.visitor.KingIdentifier; import chess.model.visitor.PiecePathChecker; @@ -26,8 +25,8 @@ public class ChessBoard { } private final Cell[][] cells; - private final Stack moves; - private final Stack ejectedPieces; + private Move lastMove; + private Piece lastEjectedPiece; public ChessBoard() { this.cells = new Cell[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX]; @@ -36,43 +35,41 @@ public class ChessBoard { this.cells[i][j] = new Cell(); } } - this.moves = new Stack<>(); - this.ejectedPieces = new Stack<>(); + this.lastMove = null; + this.lastEjectedPiece = null; } public void applyMove(Move move) { - assert (move.isValid()); + assert move.isValid() : "Invalid move !"; Piece deadPiece = pieceAt(move.getFinish()); if (deadPiece != null) { - deadPiece.eject(this.moves.size()); - this.ejectedPieces.add(deadPiece); + this.lastEjectedPiece = deadPiece; + } else { + this.lastEjectedPiece = null; } Piece movingPiece = pieceAt(move.getStart()); pieceComes(movingPiece, move.getFinish()); pieceLeaves(move.getStart()); movingPiece.move(); - this.moves.add(move); + this.lastMove = move; } public void undoLastMove() { - assert !this.moves.empty() : "Can't undo at the beginning!"; + assert this.lastMove != null: "Can't undo at the beginning!"; - Move move = this.moves.pop(); + Move move = this.lastMove; Piece movingPiece = pieceAt(move.getFinish()); pieceComes(movingPiece, move.getStart()); pieceLeaves(move.getFinish()); movingPiece.unMove(); - if (this.ejectedPieces.empty()) + if (this.lastEjectedPiece == null) return; - Piece piece = this.ejectedPieces.lastElement(); - if (piece.getEjectedMoveNumber() != this.moves.size()) - return; + Piece piece = this.lastEjectedPiece; pieceComes(piece, move.getFinish()); - piece.eject(-1); } public boolean isCellEmpty(Coordinate coordinate) { diff --git a/app/src/main/java/chess/model/Game.java b/app/src/main/java/chess/model/Game.java index dc4df25..9a63c94 100644 --- a/app/src/main/java/chess/model/Game.java +++ b/app/src/main/java/chess/model/Game.java @@ -1,20 +1,14 @@ 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 enum GameStatus { + Check, CheckMate, OnGoing, Pat; + } public Game(ChessBoard board) { this.board = board; @@ -30,12 +24,10 @@ public class Game { public void resetPlayerTurn() { this.playerTurn = Color.White; - this.OnPlayerTurn.emit(playerTurn); } public void switchPlayerTurn() { playerTurn = Color.getEnemy(playerTurn); - this.OnPlayerTurn.emit(playerTurn); } public boolean checkPromotion(Coordinate pieceCoords) { @@ -52,31 +44,22 @@ public class Game { if (destY != enemyLine) return false; - this.OnPromote.emit(pieceCoords); return true; } - /** - * - * @return true if game should end - */ - public boolean checkGameStatus() { + public GameStatus checkGameStatus() { final Color enemy = Color.getEnemy(getPlayerTurn()); - if (this.board.isKingInCheck(enemy)) { - if (this.board.hasAllowedMoves(enemy)) { - this.OnCheck.emit(); - } else { - this.OnMat.emit(); - this.OnWin.emit(getPlayerTurn()); - return true; - } - } else if (!board.hasAllowedMoves(enemy)) { - this.OnPat.emit(); - return true; - } + if (this.board.isKingInCheck(enemy)) + if (this.board.hasAllowedMoves(enemy)) + return GameStatus.Check; + else + return GameStatus.CheckMate; - return false; + if (!board.hasAllowedMoves(enemy)) + return GameStatus.Pat; + + return GameStatus.OnGoing; } public boolean pawnShouldBePromoted() {