almost working
This commit is contained in:
141
app/src/main/java/chess/ai/AlphaBetaAI.java
Normal file
141
app/src/main/java/chess/ai/AlphaBetaAI.java
Normal file
@@ -0,0 +1,141 @@
|
||||
package chess.ai;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
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.commands.UndoCommand;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Game;
|
||||
import chess.model.Move;
|
||||
|
||||
public class AlphaBetaAI extends AI {
|
||||
|
||||
private final int searchDepth;
|
||||
private final PieceCost pieceCost;
|
||||
private final CommandExecutor simulation;
|
||||
private final Game gameSimulation;
|
||||
|
||||
public AlphaBetaAI(CommandExecutor commandExecutor, Color color, int searchDepth) {
|
||||
super(commandExecutor, color);
|
||||
this.searchDepth = 3;
|
||||
this.pieceCost = new PieceCost(color);
|
||||
this.gameSimulation = new Game();
|
||||
this.simulation = new CommandExecutor(this.gameSimulation);
|
||||
}
|
||||
|
||||
public CommandExecutor getSimulation() {
|
||||
return simulation;
|
||||
}
|
||||
|
||||
private int getBoardEvaluation() {
|
||||
final ChessBoard board = this.gameSimulation.getBoard();
|
||||
int result = 0;
|
||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||
result += pieceCost.getCost(board.pieceAt(new Coordinate(i, j)));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private int negaMax(int depth, int alpha, int beta) {
|
||||
if (depth == 0)
|
||||
return getBoardEvaluation();
|
||||
|
||||
int value = Integer.MIN_VALUE;
|
||||
|
||||
List<Move> moves = getAllowedMoves(this.simulation);
|
||||
|
||||
if (moves.isEmpty()) {
|
||||
System.out.println("oaaaaaaaaa");
|
||||
int board = getBoardEvaluation();
|
||||
int result = board + (this.gameSimulation.getPlayerTurn() == this.color ? -100 : 100);
|
||||
return result;
|
||||
}
|
||||
|
||||
for (Move move : moves) {
|
||||
sendCommand(new MoveCommand(move), this.simulation);
|
||||
if (this.gameSimulation.getBoard().pawnShouldBePromoted())
|
||||
sendCommand(new PromoteCommand(PromoteType.Queen), this.simulation);
|
||||
int leaveValue = -negaMax(depth - 1, -beta, -alpha);
|
||||
value = Integer.max(value, leaveValue);
|
||||
sendCommand(new UndoCommand(), this.simulation);
|
||||
alpha = Integer.max(alpha, value);
|
||||
if (alpha >= beta)
|
||||
return value;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private Move getBestMove() {
|
||||
List<Move> moves = getAllowedMoves(this.simulation);
|
||||
int bestMove = Integer.MIN_VALUE;
|
||||
List<Move> bestMoves = new ArrayList<>(20);
|
||||
|
||||
for (Move move : moves) {
|
||||
sendCommand(new MoveCommand(move), this.simulation);
|
||||
if (this.gameSimulation.getBoard().pawnShouldBePromoted())
|
||||
sendCommand(new PromoteCommand(PromoteType.Queen), this.simulation);
|
||||
int value = -negaMax(this.searchDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||
sendCommand(new UndoCommand(), this.simulation);
|
||||
if (value > bestMove) {
|
||||
bestMove = value;
|
||||
bestMoves.clear();
|
||||
bestMoves.add(move);
|
||||
} else if (value == bestMove) {
|
||||
bestMoves.add(move);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Best move : " + bestMove + " count : " + bestMoves.size());
|
||||
|
||||
return bestMoves.get(new Random().nextInt(bestMoves.size()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGameStart() {
|
||||
sendCommand(new NewGameCommand(), this.simulation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPawnPromoted(PromoteType promotion, Color player) {
|
||||
// if (player == this.color)
|
||||
// return;
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void play() {
|
||||
long current = System.currentTimeMillis();
|
||||
Move move = getBestMove();
|
||||
long elapsed = System.currentTimeMillis() - current;
|
||||
System.out.println("Took " + elapsed + "ms");
|
||||
sendCommand(new MoveCommand(move));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void promote(Coordinate pawnCoords) {
|
||||
sendCommand(new PromoteCommand(PromoteType.Queen));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user