add general ai

This commit is contained in:
2025-04-16 19:21:47 +02:00
parent 78d48fafc6
commit 923ace22f1
10 changed files with 87 additions and 51 deletions

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.Random;
import chess.controller.Command;
import chess.controller.CommandExecutor;
import chess.controller.commands.CastlingCommand;
import chess.controller.commands.GetAllowedCastlingsCommand;
import chess.controller.commands.GetPieceAtCommand;
import chess.controller.commands.GetPlayerMovesCommand;
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
import chess.controller.commands.MoveCommand;
import chess.controller.commands.PromoteCommand;
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
import chess.controller.commands.PromoteCommand.PromoteType;
import chess.controller.event.GameAdaptator;
import chess.model.Color;
import chess.model.Coordinate;
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();
public DumbAI(CommandExecutor commandExecutor, Color color) {
this.player = color;
this.commandExecutor = commandExecutor;
super(commandExecutor, color);
}
@Override
public void onPlayerTurn(Color color) {
if (color != player)
return;
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
sendCommand(cmd);
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
sendCommand(cmd2);
CastlingResult castlings = cmd2.getCastlingResult();
List<Move> moves = cmd.getMoves();
protected void play() {
CastlingResult castlings = getAllowedCastlings();
List<Move> moves = getAllowedMoves();
switch (castlings) {
case Both: {
@@ -76,27 +58,12 @@ public class DumbAI extends GameAdaptator {
int randomMove = this.random.nextInt(moves.size());
this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove)));
}
@Override
public void onPromotePawn(Coordinate pieceCoords) {
Piece pawn = pieceAt(pieceCoords);
if (pawn.getColor() != this.player)
return;
protected void promote(Coordinate pawnCoords) {
int promote = this.random.nextInt(PromoteType.values().length);
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();
return;
}
switchPlayerTurn();
switchPlayerTurn(command instanceof UndoCommand);
return;
}
}
private void switchPlayerTurn() {
private void switchPlayerTurn(boolean undone) {
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();
outputSystem.onGameStart();
outputSystem.onPlayerTurn(game.getPlayerTurn());
outputSystem.onPlayerTurn(game.getPlayerTurn(), false);
return CommandResult.NotMoved;
}

View File

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

View File

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

View File

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

View File

@@ -56,8 +56,9 @@ public interface GameListener {
/**
* Invoked when it's the player turn
* @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

View File

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

View File

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