refactor: add signals to alphabetaai

This commit is contained in:
2025-05-02 19:47:03 +02:00
parent 2a3ea10389
commit 64bed4ea3a
3 changed files with 42 additions and 8 deletions

View File

@@ -1,8 +1,11 @@
package chess;
import chess.ai.minimax.AlphaBetaAI;
import chess.ai.minimax.AlphaBetaConsolePrinter;
import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand;
import chess.controller.event.GameAdaptator;
import chess.model.Color;
import chess.model.Game;
import chess.pgn.PgnExport;
import chess.view.audio.GameAudio;
@@ -16,8 +19,11 @@ public class SwingMain {
Window window = new Window(commandExecutor, true);
commandExecutor.addListener(window);
// AI ai = new AlphaBetaAI(commandExecutor, Color.Black, 5);
// commandExecutor.addListener(ai);
AlphaBetaAI ai = new AlphaBetaAI(commandExecutor, Color.Black, 5);
commandExecutor.addListener(ai);
AlphaBetaConsolePrinter aiResults = new AlphaBetaConsolePrinter(ai);
aiResults.connect();
// AI ai2 = new AlphaBetaAI(commandExecutor, Color.White, 5);
// commandExecutor.addListener(ai2);

View File

@@ -15,21 +15,27 @@ 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 {
private final int searchDepth;
private static final float MAX_FLOAT = Float.MAX_VALUE;
private static final float MIN_FLOAT = -MAX_FLOAT;
private final ExecutorService threadPool;
public final Signal1<Integer> onStartEval = new Signal1<>();
public final Signal1<Float> onCompleteEval = new Signal1<>();
public final Signal1<Float> onProgress = new Signal1<>();
public AlphaBetaAI(CommandExecutor commandExecutor, Color color, int searchDepth) {
super(commandExecutor, color);
this.searchDepth = searchDepth;
int threadCount = Runtime.getRuntime().availableProcessors();
this.threadPool = Executors.newFixedThreadPool(threadCount, new AlphaBetaThreadCreator(commandExecutor, color, threadCount));
this.threadPool = Executors.newFixedThreadPool(threadCount,
new AlphaBetaThreadCreator(commandExecutor, color, threadCount));
}
private Move getBestMove() {
@@ -38,7 +44,7 @@ public class AlphaBetaAI extends AI {
float bestMoveValue = MIN_FLOAT;
Move bestMove = null;
System.out.println("Evaluating " + moves.size() + " moves ...");
this.onStartEval.emit(moves.size());
for (Move move : moves) {
moveEvaluations.add(this.threadPool.submit(() -> {
@@ -47,9 +53,9 @@ public class AlphaBetaAI extends AI {
}
for (int i = 0; i < moves.size(); i++) {
System.out.printf("Progress : %.2f %% ", (float) (i) / moves.size() * 100.0f);
this.onProgress.emit((float) i / (float) moves.size());
Move move = moves.get(i);
System.out.print("\r");
float value = MIN_FLOAT;
try {
value = moveEvaluations.get(i).get();
@@ -62,7 +68,7 @@ public class AlphaBetaAI extends AI {
}
}
System.out.println("Best move : " + bestMoveValue + " ");
this.onCompleteEval.emit(bestMoveValue);
return bestMove;
}

View File

@@ -0,0 +1,22 @@
package chess.ai.minimax;
public class AlphaBetaConsolePrinter {
private final AlphaBetaAI ai;
public void connect() {
ai.onStartEval.connect((moveCount) -> {
System.out.println("Evaluating " + moveCount + " moves ...");
});
ai.onProgress.connect((progress) -> {
System.out.printf("Progress : %.2f %% \r", progress * 100.0f);
});
ai.onCompleteEval.connect((bestMove) -> {
System.out.println("Best move : " + bestMove + " ");
});
}
public AlphaBetaConsolePrinter(AlphaBetaAI ai) {
this.ai = ai;
}
}