From c2903f1712c40fc867cc0e80f8df132be4085880 Mon Sep 17 00:00:00 2001 From: YujieAki <124937574+YujieAki@users.noreply.github.com> Date: Fri, 17 Feb 2023 18:47:09 -0800 Subject: [PATCH] Add files via upload --- Yujie AI/Yujie AI.iml | 11 +++++++ Yujie AI/src/Board.java | 18 +++++++++++ Yujie AI/src/ChessAI.java | 67 +++++++++++++++++++++++++++++++++++++++ Yujie AI/src/Color.java | 2 ++ Yujie AI/src/Move.java | 2 ++ Yujie AI/src/Piece.java | 2 ++ 6 files changed, 102 insertions(+) create mode 100644 Yujie AI/Yujie AI.iml create mode 100644 Yujie AI/src/Board.java create mode 100644 Yujie AI/src/ChessAI.java create mode 100644 Yujie AI/src/Color.java create mode 100644 Yujie AI/src/Move.java create mode 100644 Yujie AI/src/Piece.java diff --git a/Yujie AI/Yujie AI.iml b/Yujie AI/Yujie AI.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Yujie AI/Yujie AI.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Yujie AI/src/Board.java b/Yujie AI/src/Board.java new file mode 100644 index 0000000..63afacf --- /dev/null +++ b/Yujie AI/src/Board.java @@ -0,0 +1,18 @@ +public class Board { + public getAllLegalMoves(){ + + } + public makeMove(){ + + } + + public undoMove(){ + + } + + public isGameover(){ + + } + + public +} diff --git a/Yujie AI/src/ChessAI.java b/Yujie AI/src/ChessAI.java new file mode 100644 index 0000000..7000e97 --- /dev/null +++ b/Yujie AI/src/ChessAI.java @@ -0,0 +1,67 @@ +public class ChessAI { + private final int MAX_DEPTH = 4; + + public Move findBestMove(Board board) { + int bestScore = Integer.MIN_VALUE; + Move bestMove = null; + + for (Move move : board.getAllLegalMoves()) { + board.makeMove(move); + int score = minimax(board, MAX_DEPTH, Integer.MIN_VALUE, Integer.MAX_VALUE, false); + board.undoMove(); + + if (score > bestScore) { + bestScore = score; + bestMove = move; + } + } + + return bestMove; + } + + private int minimax(Board board, int depth, int alpha, int beta, boolean maximizingPlayer) { + if (depth == 0 || board.isGameOver()) { + return evaluateBoard(board); + } + + int score; + if (maximizingPlayer) { + score = Integer.MIN_VALUE; + for (Move move : board.getAllLegalMoves()) { + board.makeMove(move); + score = Math.max(score, minimax(board, depth - 1, alpha, beta, false)); + board.undoMove(); + alpha = Math.max(alpha, score); + if (beta <= alpha) { + break; + } + } + } else { + score = Integer.MAX_VALUE; + for (Move move : board.getAllLegalMoves()) { + board.makeMove(move); + score = Math.min(score, minimax(board, depth - 1, alpha, beta, true)); + board.undoMove(); + beta = Math.min(beta, score); + if (beta <= alpha) { + break; + } + } + } + + return score; + } + + private int evaluateBoard(Board board) { + int score = 0; + for (Piece piece : board.getPieces()) { + if (piece.getColor() == Color.WHITE) { + score += piece.getValue(); + } else { + score -= piece.getValue(); + } + } + return score; + } +} + diff --git a/Yujie AI/src/Color.java b/Yujie AI/src/Color.java new file mode 100644 index 0000000..43f7623 --- /dev/null +++ b/Yujie AI/src/Color.java @@ -0,0 +1,2 @@ +public class Color { +} diff --git a/Yujie AI/src/Move.java b/Yujie AI/src/Move.java new file mode 100644 index 0000000..1bc888e --- /dev/null +++ b/Yujie AI/src/Move.java @@ -0,0 +1,2 @@ +public class Move { +} diff --git a/Yujie AI/src/Piece.java b/Yujie AI/src/Piece.java new file mode 100644 index 0000000..91ec34b --- /dev/null +++ b/Yujie AI/src/Piece.java @@ -0,0 +1,2 @@ +public class Piece { +}