implement SEF and Negamax
This commit is contained in:
parent
20643dfd75
commit
b09b46607a
|
|
@ -36,6 +36,67 @@ public class AutoPlayer {
|
|||
return possibleMoves.get(rand.nextInt(possibleMoves.size()));
|
||||
|
||||
}
|
||||
// Negamax search
|
||||
public int negamax(Board board, int depth, int color) {
|
||||
if (depth == 0) {
|
||||
return color * board.evaluateBoard();
|
||||
}
|
||||
|
||||
int bestScore = Integer.MIN_VALUE;
|
||||
|
||||
for (Move move : getAllPossibleMoves(board)) {
|
||||
board.playMove(move);
|
||||
int score = -negamax(board, depth - 1, -color);
|
||||
board.undoLastMove();
|
||||
|
||||
if (score > bestScore) {
|
||||
bestScore = score;
|
||||
}
|
||||
}
|
||||
|
||||
return bestScore;
|
||||
}
|
||||
|
||||
// Get best move using negamax
|
||||
public Move getBestMoveUsingNegamax(Board board, int depth) {
|
||||
int bestScore = Integer.MIN_VALUE;
|
||||
Move bestMove = null;
|
||||
int color = board.isTurnWhite() ? 1 : -1;
|
||||
|
||||
for (Move move : getAllPossibleMoves(board)) {
|
||||
board.playMove(move);
|
||||
int score = -negamax(board, depth - 1, -color);
|
||||
board.undoLastMove();
|
||||
|
||||
if (score > bestScore) {
|
||||
bestScore = score;
|
||||
bestMove = move;
|
||||
}
|
||||
}
|
||||
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
// Helper to get all legal moves for current player
|
||||
private ArrayList<Move> getAllPossibleMoves(Board board) {
|
||||
ArrayList<Move> possibleMoves = new ArrayList<>();
|
||||
boolean isWhiteTurn = board.isTurnWhite();
|
||||
|
||||
for (Piece piece : board.getPieces()) {
|
||||
if (piece.isWhite() == isWhiteTurn) {
|
||||
ArrayList<int[]> legalPositions = board.getLegalMoves(piece);
|
||||
for (int[] pos : legalPositions) {
|
||||
Piece target = board.getPieceAt(pos[0], pos[1]);
|
||||
Move move = new Move(piece, piece.getX(), piece.getY(), pos[0], pos[1], target);
|
||||
possibleMoves.add(move);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return possibleMoves;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -353,5 +353,21 @@ public void undoLastMove() {
|
|||
selected = null;
|
||||
highlighted.clear();
|
||||
}
|
||||
public int evaluateBoard() {
|
||||
int score = 0;
|
||||
for (Piece piece : pieces) {
|
||||
int value = 0;
|
||||
switch (piece.getType()) {
|
||||
case Pawn: value = 100; break;
|
||||
case Knight: value = 320; break;
|
||||
case Bishop: value = 330; break;
|
||||
case Rook: value = 500; break;
|
||||
case Queen: value = 900; break;
|
||||
}
|
||||
if (piece.isWhite()) score += value;
|
||||
else score -= value;
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -48,8 +48,11 @@ public class Game extends Thread {
|
|||
}
|
||||
|
||||
private void aiPlayerTurn() {
|
||||
if(isAITurn()) {
|
||||
board.playMove(aiPlayer.computeBestMove(new Board(board)));
|
||||
if (isAITurn()) {
|
||||
Move bestMove = aiPlayer.getBestMoveUsingNegamax(board, 2); // Change depth as needed
|
||||
if (bestMove != null) {
|
||||
board.playMove(bestMove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue