Command refactor

This commit is contained in:
2025-04-13 12:29:47 +02:00
parent 9f44548843
commit a23c334994
11 changed files with 207 additions and 100 deletions

View File

@@ -6,7 +6,9 @@ import chess.model.Game;
public abstract class Command {
public enum CommandResult {
/** The command was successfull. Should update display and switch player turn. */
/**
* The command was successfull. Should update display and switch player turn.
*/
Moved,
/** The command was successfull. Should not update anything */
NotMoved,
@@ -18,5 +20,4 @@ public abstract class Command {
public abstract CommandResult execute(Game game, GameListener outputSystem);
public void postExec(Game game, GameListener outputSystem) {}
}

View File

@@ -34,7 +34,6 @@ public class CommandExecutor {
if (command instanceof PlayerCommand playerCommand && result != CommandResult.NotAllowed)
this.game.addAction(playerCommand);
command.postExec(game, dispatcher);
return result;
}
@@ -45,21 +44,23 @@ public class CommandExecutor {
return;
case ActionNeeded:
this.dispatcher.updateDisplay();
this.dispatcher.onBoardUpdate();
return;
case Moved:
if (checkGameStatus())
this.dispatcher.onBoardUpdate();
if (checkGameStatus()) {
this.dispatcher.onGameEnd();
return;
}
switchPlayerTurn();
this.dispatcher.updateDisplay();
return;
}
}
private void switchPlayerTurn() {
this.game.switchPlayerTurn();
this.dispatcher.playerTurn(this.game.getPlayerTurn());
this.dispatcher.onPlayerTurn(this.game.getPlayerTurn());
}
/**
@@ -71,19 +72,19 @@ public class CommandExecutor {
switch (gameStatus) {
case Check:
this.dispatcher.kingIsInCheck();
this.dispatcher.onKingInCheck();
return false;
case CheckMate:
this.dispatcher.kingIsInMat();
this.dispatcher.winnerIs(this.game.getPlayerTurn());
this.dispatcher.onKingInMat();
this.dispatcher.onWin(this.game.getPlayerTurn());
return true;
case OnGoing:
return false;
case Pat:
this.dispatcher.patSituation();
this.dispatcher.onPatSituation();
return true;
}

View File

@@ -25,6 +25,26 @@ public class MoveCommand extends PlayerCommand {
@Override
public CommandResult execute(Game game, GameListener outputSystem) {
CommandResult result = processMove(game, outputSystem);
switch (result) {
case NotAllowed:
outputSystem.onMoveNotAllowed(this.move);
return result;
case Moved:
outputSystem.onMove(this.move);
return result;
case ActionNeeded:
case NotMoved:
return result;
}
return null;
}
private CommandResult processMove(Game game, GameListener outputSystem) {
final ChessBoard board = game.getBoard();
// we must promote the pending pawn before
@@ -50,10 +70,12 @@ public class MoveCommand extends PlayerCommand {
return CommandResult.NotAllowed;
}
if (board.pawnShouldBePromoted())
if (tryPromote(game, outputSystem)) {
return CommandResult.ActionNeeded;
}
board.setLastMove(this.move);
return CommandResult.Moved;
}
@@ -65,18 +87,14 @@ public class MoveCommand extends PlayerCommand {
return CommandResult.Moved;
}
@Override
public void postExec(Game game, GameListener outputSystem) {
tryPromote(game, outputSystem);
}
private void tryPromote(Game game, GameListener outputSystem) {
private boolean tryPromote(Game game, GameListener outputSystem) {
Coordinate pawnPos = game.getBoard().pawnPromotePosition();
if (pawnPos == null)
return;
return false;
outputSystem.promotePawn(pawnPos);
outputSystem.onPromotePawn(pawnPos);
return true;
}
}

View File

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

View File

@@ -15,8 +15,8 @@ public class SurrenderCommand extends Command {
@Override
public CommandResult execute(Game game, GameListener outputSystem) {
outputSystem.hasSurrendered(player);
outputSystem.winnerIs(Color.getEnemy(player));
outputSystem.onSurrender(player);
outputSystem.onWin(Color.getEnemy(player));
return CommandResult.NotMoved;
}

View File

@@ -2,34 +2,44 @@ package chess.controller.event;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
public abstract class GameAdaptator implements GameListener {
@Override
public void playerTurn(Color color) {}
public void onPlayerTurn(Color color) {}
@Override
public void winnerIs(Color color) {}
public void onWin(Color color) {}
@Override
public void kingIsInCheck() {}
public void onKingInCheck() {}
@Override
public void kingIsInMat() {}
public void onKingInMat() {}
@Override
public void patSituation() {}
public void onPatSituation() {}
@Override
public void hasSurrendered(Color color) {}
public void onSurrender(Color color) {}
@Override
public void gameStarted() {}
public void onGameStart() {}
@Override
public void promotePawn(Coordinate pieceCoords) {}
public void onPromotePawn(Coordinate pieceCoords) {}
@Override
public void updateDisplay() {}
public void onBoardUpdate() {}
@Override
public void onGameEnd() {}
@Override
public void onMove(Move move) {}
@Override
public void onMoveNotAllowed(Move move) {}
}

View File

@@ -8,6 +8,7 @@ import java.util.function.Consumer;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
public class GameDispatcher implements GameListener{
@@ -28,48 +29,63 @@ public class GameDispatcher implements GameListener{
}
@Override
public void playerTurn(Color color) {
asyncForEachCall((l) -> l.playerTurn(color));
public void onPlayerTurn(Color color) {
asyncForEachCall((l) -> l.onPlayerTurn(color));
}
@Override
public void winnerIs(Color color) {
asyncForEachCall((l) -> l.winnerIs(color));
public void onWin(Color color) {
asyncForEachCall((l) -> l.onWin(color));
}
@Override
public void kingIsInCheck() {
asyncForEachCall((l) -> l.kingIsInCheck());
public void onKingInCheck() {
asyncForEachCall((l) -> l.onKingInCheck());
}
@Override
public void kingIsInMat() {
asyncForEachCall((l) -> l.kingIsInMat());
public void onKingInMat() {
asyncForEachCall((l) -> l.onKingInMat());
}
@Override
public void patSituation() {
asyncForEachCall((l) -> l.patSituation());
public void onPatSituation() {
asyncForEachCall((l) -> l.onPatSituation());
}
@Override
public void hasSurrendered(Color color) {
asyncForEachCall((l) -> l.hasSurrendered(color));
public void onSurrender(Color color) {
asyncForEachCall((l) -> l.onSurrender(color));
}
@Override
public void gameStarted() {
asyncForEachCall((l) -> l.gameStarted());
public void onGameStart() {
asyncForEachCall((l) -> l.onGameStart());
}
@Override
public void promotePawn(Coordinate pieceCoords) {
asyncForEachCall((l) -> l.promotePawn(pieceCoords));
public void onPromotePawn(Coordinate pieceCoords) {
asyncForEachCall((l) -> l.onPromotePawn(pieceCoords));
}
@Override
public void updateDisplay() {
asyncForEachCall((l) -> l.updateDisplay());
public void onBoardUpdate() {
asyncForEachCall((l) -> l.onBoardUpdate());
}
@Override
public void onGameEnd() {
asyncForEachCall((l) -> l.onGameEnd());
}
@Override
public void onMove(Move move) {
asyncForEachCall((l) -> l.onMove(move));
}
@Override
public void onMoveNotAllowed(Move move) {
asyncForEachCall((l) -> l.onMoveNotAllowed(move));
}
public void stopService() {

View File

@@ -2,24 +2,74 @@ package chess.controller.event;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
public interface GameListener {
void playerTurn(Color color);
/**
* Invoked when the display of the board should be updated
*/
void onBoardUpdate();
/**
* Invoked when the game has ended (by a win or a draw)
*/
void onGameEnd();
void winnerIs(Color color);
/**
* Invoked when the game has started
*/
void onGameStart();
/**
* Invoked when a king is in check
*/
void onKingInCheck();
void kingIsInCheck();
/**
* Invoked when a checkmate occurs
*/
void onKingInMat();
/**
* Invoked when a valid move on the board occurs
* @param move the move to be processed
*/
void onMove(Move move);
void kingIsInMat();
/**
* Invoked when a sent move is not allowed
* @param move the move to be processed
*/
void onMoveNotAllowed(Move move);
/**
* Invoked when a pat situation occurs
*/
void onPatSituation();
/**
* Invoked when it's the player turn
* @param color the color of the player who should play
*/
void onPlayerTurn(Color color);
/**
* Invoked when a pawn should be promoted
* @param pieceCoords the coordinates of the pawn
*/
void onPromotePawn(Coordinate pieceCoords);
/**
* Invoked when a players surrenders
* @param coward the player who gave up
*/
void onSurrender(Color coward);
/**
* Invoked when a player wins (by checkmate or if the other one surrenders)
* @param winner
*/
void onWin(Color winner);
void patSituation();
void hasSurrendered(Color color);
void gameStarted();
void promotePawn(Coordinate pieceCoords);
void updateDisplay();
}