remove game signals

This commit is contained in:
2025-04-04 19:42:23 +02:00
parent 55ef180f57
commit 48a215eae5
4 changed files with 68 additions and 77 deletions

View File

@@ -1,8 +1,8 @@
package chess.io; package chess.io;
import chess.io.commands.MoveCommand; import chess.io.commands.MoveCommand;
import chess.io.commands.PromoteCommand;
import chess.model.Game; import chess.model.Game;
import chess.model.Game.GameStatus;
public class CommandExecutor { public class CommandExecutor {
@@ -33,48 +33,58 @@ public class CommandExecutor {
if (command instanceof MoveCommand move) { if (command instanceof MoveCommand move) {
boolean needsPromote = this.game.checkPromotion(move.getMove().getFinish()); boolean needsPromote = this.game.checkPromotion(move.getMove().getFinish());
if (this.game.checkGameStatus()) if (needsPromote)
this.outputSystem.promotePawn(move.getMove().getFinish());
if (checkGameStatus())
return; return;
if (!needsPromote) if(!needsPromote)
this.game.switchPlayerTurn(); switchPlayerTurn();
} else if (command instanceof PlayerCommand) { } 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) { public void setOutputSystem(OutputSystem outputSystem) {
unbindListeners();
this.outputSystem = outputSystem; this.outputSystem = outputSystem;
bindListeners();
} }
public void setGame(Game game) { public void setGame(Game game) {
unbindListeners();
this.game = game; 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);
} }
} }

View File

@@ -50,6 +50,7 @@ public class NewGameCommand extends Command {
game.resetPlayerTurn(); game.resetPlayerTurn();
outputSystem.gameStarted(); outputSystem.gameStarted();
outputSystem.playerTurn(game.getPlayerTurn());
return CommandResult.NotMoved; return CommandResult.NotMoved;
} }

View File

@@ -2,7 +2,6 @@ package chess.model;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Stack;
import chess.model.visitor.KingIdentifier; import chess.model.visitor.KingIdentifier;
import chess.model.visitor.PiecePathChecker; import chess.model.visitor.PiecePathChecker;
@@ -26,8 +25,8 @@ public class ChessBoard {
} }
private final Cell[][] cells; private final Cell[][] cells;
private final Stack<Move> moves; private Move lastMove;
private final Stack<Piece> ejectedPieces; private Piece lastEjectedPiece;
public ChessBoard() { public ChessBoard() {
this.cells = new Cell[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX]; this.cells = new Cell[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX];
@@ -36,43 +35,41 @@ public class ChessBoard {
this.cells[i][j] = new Cell(); this.cells[i][j] = new Cell();
} }
} }
this.moves = new Stack<>(); this.lastMove = null;
this.ejectedPieces = new Stack<>(); this.lastEjectedPiece = null;
} }
public void applyMove(Move move) { public void applyMove(Move move) {
assert (move.isValid()); assert move.isValid() : "Invalid move !";
Piece deadPiece = pieceAt(move.getFinish()); Piece deadPiece = pieceAt(move.getFinish());
if (deadPiece != null) { if (deadPiece != null) {
deadPiece.eject(this.moves.size()); this.lastEjectedPiece = deadPiece;
this.ejectedPieces.add(deadPiece); } else {
this.lastEjectedPiece = null;
} }
Piece movingPiece = pieceAt(move.getStart()); Piece movingPiece = pieceAt(move.getStart());
pieceComes(movingPiece, move.getFinish()); pieceComes(movingPiece, move.getFinish());
pieceLeaves(move.getStart()); pieceLeaves(move.getStart());
movingPiece.move(); movingPiece.move();
this.moves.add(move); this.lastMove = move;
} }
public void undoLastMove() { 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()); Piece movingPiece = pieceAt(move.getFinish());
pieceComes(movingPiece, move.getStart()); pieceComes(movingPiece, move.getStart());
pieceLeaves(move.getFinish()); pieceLeaves(move.getFinish());
movingPiece.unMove(); movingPiece.unMove();
if (this.ejectedPieces.empty()) if (this.lastEjectedPiece == null)
return; return;
Piece piece = this.ejectedPieces.lastElement(); Piece piece = this.lastEjectedPiece;
if (piece.getEjectedMoveNumber() != this.moves.size())
return;
pieceComes(piece, move.getFinish()); pieceComes(piece, move.getFinish());
piece.eject(-1);
} }
public boolean isCellEmpty(Coordinate coordinate) { public boolean isCellEmpty(Coordinate coordinate) {

View File

@@ -1,20 +1,14 @@
package chess.model; package chess.model;
import chess.model.pieces.Pawn; import chess.model.pieces.Pawn;
import common.Signal0;
import common.Signal1;
public class Game { public class Game {
private final ChessBoard board; private final ChessBoard board;
private Color playerTurn; private Color playerTurn;
public final Signal1<Coordinate> OnPromote = new Signal1<>(); public enum GameStatus {
public final Signal1<Color> OnWin = new Signal1<>(); Check, CheckMate, OnGoing, Pat;
public final Signal1<Color> 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) { public Game(ChessBoard board) {
this.board = board; this.board = board;
@@ -30,12 +24,10 @@ public class Game {
public void resetPlayerTurn() { public void resetPlayerTurn() {
this.playerTurn = Color.White; this.playerTurn = Color.White;
this.OnPlayerTurn.emit(playerTurn);
} }
public void switchPlayerTurn() { public void switchPlayerTurn() {
playerTurn = Color.getEnemy(playerTurn); playerTurn = Color.getEnemy(playerTurn);
this.OnPlayerTurn.emit(playerTurn);
} }
public boolean checkPromotion(Coordinate pieceCoords) { public boolean checkPromotion(Coordinate pieceCoords) {
@@ -52,31 +44,22 @@ public class Game {
if (destY != enemyLine) if (destY != enemyLine)
return false; return false;
this.OnPromote.emit(pieceCoords);
return true; return true;
} }
/** public GameStatus checkGameStatus() {
*
* @return true if game should end
*/
public boolean checkGameStatus() {
final Color enemy = Color.getEnemy(getPlayerTurn()); final Color enemy = Color.getEnemy(getPlayerTurn());
if (this.board.isKingInCheck(enemy)) { if (this.board.isKingInCheck(enemy))
if (this.board.hasAllowedMoves(enemy)) { if (this.board.hasAllowedMoves(enemy))
this.OnCheck.emit(); return GameStatus.Check;
} else { else
this.OnMat.emit(); return GameStatus.CheckMate;
this.OnWin.emit(getPlayerTurn());
return true;
}
} else if (!board.hasAllowedMoves(enemy)) {
this.OnPat.emit();
return true;
}
return false; if (!board.hasAllowedMoves(enemy))
return GameStatus.Pat;
return GameStatus.OnGoing;
} }
public boolean pawnShouldBePromoted() { public boolean pawnShouldBePromoted() {