working
This commit is contained in:
@@ -10,6 +10,7 @@ import chess.controller.commands.MoveCommand;
|
|||||||
import chess.controller.commands.NewGameCommand;
|
import chess.controller.commands.NewGameCommand;
|
||||||
import chess.controller.commands.PromoteCommand;
|
import chess.controller.commands.PromoteCommand;
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
|
import chess.controller.event.EmptyGameDispatcher;
|
||||||
import chess.controller.commands.UndoCommand;
|
import chess.controller.commands.UndoCommand;
|
||||||
import chess.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
@@ -24,12 +25,15 @@ public class AlphaBetaAI extends AI {
|
|||||||
private final CommandExecutor simulation;
|
private final CommandExecutor simulation;
|
||||||
private final Game gameSimulation;
|
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) {
|
public AlphaBetaAI(CommandExecutor commandExecutor, Color color, int searchDepth) {
|
||||||
super(commandExecutor, color);
|
super(commandExecutor, color);
|
||||||
this.searchDepth = 3;
|
this.searchDepth = searchDepth;
|
||||||
this.pieceCost = new PieceCost(color);
|
this.pieceCost = new PieceCost(color);
|
||||||
this.gameSimulation = new Game();
|
this.gameSimulation = new Game();
|
||||||
this.simulation = new CommandExecutor(this.gameSimulation);
|
this.simulation = new CommandExecutor(this.gameSimulation, new EmptyGameDispatcher());
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandExecutor getSimulation() {
|
public CommandExecutor getSimulation() {
|
||||||
@@ -44,9 +48,32 @@ public class AlphaBetaAI extends AI {
|
|||||||
result += pieceCost.getCost(board.pieceAt(new Coordinate(i, j)));
|
result += pieceCost.getCost(board.pieceAt(new Coordinate(i, j)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (this.gameSimulation.getPlayerTurn() != color)
|
||||||
|
return -result;
|
||||||
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) {
|
private int negaMax(int depth, int alpha, int beta) {
|
||||||
if (depth == 0)
|
if (depth == 0)
|
||||||
return getBoardEvaluation();
|
return getBoardEvaluation();
|
||||||
@@ -55,20 +82,13 @@ public class AlphaBetaAI extends AI {
|
|||||||
|
|
||||||
List<Move> moves = getAllowedMoves(this.simulation);
|
List<Move> moves = getAllowedMoves(this.simulation);
|
||||||
|
|
||||||
if (moves.isEmpty()) {
|
if (moves.isEmpty())
|
||||||
System.out.println("oaaaaaaaaa");
|
return getEndGameEvaluation();
|
||||||
int board = getBoardEvaluation();
|
|
||||||
int result = board + (this.gameSimulation.getPlayerTurn() == this.color ? -100 : 100);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Move move : moves) {
|
for (Move move : moves) {
|
||||||
sendCommand(new MoveCommand(move), this.simulation);
|
simulateMove(move);
|
||||||
if (this.gameSimulation.getBoard().pawnShouldBePromoted())
|
value = Integer.max(value, -negaMax(depth - 1, -beta, -alpha));
|
||||||
sendCommand(new PromoteCommand(PromoteType.Queen), this.simulation);
|
simulateUndo();
|
||||||
int leaveValue = -negaMax(depth - 1, -beta, -alpha);
|
|
||||||
value = Integer.max(value, leaveValue);
|
|
||||||
sendCommand(new UndoCommand(), this.simulation);
|
|
||||||
alpha = Integer.max(alpha, value);
|
alpha = Integer.max(alpha, value);
|
||||||
if (alpha >= beta)
|
if (alpha >= beta)
|
||||||
return value;
|
return value;
|
||||||
@@ -83,11 +103,9 @@ public class AlphaBetaAI extends AI {
|
|||||||
List<Move> bestMoves = new ArrayList<>(20);
|
List<Move> bestMoves = new ArrayList<>(20);
|
||||||
|
|
||||||
for (Move move : moves) {
|
for (Move move : moves) {
|
||||||
sendCommand(new MoveCommand(move), this.simulation);
|
simulateMove(move);
|
||||||
if (this.gameSimulation.getBoard().pawnShouldBePromoted())
|
|
||||||
sendCommand(new PromoteCommand(PromoteType.Queen), this.simulation);
|
|
||||||
int value = -negaMax(this.searchDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
int value = -negaMax(this.searchDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||||
sendCommand(new UndoCommand(), this.simulation);
|
simulateUndo();
|
||||||
if (value > bestMove) {
|
if (value > bestMove) {
|
||||||
bestMove = value;
|
bestMove = value;
|
||||||
bestMoves.clear();
|
bestMoves.clear();
|
||||||
@@ -108,9 +126,12 @@ public class AlphaBetaAI extends AI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPawnPromoted(PromoteType promotion, Color player) {
|
public void onGameEnd() {
|
||||||
// if (player == this.color)
|
this.simulation.close();
|
||||||
// return;
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPawnPromoted(PromoteType promotion) {
|
||||||
sendCommand(new PromoteCommand(promotion), this.simulation);
|
sendCommand(new PromoteCommand(promotion), this.simulation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,13 @@ public class PieceCost implements PieceVisitor<Integer> {
|
|||||||
|
|
||||||
private final Color player;
|
private final Color player;
|
||||||
|
|
||||||
|
public static final int BISHOP = 3;
|
||||||
|
public static final int KING = 90;
|
||||||
|
public static final int KNIGHT = 3;
|
||||||
|
public static final int PAWN = 1;
|
||||||
|
public static final int QUEEN = 9;
|
||||||
|
public static final int ROOK = 5;
|
||||||
|
|
||||||
public PieceCost(Color color) {
|
public PieceCost(Color color) {
|
||||||
this.player = color;
|
this.player = color;
|
||||||
}
|
}
|
||||||
@@ -22,39 +29,39 @@ public class PieceCost implements PieceVisitor<Integer> {
|
|||||||
if (piece == null)
|
if (piece == null)
|
||||||
return 0;
|
return 0;
|
||||||
int cost = visit(piece);
|
int cost = visit(piece);
|
||||||
if (piece.getColor() == player)
|
if (piece.getColor() != player)
|
||||||
cost = -cost;
|
cost = -cost;
|
||||||
return cost;
|
return cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer visitPiece(Bishop bishop) {
|
public Integer visitPiece(Bishop bishop) {
|
||||||
return 3;
|
return BISHOP;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer visitPiece(King king) {
|
public Integer visitPiece(King king) {
|
||||||
return 90;
|
return KING;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer visitPiece(Knight knight) {
|
public Integer visitPiece(Knight knight) {
|
||||||
return 3;
|
return KNIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer visitPiece(Pawn pawn) {
|
public Integer visitPiece(Pawn pawn) {
|
||||||
return 1;
|
return PAWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer visitPiece(Queen queen) {
|
public Integer visitPiece(Queen queen) {
|
||||||
return 9;
|
return QUEEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer visitPiece(Rook rook) {
|
public Integer visitPiece(Rook rook) {
|
||||||
return 5;
|
return ROOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,17 +52,20 @@ public class CommandExecutor {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
case Moved:
|
case Moved:
|
||||||
|
boolean notifyPlayerTurn = true;
|
||||||
this.dispatcher.onBoardUpdate();
|
this.dispatcher.onBoardUpdate();
|
||||||
if (checkGameStatus()) {
|
if (checkGameStatus()) {
|
||||||
this.dispatcher.onGameEnd();
|
this.dispatcher.onGameEnd();
|
||||||
|
notifyPlayerTurn = false;
|
||||||
}
|
}
|
||||||
switchPlayerTurn(command instanceof UndoCommand);
|
switchPlayerTurn(command instanceof UndoCommand, notifyPlayerTurn);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void switchPlayerTurn(boolean undone) {
|
private void switchPlayerTurn(boolean undone, boolean notifyPlayerTurn) {
|
||||||
this.game.switchPlayerTurn();
|
this.game.switchPlayerTurn();
|
||||||
|
if (notifyPlayerTurn)
|
||||||
this.dispatcher.onPlayerTurn(this.game.getPlayerTurn(), undone);
|
this.dispatcher.onPlayerTurn(this.game.getPlayerTurn(), undone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public class PromoteCommand extends PlayerCommand {
|
|||||||
this.oldPawn = pawn;
|
this.oldPawn = pawn;
|
||||||
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
||||||
|
|
||||||
outputSystem.onPawnPromoted(this.promoteType, game.getPlayerTurn());
|
outputSystem.onPawnPromoted(this.promoteType);
|
||||||
|
|
||||||
return CommandResult.Moved;
|
return CommandResult.Moved;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package chess.controller.event;
|
package chess.controller.event;
|
||||||
|
|
||||||
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
@@ -58,4 +59,12 @@ public class EmptyGameDispatcher extends GameDispatcher {
|
|||||||
public void onWin(Color winner) {
|
public void onWin(Color winner) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCastling(boolean bigCastling) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPawnPromoted(PromoteType promotion) {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,6 @@ public abstract class GameAdaptator implements GameListener {
|
|||||||
public void onCastling(boolean bigCastling) {}
|
public void onCastling(boolean bigCastling) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPawnPromoted(PromoteType promotion, Color player) {}
|
public void onPawnPromoted(PromoteType promotion) {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,8 +100,8 @@ public class GameDispatcher implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPawnPromoted(PromoteType promotion, Color player) {
|
public void onPawnPromoted(PromoteType promotion) {
|
||||||
asyncForEachCall((l) -> l.onPawnPromoted(promotion, player));
|
asyncForEachCall((l) -> l.onPawnPromoted(promotion));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopService() {
|
public void stopService() {
|
||||||
|
|||||||
@@ -98,6 +98,6 @@ public interface GameListener {
|
|||||||
* @param promotion the type of promotion
|
* @param promotion the type of promotion
|
||||||
* @param player the player who promoted the pawns
|
* @param player the player who promoted the pawns
|
||||||
*/
|
*/
|
||||||
void onPawnPromoted(PromoteType promotion, Color player);
|
void onPawnPromoted(PromoteType promotion);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,24 +70,26 @@ public class Game {
|
|||||||
playerTurn = Color.getEnemy(playerTurn);
|
playerTurn = Color.getEnemy(playerTurn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameStatus checkGameStatus() {
|
public GameStatus checkGameStatus(Color color) {
|
||||||
final Color enemy = Color.getEnemy(getPlayerTurn());
|
|
||||||
|
|
||||||
if (checkDraw())
|
if (checkDraw())
|
||||||
return GameStatus.Draw;
|
return GameStatus.Draw;
|
||||||
|
|
||||||
if (this.board.isKingInCheck(enemy))
|
if (this.board.isKingInCheck(color))
|
||||||
if (this.board.hasAllowedMoves(enemy))
|
if (this.board.hasAllowedMoves(color))
|
||||||
return GameStatus.Check;
|
return GameStatus.Check;
|
||||||
else
|
else
|
||||||
return GameStatus.CheckMate;
|
return GameStatus.CheckMate;
|
||||||
|
|
||||||
if (!board.hasAllowedMoves(enemy))
|
if (!board.hasAllowedMoves(color))
|
||||||
return GameStatus.Pat;
|
return GameStatus.Pat;
|
||||||
|
|
||||||
return GameStatus.OnGoing;
|
return GameStatus.OnGoing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GameStatus checkGameStatus() {
|
||||||
|
return checkGameStatus(Color.getEnemy(getPlayerTurn()));
|
||||||
|
}
|
||||||
|
|
||||||
public void addAction(PlayerCommand command) {
|
public void addAction(PlayerCommand command) {
|
||||||
this.movesHistory.add(command);
|
this.movesHistory.add(command);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ public class PgnExport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String checkCheckMate(Game game) {
|
private static String checkCheckMate(Game game) {
|
||||||
switch (game.checkGameStatus()) {
|
switch (game.checkGameStatus(game.getPlayerTurn())) {
|
||||||
case CheckMate:
|
case CheckMate:
|
||||||
return "#";
|
return "#";
|
||||||
|
|
||||||
|
|||||||
@@ -337,7 +337,7 @@ public class Console implements GameListener {
|
|||||||
public void onCastling(boolean bigCastling) {}
|
public void onCastling(boolean bigCastling) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPawnPromoted(PromoteType promotion, Color player) {}
|
public void onPawnPromoted(PromoteType promotion) {}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -322,6 +322,6 @@ public class Window extends JFrame implements GameListener {
|
|||||||
public void onCastling(boolean bigCastling) {}
|
public void onCastling(boolean bigCastling) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPawnPromoted(PromoteType promotion, chess.model.Color player) {}
|
public void onPawnPromoted(PromoteType promotion) {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user