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;
import chess.controller.Command;
import chess.controller.CommandExecutor;
import chess.controller.Command.CommandResult;
import chess.controller.CommandSender;
import chess.controller.commands.UndoCommand;
public abstract class AIAction {
public abstract class AIAction implements CommandSender {
private final CommandExecutor commandExecutor;
@@ -13,8 +12,9 @@ public abstract class AIAction {
this.commandExecutor = commandExecutor;
}
protected CommandResult sendCommand(Command cmd, CommandExecutor commandExecutor) {
return commandExecutor.executeCommand(cmd);
@Override
public CommandExecutor getCommandExecutor() {
return this.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.AIActions;
import chess.controller.Command;
import chess.controller.Command.CommandResult;
import chess.controller.CommandExecutor;
import chess.controller.commands.CastlingCommand;
import chess.controller.commands.MoveCommand;
import chess.controller.commands.NewGameCommand;
import chess.controller.commands.PromoteCommand;
import chess.controller.CommandSender;
import chess.controller.commands.PromoteCommand.PromoteType;
import chess.controller.commands.UndoCommand;
import chess.controller.event.EmptyGameDispatcher;
import chess.controller.event.GameAdapter;
import chess.model.ChessBoard;
@@ -21,7 +15,7 @@ import chess.model.Game;
import chess.model.Move;
import chess.model.PermissiveGame;
public class GameSimulation extends GameAdapter {
public class GameSimulation extends GameAdapter implements CommandSender {
private final CommandExecutor simulation;
private final Game gameSimulation;
@@ -31,48 +25,33 @@ public class GameSimulation extends GameAdapter {
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
public void onPawnPromoted(PromoteType promotion) {
sendCommand(new PromoteCommand(promotion));
sendPawnPromotion(promotion);
}
@Override
public void onCastling(boolean bigCastling) {
sendCommand(new CastlingCommand(bigCastling));
if (bigCastling)
sendBigCastling();
else
sendCastling();
}
@Override
public void onMove(Move move, boolean captured) {
sendCommand(new MoveCommand(move));
sendMove(move);
}
@Override
public void onGameStart() {
sendCommand(new NewGameCommand());
sendStartGame();
}
@Override
public void onPlayerTurn(Color color, boolean undone) {
if (undone)
sendCommand(new UndoCommand());
sendUndo();
}
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.Vector3f;
import chess.controller.Command;
import chess.controller.Command.CommandResult;
import chess.controller.CommandExecutor;
import chess.controller.CommandSender;
import chess.controller.event.GameAdapter;
import chess.model.Color;
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.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 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 Window window;
@@ -50,6 +52,11 @@ public class DDDView extends GameAdapter {
this.window = new Window(new Renderer(), this.world, this.camera);
}
@Override
public CommandExecutor getCommandExecutor() {
return this.commandExecutor;
}
private void cancelClick() {
this.click = null;
}
@@ -58,19 +65,10 @@ public class DDDView extends GameAdapter {
this.click = coordinate;
}
private CommandResult sendCommand(Command command) {
return this.commandExecutor.executeCommand(command);
}
// Invoked when a cell is clicked
private void onCellClick(Coordinate coordinate) {
if (this.click == null) { // case: first click
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed) { // case: invalid piece to move
System.out.println("Nothing to do here.");
return;
}
List<Coordinate> allowedMoves = movesCommand.getDestinations();
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty()) { // case: no movement possible for piece
System.out.println("This piece cannot be moved at the moment.");
return;
@@ -98,7 +96,7 @@ public class DDDView extends GameAdapter {
if (allowedMoves.contains(coordinate)) { // case: valid attempt to move
System.out.println("Move on " + coordinate);
cancelPreview(this.click);
Command.CommandResult result = sendCommand(new MoveCommand(new Move(click, coordinate)));
sendMove(new Move(click, coordinate));
cancelClick();
return;
}
@@ -114,21 +112,13 @@ public class DDDView extends GameAdapter {
}
private void previewMoves(Coordinate coordinate) {
Piece p = pieceAt(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();
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty())
return;
this.boardEntity.setCellColor(coordinate, RED);
this.world.getPiece(coordinate).setColor(RED);
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) {
if (this.click == null) {
// 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);
if (p == null)
return;
this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0));
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
return;
List<Coordinate> allowedMoves = movesCommand.getDestinations();
this.world.getPiece(coordinate).setColor(RED);
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty())
return;
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;
pEntity.setColor(p.getColor() == Color.White ? WHITE : BLACK);
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
return;
List<Coordinate> allowedMoves = movesCommand.getDestinations();
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty())
return;
for (Coordinate destCoord : allowedMoves) {
@@ -183,10 +167,7 @@ public class DDDView extends GameAdapter {
if (p == null)
return;
this.world.getPiece(coordinate).setColor(p.getColor() == Color.White ? WHITE : BLACK);
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
return;
List<Coordinate> allowedMoves = movesCommand.getDestinations();
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty())
return;
for (Coordinate destCoord : allowedMoves) {
@@ -223,15 +204,15 @@ public class DDDView extends GameAdapter {
private void onFooterRender() {
if (ImGui.button("Roque")) {
sendCommand(new CastlingCommand(false));
sendCastling();
}
ImGui.sameLine();
if (ImGui.button("Grand Roque")) {
sendCommand(new CastlingCommand(true));
sendBigCastling();
}
ImGui.sameLine();
if (ImGui.button("Annuler le coup précédent")) {
sendCommand(new UndoCommand());
sendUndo();
}
openPopup();
renderPopups();
@@ -279,7 +260,7 @@ public class DDDView extends GameAdapter {
private Function<Float, Float> lagrangeInterpolation(Vector3f begin, Vector3f middle, Vector3f end) {
return (t) -> {
return t+1.0f;
return t + 1.0f;
};
}
@@ -288,7 +269,8 @@ public class DDDView extends GameAdapter {
Vector2f pieceStartBoard = DDDPlacement.coordinatesToVector(move.getStart());
Vector2f pieceDestinationBoard = DDDPlacement.coordinatesToVector(move.getFinish());
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());
piece.setPosition(bezierCurve(start, top, end, progress));
@@ -362,14 +344,14 @@ public class DDDView extends GameAdapter {
}
private void renderPopup(String title, String text) {
if(ImGui.beginPopupModal(title, popupOpened)){
if (ImGui.beginPopupModal(title, popupOpened)) {
ImGui.text(text);
ImGui.endPopup();
}
}
private void renderPopups() {
renderPopup("Check","Your king is in check");
renderPopup("Check", "Your king is in check");
renderPopup("Checkmate", "Checkmate, it's a win!");
renderPopup("Promotion", "Please promote your pawn.");
renderPopup("Pat", "It's a pat!");
@@ -387,7 +369,7 @@ public class DDDView extends GameAdapter {
}
@Override
public void onKingInCheck(){
public void onKingInCheck() {
openPopup("Check");
}
@@ -413,7 +395,7 @@ public class DDDView extends GameAdapter {
@Override
public void onSurrender(Color color) {
openPopup(color == Color.White ? "White surrender": "Black surrender");
openPopup(color == Color.White ? "White surrender" : "Black surrender");
openPopup(color == Color.White ? "Black victory" : "White victory");
}
}

View File

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

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;
}
}