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; package chess;
import chess.ai.minimax.AlphaBetaAI;
import chess.ai.minimax.AlphaBetaConsolePrinter;
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.Color;
import chess.model.Game; import chess.model.Game;
import chess.pgn.PgnExport; import chess.pgn.PgnExport;
import chess.view.audio.GameAudio; import chess.view.audio.GameAudio;
@@ -16,8 +19,11 @@ public class SwingMain {
Window window = new Window(commandExecutor, true); Window window = new Window(commandExecutor, true);
commandExecutor.addListener(window); commandExecutor.addListener(window);
// AI ai = new AlphaBetaAI(commandExecutor, Color.Black, 5); AlphaBetaAI ai = new AlphaBetaAI(commandExecutor, Color.Black, 5);
// commandExecutor.addListener(ai); commandExecutor.addListener(ai);
AlphaBetaConsolePrinter aiResults = new AlphaBetaConsolePrinter(ai);
aiResults.connect();
// AI ai2 = new AlphaBetaAI(commandExecutor, Color.White, 5); // AI ai2 = new AlphaBetaAI(commandExecutor, Color.White, 5);
// commandExecutor.addListener(ai2); // commandExecutor.addListener(ai2);

View File

@@ -15,6 +15,7 @@ import chess.controller.commands.PromoteCommand.PromoteType;
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 common.Signal1;
public class AlphaBetaAI extends AI { public class AlphaBetaAI extends AI {
@@ -25,11 +26,16 @@ public class AlphaBetaAI extends AI {
private final ExecutorService threadPool; 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) { public AlphaBetaAI(CommandExecutor commandExecutor, Color color, int searchDepth) {
super(commandExecutor, color); super(commandExecutor, color);
this.searchDepth = searchDepth; this.searchDepth = searchDepth;
int threadCount = Runtime.getRuntime().availableProcessors(); 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() { private Move getBestMove() {
@@ -38,7 +44,7 @@ public class AlphaBetaAI extends AI {
float bestMoveValue = MIN_FLOAT; float bestMoveValue = MIN_FLOAT;
Move bestMove = null; Move bestMove = null;
System.out.println("Evaluating " + moves.size() + " moves ..."); this.onStartEval.emit(moves.size());
for (Move move : moves) { for (Move move : moves) {
moveEvaluations.add(this.threadPool.submit(() -> { moveEvaluations.add(this.threadPool.submit(() -> {
@@ -47,9 +53,9 @@ public class AlphaBetaAI extends AI {
} }
for (int i = 0; i < moves.size(); i++) { 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); Move move = moves.get(i);
System.out.print("\r");
float value = MIN_FLOAT; float value = MIN_FLOAT;
try { try {
value = moveEvaluations.get(i).get(); 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; 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;
}
}