From dcfc4ce3d9bf19f64fc1a659b74d19c07607c06c Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Mon, 13 Mar 2023 20:10:33 -0700 Subject: [PATCH] WIP broken AI integration --- Yujie AI/Yujie AI.iml | 11 ------ Yujie AI/src/Board.java | 18 --------- Yujie AI/src/Color.java | 2 - Yujie AI/src/Move.java | 2 - Yujie AI/src/Piece.java | 2 - src/Bishop.java | 4 ++ src/Board.java | 63 ++++++++++++++++++++++++------ {Yujie AI/src => src}/ChessAI.java | 36 ++++++++++------- src/King.java | 4 ++ src/Knight.java | 4 ++ src/Move.java | 9 +++++ src/Pawn.java | 4 ++ src/Piece.java | 7 ++-- src/Queen.java | 4 ++ src/Rook.java | 4 ++ 15 files changed, 110 insertions(+), 64 deletions(-) delete mode 100644 Yujie AI/Yujie AI.iml delete mode 100644 Yujie AI/src/Board.java delete mode 100644 Yujie AI/src/Color.java delete mode 100644 Yujie AI/src/Move.java delete mode 100644 Yujie AI/src/Piece.java rename {Yujie AI/src => src}/ChessAI.java (64%) create mode 100644 src/Move.java diff --git a/Yujie AI/Yujie AI.iml b/Yujie AI/Yujie AI.iml deleted file mode 100644 index c90834f..0000000 --- a/Yujie AI/Yujie AI.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/Yujie AI/src/Board.java b/Yujie AI/src/Board.java deleted file mode 100644 index 63afacf..0000000 --- a/Yujie AI/src/Board.java +++ /dev/null @@ -1,18 +0,0 @@ -public class Board { - public getAllLegalMoves(){ - - } - public makeMove(){ - - } - - public undoMove(){ - - } - - public isGameover(){ - - } - - public -} diff --git a/Yujie AI/src/Color.java b/Yujie AI/src/Color.java deleted file mode 100644 index 43f7623..0000000 --- a/Yujie AI/src/Color.java +++ /dev/null @@ -1,2 +0,0 @@ -public class Color { -} diff --git a/Yujie AI/src/Move.java b/Yujie AI/src/Move.java deleted file mode 100644 index 1bc888e..0000000 --- a/Yujie AI/src/Move.java +++ /dev/null @@ -1,2 +0,0 @@ -public class Move { -} diff --git a/Yujie AI/src/Piece.java b/Yujie AI/src/Piece.java deleted file mode 100644 index 91ec34b..0000000 --- a/Yujie AI/src/Piece.java +++ /dev/null @@ -1,2 +0,0 @@ -public class Piece { -} diff --git a/src/Bishop.java b/src/Bishop.java index d9401d2..85c5b06 100644 --- a/src/Bishop.java +++ b/src/Bishop.java @@ -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 getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); diff --git a/src/Board.java b/src/Board.java index 05a0989..0f0e792 100644 --- a/src/Board.java +++ b/src/Board.java @@ -25,6 +25,9 @@ public class Board { // The current board coordinate that's being dragged BoardCoordinate dragging = null; ArrayList 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 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 getAllLegalMoves() { + ArrayList allLegalMoves = new ArrayList<>(); + forEachPiece((from, piece) -> { + ArrayList legalTiles = piece.getLegalMoves(from, this); + for (BoardCoordinate to : legalTiles) { + allLegalMoves.add(new Move(from, to)); + } + }); + return allLegalMoves; + } } \ No newline at end of file diff --git a/Yujie AI/src/ChessAI.java b/src/ChessAI.java similarity index 64% rename from Yujie AI/src/ChessAI.java rename to src/ChessAI.java index 7000e97..d21a4b3 100644 --- a/Yujie AI/src/ChessAI.java +++ b/src/ChessAI.java @@ -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; + } +} \ No newline at end of file diff --git a/src/King.java b/src/King.java index 04397da..d98631a 100644 --- a/src/King.java +++ b/src/King.java @@ -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 getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 1)); diff --git a/src/Knight.java b/src/Knight.java index 8412938..c9ed6e4 100644 --- a/src/Knight.java +++ b/src/Knight.java @@ -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 getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); possibleMoves.add(new BoardCoordinate(position.x - 2, position.y - 1)); diff --git a/src/Move.java b/src/Move.java new file mode 100644 index 0000000..48a3889 --- /dev/null +++ b/src/Move.java @@ -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; + } +} diff --git a/src/Pawn.java b/src/Pawn.java index 6ccb100..1d1ea90 100644 --- a/src/Pawn.java +++ b/src/Pawn.java @@ -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 getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); if (this.black) { diff --git a/src/Piece.java b/src/Piece.java index c4e0b66..a98ad2e 100644 --- a/src/Piece.java +++ b/src/Piece.java @@ -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; } diff --git a/src/Queen.java b/src/Queen.java index c641fa6..833f34f 100644 --- a/src/Queen.java +++ b/src/Queen.java @@ -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 getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); diff --git a/src/Rook.java b/src/Rook.java index 5769e6d..e9aba63 100644 --- a/src/Rook.java +++ b/src/Rook.java @@ -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 getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>();