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;
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();
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);
}
}

View File

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

View File

@@ -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<Move> moves;
private final Stack<Piece> 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) {

View File

@@ -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<Coordinate> OnPromote = new Signal1<>();
public final Signal1<Color> OnWin = new Signal1<>();
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 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() {