promote not that uggly

This commit is contained in:
2025-04-02 17:19:26 +02:00
parent 97cafb903a
commit 9179b3cda9
7 changed files with 142 additions and 27 deletions

View File

@@ -1,6 +1,12 @@
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 {
@@ -14,15 +20,18 @@ public class CommandExecutor {
public CommandResult executeCommand(Command command) {
CommandResult result = command.execute(this.game, this.outputSystem);
if (result == CommandResult.Moved)
if (result == CommandResult.Moved) {
checkPromotion(((MoveCommand) command).getMove().getFinish());
checkGameStatus();
alternatePlayers();
}
return result;
}
private void alternatePlayers() {
this.game.switchPlayerTurn();
this.outputSystem.playerTurn(this.game.getPlayerTurn());
}
}
public void setOutputSystem(OutputSystem outputSystem) {
this.outputSystem = outputSystem;
@@ -32,4 +41,37 @@ public class CommandExecutor {
this.game = game;
}
private void checkPromotion(Coordinate pieceCoords) {
Piece piece = this.game.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) {
outputSystem.promotePawn(pieceCoords);
}
}
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();
}
}
}