big refactor

This commit is contained in:
2025-04-19 12:56:08 +02:00
parent 031d3d94ec
commit 13b61ad71c
10 changed files with 366 additions and 264 deletions

View File

@@ -2,20 +2,18 @@ package chess.ai;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import chess.controller.CommandExecutor;
import chess.controller.commands.CastlingCommand;
import chess.controller.commands.MoveCommand;
import chess.controller.commands.NewGameCommand;
import chess.controller.commands.PromoteCommand;
import chess.controller.commands.PromoteCommand.PromoteType;
import chess.controller.event.EmptyGameDispatcher;
import chess.controller.commands.UndoCommand;
import chess.model.ChessBoard;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Game;
import chess.model.Move;
import chess.model.Piece;
@@ -24,28 +22,30 @@ public class AlphaBetaAI extends AI {
private final int searchDepth;
private final PieceCost pieceCost;
private final PiecePosCost piecePosCost;
private final CommandExecutor simulation;
private final Game gameSimulation;
private final GameSimulation simulation;
private final int GREAT_MOVE = -9999;
private final int HORRIBLE_MOVE = -GREAT_MOVE;
private static final float MAX_FLOAT = Float.MAX_VALUE;
private static final float MIN_FLOAT = -MAX_FLOAT;
private static final int GREAT_MOVE = -9999;
private static final int HORRIBLE_MOVE = -GREAT_MOVE;
private final ExecutorService threadPool;
public AlphaBetaAI(CommandExecutor commandExecutor, Color color, int searchDepth) {
super(commandExecutor, color);
this.searchDepth = searchDepth;
this.pieceCost = new PieceCost(color);
this.piecePosCost = new PiecePosCost(color);
this.gameSimulation = new Game();
this.simulation = new CommandExecutor(this.gameSimulation, new EmptyGameDispatcher());
this.simulation = new GameSimulation();
commandExecutor.addListener(simulation);
this.threadPool = Executors.newSingleThreadExecutor();
System.out.println(Runtime.getRuntime().availableProcessors());
}
public CommandExecutor getSimulation() {
return simulation;
}
private int getBoardEvaluation() {
final ChessBoard board = this.gameSimulation.getBoard();
int result = 0;
private float getBoardEvaluation() {
final ChessBoard board = this.simulation.getBoard();
float result = 0;
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
Coordinate coordinate = new Coordinate(i, j);
@@ -53,48 +53,45 @@ public class AlphaBetaAI extends AI {
result += pieceCost.getCost(piece) + piecePosCost.getEvaluation(piece, coordinate);
}
}
if (this.gameSimulation.getPlayerTurn() != color)
if (this.simulation.getPlayerTurn() != color)
return -result;
return result;
}
private int getEndGameEvaluation() {
Color currentTurn = this.gameSimulation.getPlayerTurn();
private float getEndGameEvaluation() {
Color currentTurn = this.simulation.getPlayerTurn();
if (currentTurn == this.color) {
return HORRIBLE_MOVE;
} else {
if (this.gameSimulation.getBoard().isKingInCheck(currentTurn))
if (this.simulation.getBoard().isKingInCheck(currentTurn))
return GREAT_MOVE;
return getBoardEvaluation() + PieceCost.PAWN;
}
}
private void simulateMove(Move move) {
sendCommand(new MoveCommand(move), this.simulation);
if (this.gameSimulation.getBoard().pawnShouldBePromoted())
sendCommand(new PromoteCommand(PromoteType.Queen), this.simulation);
private float getMoveValue(Move move) {
this.simulation.tryMove(move);
float value = -negaMax(this.searchDepth - 1, MIN_FLOAT, MAX_FLOAT);
this.simulation.undoMove();
return value;
}
private void simulateUndo() {
sendCommand(new UndoCommand(), this.simulation);
}
private int negaMax(int depth, int alpha, int beta) {
private float negaMax(int depth, float alpha, float beta) {
if (depth == 0)
return getBoardEvaluation();
int value = Integer.MIN_VALUE;
float value = MIN_FLOAT;
List<Move> moves = getAllowedMoves(this.simulation);
List<Move> moves = this.simulation.getAllowedMoves();
if (moves.isEmpty())
return getEndGameEvaluation();
for (Move move : moves) {
simulateMove(move);
value = Integer.max(value, -negaMax(depth - 1, -beta, -alpha));
simulateUndo();
alpha = Integer.max(alpha, value);
this.simulation.tryMove(move);
value = Float.max(value, -negaMax(depth - 1, -beta, -alpha));
this.simulation.undoMove();
alpha = Float.max(alpha, value);
if (alpha >= beta)
return value;
}
@@ -103,51 +100,39 @@ public class AlphaBetaAI extends AI {
}
private Move getBestMove() {
List<Move> moves = getAllowedMoves(this.simulation);
int bestMove = Integer.MIN_VALUE;
List<Move> bestMoves = new ArrayList<>(20);
List<Move> moves = this.simulation.getAllowedMoves();
List<Future<Float>> moveEvaluations = new ArrayList<>(50);
float bestMoveValue = MIN_FLOAT;
Move bestMove = null;
for (Move move : moves) {
simulateMove(move);
int value = -negaMax(this.searchDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
simulateUndo();
if (value > bestMove) {
bestMove = value;
bestMoves.clear();
bestMoves.add(move);
} else if (value == bestMove) {
bestMoves.add(move);
moveEvaluations.add(this.threadPool.submit(() -> {
return getMoveValue(move);
}));
}
for (int i = 0; i < moves.size(); i++) {
Move move = moves.get(i);
float value = MIN_FLOAT;
try {
value = moveEvaluations.get(i).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
if (value > bestMoveValue) {
bestMoveValue = value;
bestMove = move;
}
}
System.out.println("Best move : " + bestMove + " count : " + bestMoves.size());
System.out.println("Best move : " + bestMoveValue);
return bestMoves.get(new Random().nextInt(bestMoves.size()));
}
@Override
public void onGameStart() {
sendCommand(new NewGameCommand(), this.simulation);
return bestMove;
}
@Override
public void onGameEnd() {
this.simulation.close();
}
@Override
public void onPawnPromoted(PromoteType promotion) {
sendCommand(new PromoteCommand(promotion), this.simulation);
}
@Override
public void onCastling(boolean bigCastling) {
sendCommand(new CastlingCommand(bigCastling), this.simulation);
}
@Override
public void onMove(Move move) {
sendCommand(new MoveCommand(move), this.simulation);
this.threadPool.close();
}
@Override