feat: add ai castling (Fixes #4)
All checks were successful
Linux arm64 / Build (push) Successful in 41s

This commit is contained in:
2025-05-06 17:15:55 +02:00
parent 58f02f681c
commit 810a0f2159
12 changed files with 252 additions and 150 deletions

View File

@@ -8,13 +8,9 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import chess.ai.AI;
import chess.ai.actions.AIAction;
import chess.controller.CommandExecutor;
import chess.controller.commands.MoveCommand;
import chess.controller.commands.PromoteCommand;
import chess.controller.commands.PromoteCommand.PromoteType;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
import common.Signal1;
public class AlphaBetaAI extends AI {
@@ -38,23 +34,23 @@ public class AlphaBetaAI extends AI {
new AlphaBetaThreadCreator(commandExecutor, color, threadCount));
}
private Move getBestMove() {
List<Move> moves = getAllowedMoves();
List<Future<Float>> moveEvaluations = new ArrayList<>(50);
private AIAction getBestMove() {
List<AIAction> actions = getAllowedActions();
List<Future<Float>> moveEvaluations = new ArrayList<>(actions.size());
float bestMoveValue = MIN_FLOAT;
Move bestMove = null;
AIAction bestMove = null;
this.onStartEval.emit(moves.size());
this.onStartEval.emit(actions.size());
for (Move move : moves) {
for (AIAction action : actions) {
moveEvaluations.add(this.threadPool.submit(() -> {
return AlphaBetaThreadCreator.getMoveValue(move, this.searchDepth);
return AlphaBetaThreadCreator.getMoveValue(action, this.searchDepth);
}));
}
for (int i = 0; i < moves.size(); i++) {
this.onProgress.emit((float) i / (float) moves.size());
Move move = moves.get(i);
for (int i = 0; i < actions.size(); i++) {
this.onProgress.emit((float) i / (float) actions.size());
AIAction action = actions.get(i);
float value = MIN_FLOAT;
try {
@@ -64,7 +60,7 @@ public class AlphaBetaAI extends AI {
}
if (value > bestMoveValue) {
bestMoveValue = value;
bestMove = move;
bestMove = action;
}
}
@@ -80,13 +76,8 @@ public class AlphaBetaAI extends AI {
@Override
protected void play() {
Move move = getBestMove();
sendCommand(new MoveCommand(move));
}
@Override
protected void promote(Coordinate pawnCoords) {
sendCommand(new PromoteCommand(PromoteType.Queen));
AIAction move = getBestMove();
move.applyAction();
}
}

View File

@@ -8,10 +8,10 @@ import java.util.Map.Entry;
import chess.ai.PieceCost;
import chess.ai.PiecePosCost;
import chess.ai.actions.AIAction;
import chess.model.ChessBoard;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
import chess.model.Piece;
public class AlphaBetaThread extends Thread {
@@ -52,27 +52,27 @@ public class AlphaBetaThread extends Thread {
return result;
}
public float getMoveValue(Move move, int searchDepth) {
this.simulation.tryMove(move);
public float getMoveValue(AIAction move, int searchDepth) {
move.applyAction(this.simulation.getCommandExecutor());
float value = -negaMax(searchDepth - 1, MIN_FLOAT, MAX_FLOAT);
this.simulation.undoMove();
move.undoAction(this.simulation.getCommandExecutor());
return value;
}
private float negaMax(int depth, float alpha, float beta) {
float value = MIN_FLOAT;
List<Move> moves = this.simulation.getAllowedMoves();
List<AIAction> moves = this.simulation.getAllowedActions();
if (moves.isEmpty())
return -getEndGameEvaluation();
List<Entry<Move, Float>> movesCost = new ArrayList<>(moves.size());
List<Entry<AIAction, Float>> movesCost = new ArrayList<>(moves.size());
for (Move move : moves) {
this.simulation.tryMove(move);
for (AIAction move : moves) {
move.applyAction();
movesCost.add(Map.entry(move, -getBoardEvaluation()));
this.simulation.undoMove();
move.undoAction();
}
Collections.sort(movesCost, (first, second) -> {
@@ -83,10 +83,10 @@ public class AlphaBetaThread extends Thread {
return -movesCost.getFirst().getValue();
for (var moveEntry : movesCost) {
Move move = moveEntry.getKey();
this.simulation.tryMove(move);
AIAction move = moveEntry.getKey();
move.applyAction();
value = Float.max(value, -negaMax(depth - 1, -beta, -alpha));
this.simulation.undoMove();
move.undoAction();
alpha = Float.max(alpha, value);
if (alpha >= beta)
return value;

View File

@@ -2,9 +2,9 @@ package chess.ai.minimax;
import java.util.concurrent.ThreadFactory;
import chess.ai.actions.AIAction;
import chess.controller.CommandExecutor;
import chess.model.Color;
import chess.model.Move;
public class AlphaBetaThreadCreator implements ThreadFactory{
@@ -21,7 +21,7 @@ public class AlphaBetaThreadCreator implements ThreadFactory{
}
}
public static float getMoveValue(Move move, int searchDepth) {
public static float getMoveValue(AIAction move, int searchDepth) {
AlphaBetaThread t = (AlphaBetaThread) Thread.currentThread();
return t.getMoveValue(move, searchDepth);
}

View File

@@ -2,16 +2,17 @@ package chess.ai.minimax;
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.GetPlayerMovesCommand;
import chess.controller.commands.MoveCommand;
import chess.controller.commands.NewGameCommand;
import chess.controller.commands.PromoteCommand;
import chess.controller.commands.UndoCommand;
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;
@@ -90,10 +91,8 @@ public class GameSimulation extends GameAdapter {
return this.gameSimulation.getPlayerTurn();
}
public List<Move> getAllowedMoves() {
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
sendCommand(cmd);
return cmd.getMoves();
public List<AIAction> getAllowedActions() {
return AIActions.getAllowedActions(this.simulation);
}
public void close() {