add CommanderSender (Fixes #9)
All checks were successful
Linux arm64 / Build (push) Successful in 36s

This commit is contained in:
2025-05-17 16:48:04 +02:00
parent b33e333276
commit 90daf662ea
6 changed files with 179 additions and 164 deletions

View File

@@ -17,24 +17,15 @@ import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
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.CommandSender;
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;
import chess.model.Piece;
public class Window extends JFrame implements GameListener {
public class Window extends JFrame implements GameListener, CommandSender {
private final CommandExecutor commandExecutor;
@@ -65,25 +56,21 @@ public class Window extends JFrame implements GameListener {
});
}
private CommandResult sendCommand(Command command) {
return this.commandExecutor.executeCommand(command);
}
private Color getCellColor(int x, int y) {
return ((x + y) % 2 == 1) ? Color.DARK_GRAY : Color.LIGHT_GRAY;
}
private void buildButtons(JPanel bottom) {
castlingButton.addActionListener((event) -> {
sendCommand(new CastlingCommand(false));
sendCastling();
});
bigCastlingButton.addActionListener((event) -> {
sendCommand(new CastlingCommand(true));
sendBigCastling();
});
undoButton.addActionListener((event) -> {
sendCommand(new UndoCommand());
sendUndo();
});
bottom.add(castlingButton);
@@ -127,23 +114,13 @@ public class Window extends JFrame implements GameListener {
updateBoard();
}
private boolean isCellEmpty(int x, int y) {
return pieceAt(x, y) == null;
}
private Piece pieceAt(int x, int y) {
GetPieceAtCommand command = new GetPieceAtCommand(new Coordinate(x, y));
sendCommand(command);
return command.getPiece();
}
private void updateBoard() {
PieceIcon pieceIcon = new PieceIcon();
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
JLabel cell = this.cells[x][y];
try {
cell.setIcon(pieceIcon.getIcon(pieceAt(x, y)));
cell.setIcon(pieceIcon.getIcon(getPieceAt(x, y)));
} catch (IOException e) {
e.printStackTrace();
}
@@ -152,11 +129,7 @@ public class Window extends JFrame implements GameListener {
}
private boolean previewMoves(int x, int y) {
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(new Coordinate(x, y));
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
return false;
List<Coordinate> allowedMoves = movesCommand.getDestinations();
List<Coordinate> allowedMoves = getPieceAllowedMoves(new Coordinate(x, y));
if (allowedMoves.isEmpty())
return false;
@@ -198,18 +171,17 @@ public class Window extends JFrame implements GameListener {
}
if (!this.lastClick.equals(new Coordinate(x, y))) {
Move move = new Move(lastClick, new Coordinate(x, y));
sendCommand(new MoveCommand(move));
sendMove(move);
}
this.lastClick = null;
}
private void updateButtons() {
GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand();
sendCommand(cmd);
CastlingResult castlings = getAllowedCastlings();
this.castlingButton.setEnabled(
cmd.getCastlingResult() == CastlingResult.Small || cmd.getCastlingResult() == CastlingResult.Both);
castlings == CastlingResult.Small || castlings == CastlingResult.Both);
this.bigCastlingButton.setEnabled(
cmd.getCastlingResult() == CastlingResult.Big || cmd.getCastlingResult() == CastlingResult.Both);
castlings == CastlingResult.Big || castlings == CastlingResult.Both);
}
@Override
@@ -296,7 +268,7 @@ public class Window extends JFrame implements GameListener {
}
if (choosedType != null)
sendCommand(new PromoteCommand(choosedType));
sendPawnPromotion(choosedType);
});
}
@@ -324,4 +296,9 @@ public class Window extends JFrame implements GameListener {
@Override
public void onPawnPromoted(PromoteType promotion) {}
@Override
public CommandExecutor getCommandExecutor() {
return this.commandExecutor;
}
}