WIP broken AI integration

main
Elnu 2 years ago
parent 3eec0e7833
commit dcfc4ce3d9

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -1,18 +0,0 @@
public class Board {
public getAllLegalMoves(){
}
public makeMove(){
}
public undoMove(){
}
public isGameover(){
}
public
}

@ -1,2 +0,0 @@
public class Color {
}

@ -1,2 +0,0 @@
public class Move {
}

@ -1,2 +0,0 @@
public class Piece {
}

@ -5,6 +5,10 @@ public class Bishop extends Piece {
super(black, panel, "black-bishop.png", "white-bishop.png");
}
public int getValue() {
return 3;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();

@ -25,6 +25,9 @@ public class Board {
// The current board coordinate that's being dragged
BoardCoordinate dragging = null;
ArrayList<BoardCoordinate> legalMoves = null;
Move lastMove;
Piece captured;
public boolean isGameOver;
public Board() {
// Initialize DrawingPanel
@ -99,15 +102,32 @@ public class Board {
set(coordinate.x, coordinate.y, piece);
}
public Piece move(int fromX, int fromY, int toX, int toY) {
Piece captured = get(toX, toY);
public void move(int fromX, int fromY, int toX, int toY) {
lastMove = new Move(new BoardCoordinate(fromX, fromY), new BoardCoordinate(toX, toY));
captured = get(toX, toY);
set(toX, toY, get(fromX, fromY));
set(fromX, fromY, null);
return captured;
}
public Piece move(BoardCoordinate from, BoardCoordinate to) {
return move(from.x, from.y, to.x, to.y);
public void move(BoardCoordinate from, BoardCoordinate to) {
move(from.x, from.y, to.x, to.y);
}
public void move(Move move) {
move(move.from, move.to);
try {
Thread.sleep(500);
} catch(InterruptedException e) {
e.printStackTrace();
}
draw();
}
public void undoMove() {
if (lastMove == null) return;
set(lastMove.from, get(lastMove.to));
set(lastMove.to, captured);
lastMove = null;
}
// Mouse down event handler
@ -117,7 +137,7 @@ public class Board {
BoardCoordinate coordinate = new ScreenCoordinate(x, y).toBoard();
// If there's no piece there, return
Piece piece = get(coordinate);
if (piece == null) return;
if (piece == null || piece.black) return;
// Set currently dragging piece to that coordinate
dragging = coordinate;
legalMoves = piece.getLegalMoves(coordinate, this);
@ -132,6 +152,9 @@ public class Board {
if (dragging != null && !newCoordinate.equals(dragging)) {
// dragging is BoardCoordinate of piece being dragged
Piece piece = get(dragging);
move(dragging, newCoordinate);
move(ChessAI.findBestMove(this));
/*
ArrayList<BoardCoordinate> legalMoves = piece.getLegalMoves(dragging, this);
for (BoardCoordinate legalMove : legalMoves) {
if (newCoordinate.equals(legalMove)) {
@ -148,10 +171,15 @@ public class Board {
break;
}
}
if (inCheck && oppositeKing.getLegalMoves(oppositeKingPosition, this).size() == 0) System.out.println("Checkmate");
if (inCheck && oppositeKing.getLegalMoves(oppositeKingPosition, this).size() == 0)
isGameOver = true;
move(ChessAI.findBestMove(this));
break;
}
}
*/
}
// Clear dragging
dragging = null;
@ -187,10 +215,12 @@ public class Board {
graphics.setColor(new Color(0, 128, 0, 128));
for (BoardCoordinate legalMove : legalMoves)
drawRect(legalMove);
BoardCoordinate hovering = mousePosition.toBoard();
if (legalMoves.contains(hovering)) {
graphics.setColor(get(hovering) == null ? new Color(0, 0, 255, 128) : new Color(255, 0, 0, 128));
drawRect(mousePosition.toBoard());
if (mousePosition != null) {
BoardCoordinate hovering = mousePosition.toBoard();
if (legalMoves.contains(hovering)) {
graphics.setColor(get(hovering) == null ? new Color(0, 0, 255, 128) : new Color(255, 0, 0, 128));
drawRect(mousePosition.toBoard());
}
}
}
@ -237,4 +267,15 @@ public class Board {
public void forEachPiece(PieceActionCoordinate tileAction) {
forEachPiece((x, y, piece) -> tileAction.forEachTile(new BoardCoordinate(x, y), get(x, y)));
}
public ArrayList<Move> getAllLegalMoves() {
ArrayList<Move> allLegalMoves = new ArrayList<>();
forEachPiece((from, piece) -> {
ArrayList<BoardCoordinate> legalTiles = piece.getLegalMoves(from, this);
for (BoardCoordinate to : legalTiles) {
allLegalMoves.add(new Move(from, to));
}
});
return allLegalMoves;
}
}

@ -1,12 +1,12 @@
public class ChessAI {
private final int MAX_DEPTH = 4;
private static final int MAX_DEPTH = 4;
public Move findBestMove(Board board) {
public static Move findBestMove(Board board) {
int bestScore = Integer.MIN_VALUE;
Move bestMove = null;
for (Move move : board.getAllLegalMoves()) {
board.makeMove(move);
board.move(move);
int score = minimax(board, MAX_DEPTH, Integer.MIN_VALUE, Integer.MAX_VALUE, false);
board.undoMove();
@ -19,8 +19,8 @@ public class ChessAI {
return bestMove;
}
private int minimax(Board board, int depth, int alpha, int beta, boolean maximizingPlayer) {
if (depth == 0 || board.isGameOver()) {
private static int minimax(Board board, int depth, int alpha, int beta, boolean maximizingPlayer) {
if (depth == 0 || board.isGameOver) {
return evaluateBoard(board);
}
@ -28,7 +28,7 @@ public class ChessAI {
if (maximizingPlayer) {
score = Integer.MIN_VALUE;
for (Move move : board.getAllLegalMoves()) {
board.makeMove(move);
board.move(move);
score = Math.max(score, minimax(board, depth - 1, alpha, beta, false));
board.undoMove();
alpha = Math.max(alpha, score);
@ -39,7 +39,7 @@ public class ChessAI {
} else {
score = Integer.MAX_VALUE;
for (Move move : board.getAllLegalMoves()) {
board.makeMove(move);
board.move(move);
score = Math.min(score, minimax(board, depth - 1, alpha, beta, true));
board.undoMove();
beta = Math.min(beta, score);
@ -52,16 +52,22 @@ public class ChessAI {
return score;
}
private int evaluateBoard(Board board) {
int score = 0;
for (Piece piece : board.getPieces()) {
if (piece.getColor() == Color.WHITE) {
score += piece.getValue();
private static int evaluateBoard(Board board) {
Score score = new Score();
board.forEachPiece((coordinate, piece) -> {
if (!piece.black) {
score.add(piece.getValue());
} else {
score -= piece.getValue();
score.add(piece.getValue());
}
}
return score;
});
return score.score;
}
}
class Score {
public int score;
public void add(int value) {
score += value;
}
}

@ -5,6 +5,10 @@ public class King extends Piece {
super(black, panel, "black-king.png", "white-king.png");
}
public int getValue() {
return Integer.MAX_VALUE;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 1));

@ -6,6 +6,10 @@ public class Knight extends Piece {
super(black, panel, "black-knight.png", "white-knight.png");
}
public int getValue() {
return 3;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
possibleMoves.add(new BoardCoordinate(position.x - 2, position.y - 1));

@ -0,0 +1,9 @@
public class Move {
public BoardCoordinate from;
public BoardCoordinate to;
public Move(BoardCoordinate from, BoardCoordinate to) {
this.from = from;
this.to = to;
}
}

@ -5,6 +5,10 @@ public class Pawn extends Piece {
super(black, panel, "black-pawn.png", "white-pawn.png");
}
public int getValue() {
return 1;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
if (this.black) {

@ -9,6 +9,8 @@ public abstract class Piece {
Image image;
public boolean black;
public abstract int getValue();
public Piece(boolean black, DrawingPanel panel, String blackImagePath, String whiteImagePath) {
this.black = black;
image = panel.loadImage(black ? blackImagePath : whiteImagePath);
@ -48,7 +50,7 @@ public abstract class Piece {
boolean isInCheck(BoardCoordinate from, BoardCoordinate to, Board board) {
boolean isInCheck = false;
Piece captured = board.move(from, to);
board.move(from, to);
outer: for (int y = 0; y < Board.BOARD_SIZE; y++) {
for (int x = 0; x < Board.BOARD_SIZE; x++) {
Piece piece = board.get(x, y);
@ -63,8 +65,7 @@ public abstract class Piece {
}
}
}
board.move(to, from);
board.set(to, captured);
board.undoMove();
return isInCheck;
}

@ -5,6 +5,10 @@ public class Queen extends Piece {
super(black, panel, "black-queen.png", "white-queen.png");
}
public int getValue() {
return 9;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();

@ -5,6 +5,10 @@ public class Rook extends Piece {
super(black, panel, "black-rook.png", "white-rook.png");
}
public int getValue() {
return 5;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();