diff --git a/app/src/main/java/chess/io/Command.java b/app/src/main/java/chess/io/Command.java index 38288b8..d49c73c 100644 --- a/app/src/main/java/chess/io/Command.java +++ b/app/src/main/java/chess/io/Command.java @@ -5,7 +5,14 @@ import chess.model.Game; public abstract class Command { public enum CommandResult { - Moved, NotMoved, NotAllowed; + /** The command was successfull. Should update display and switch player turn. */ + Moved, + /** The command was successfull. Should not update anything */ + NotMoved, + /** The command was successfull. Should only update display */ + ActionNeeded, + /** The command was not successfull */ + NotAllowed; } public abstract CommandResult execute(Game game, OutputSystem outputSystem); diff --git a/app/src/main/java/chess/io/CommandExecutor.java b/app/src/main/java/chess/io/CommandExecutor.java index 9db17c5..5732604 100644 --- a/app/src/main/java/chess/io/CommandExecutor.java +++ b/app/src/main/java/chess/io/CommandExecutor.java @@ -1,7 +1,6 @@ package chess.io; import chess.io.Command.CommandResult; -import chess.io.commands.MoveCommand; import chess.model.Game; import chess.model.Game.GameStatus; @@ -29,21 +28,21 @@ public class CommandExecutor { } private void processResult(Command command, CommandResult result) { - if (result != CommandResult.Moved) - return; - - if (command instanceof MoveCommand move) { - boolean needsPromote = this.game.pawnShouldBePromoted(); - if (needsPromote) - this.outputSystem.promotePawn(move.getMove().getFinish()); - - if (checkGameStatus()) + switch (result) { + case NotAllowed: + case NotMoved: return; - if(!needsPromote) + case ActionNeeded: + this.outputSystem.updateDisplay(); + return; + + case Moved: + if (checkGameStatus()) + return; switchPlayerTurn(); - } else if (command instanceof PlayerCommand) { - switchPlayerTurn(); + this.outputSystem.updateDisplay(); + return; } } @@ -63,7 +62,7 @@ public class CommandExecutor { case Check: this.outputSystem.kingIsInCheck(); return false; - + case CheckMate: this.outputSystem.kingIsInMat(); this.outputSystem.winnerIs(this.game.getPlayerTurn()); diff --git a/app/src/main/java/chess/io/OutputSystem.java b/app/src/main/java/chess/io/OutputSystem.java index 521ef8b..837bf70 100644 --- a/app/src/main/java/chess/io/OutputSystem.java +++ b/app/src/main/java/chess/io/OutputSystem.java @@ -20,4 +20,6 @@ public interface OutputSystem { void gameStarted(); void promotePawn(Coordinate pieceCoords); + + void updateDisplay(); } diff --git a/app/src/main/java/chess/io/commands/MoveCommand.java b/app/src/main/java/chess/io/commands/MoveCommand.java index c8249e9..755109f 100644 --- a/app/src/main/java/chess/io/commands/MoveCommand.java +++ b/app/src/main/java/chess/io/commands/MoveCommand.java @@ -3,6 +3,7 @@ package chess.io.commands; import chess.io.OutputSystem; import chess.io.PlayerCommand; import chess.model.ChessBoard; +import chess.model.Coordinate; import chess.model.Game; import chess.model.Move; import chess.model.Piece; @@ -49,6 +50,9 @@ public class MoveCommand extends PlayerCommand { return CommandResult.NotAllowed; } + if (shouldPromote(game, outputSystem)) + return CommandResult.ActionNeeded; + return CommandResult.Moved; } @@ -59,4 +63,14 @@ public class MoveCommand extends PlayerCommand { board.undoMove(move, deadPiece); } + private boolean shouldPromote(Game game, OutputSystem outputSystem) { + Coordinate pawnPos = game.pawnPromotePosition(); + + if (pawnPos == null) + return false; + + outputSystem.promotePawn(pawnPos); + return true; + } + } diff --git a/app/src/main/java/chess/simplerender/Window.java b/app/src/main/java/chess/simplerender/Window.java index 0dac115..eb510cb 100644 --- a/app/src/main/java/chess/simplerender/Window.java +++ b/app/src/main/java/chess/simplerender/Window.java @@ -44,11 +44,7 @@ public class Window extends JFrame implements OutputSystem { } private CommandResult sendCommand(Command command) { - CommandResult result = this.commandExecutor.executeCommand(command); - if (result == CommandResult.Moved) { - updateBoard(); - } - return result; + return this.commandExecutor.executeCommand(command); } private Color getCellColor(int x, int y) { @@ -241,4 +237,9 @@ public class Window extends JFrame implements OutputSystem { }); } + @Override + public void updateDisplay() { + updateBoard(); + } + }