3 Commits

Author SHA1 Message Date
17ef882d44 remove board from constructor 2025-04-16 19:29:31 +02:00
bee5e613bb refactor dumb ai castling 2025-04-16 19:25:04 +02:00
923ace22f1 add general ai 2025-04-16 19:21:47 +02:00
15 changed files with 102 additions and 82 deletions

View File

@@ -5,14 +5,13 @@ package chess;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand; import chess.controller.commands.NewGameCommand;
import chess.model.ChessBoard;
import chess.model.Game; import chess.model.Game;
import chess.simulator.PromoteTest; import chess.simulator.PromoteTest;
import chess.view.consolerender.Console; import chess.view.consolerender.Console;
public class ConsoleMain { public class ConsoleMain {
public static void main(String[] args) { public static void main(String[] args) {
Game game = new Game(new ChessBoard()); Game game = new Game();
CommandExecutor commandExecutor = new CommandExecutor(game); CommandExecutor commandExecutor = new CommandExecutor(game);
PromoteTest promoteTest = new PromoteTest(commandExecutor); PromoteTest promoteTest = new PromoteTest(commandExecutor);

View File

@@ -4,7 +4,6 @@ import chess.ai.DumbAI;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand; import chess.controller.commands.NewGameCommand;
import chess.controller.event.GameAdaptator; import chess.controller.event.GameAdaptator;
import chess.model.ChessBoard;
import chess.model.Color; import chess.model.Color;
import chess.model.Game; import chess.model.Game;
import chess.pgn.PgnExport; import chess.pgn.PgnExport;
@@ -12,7 +11,7 @@ import chess.view.simplerender.Window;
public class SwingMain { public class SwingMain {
public static void main(String[] args) { public static void main(String[] args) {
Game game = new Game(new ChessBoard()); Game game = new Game();
CommandExecutor commandExecutor = new CommandExecutor(game); CommandExecutor commandExecutor = new CommandExecutor(game);
Window window = new Window(commandExecutor, false); Window window = new Window(commandExecutor, false);

View File

@@ -0,0 +1,68 @@
package chess.ai;
import java.util.List;
import chess.controller.Command;
import chess.controller.CommandExecutor;
import chess.controller.commands.GetPieceAtCommand;
import chess.controller.commands.GetPlayerMovesCommand;
import chess.controller.commands.GetAllowedCastlingsCommand;
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
import chess.controller.event.GameAdaptator;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
import chess.model.Piece;
public abstract class AI extends GameAdaptator{
protected final CommandExecutor commandExecutor;
protected final Color color;
public AI(CommandExecutor commandExecutor, Color color) {
this.commandExecutor = commandExecutor;
this.color = color;
}
protected abstract void play();
protected abstract void promote(Coordinate pawnCoords);
@Override
public void onPlayerTurn(Color color, boolean undone) {
if (this.color != color || undone)
return;
play();
}
@Override
public void onPromotePawn(Coordinate pieceCoords) {
Piece pawn = pieceAt(pieceCoords);
if (pawn.getColor() != this.color)
return;
promote(pieceCoords);
}
protected Piece pieceAt(Coordinate coordinate) {
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
sendCommand(command);
return command.getPiece();
}
protected List<Move> getAllowedMoves() {
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
sendCommand(cmd);
return cmd.getMoves();
}
protected CastlingResult getAllowedCastlings() {
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
sendCommand(cmd2);
return cmd2.getCastlingResult();
}
protected void sendCommand(Command command) {
this.commandExecutor.executeCommand(command);
}
}

View File

@@ -3,46 +3,28 @@ package chess.ai;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import chess.controller.Command;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.commands.CastlingCommand; import chess.controller.commands.CastlingCommand;
import chess.controller.commands.GetAllowedCastlingsCommand; import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
import chess.controller.commands.GetPieceAtCommand;
import chess.controller.commands.GetPlayerMovesCommand;
import chess.controller.commands.MoveCommand; import chess.controller.commands.MoveCommand;
import chess.controller.commands.PromoteCommand; import chess.controller.commands.PromoteCommand;
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
import chess.controller.commands.PromoteCommand.PromoteType; import chess.controller.commands.PromoteCommand.PromoteType;
import chess.controller.event.GameAdaptator;
import chess.model.Color; import chess.model.Color;
import chess.model.Coordinate; import chess.model.Coordinate;
import chess.model.Move; import chess.model.Move;
import chess.model.Piece;
public class DumbAI extends GameAdaptator { public class DumbAI extends AI {
private final Color player;
private final CommandExecutor commandExecutor;
private final Random random = new Random(); private final Random random = new Random();
public DumbAI(CommandExecutor commandExecutor, Color color) { public DumbAI(CommandExecutor commandExecutor, Color color) {
this.player = color; super(commandExecutor, color);
this.commandExecutor = commandExecutor;
} }
@Override @Override
public void onPlayerTurn(Color color) { protected void play() {
if (color != player) CastlingResult castlings = getAllowedCastlings();
return; List<Move> moves = getAllowedMoves();
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
sendCommand(cmd);
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
sendCommand(cmd2);
CastlingResult castlings = cmd2.getCastlingResult();
List<Move> moves = cmd.getMoves();
switch (castlings) { switch (castlings) {
case Both: { case Both: {
@@ -53,20 +35,12 @@ public class DumbAI extends GameAdaptator {
return; return;
} }
case Small: { case Small:
int randomMove = this.random.nextInt(moves.size() + 1);
if (randomMove != moves.size())
break;
this.commandExecutor.executeCommand(new CastlingCommand(false));
return;
}
case Big: { case Big: {
int randomMove = this.random.nextInt(moves.size() + 1); int randomMove = this.random.nextInt(moves.size() + 1);
if (randomMove != moves.size()) if (randomMove != moves.size())
break; break;
this.commandExecutor.executeCommand(new CastlingCommand(true)); this.commandExecutor.executeCommand(new CastlingCommand(castlings == CastlingResult.Big));
return; return;
} }
@@ -76,27 +50,12 @@ public class DumbAI extends GameAdaptator {
int randomMove = this.random.nextInt(moves.size()); int randomMove = this.random.nextInt(moves.size());
this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove))); this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove)));
} }
@Override @Override
public void onPromotePawn(Coordinate pieceCoords) { protected void promote(Coordinate pawnCoords) {
Piece pawn = pieceAt(pieceCoords);
if (pawn.getColor() != this.player)
return;
int promote = this.random.nextInt(PromoteType.values().length); int promote = this.random.nextInt(PromoteType.values().length);
this.commandExecutor.executeCommand(new PromoteCommand(PromoteType.values()[promote])); this.commandExecutor.executeCommand(new PromoteCommand(PromoteType.values()[promote]));
} }
private Piece pieceAt(Coordinate coordinate) {
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
sendCommand(command);
return command.getPiece();
}
private void sendCommand(Command command) {
this.commandExecutor.executeCommand(command);
}
} }

View File

@@ -57,14 +57,14 @@ public class CommandExecutor {
this.dispatcher.onGameEnd(); this.dispatcher.onGameEnd();
return; return;
} }
switchPlayerTurn(); switchPlayerTurn(command instanceof UndoCommand);
return; return;
} }
} }
private void switchPlayerTurn() { private void switchPlayerTurn(boolean undone) {
this.game.switchPlayerTurn(); this.game.switchPlayerTurn();
this.dispatcher.onPlayerTurn(this.game.getPlayerTurn()); this.dispatcher.onPlayerTurn(this.game.getPlayerTurn(), undone);
} }
/** /**

View File

@@ -51,7 +51,7 @@ public class NewGameCommand extends Command {
game.reset(); game.reset();
outputSystem.onGameStart(); outputSystem.onGameStart();
outputSystem.onPlayerTurn(game.getPlayerTurn()); outputSystem.onPlayerTurn(game.getPlayerTurn(), false);
return CommandResult.NotMoved; return CommandResult.NotMoved;
} }

View File

@@ -43,7 +43,7 @@ public class EmptyGameDispatcher extends GameDispatcher {
} }
@Override @Override
public void onPlayerTurn(Color color) { public void onPlayerTurn(Color color, boolean undone) {
} }
@Override @Override

View File

@@ -7,7 +7,7 @@ import chess.model.Move;
public abstract class GameAdaptator implements GameListener { public abstract class GameAdaptator implements GameListener {
@Override @Override
public void onPlayerTurn(Color color) {} public void onPlayerTurn(Color color, boolean undone) {}
@Override @Override
public void onWin(Color color) {} public void onWin(Color color) {}

View File

@@ -29,8 +29,8 @@ public class GameDispatcher implements GameListener {
} }
@Override @Override
public void onPlayerTurn(Color color) { public void onPlayerTurn(Color color, boolean undone) {
asyncForEachCall((l) -> l.onPlayerTurn(color)); asyncForEachCall((l) -> l.onPlayerTurn(color, undone));
} }
@Override @Override

View File

@@ -56,8 +56,9 @@ public interface GameListener {
/** /**
* Invoked when it's the player turn * Invoked when it's the player turn
* @param color the color of the player who should play * @param color the color of the player who should play
* @param undone true if it's a result of an undo command
*/ */
void onPlayerTurn(Color color); void onPlayerTurn(Color color, boolean undone);
/** /**
* Invoked when a pawn should be promoted * Invoked when a pawn should be promoted

View File

@@ -20,8 +20,8 @@ public class Game {
Draw, Check, CheckMate, OnGoing, Pat; Draw, Check, CheckMate, OnGoing, Pat;
} }
public Game(ChessBoard board) { public Game() {
this.board = board; this.board = new ChessBoard();
this.movesHistory = new Stack<>(); this.movesHistory = new Stack<>();
this.traitsPos = new HashMap<>(); this.traitsPos = new HashMap<>();
} }

View File

@@ -11,7 +11,6 @@ import chess.controller.commands.MoveCommand;
import chess.controller.commands.NewGameCommand; import chess.controller.commands.NewGameCommand;
import chess.controller.commands.PromoteCommand; import chess.controller.commands.PromoteCommand;
import chess.controller.event.EmptyGameDispatcher; import chess.controller.event.EmptyGameDispatcher;
import chess.model.ChessBoard;
import chess.model.Color; import chess.model.Color;
import chess.model.Coordinate; import chess.model.Coordinate;
import chess.model.Game; import chess.model.Game;
@@ -22,7 +21,7 @@ import chess.model.pieces.Pawn;
public class PgnExport { public class PgnExport {
// public static void main(String[] args) { // public static void main(String[] args) {
// final Game game = new Game(new ChessBoard()); // final Game game = new Game();
// final CommandExecutor commandExecutor = new CommandExecutor(game); // final CommandExecutor commandExecutor = new CommandExecutor(game);
// DumbAI ai1 = new DumbAI(commandExecutor, Color.White); // DumbAI ai1 = new DumbAI(commandExecutor, Color.White);
@@ -190,8 +189,7 @@ public class PgnExport {
public static String exportGame(Game game) { public static String exportGame(Game game) {
ChessBoard board = new ChessBoard(); Game virtualGame = new Game();
Game virtualGame = new Game(board);
CommandExecutor executor = new CommandExecutor(virtualGame, new EmptyGameDispatcher()); CommandExecutor executor = new CommandExecutor(virtualGame, new EmptyGameDispatcher());
executor.executeCommand(new NewGameCommand()); executor.executeCommand(new NewGameCommand());

View File

@@ -61,7 +61,7 @@ public class Console implements GameListener {
} }
@Override @Override
public void onPlayerTurn(Color color) { public void onPlayerTurn(Color color, boolean undone) {
if (!captureInput) if (!captureInput)
return; return;
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET); System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);

View File

@@ -213,7 +213,7 @@ public class Window extends JFrame implements GameListener {
} }
@Override @Override
public void onPlayerTurn(chess.model.Color color) { public void onPlayerTurn(chess.model.Color color, boolean undone) {
this.displayText.setText("Current turn: " + color); this.displayText.setText("Current turn: " + color);
updateButtons(); updateButtons();
} }

View File

@@ -3,24 +3,20 @@
*/ */
package chess; package chess;
import org.junit.jupiter.api.Test;
import chess.ai.DumbAI; import chess.ai.DumbAI;
import chess.controller.Command; import chess.controller.Command;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand; import chess.controller.commands.NewGameCommand;
import chess.controller.commands.UndoCommand; import chess.controller.commands.UndoCommand;
import chess.controller.event.GameAdaptator; import chess.controller.event.GameAdaptator;
import chess.model.*; import chess.model.Color;
import chess.model.pieces.*; import chess.model.Game;
import chess.simulator.Simulator;
import chess.view.simplerender.Window;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Objects;
class AppTest { class AppTest {
@Test void functionalRollback(){ @Test void functionalRollback(){
Game game = new Game(new ChessBoard()); Game game = new Game();
CommandExecutor commandExecutor = new CommandExecutor(game); CommandExecutor commandExecutor = new CommandExecutor(game);
DumbAI ai = new DumbAI(commandExecutor, Color.Black); DumbAI ai = new DumbAI(commandExecutor, Color.Black);
@@ -38,9 +34,9 @@ class AppTest {
result = commandExecutor.executeCommand(new UndoCommand()); result = commandExecutor.executeCommand(new UndoCommand());
} while (result != Command.CommandResult.NotAllowed); } while (result != Command.CommandResult.NotAllowed);
Game initialGame = new Game(new ChessBoard()); Game initialGame = new Game();
CommandExecutor initialCommandExecutor = new CommandExecutor(initialGame); CommandExecutor initialCommandExecutor = new CommandExecutor(initialGame);
commandExecutor.executeCommand(new NewGameCommand()); initialCommandExecutor.executeCommand(new NewGameCommand());
assert(game.getBoard().equals(initialGame.getBoard())); assert(game.getBoard().equals(initialGame.getBoard()));