Super IA #5
@@ -20,10 +20,10 @@ public class SwingMain {
|
|||||||
Window window = new Window(commandExecutor, false);
|
Window window = new Window(commandExecutor, false);
|
||||||
commandExecutor.addListener(window);
|
commandExecutor.addListener(window);
|
||||||
|
|
||||||
AI ai = new AlphaBetaAI(commandExecutor, Color.White, 3);
|
AI ai = new HungryAI(commandExecutor, Color.White);
|
||||||
commandExecutor.addListener(ai);
|
commandExecutor.addListener(ai);
|
||||||
|
|
||||||
AI ai2 = new AlphaBetaAI(commandExecutor, Color.Black, 3);
|
AI ai2 = new AlphaBetaAI(commandExecutor, Color.Black, 1);
|
||||||
commandExecutor.addListener(ai2);
|
commandExecutor.addListener(ai2);
|
||||||
|
|
||||||
// Window window2 = new Window(ai2.getSimulation(), false);
|
// Window window2 = new Window(ai2.getSimulation(), false);
|
||||||
|
|||||||
@@ -2,20 +2,18 @@ package chess.ai;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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.CommandExecutor;
|
||||||
import chess.controller.commands.CastlingCommand;
|
|
||||||
import chess.controller.commands.MoveCommand;
|
import chess.controller.commands.MoveCommand;
|
||||||
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.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Game;
|
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
|
|
||||||
@@ -24,28 +22,30 @@ public class AlphaBetaAI extends AI {
|
|||||||
private final int searchDepth;
|
private final int searchDepth;
|
||||||
private final PieceCost pieceCost;
|
private final PieceCost pieceCost;
|
||||||
private final PiecePosCost piecePosCost;
|
private final PiecePosCost piecePosCost;
|
||||||
private final CommandExecutor simulation;
|
private final GameSimulation simulation;
|
||||||
private final Game gameSimulation;
|
|
||||||
|
|
||||||
private final int GREAT_MOVE = -9999;
|
private static final float MAX_FLOAT = Float.MAX_VALUE;
|
||||||
private final int HORRIBLE_MOVE = -GREAT_MOVE;
|
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) {
|
public AlphaBetaAI(CommandExecutor commandExecutor, Color color, int searchDepth) {
|
||||||
super(commandExecutor, color);
|
super(commandExecutor, color);
|
||||||
this.searchDepth = searchDepth;
|
this.searchDepth = searchDepth;
|
||||||
this.pieceCost = new PieceCost(color);
|
this.pieceCost = new PieceCost(color);
|
||||||
this.piecePosCost = new PiecePosCost(color);
|
this.piecePosCost = new PiecePosCost(color);
|
||||||
this.gameSimulation = new Game();
|
this.simulation = new GameSimulation();
|
||||||
this.simulation = new CommandExecutor(this.gameSimulation, new EmptyGameDispatcher());
|
commandExecutor.addListener(simulation);
|
||||||
|
this.threadPool = Executors.newSingleThreadExecutor();
|
||||||
|
System.out.println(Runtime.getRuntime().availableProcessors());
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandExecutor getSimulation() {
|
private float getBoardEvaluation() {
|
||||||
return simulation;
|
final ChessBoard board = this.simulation.getBoard();
|
||||||
}
|
float result = 0;
|
||||||
|
|
||||||
private int getBoardEvaluation() {
|
|
||||||
final ChessBoard board = this.gameSimulation.getBoard();
|
|
||||||
int result = 0;
|
|
||||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
Coordinate coordinate = new Coordinate(i, j);
|
Coordinate coordinate = new Coordinate(i, j);
|
||||||
@@ -53,48 +53,45 @@ public class AlphaBetaAI extends AI {
|
|||||||
result += pieceCost.getCost(piece) + piecePosCost.getEvaluation(piece, coordinate);
|
result += pieceCost.getCost(piece) + piecePosCost.getEvaluation(piece, coordinate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.gameSimulation.getPlayerTurn() != color)
|
if (this.simulation.getPlayerTurn() != color)
|
||||||
return -result;
|
return -result;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getEndGameEvaluation() {
|
private float getEndGameEvaluation() {
|
||||||
Color currentTurn = this.gameSimulation.getPlayerTurn();
|
Color currentTurn = this.simulation.getPlayerTurn();
|
||||||
if (currentTurn == this.color) {
|
if (currentTurn == this.color) {
|
||||||
return HORRIBLE_MOVE;
|
return HORRIBLE_MOVE;
|
||||||
} else {
|
} else {
|
||||||
if (this.gameSimulation.getBoard().isKingInCheck(currentTurn))
|
if (this.simulation.getBoard().isKingInCheck(currentTurn))
|
||||||
return GREAT_MOVE;
|
return GREAT_MOVE;
|
||||||
return getBoardEvaluation() + PieceCost.PAWN;
|
return getBoardEvaluation() + PieceCost.PAWN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void simulateMove(Move move) {
|
private float getMoveValue(Move move) {
|
||||||
sendCommand(new MoveCommand(move), this.simulation);
|
this.simulation.tryMove(move);
|
||||||
if (this.gameSimulation.getBoard().pawnShouldBePromoted())
|
float value = -negaMax(this.searchDepth - 1, MIN_FLOAT, MAX_FLOAT);
|
||||||
sendCommand(new PromoteCommand(PromoteType.Queen), this.simulation);
|
this.simulation.undoMove();
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void simulateUndo() {
|
private float negaMax(int depth, float alpha, float beta) {
|
||||||
sendCommand(new UndoCommand(), this.simulation);
|
|
||||||
}
|
|
||||||
|
|
||||||
private int negaMax(int depth, int alpha, int beta) {
|
|
||||||
if (depth == 0)
|
if (depth == 0)
|
||||||
return getBoardEvaluation();
|
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())
|
if (moves.isEmpty())
|
||||||
return getEndGameEvaluation();
|
return getEndGameEvaluation();
|
||||||
|
|
||||||
for (Move move : moves) {
|
for (Move move : moves) {
|
||||||
simulateMove(move);
|
this.simulation.tryMove(move);
|
||||||
value = Integer.max(value, -negaMax(depth - 1, -beta, -alpha));
|
value = Float.max(value, -negaMax(depth - 1, -beta, -alpha));
|
||||||
simulateUndo();
|
this.simulation.undoMove();
|
||||||
alpha = Integer.max(alpha, value);
|
alpha = Float.max(alpha, value);
|
||||||
if (alpha >= beta)
|
if (alpha >= beta)
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@@ -103,51 +100,39 @@ public class AlphaBetaAI extends AI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Move getBestMove() {
|
private Move getBestMove() {
|
||||||
List<Move> moves = getAllowedMoves(this.simulation);
|
List<Move> moves = this.simulation.getAllowedMoves();
|
||||||
int bestMove = Integer.MIN_VALUE;
|
List<Future<Float>> moveEvaluations = new ArrayList<>(50);
|
||||||
List<Move> bestMoves = new ArrayList<>(20);
|
float bestMoveValue = MIN_FLOAT;
|
||||||
|
Move bestMove = null;
|
||||||
|
|
||||||
for (Move move : moves) {
|
for (Move move : moves) {
|
||||||
simulateMove(move);
|
moveEvaluations.add(this.threadPool.submit(() -> {
|
||||||
int value = -negaMax(this.searchDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
return getMoveValue(move);
|
||||||
simulateUndo();
|
}));
|
||||||
if (value > bestMove) {
|
}
|
||||||
bestMove = value;
|
|
||||||
bestMoves.clear();
|
for (int i = 0; i < moves.size(); i++) {
|
||||||
bestMoves.add(move);
|
Move move = moves.get(i);
|
||||||
} else if (value == bestMove) {
|
float value = MIN_FLOAT;
|
||||||
bestMoves.add(move);
|
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()));
|
return bestMove;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onGameStart() {
|
|
||||||
sendCommand(new NewGameCommand(), this.simulation);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onGameEnd() {
|
public void onGameEnd() {
|
||||||
this.simulation.close();
|
this.threadPool.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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
96
app/src/main/java/chess/ai/GameSimulation.java
Normal file
96
app/src/main/java/chess/ai/GameSimulation.java
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
package chess.ai;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.Command.CommandResult;
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.CastlingCommand;
|
||||||
|
import chess.controller.commands.GetPlayerMovesCommand;
|
||||||
|
import chess.controller.commands.MoveCommand;
|
||||||
|
import chess.controller.commands.NewGameCommand;
|
||||||
|
import chess.controller.commands.PromoteCommand;
|
||||||
|
import chess.controller.commands.UndoCommand;
|
||||||
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
|
import chess.controller.event.EmptyGameDispatcher;
|
||||||
|
import chess.controller.event.GameAdaptator;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
|
public class GameSimulation extends GameAdaptator {
|
||||||
|
|
||||||
|
private final CommandExecutor simulation;
|
||||||
|
private final Game gameSimulation;
|
||||||
|
|
||||||
|
public GameSimulation() {
|
||||||
|
this.gameSimulation = new Game();
|
||||||
|
this.simulation = new CommandExecutor(gameSimulation, new EmptyGameDispatcher());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected CommandResult sendCommand(Command command) {
|
||||||
|
CommandResult result = this.simulation.executeCommand(command);
|
||||||
|
if (result == CommandResult.NotAllowed) {
|
||||||
|
System.out.println("eeeeee");
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tryMove(Move move) {
|
||||||
|
sendCommand(new MoveCommand(move));
|
||||||
|
if (this.gameSimulation.getBoard().pawnShouldBePromoted())
|
||||||
|
sendCommand(new PromoteCommand(PromoteType.Queen));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void undoMove() {
|
||||||
|
sendCommand(new UndoCommand());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPawnPromoted(PromoteType promotion) {
|
||||||
|
sendCommand(new PromoteCommand(promotion));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCastling(boolean bigCastling) {
|
||||||
|
sendCommand(new CastlingCommand(bigCastling));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMove(Move move) {
|
||||||
|
sendCommand(new MoveCommand(move));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameStart() {
|
||||||
|
sendCommand(new NewGameCommand());
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandExecutor getCommandExecutor() {
|
||||||
|
return simulation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Game getGame() {
|
||||||
|
return gameSimulation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChessBoard getBoard() {
|
||||||
|
return this.gameSimulation.getBoard();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color getPlayerTurn() {
|
||||||
|
return this.gameSimulation.getPlayerTurn();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Move> getAllowedMoves() {
|
||||||
|
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
||||||
|
sendCommand(cmd);
|
||||||
|
return cmd.getMoves();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
this.simulation.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -26,7 +26,7 @@ public class HungryAI extends AI {
|
|||||||
|
|
||||||
private int getMoveCost(Move move) {
|
private int getMoveCost(Move move) {
|
||||||
Piece piece = pieceAt(move.getDeadPieceCoords());
|
Piece piece = pieceAt(move.getDeadPieceCoords());
|
||||||
return -pieceCost.getCost(piece);
|
return - (int) pieceCost.getCost(piece);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Move> getBestMoves() {
|
private List<Move> getBestMoves() {
|
||||||
|
|||||||
@@ -10,57 +10,57 @@ import chess.model.pieces.Pawn;
|
|||||||
import chess.model.pieces.Queen;
|
import chess.model.pieces.Queen;
|
||||||
import chess.model.pieces.Rook;
|
import chess.model.pieces.Rook;
|
||||||
|
|
||||||
public class PieceCost implements PieceVisitor<Integer> {
|
public class PieceCost implements PieceVisitor<Float> {
|
||||||
|
|
||||||
private final Color player;
|
private final Color player;
|
||||||
|
|
||||||
public static final int BISHOP = 30;
|
public static final float BISHOP = 30;
|
||||||
public static final int KING = 900;
|
public static final float KING = 900;
|
||||||
public static final int KNIGHT = 30;
|
public static final float KNIGHT = 30;
|
||||||
public static final int PAWN = 10;
|
public static final float PAWN = 10;
|
||||||
public static final int QUEEN = 90;
|
public static final float QUEEN = 90;
|
||||||
public static final int ROOK = 50;
|
public static final float ROOK = 50;
|
||||||
|
|
||||||
public PieceCost(Color color) {
|
public PieceCost(Color color) {
|
||||||
this.player = color;
|
this.player = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCost(Piece piece) {
|
public float getCost(Piece piece) {
|
||||||
if (piece == null)
|
if (piece == null)
|
||||||
return 0;
|
return 0;
|
||||||
int cost = visit(piece);
|
float 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 Float visitPiece(Bishop bishop) {
|
||||||
return BISHOP;
|
return BISHOP;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer visitPiece(King king) {
|
public Float visitPiece(King king) {
|
||||||
return KING;
|
return KING;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer visitPiece(Knight knight) {
|
public Float visitPiece(Knight knight) {
|
||||||
return KNIGHT;
|
return KNIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer visitPiece(Pawn pawn) {
|
public Float visitPiece(Pawn pawn) {
|
||||||
return PAWN;
|
return PAWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer visitPiece(Queen queen) {
|
public Float visitPiece(Queen queen) {
|
||||||
return QUEEN;
|
return QUEEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer visitPiece(Rook rook) {
|
public Float visitPiece(Rook rook) {
|
||||||
return ROOK;
|
return ROOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,114 +14,115 @@ import chess.model.pieces.Pawn;
|
|||||||
import chess.model.pieces.Queen;
|
import chess.model.pieces.Queen;
|
||||||
import chess.model.pieces.Rook;
|
import chess.model.pieces.Rook;
|
||||||
|
|
||||||
public class PiecePosCost implements PieceVisitor<List<Integer>> {
|
public class PiecePosCost implements PieceVisitor<List<Float>> {
|
||||||
|
|
||||||
private final Color color;
|
private final Color color;
|
||||||
|
|
||||||
private static final List<Integer> BISHOP = Arrays.asList(
|
private static final List<Float> BISHOP = Arrays.asList(
|
||||||
-2, -1, -1, -1, -1, -1, -1, -2, -1,
|
-2.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -2.0f,
|
||||||
0, 0, 0, 0, 0, 0, -1,
|
-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f,
|
||||||
-1, 0, 0, 1, 1, 0, 0, -1,
|
-1.0f, 0.0f, 0.5f, 1.0f, 1.0f, 0.5f, 0.0f, -1.0f,
|
||||||
-1, 0, 0, 1, 1, 0, 0, -1,
|
-1.0f, 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, -1.0f,
|
||||||
-1, 0, 0, 1, 1, 0, 0, -1,
|
-1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, -1.0f,
|
||||||
-1, 1, 1, 1, 1, 1, 1, -1,
|
-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
|
||||||
-1, 0, 0, 0, 0, 0, 0, -1,
|
-1.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, -1.0f,
|
||||||
-2, -1, -1, -1, -1, -1, -1, -2);
|
-2.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -2.0f);
|
||||||
|
|
||||||
private static final List<Integer> KING = Arrays.asList(
|
private static final List<Float> KING = Arrays.asList(
|
||||||
-3, -4, -4, -5, -5, -4, -4, -3,
|
-3.0f, -4.0f, -4.0f, -5.0f, -5.0f, -4.0f, -4.0f, -3.0f,
|
||||||
-3, -4, -4, -5, -5, -4, -4, -3,
|
-3.0f, -4.0f, -4.0f, -5.0f, -5.0f, -4.0f, -4.0f, -3.0f,
|
||||||
-3, -4, -4, -5, -5, -4, -4, -3,
|
-3.0f, -4.0f, -4.0f, -5.0f, -5.0f, -4.0f, -4.0f, -3.0f,
|
||||||
-3, -4, -4, -5, -5, -4, -4, -3,
|
-3.0f, -4.0f, -4.0f, -5.0f, -5.0f, -4.0f, -4.0f, -3.0f,
|
||||||
-2, -3, -3, -4, -4, -3, -3, -2,
|
-2.0f, -3.0f, -3.0f, -4.0f, -4.0f, -3.0f, -3.0f, -2.0f,
|
||||||
-1, -2, -2, -2, -2, -2, -2, -1,
|
-1.0f, -2.0f, -2.0f, -2.0f, -2.0f, -2.0f, -2.0f, -1.0f,
|
||||||
2, 2, 0, 0, 0, 0, 2, 2,
|
2.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 2.0f, 2.0f,
|
||||||
2, 3, 1, 0, 0, 1, 3, 2);
|
2.0f, 3.0f, 1.0f, 0.0f, 0.0f, 1.0f, 3.0f, 2.0f);
|
||||||
|
|
||||||
private static final List<Integer> KNIGHT = Arrays.asList(
|
private static final List<Float> KNIGHT = Arrays.asList(
|
||||||
-5, -4, -3, -3, -3, -3, -4, -5, -4,
|
-5.0f, -4.0f, -3.0f, -3.0f, -3.0f, -3.0f, -4.0f, -5.0f,
|
||||||
-2, 0, 0, 0, 0, -2, -4,
|
-4.0f, -2.0f, 0.0f, 0.0f, 0.0f, 0.0f, -2.0f, -4.0f,
|
||||||
-3, 0, 1, 1, 1, 1, 0, -3,
|
-3.0f, 0.0f, 1.0f, 1.5f, 1.5f, 1.0f, 0.0f, -3.0f,
|
||||||
-3, 0, 1, 2, 2, 1, 0, -3,
|
-3.0f, 0.5f, 1.5f, 2.0f, 2.0f, 1.5f, 0.5f, -3.0f,
|
||||||
-3, 0, 1, 2, 2, 1, 0, -3,
|
-3.0f, 0.0f, 1.5f, 2.0f, 2.0f, 1.5f, 0.0f, -3.0f,
|
||||||
-3, 0, 1, 1, 1, 1, 0, -3,
|
-3.0f, 0.5f, 1.0f, 1.5f, 1.5f, 1.0f, 0.5f, -3.0f,
|
||||||
-4, -2, 0, 0, 0, 0, -2, -4,
|
-4.0f, -2.0f, 0.0f, 0.5f, 0.5f, 0.0f, -2.0f, -4.0f,
|
||||||
-5, -4, -3, -3, -3, -3, -4, -5);
|
-5.0f, -4.0f, -3.0f, -3.0f, -3.0f, -3.0f, -4.0f, -5.0f);
|
||||||
|
|
||||||
private static final List<Integer> PAWN = Arrays.asList(
|
private static final List<Float> PAWN = Arrays.asList(
|
||||||
0, 0, 0, 0, 0, 0, 0, 0,
|
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
||||||
5, 5, 5, 5, 5, 5, 5, 5,
|
5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f,
|
||||||
1, 1, 2, 3, 3, 2, 1, 1,
|
1.0f, 1.0f, 2.0f, 3.0f, 3.0f, 2.0f, 1.0f, 1.0f,
|
||||||
0, 0, 1, 2, 2, 1, 0, 0,
|
0.5f, 0.5f, 1.0f, 2.5f, 2.5f, 1.0f, 0.5f, 0.5f,
|
||||||
0, 0, 1, 2, 2, 1, 0, 0,
|
0.0f, 0.0f, 1.0f, 2.0f, 2.0f, 1.0f, 0.0f, 0.0f,
|
||||||
0, 0, -1, 0, 0, -1, 0, 0,
|
0.5f, -0.5f, -1.0f, 0.0f, 0.0f, -1.0f, -0.5f, 0.5f,
|
||||||
0, 1, 1, -2, -2, 1, 1, 0,
|
0.5f, 1.0f, 1.0f, -2.0f, -2.0f, 1.0f, 1.0f, 0.5f,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0);
|
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
private static final List<Integer> QUEEN = Arrays.asList(
|
private static final List<Float> QUEEN = Arrays.asList(
|
||||||
-2, -1, -1, 0, 0, -1, -1, -2,
|
-2.0f, -1.0f, -1.0f, -0.5f, -0.5f, -1.0f, -1.0f, -2.0f,
|
||||||
-1, 0, 0, 0, 0, 0, 0, -1,
|
-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f,
|
||||||
-1, 0, 0, 0, 0, 0, 0, -1,
|
-1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.5f, 0.0f, -1.0f,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0,
|
-0.5f, 0.0f, 0.5f, 0.5f, 0.5f, 0.5f, 0.0f, -0.5f,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0,
|
0.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.5f, 0.0f, -0.5f,
|
||||||
-1, 0, 0, 0, 0, 0, 0, -1,
|
-1.0f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.0f, -1.0f,
|
||||||
-1, 0, 0, 0, 0, 0, 0, -1,
|
-1.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f,
|
||||||
-2, -1, -1, 0, 0, -1, -1, -2);
|
-2.0f, -1.0f, -1.0f, -0.5f, -0.5f, -1.0f, -1.0f, -2.0f);
|
||||||
|
|
||||||
private static final List<Integer> ROOK = Arrays.asList(
|
private static final List<Float> ROOK = Arrays.asList(
|
||||||
0, 0, 0, 0, 0, 0, 0, 0,
|
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
||||||
0, 1, 1, 1, 1, 1, 1, 0,
|
0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.5f,
|
||||||
-1, 0, 0, 0, 0, 0, 0, 0, -1,
|
-0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f,
|
||||||
-1, 0, 0, 0, 0, 0, 0, 0, -1,
|
-0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f,
|
||||||
-1, 0, 0, 0, 0, 0, 0, 0, -1,
|
-0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f,
|
||||||
-1, 0, 0, 0, 0, 0, 0, 0, -1,
|
-0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f,
|
||||||
-1, 0, 0, 0, 0, 0, 0, 0, -1,
|
-0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f,
|
||||||
0, 0, 0, 0, 1, 1, 0, 0, 0);
|
0.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
public PiecePosCost(Color color) {
|
public PiecePosCost(Color color) {
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getEvaluation(Piece piece, Coordinate coordinate) {
|
public float getEvaluation(Piece piece, Coordinate coordinate) {
|
||||||
if (piece == null)
|
if (piece == null)
|
||||||
return 0;
|
return 0;
|
||||||
List<Integer> positions = visit(piece);
|
List<Float> positions = visit(piece);
|
||||||
|
int x = piece.getColor() == Color.Black ? (Coordinate.VALUE_MAX - 1 - coordinate.getX()) : coordinate.getX();
|
||||||
int y = piece.getColor() == Color.Black ? (Coordinate.VALUE_MAX - 1 - coordinate.getY()) : coordinate.getY();
|
int y = piece.getColor() == Color.Black ? (Coordinate.VALUE_MAX - 1 - coordinate.getY()) : coordinate.getY();
|
||||||
Coordinate newCoords = new Coordinate(coordinate.getX(), y);
|
Coordinate newCoords = new Coordinate(x, y);
|
||||||
assert newCoords.isValid();
|
assert newCoords.isValid();
|
||||||
int result = positions.get(newCoords.toIndex());
|
float result = positions.get(newCoords.toIndex());
|
||||||
if (piece.getColor() != color)
|
if (piece.getColor() != color)
|
||||||
return -result;
|
return -result;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Integer> visitPiece(Bishop bishop) {
|
public List<Float> visitPiece(Bishop bishop) {
|
||||||
return BISHOP;
|
return BISHOP;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Integer> visitPiece(King king) {
|
public List<Float> visitPiece(King king) {
|
||||||
return KING;
|
return KING;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Integer> visitPiece(Knight knight) {
|
public List<Float> visitPiece(Knight knight) {
|
||||||
return KNIGHT;
|
return KNIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Integer> visitPiece(Pawn pawn) {
|
public List<Float> visitPiece(Pawn pawn) {
|
||||||
return PAWN;
|
return PAWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Integer> visitPiece(Queen queen) {
|
public List<Float> visitPiece(Queen queen) {
|
||||||
return QUEEN;
|
return QUEEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Integer> visitPiece(Rook rook) {
|
public List<Float> visitPiece(Rook rook) {
|
||||||
return ROOK;
|
return ROOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package chess.controller;
|
|||||||
|
|
||||||
import chess.controller.Command.CommandResult;
|
import chess.controller.Command.CommandResult;
|
||||||
import chess.controller.commands.UndoCommand;
|
import chess.controller.commands.UndoCommand;
|
||||||
|
import chess.controller.event.AsyncGameDispatcher;
|
||||||
import chess.controller.event.GameDispatcher;
|
import chess.controller.event.GameDispatcher;
|
||||||
import chess.controller.event.GameListener;
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
@@ -17,7 +18,7 @@ public class CommandExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public CommandExecutor(Game game) {
|
public CommandExecutor(Game game) {
|
||||||
this(game, new GameDispatcher());
|
this(game, new AsyncGameDispatcher());
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandExecutor(Game game, GameDispatcher dispatcher) {
|
public CommandExecutor(Game game, GameDispatcher dispatcher) {
|
||||||
@@ -109,6 +110,6 @@ public class CommandExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
this.dispatcher.stopService();
|
this.dispatcher.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,113 @@
|
|||||||
|
package chess.controller.event;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
|
public class AsyncGameDispatcher extends GameDispatcher {
|
||||||
|
|
||||||
|
private final List<GameListener> listeners;
|
||||||
|
private final ExecutorService executor;
|
||||||
|
|
||||||
|
public AsyncGameDispatcher() {
|
||||||
|
this.listeners = new ArrayList<>();
|
||||||
|
this.executor = Executors.newSingleThreadExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addListener(GameListener listener) {
|
||||||
|
this.listeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void asyncForEachCall(Consumer<GameListener> func) {
|
||||||
|
this.executor.execute(() -> this.listeners.forEach(func));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPlayerTurn(Color color, boolean undone) {
|
||||||
|
asyncForEachCall((l) -> l.onPlayerTurn(color, undone));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWin(Color color) {
|
||||||
|
asyncForEachCall((l) -> l.onWin(color));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInCheck() {
|
||||||
|
asyncForEachCall((l) -> l.onKingInCheck());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInMat() {
|
||||||
|
asyncForEachCall((l) -> l.onKingInMat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPatSituation() {
|
||||||
|
asyncForEachCall((l) -> l.onPatSituation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurrender(Color color) {
|
||||||
|
asyncForEachCall((l) -> l.onSurrender(color));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameStart() {
|
||||||
|
asyncForEachCall((l) -> l.onGameStart());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPromotePawn(Coordinate pieceCoords) {
|
||||||
|
asyncForEachCall((l) -> l.onPromotePawn(pieceCoords));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBoardUpdate() {
|
||||||
|
asyncForEachCall((l) -> l.onBoardUpdate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameEnd() {
|
||||||
|
asyncForEachCall((l) -> l.onGameEnd());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMove(Move move) {
|
||||||
|
asyncForEachCall((l) -> l.onMove(move));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMoveNotAllowed(Move move) {
|
||||||
|
asyncForEachCall((l) -> l.onMoveNotAllowed(move));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDraw() {
|
||||||
|
asyncForEachCall((l) -> l.onDraw());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCastling(boolean bigCastling) {
|
||||||
|
asyncForEachCall((l) -> l.onCastling(bigCastling));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPawnPromoted(PromoteType promotion) {
|
||||||
|
asyncForEachCall((l) -> l.onPawnPromoted(promotion));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
this.executor.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -67,4 +67,12 @@ public class EmptyGameDispatcher extends GameDispatcher {
|
|||||||
public void onPawnPromoted(PromoteType promotion) {
|
public void onPawnPromoted(PromoteType promotion) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addListener(GameListener listener) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,111 +1,9 @@
|
|||||||
package chess.controller.event;
|
package chess.controller.event;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
public abstract class GameDispatcher extends GameAdaptator {
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
public abstract void addListener(GameListener listener);
|
||||||
import chess.model.Color;
|
|
||||||
import chess.model.Coordinate;
|
|
||||||
import chess.model.Move;
|
|
||||||
|
|
||||||
public class GameDispatcher implements GameListener {
|
|
||||||
|
|
||||||
private final List<GameListener> listeners;
|
|
||||||
private final ExecutorService executor;
|
|
||||||
|
|
||||||
public GameDispatcher() {
|
|
||||||
this.listeners = new ArrayList<>();
|
|
||||||
this.executor = Executors.newSingleThreadExecutor();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addListener(GameListener listener) {
|
|
||||||
this.listeners.add(listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void asyncForEachCall(Consumer<GameListener> func) {
|
|
||||||
this.executor.execute(() -> this.listeners.forEach(func));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPlayerTurn(Color color, boolean undone) {
|
|
||||||
asyncForEachCall((l) -> l.onPlayerTurn(color, undone));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onWin(Color color) {
|
|
||||||
asyncForEachCall((l) -> l.onWin(color));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onKingInCheck() {
|
|
||||||
asyncForEachCall((l) -> l.onKingInCheck());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onKingInMat() {
|
|
||||||
asyncForEachCall((l) -> l.onKingInMat());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPatSituation() {
|
|
||||||
asyncForEachCall((l) -> l.onPatSituation());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSurrender(Color color) {
|
|
||||||
asyncForEachCall((l) -> l.onSurrender(color));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onGameStart() {
|
|
||||||
asyncForEachCall((l) -> l.onGameStart());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPromotePawn(Coordinate pieceCoords) {
|
|
||||||
asyncForEachCall((l) -> l.onPromotePawn(pieceCoords));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onBoardUpdate() {
|
|
||||||
asyncForEachCall((l) -> l.onBoardUpdate());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onGameEnd() {
|
|
||||||
asyncForEachCall((l) -> l.onGameEnd());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMove(Move move) {
|
|
||||||
asyncForEachCall((l) -> l.onMove(move));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMoveNotAllowed(Move move) {
|
|
||||||
asyncForEachCall((l) -> l.onMoveNotAllowed(move));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDraw() {
|
|
||||||
asyncForEachCall((l) -> l.onDraw());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCastling(boolean bigCastling) {
|
|
||||||
asyncForEachCall((l) -> l.onCastling(bigCastling));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPawnPromoted(PromoteType promotion) {
|
|
||||||
asyncForEachCall((l) -> l.onPawnPromoted(promotion));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stopService() {
|
|
||||||
this.executor.shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public abstract void close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user