working
This commit is contained in:
@@ -10,6 +10,7 @@ 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;
|
||||
@@ -24,12 +25,15 @@ public class AlphaBetaAI extends AI {
|
||||
private final CommandExecutor simulation;
|
||||
private final Game gameSimulation;
|
||||
|
||||
private final int GREAT_MOVE = -9999;
|
||||
private final int HORRIBLE_MOVE = -GREAT_MOVE;
|
||||
|
||||
public AlphaBetaAI(CommandExecutor commandExecutor, Color color, int searchDepth) {
|
||||
super(commandExecutor, color);
|
||||
this.searchDepth = 3;
|
||||
this.searchDepth = searchDepth;
|
||||
this.pieceCost = new PieceCost(color);
|
||||
this.gameSimulation = new Game();
|
||||
this.simulation = new CommandExecutor(this.gameSimulation);
|
||||
this.simulation = new CommandExecutor(this.gameSimulation, new EmptyGameDispatcher());
|
||||
}
|
||||
|
||||
public CommandExecutor getSimulation() {
|
||||
@@ -44,9 +48,32 @@ public class AlphaBetaAI extends AI {
|
||||
result += pieceCost.getCost(board.pieceAt(new Coordinate(i, j)));
|
||||
}
|
||||
}
|
||||
if (this.gameSimulation.getPlayerTurn() != color)
|
||||
return -result;
|
||||
return result;
|
||||
}
|
||||
|
||||
private int getEndGameEvaluation() {
|
||||
Color currentTurn = this.gameSimulation.getPlayerTurn();
|
||||
if (currentTurn == this.color) {
|
||||
return HORRIBLE_MOVE;
|
||||
} else {
|
||||
if (this.gameSimulation.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 void simulateUndo() {
|
||||
sendCommand(new UndoCommand(), this.simulation);
|
||||
}
|
||||
|
||||
private int negaMax(int depth, int alpha, int beta) {
|
||||
if (depth == 0)
|
||||
return getBoardEvaluation();
|
||||
@@ -55,20 +82,13 @@ public class AlphaBetaAI extends AI {
|
||||
|
||||
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;
|
||||
}
|
||||
if (moves.isEmpty())
|
||||
return getEndGameEvaluation();
|
||||
|
||||
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);
|
||||
simulateMove(move);
|
||||
value = Integer.max(value, -negaMax(depth - 1, -beta, -alpha));
|
||||
simulateUndo();
|
||||
alpha = Integer.max(alpha, value);
|
||||
if (alpha >= beta)
|
||||
return value;
|
||||
@@ -83,11 +103,9 @@ public class AlphaBetaAI extends AI {
|
||||
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);
|
||||
simulateMove(move);
|
||||
int value = -negaMax(this.searchDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||
sendCommand(new UndoCommand(), this.simulation);
|
||||
simulateUndo();
|
||||
if (value > bestMove) {
|
||||
bestMove = value;
|
||||
bestMoves.clear();
|
||||
@@ -108,9 +126,12 @@ public class AlphaBetaAI extends AI {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPawnPromoted(PromoteType promotion, Color player) {
|
||||
// if (player == this.color)
|
||||
// return;
|
||||
public void onGameEnd() {
|
||||
this.simulation.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPawnPromoted(PromoteType promotion) {
|
||||
sendCommand(new PromoteCommand(promotion), this.simulation);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user