add castling commands

This commit is contained in:
2025-04-14 10:34:54 +02:00
parent 2578e8cf6f
commit 07016be5d8
3 changed files with 62 additions and 2 deletions

View File

@@ -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