refactor game

This commit is contained in:
2025-04-03 21:02:04 +02:00
parent 9179b3cda9
commit a81da804f0
4 changed files with 76 additions and 44 deletions

View File

@@ -1,12 +1,7 @@
package chess.io; package chess.io;
import chess.io.commands.MoveCommand; import chess.io.commands.MoveCommand;
import chess.model.ChessBoard;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Game; import chess.model.Game;
import chess.model.Piece;
import chess.model.pieces.Pawn;
public class CommandExecutor { public class CommandExecutor {
@@ -19,59 +14,52 @@ public class CommandExecutor {
} }
public CommandResult executeCommand(Command command) { 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); CommandResult result = command.execute(this.game, this.outputSystem);
if (result == CommandResult.Moved) { if (result == CommandResult.Moved) {
checkPromotion(((MoveCommand) command).getMove().getFinish()); this.game.checkPromotion(((MoveCommand) command).getMove().getFinish());
checkGameStatus(); this.game.checkGameStatus();
alternatePlayers(); this.game.switchPlayerTurn();
} }
return result; return result;
} }
private void alternatePlayers() {
this.game.switchPlayerTurn();
this.outputSystem.playerTurn(this.game.getPlayerTurn());
}
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 checkPromotion(Coordinate pieceCoords) { private void unbindListeners() {
if (this.game == null || this.outputSystem == null)
Piece piece = this.game.getBoard().pieceAt(pieceCoords);
if (piece == null || !(piece instanceof Pawn))
return; return;
this.game.OnCheck.disconnect(outputSystem::kingIsInCheck);
int destY = pieceCoords.getY(); this.game.OnMat.disconnect(outputSystem::kingIsInMat);
this.game.OnPat.disconnect(outputSystem::patSituation);
int enemyLine = piece.getColor() == Color.White ? 0 : 7; this.game.OnPromote.disconnect(outputSystem::promotePawn);
this.game.OnWin.disconnect(outputSystem::winnerIs);
if (destY == enemyLine) { this.game.OnPlayerTurn.disconnect(outputSystem::playerTurn);
outputSystem.promotePawn(pieceCoords);
}
} }
private void checkGameStatus() { private void bindListeners() {
final ChessBoard board = game.getBoard(); if (this.game == null || this.outputSystem == null)
return;
final Color enemy = Color.getEnemy(game.getPlayerTurn()); this.game.OnCheck.connect(outputSystem::kingIsInCheck);
this.game.OnMat.connect(outputSystem::kingIsInMat);
if (board.isKingInCheck(enemy)) { this.game.OnPat.connect(outputSystem::patSituation);
if (board.hasAllowedMoves(enemy)) { this.game.OnPromote.connect(outputSystem::promotePawn);
outputSystem.kingIsInCheck(); this.game.OnWin.connect(outputSystem::winnerIs);
} else { this.game.OnPlayerTurn.connect(outputSystem::playerTurn);
outputSystem.kingIsInMat();
outputSystem.winnerIs(game.getPlayerTurn());
}
} else if (!board.hasAllowedMoves(enemy)) {
outputSystem.patSituation();
}
} }
} }

View File

@@ -1,7 +1,5 @@
package chess.io; package chess.io;
import chess.io.commands.MoveCommand;
import chess.io.commands.PromoteCommand;
import chess.model.Color; import chess.model.Color;
import chess.model.Coordinate; import chess.model.Coordinate;

View File

@@ -3,7 +3,6 @@ package chess.io.commands;
import chess.io.Command; import chess.io.Command;
import chess.io.CommandResult; import chess.io.CommandResult;
import chess.io.OutputSystem; import chess.io.OutputSystem;
import chess.io.commands.PromoteCommand.PromoteType;
import chess.model.ChessBoard; import chess.model.ChessBoard;
import chess.model.Game; import chess.model.Game;
import chess.model.Move; import chess.model.Move;

View File

@@ -1,9 +1,22 @@
package chess.model; package chess.model;
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 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 Game(ChessBoard board) { public Game(ChessBoard board) {
this.board = board; this.board = board;
} }
@@ -22,6 +35,40 @@ public class Game {
public void switchPlayerTurn() { public void switchPlayerTurn() {
playerTurn = Color.getEnemy(playerTurn); 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();
}
} }
} }