refactor: add signals to alphabetaai
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -15,6 +15,7 @@ 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 {
|
||||
|
||||
@@ -25,11 +26,16 @@ public class AlphaBetaAI extends AI {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user