class documentation - a shitload of it
All checks were successful
Linux arm64 / Build (push) Successful in 33s
All checks were successful
Linux arm64 / Build (push) Successful in 33s
This commit is contained in:
93
app/src/main/java/chess/ai/ais/AlphaBetaAI.java
Normal file
93
app/src/main/java/chess/ai/ais/AlphaBetaAI.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package chess.ai.ais;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import chess.ai.*;
|
||||
import chess.ai.actions.*;
|
||||
import chess.ai.alphabeta.*;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.model.Color;
|
||||
import common.Signal1;
|
||||
|
||||
/**
|
||||
* Extends AI. Bot based on the alpha-beta method of resolution.
|
||||
*/
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
private AIAction getBestMove() {
|
||||
List<AIAction> actions = getAllowedActions();
|
||||
List<Future<Float>> moveEvaluations = new ArrayList<>(actions.size());
|
||||
float bestMoveValue = MIN_FLOAT;
|
||||
AIAction bestMove = null;
|
||||
|
||||
this.onStartEval.emit(actions.size());
|
||||
|
||||
for (AIAction action : actions) {
|
||||
moveEvaluations.add(this.threadPool.submit(() -> {
|
||||
return AlphaBetaThreadCreator.getMoveValue(action, this.searchDepth);
|
||||
}));
|
||||
}
|
||||
|
||||
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 {
|
||||
value = moveEvaluations.get(i).get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (value > bestMoveValue) {
|
||||
bestMoveValue = value;
|
||||
bestMove = action;
|
||||
}
|
||||
}
|
||||
|
||||
this.onCompleteEval.emit(bestMoveValue);
|
||||
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGameEnd() {
|
||||
this.threadPool.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void play() {
|
||||
AIAction move = getBestMove();
|
||||
move.applyAction();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandExecutor getCommandExecutor() {
|
||||
return super.getCommandExecutor();
|
||||
}
|
||||
|
||||
}
|
||||
32
app/src/main/java/chess/ai/ais/DumbAI.java
Normal file
32
app/src/main/java/chess/ai/ais/DumbAI.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package chess.ai.ais;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import chess.ai.*;
|
||||
import chess.ai.actions.AIAction;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.model.Color;
|
||||
|
||||
/**
|
||||
* Bot, takes a random decision among his possible moves.
|
||||
*/
|
||||
|
||||
public class DumbAI extends AI {
|
||||
|
||||
private final Random random = new Random();
|
||||
|
||||
public DumbAI(CommandExecutor commandExecutor, Color color) {
|
||||
super(commandExecutor, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void play() {
|
||||
List<AIAction> actions = getAllowedActions();
|
||||
|
||||
int randomAction = this.random.nextInt(actions.size());
|
||||
|
||||
actions.get(randomAction).applyAction();
|
||||
}
|
||||
|
||||
}
|
||||
64
app/src/main/java/chess/ai/ais/HungryAI.java
Normal file
64
app/src/main/java/chess/ai/ais/HungryAI.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package chess.ai.ais;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import chess.ai.actions.AIAction;
|
||||
import chess.ai.actions.AIActionMove;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.model.Color;
|
||||
import chess.model.Move;
|
||||
import chess.model.Piece;
|
||||
import chess.ai.*;
|
||||
import chess.ai.actions.*;
|
||||
import chess.ai.actions.AIActions;
|
||||
|
||||
|
||||
/**
|
||||
* Bot, takes the optimal piece when possible, or make a random move if no piece can be taken during the turn.
|
||||
* @see PieceCost
|
||||
* @see PiecePosCost
|
||||
*/
|
||||
|
||||
public class HungryAI extends AI {
|
||||
|
||||
private final PieceCost pieceCost;
|
||||
private final Random random;
|
||||
|
||||
public HungryAI(CommandExecutor commandExecutor, Color color) {
|
||||
super(commandExecutor, color);
|
||||
this.pieceCost = new PieceCost(color);
|
||||
this.random = new Random();
|
||||
}
|
||||
|
||||
private int getMoveCost(Move move) {
|
||||
Piece piece = getPieceAt(move.getDeadPieceCoords());
|
||||
return -(int) pieceCost.getCost(piece);
|
||||
}
|
||||
|
||||
private List<AIAction> getBestMoves() {
|
||||
List<AIAction> actions = getAllowedActions();
|
||||
List<AIAction> bestMoves = new ArrayList<>();
|
||||
int bestCost = 0;
|
||||
for (AIAction action : actions) {
|
||||
if (action instanceof AIActionMove move) {
|
||||
int moveCost = getMoveCost(move.getMove());
|
||||
if (moveCost == bestCost) {
|
||||
bestMoves.add(move);
|
||||
} else if (moveCost > bestCost) {
|
||||
bestMoves.clear();
|
||||
bestMoves.add(move);
|
||||
bestCost = moveCost;
|
||||
}
|
||||
}
|
||||
}
|
||||
return bestMoves;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void play() {
|
||||
List<AIAction> bestMoves = getBestMoves();
|
||||
bestMoves.get(this.random.nextInt(bestMoves.size())).applyAction();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user