From 07016be5d8f731c67da0227b78ea2aba78395b72 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Mon, 14 Apr 2025 10:34:54 +0200 Subject: [PATCH] add castling commands --- .../commands/GetAllowedCastlingsCommand.java | 37 +++++++++++++++++++ app/src/main/java/chess/model/ChessBoard.java | 5 ++- .../java/chess/view/simplerender/Window.java | 22 ++++++++++- 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/chess/controller/commands/GetAllowedCastlingsCommand.java diff --git a/app/src/main/java/chess/controller/commands/GetAllowedCastlingsCommand.java b/app/src/main/java/chess/controller/commands/GetAllowedCastlingsCommand.java new file mode 100644 index 0000000..773b8b8 --- /dev/null +++ b/app/src/main/java/chess/controller/commands/GetAllowedCastlingsCommand.java @@ -0,0 +1,37 @@ +package chess.controller.commands; + +import chess.controller.Command; +import chess.controller.event.GameListener; +import chess.model.Game; + +public class GetAllowedCastlingsCommand extends Command{ + + public enum CastlingResult { + None, Small, Big, Both; + } + + private CastlingResult castlingResult; + + public GetAllowedCastlingsCommand() {} + + @Override + public CommandResult execute(Game game, GameListener outputSystem) { + boolean canSmallCastle = game.getBoard().canSmallCastle(game.getPlayerTurn()); + boolean canBigCastle = game.getBoard().canBigCastle(game.getPlayerTurn()); + + int result = 0; + if (canSmallCastle) + result += 1; + if (canBigCastle) + result += 2; + + this.castlingResult = CastlingResult.values()[result]; + + return CommandResult.NotMoved; + } + + public CastlingResult getCastlingResult() { + return castlingResult; + } + +} diff --git a/app/src/main/java/chess/model/ChessBoard.java b/app/src/main/java/chess/model/ChessBoard.java index afb23b8..db39c41 100644 --- a/app/src/main/java/chess/model/ChessBoard.java +++ b/app/src/main/java/chess/model/ChessBoard.java @@ -230,7 +230,10 @@ public class ChessBoard { undoLastMove(); } - return true; + Coordinate rookObstacleCoords = Coordinate.fromIndex(rookCoords.toIndex() - kingDirection.getIndexOffset()); + Piece obstacle = pieceAt(rookObstacleCoords); + + return obstacle == null; } public boolean canSmallCastle(Color color) { diff --git a/app/src/main/java/chess/view/simplerender/Window.java b/app/src/main/java/chess/view/simplerender/Window.java index bc265c0..d35c012 100644 --- a/app/src/main/java/chess/view/simplerender/Window.java +++ b/app/src/main/java/chess/view/simplerender/Window.java @@ -5,6 +5,8 @@ import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.io.IOException; import java.util.List; @@ -19,12 +21,14 @@ import chess.controller.Command; import chess.controller.Command.CommandResult; import chess.controller.CommandExecutor; import chess.controller.commands.CastlingCommand; +import chess.controller.commands.GetAllowedCastlingsCommand; import chess.controller.commands.GetAllowedMovesPieceCommand; import chess.controller.commands.GetPieceAtCommand; import chess.controller.commands.MoveCommand; import chess.controller.commands.PromoteCommand; import chess.controller.commands.PromoteCommand.PromoteType; import chess.controller.commands.UndoCommand; +import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult; import chess.controller.event.GameListener; import chess.model.Coordinate; import chess.model.Move; @@ -44,7 +48,7 @@ public class Window extends JFrame implements GameListener { private final boolean showPopups; - public Window(CommandExecutor commandExecutor, boolean showPopups) { + public Window(final CommandExecutor commandExecutor, boolean showPopups) { this.cells = new JLabel[8][8]; this.displayText = new JLabel(); this.commandExecutor = commandExecutor; @@ -53,6 +57,12 @@ public class Window extends JFrame implements GameListener { setVisible(true); setLocationRelativeTo(null); setDefaultCloseOperation(DISPOSE_ON_CLOSE); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosed(WindowEvent e) { + commandExecutor.close(); + } + }); } private CommandResult sendCommand(Command command) { @@ -193,9 +203,19 @@ public class Window extends JFrame implements GameListener { this.lastClick = null; } + private void updateButtons() { + GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand(); + sendCommand(cmd); + this.castlingButton.setEnabled( + cmd.getCastlingResult() == CastlingResult.Small || cmd.getCastlingResult() == CastlingResult.Both); + this.bigCastlingButton.setEnabled( + cmd.getCastlingResult() == CastlingResult.Big || cmd.getCastlingResult() == CastlingResult.Both); + } + @Override public void onPlayerTurn(chess.model.Color color) { this.displayText.setText("Current turn: " + color); + updateButtons(); } @Override