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

@@ -1,11 +1,10 @@
package chess.ai.actions; package chess.ai.actions;
import chess.controller.Command;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.Command.CommandResult; import chess.controller.CommandSender;
import chess.controller.commands.UndoCommand; import chess.controller.commands.UndoCommand;
public abstract class AIAction { public abstract class AIAction implements CommandSender {
private final CommandExecutor commandExecutor; private final CommandExecutor commandExecutor;
@@ -13,8 +12,9 @@ public abstract class AIAction {
this.commandExecutor = commandExecutor; this.commandExecutor = commandExecutor;
} }
protected CommandResult sendCommand(Command cmd, CommandExecutor commandExecutor) { @Override
return commandExecutor.executeCommand(cmd); public CommandExecutor getCommandExecutor() {
return this.commandExecutor;
} }
public void undoAction(CommandExecutor commandExecutor) { public void undoAction(CommandExecutor commandExecutor) {

View File

@@ -4,15 +4,9 @@ import java.util.List;
import chess.ai.actions.AIAction; import chess.ai.actions.AIAction;
import chess.ai.actions.AIActions; import chess.ai.actions.AIActions;
import chess.controller.Command;
import chess.controller.Command.CommandResult;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.commands.CastlingCommand; import chess.controller.CommandSender;
import chess.controller.commands.MoveCommand;
import chess.controller.commands.NewGameCommand;
import chess.controller.commands.PromoteCommand;
import chess.controller.commands.PromoteCommand.PromoteType; import chess.controller.commands.PromoteCommand.PromoteType;
import chess.controller.commands.UndoCommand;
import chess.controller.event.EmptyGameDispatcher; import chess.controller.event.EmptyGameDispatcher;
import chess.controller.event.GameAdapter; import chess.controller.event.GameAdapter;
import chess.model.ChessBoard; import chess.model.ChessBoard;
@@ -21,7 +15,7 @@ import chess.model.Game;
import chess.model.Move; import chess.model.Move;
import chess.model.PermissiveGame; import chess.model.PermissiveGame;
public class GameSimulation extends GameAdapter { public class GameSimulation extends GameAdapter implements CommandSender {
private final CommandExecutor simulation; private final CommandExecutor simulation;
private final Game gameSimulation; private final Game gameSimulation;
@@ -31,48 +25,33 @@ public class GameSimulation extends GameAdapter {
this.simulation = new CommandExecutor(gameSimulation, new EmptyGameDispatcher()); this.simulation = new CommandExecutor(gameSimulation, new EmptyGameDispatcher());
} }
protected CommandResult sendCommand(Command command) {
CommandResult result = this.simulation.executeCommand(command);
if (result == CommandResult.NotAllowed) {
System.out.println("eeeeee");
}
return result;
}
public void tryMove(Move move) {
sendCommand(new MoveCommand(move));
if (this.gameSimulation.getBoard().pawnShouldBePromoted())
sendCommand(new PromoteCommand(PromoteType.Queen));
}
public void undoMove() {
sendCommand(new UndoCommand());
}
@Override @Override
public void onPawnPromoted(PromoteType promotion) { public void onPawnPromoted(PromoteType promotion) {
sendCommand(new PromoteCommand(promotion)); sendPawnPromotion(promotion);
} }
@Override @Override
public void onCastling(boolean bigCastling) { public void onCastling(boolean bigCastling) {
sendCommand(new CastlingCommand(bigCastling)); if (bigCastling)
sendBigCastling();
else
sendCastling();
} }
@Override @Override
public void onMove(Move move, boolean captured) { public void onMove(Move move, boolean captured) {
sendCommand(new MoveCommand(move)); sendMove(move);
} }
@Override @Override
public void onGameStart() { public void onGameStart() {
sendCommand(new NewGameCommand()); sendStartGame();
} }
@Override @Override
public void onPlayerTurn(Color color, boolean undone) { public void onPlayerTurn(Color color, boolean undone) {
if (undone) if (undone)
sendCommand(new UndoCommand()); sendUndo();
} }
public CommandExecutor getCommandExecutor() { public CommandExecutor getCommandExecutor() {

View File

@@ -0,0 +1,102 @@
package chess.controller;
import java.util.List;
import chess.controller.Command.CommandResult;
import chess.controller.commands.CastlingCommand;
import chess.controller.commands.GetAllowedCastlingsCommand;
import chess.controller.commands.GetAllowedMovesPieceCommand;
import chess.controller.commands.GetPieceAtCommand;
import chess.controller.commands.GetPlayerMovesCommand;
import chess.controller.commands.MoveCommand;
import chess.controller.commands.NewGameCommand;
import chess.controller.commands.PromoteCommand;
import chess.controller.commands.SurrenderCommand;
import chess.controller.commands.UndoCommand;
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
import chess.controller.commands.PromoteCommand.PromoteType;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
import chess.model.Piece;
public interface CommandSender {
CommandExecutor getCommandExecutor();
default CommandResult sendCommand(Command cmd, CommandExecutor commandExecutor) {
CommandResult result = commandExecutor.executeCommand(cmd);
if (result == CommandResult.NotAllowed) {
// maybe do something
}
return result;
}
default CommandResult sendCommand(Command cmd) {
return sendCommand(cmd, getCommandExecutor());
}
// inputs
default CastlingResult getAllowedCastlings() {
GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand();
sendCommand(cmd);
return cmd.getCastlingResult();
}
default Piece getPieceAt(int x, int y) {
return getPieceAt(new Coordinate(x, y));
}
default Piece getPieceAt(Coordinate coordinate) {
GetPieceAtCommand cmd = new GetPieceAtCommand(coordinate);
sendCommand(cmd);
return cmd.getPiece();
}
default boolean isCellEmpty(int x, int y) {
return getPieceAt(x, y) == null;
}
default List<Move> getPlayerMoves() {
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
sendCommand(cmd);
return cmd.getMoves();
}
default List<Coordinate> getPieceAllowedMoves(Coordinate piecePos) {
GetAllowedMovesPieceCommand cmd = new GetAllowedMovesPieceCommand(piecePos);
sendCommand(cmd);
return cmd.getDestinations();
}
// outputs
default CommandResult sendCastling() {
return sendCommand(new CastlingCommand(false));
}
default CommandResult sendBigCastling() {
return sendCommand(new CastlingCommand(false));
}
default CommandResult sendMove(Move move) {
return sendCommand(new MoveCommand(move));
}
default CommandResult sendStartGame() {
return sendCommand(new NewGameCommand());
}
default CommandResult sendPawnPromotion(PromoteType promote) {
return sendCommand(new PromoteCommand(promote));
}
default CommandResult sendSurrender(Color player) {
return sendCommand(new SurrenderCommand(player));
}
default CommandResult sendUndo() {
return sendCommand(new UndoCommand());
}
}

View File

@@ -11,9 +11,9 @@ import imgui.type.ImBoolean;
import org.joml.Vector2f; import org.joml.Vector2f;
import org.joml.Vector3f; import org.joml.Vector3f;
import chess.controller.Command;
import chess.controller.Command.CommandResult; import chess.controller.Command.CommandResult;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.CommandSender;
import chess.controller.event.GameAdapter; import chess.controller.event.GameAdapter;
import chess.model.Color; import chess.model.Color;
import chess.model.Coordinate; import chess.model.Coordinate;
@@ -23,10 +23,12 @@ import chess.view.DDDrender.world.BoardEntity;
import chess.view.DDDrender.world.PieceEntity; import chess.view.DDDrender.world.PieceEntity;
import chess.view.DDDrender.world.World; import chess.view.DDDrender.world.World;
public class DDDView extends GameAdapter { public class DDDView extends GameAdapter implements CommandSender {
private static final Vector3f BLACK = new Vector3f(0.3f, 0.3f, 0.3f); private static final Vector3f BLACK = new Vector3f(0.3f, 0.3f, 0.3f);
private static final Vector3f WHITE = new Vector3f(1.0f, 1.0f, 1.0f); private static final Vector3f WHITE = new Vector3f(1.0f, 1.0f, 1.0f);
private static final Vector3f RED = new Vector3f(1.0f, 0.0f, 0.0f);
private static final Vector3f YELLOW = new Vector3f(1.0f, 1.0f, 0.0f);
private final CommandExecutor commandExecutor; private final CommandExecutor commandExecutor;
private final Window window; private final Window window;
@@ -50,6 +52,11 @@ public class DDDView extends GameAdapter {
this.window = new Window(new Renderer(), this.world, this.camera); this.window = new Window(new Renderer(), this.world, this.camera);
} }
@Override
public CommandExecutor getCommandExecutor() {
return this.commandExecutor;
}
private void cancelClick() { private void cancelClick() {
this.click = null; this.click = null;
} }
@@ -58,19 +65,10 @@ public class DDDView extends GameAdapter {
this.click = coordinate; this.click = coordinate;
} }
private CommandResult sendCommand(Command command) {
return this.commandExecutor.executeCommand(command);
}
// Invoked when a cell is clicked // Invoked when a cell is clicked
private void onCellClick(Coordinate coordinate) { private void onCellClick(Coordinate coordinate) {
if (this.click == null) { // case: first click if (this.click == null) { // case: first click
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate); List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed) { // case: invalid piece to move
System.out.println("Nothing to do here.");
return;
}
List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty()) { // case: no movement possible for piece if (allowedMoves.isEmpty()) { // case: no movement possible for piece
System.out.println("This piece cannot be moved at the moment."); System.out.println("This piece cannot be moved at the moment.");
return; return;
@@ -98,7 +96,7 @@ public class DDDView extends GameAdapter {
if (allowedMoves.contains(coordinate)) { // case: valid attempt to move if (allowedMoves.contains(coordinate)) { // case: valid attempt to move
System.out.println("Move on " + coordinate); System.out.println("Move on " + coordinate);
cancelPreview(this.click); cancelPreview(this.click);
Command.CommandResult result = sendCommand(new MoveCommand(new Move(click, coordinate))); sendMove(new Move(click, coordinate));
cancelClick(); cancelClick();
return; return;
} }
@@ -114,21 +112,13 @@ public class DDDView extends GameAdapter {
} }
private void previewMoves(Coordinate coordinate) { private void previewMoves(Coordinate coordinate) {
Piece p = pieceAt(coordinate); List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (p == null) {
return;
}
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed) {
return;
}
this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0));
this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0));
List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty()) if (allowedMoves.isEmpty())
return; return;
this.boardEntity.setCellColor(coordinate, RED);
this.world.getPiece(coordinate).setColor(RED);
for (Coordinate destCoord : allowedMoves) { for (Coordinate destCoord : allowedMoves) {
this.boardEntity.setCellColor(destCoord, new Vector3f(1, 1, 0)); this.boardEntity.setCellColor(destCoord, YELLOW);
} }
} }
@@ -136,19 +126,16 @@ public class DDDView extends GameAdapter {
private void onCellEnter(Coordinate coordinate) { private void onCellEnter(Coordinate coordinate) {
if (this.click == null) { if (this.click == null) {
// small test turning a cell red when hovered // small test turning a cell red when hovered
this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0)); this.boardEntity.setCellColor(coordinate, RED);
Piece p = pieceAt(coordinate); Piece p = pieceAt(coordinate);
if (p == null) if (p == null)
return; return;
this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0)); this.world.getPiece(coordinate).setColor(RED);
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate); List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
return;
List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty()) if (allowedMoves.isEmpty())
return; return;
for (Coordinate destCoord : allowedMoves) { for (Coordinate destCoord : allowedMoves) {
this.boardEntity.setCellColor(destCoord, new Vector3f(1, 0, 0)); this.boardEntity.setCellColor(destCoord, RED);
} }
} }
} }
@@ -165,10 +152,7 @@ public class DDDView extends GameAdapter {
return; return;
pEntity.setColor(p.getColor() == Color.White ? WHITE : BLACK); pEntity.setColor(p.getColor() == Color.White ? WHITE : BLACK);
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate); List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
return;
List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty()) if (allowedMoves.isEmpty())
return; return;
for (Coordinate destCoord : allowedMoves) { for (Coordinate destCoord : allowedMoves) {
@@ -183,10 +167,7 @@ public class DDDView extends GameAdapter {
if (p == null) if (p == null)
return; return;
this.world.getPiece(coordinate).setColor(p.getColor() == Color.White ? WHITE : BLACK); this.world.getPiece(coordinate).setColor(p.getColor() == Color.White ? WHITE : BLACK);
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate); List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
return;
List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty()) if (allowedMoves.isEmpty())
return; return;
for (Coordinate destCoord : allowedMoves) { for (Coordinate destCoord : allowedMoves) {
@@ -223,15 +204,15 @@ public class DDDView extends GameAdapter {
private void onFooterRender() { private void onFooterRender() {
if (ImGui.button("Roque")) { if (ImGui.button("Roque")) {
sendCommand(new CastlingCommand(false)); sendCastling();
} }
ImGui.sameLine(); ImGui.sameLine();
if (ImGui.button("Grand Roque")) { if (ImGui.button("Grand Roque")) {
sendCommand(new CastlingCommand(true)); sendBigCastling();
} }
ImGui.sameLine(); ImGui.sameLine();
if (ImGui.button("Annuler le coup précédent")) { if (ImGui.button("Annuler le coup précédent")) {
sendCommand(new UndoCommand()); sendUndo();
} }
openPopup(); openPopup();
renderPopups(); renderPopups();
@@ -288,7 +269,8 @@ public class DDDView extends GameAdapter {
Vector2f pieceStartBoard = DDDPlacement.coordinatesToVector(move.getStart()); Vector2f pieceStartBoard = DDDPlacement.coordinatesToVector(move.getStart());
Vector2f pieceDestinationBoard = DDDPlacement.coordinatesToVector(move.getFinish()); Vector2f pieceDestinationBoard = DDDPlacement.coordinatesToVector(move.getFinish());
Vector3f start = new Vector3f(pieceStartBoard.x(), 0, pieceStartBoard.y()); Vector3f start = new Vector3f(pieceStartBoard.x(), 0, pieceStartBoard.y());
Vector3f top = new Vector3f(pieceDestinationBoard.x() - pieceStartBoard.x(), height, pieceDestinationBoard.y() - pieceStartBoard.y()); Vector3f top = new Vector3f(pieceDestinationBoard.x() - pieceStartBoard.x(), height,
pieceDestinationBoard.y() - pieceStartBoard.y());
Vector3f end = new Vector3f(pieceDestinationBoard.x(), 0, pieceDestinationBoard.y()); Vector3f end = new Vector3f(pieceDestinationBoard.x(), 0, pieceDestinationBoard.y());
piece.setPosition(bezierCurve(start, top, end, progress)); piece.setPosition(bezierCurve(start, top, end, progress));

View File

@@ -2,9 +2,10 @@ package chess.view.consolerender;
import chess.controller.Command; import chess.controller.Command;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.CommandSender;
import chess.controller.commands.*; import chess.controller.commands.*;
import chess.controller.commands.PromoteCommand.PromoteType; import chess.controller.commands.PromoteCommand.PromoteType;
import chess.controller.event.GameListener; import chess.controller.event.GameAdapter;
import chess.model.Color; import chess.model.Color;
import chess.model.Coordinate; import chess.model.Coordinate;
import chess.model.Move; import chess.model.Move;
@@ -16,7 +17,7 @@ import java.util.Scanner;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
public class Console implements GameListener { public class Console extends GameAdapter implements CommandSender {
private final Scanner scanner = new Scanner(System.in); private final Scanner scanner = new Scanner(System.in);
private final CommandExecutor commandExecutor; private final CommandExecutor commandExecutor;
private final ConsolePieceName consolePieceName = new ConsolePieceName(); private final ConsolePieceName consolePieceName = new ConsolePieceName();
@@ -33,20 +34,6 @@ public class Console implements GameListener {
this(commandExecutor, true); this(commandExecutor, true);
} }
private Piece pieceAt(int x, int y) {
return pieceAt(new Coordinate(x, y));
}
private Command.CommandResult sendCommand(Command command) {
return this.commandExecutor.executeCommand(command);
}
private Piece pieceAt(Coordinate coordinate) {
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
sendCommand(command);
return command.getPiece();
}
public Coordinate stringToCoordinate(String coordinates) throws Exception { public Coordinate stringToCoordinate(String coordinates) throws Exception {
char xPos = coordinates.charAt(0); char xPos = coordinates.charAt(0);
char yPos = coordinates.charAt(1); char yPos = coordinates.charAt(1);
@@ -95,7 +82,7 @@ public class Console implements GameListener {
} }
private boolean playerPickedSurrender(Color color) { private boolean playerPickedSurrender(Color color) {
sendCommand(new SurrenderCommand(color)); sendSurrender(color);
return true; return true;
} }
@@ -109,7 +96,7 @@ public class Console implements GameListener {
Coordinate start = stringToCoordinate(answer); Coordinate start = stringToCoordinate(answer);
System.out.println("New position: "); System.out.println("New position: ");
Coordinate end = stringToCoordinate(scanner.nextLine()); Coordinate end = stringToCoordinate(scanner.nextLine());
Command.CommandResult result = sendCommand(new MoveCommand(new Move(start, end))); Command.CommandResult result = sendMove(new Move(start, end));
return switch (Objects.requireNonNull(result)) { return switch (Objects.requireNonNull(result)) {
case Command.CommandResult.Moved, Command.CommandResult.ActionNeeded -> true; case Command.CommandResult.Moved, Command.CommandResult.ActionNeeded -> true;
@@ -126,12 +113,7 @@ public class Console implements GameListener {
try { try {
System.out.println("Piece to examine: "); System.out.println("Piece to examine: ");
Coordinate piece = stringToCoordinate(scanner.nextLine()); Coordinate piece = stringToCoordinate(scanner.nextLine());
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(piece); List<Coordinate> allowedMoves = getPieceAllowedMoves(piece);
if (sendCommand(movesCommand) == Command.CommandResult.NotAllowed) {
System.out.println("Not allowed.");
return false;
}
List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty()) { if (allowedMoves.isEmpty()) {
System.out.println("No moves allowed for this piece."); System.out.println("No moves allowed for this piece.");
return false; return false;
@@ -201,7 +183,7 @@ public class Console implements GameListener {
default -> throw new Exception(); default -> throw new Exception();
}; };
valid = true; valid = true;
sendCommand(new PromoteCommand(newPiece)); sendPawnPromotion(newPiece);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Invalid input!"); System.out.println("Invalid input!");
} }
@@ -218,7 +200,7 @@ public class Console implements GameListener {
for (int i = 0; i < Coordinate.VALUE_MAX; i++) { for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
string.append(8 - i).append(" "); string.append(8 - i).append(" ");
for (int j = 0; j < Coordinate.VALUE_MAX; j++) { for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
Piece p = pieceAt(j, i); Piece p = getPieceAt(new Coordinate(j, i));
if ((i + j) % 2 == 0) { if ((i + j) % 2 == 0) {
string.append(Colors.LIGHT_GRAY_BACKGROUND); string.append(Colors.LIGHT_GRAY_BACKGROUND);
} else { } else {
@@ -243,7 +225,7 @@ public class Console implements GameListener {
string.append(8 - i).append(" "); string.append(8 - i).append(" ");
for (int j = 0; j < Coordinate.VALUE_MAX; j++) { for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
Coordinate currentCell = new Coordinate(j, i); Coordinate currentCell = new Coordinate(j, i);
Piece p = pieceAt(j, i); Piece p = getPieceAt(new Coordinate(j, i));
if (moves.contains(currentCell)) { if (moves.contains(currentCell)) {
string.append(Colors.YELLOW_BACKGROUND); string.append(Colors.YELLOW_BACKGROUND);
} else { } else {
@@ -269,10 +251,6 @@ public class Console implements GameListener {
System.out.println(string); System.out.println(string);
} }
@Override
public void onMove(Move move, boolean captured) {
}
@Override @Override
public void onMoveNotAllowed(Move move) { public void onMoveNotAllowed(Move move) {
System.out.println("Move not allowed."); System.out.println("Move not allowed.");
@@ -284,9 +262,7 @@ public class Console implements GameListener {
} }
private boolean onAskedCastling() { private boolean onAskedCastling() {
GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand(); return switch (getAllowedCastlings()) {
sendCommand(cmd);
return switch (cmd.getCastlingResult()) {
case Small -> onSmallCastling(); case Small -> onSmallCastling();
case Big -> onBigCastling(); case Big -> onBigCastling();
case Both -> onBothCastling(); case Both -> onBothCastling();
@@ -334,10 +310,9 @@ public class Console implements GameListener {
} }
@Override @Override
public void onCastling(boolean bigCastling) {} public CommandExecutor getCommandExecutor() {
return this.commandExecutor;
@Override }
public void onPawnPromoted(PromoteType promotion) {}
} }

View File

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