Actualiser src/backend/AutoPlayer.java
This commit is contained in:
parent
91da7823ad
commit
9f5b27769f
|
|
@ -1,10 +1,165 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class AutoPlayer {
|
||||
private static final int MAX_DEPTH = 2; // Reduced depth for faster computation
|
||||
private static final Random random = new Random();
|
||||
|
||||
/**
|
||||
* Computes a move for the current player with a deliberate delay
|
||||
*
|
||||
* @param board Current state of the board
|
||||
* @return A reasonable move
|
||||
*/
|
||||
public Move computeBestMove(Board board) {
|
||||
// This will be implemented in Part 4
|
||||
// For now, just return null or a placeholder move
|
||||
return null;
|
||||
if (board == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Add deliberate delay to simulate "thinking"
|
||||
try {
|
||||
Thread.sleep(500); // 0.5 second delay
|
||||
} catch (InterruptedException e) {
|
||||
// Ignore interruption
|
||||
}
|
||||
|
||||
boolean isWhiteTurn = board.isTurnWhite();
|
||||
ArrayList<Move> possibleMoves = generatePossibleMoves(board);
|
||||
|
||||
if (possibleMoves.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 20% chance to make a random move for unpredictability
|
||||
if (random.nextDouble() < 0.2) {
|
||||
return possibleMoves.get(random.nextInt(possibleMoves.size()));
|
||||
}
|
||||
|
||||
Move bestMove = null;
|
||||
int bestScore = isWhiteTurn ? Integer.MIN_VALUE : Integer.MAX_VALUE;
|
||||
|
||||
for (Move move : possibleMoves) {
|
||||
Board boardCopy = new Board(board);
|
||||
boardCopy.playMove(move);
|
||||
|
||||
int score = minimax(boardCopy, MAX_DEPTH - 1, !isWhiteTurn);
|
||||
|
||||
if ((isWhiteTurn && score > bestScore) || (!isWhiteTurn && score < bestScore)) {
|
||||
bestScore = score;
|
||||
bestMove = move;
|
||||
}
|
||||
}
|
||||
|
||||
return bestMove != null ? bestMove : possibleMoves.get(random.nextInt(possibleMoves.size()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simplified minimax algorithm without alpha-beta pruning
|
||||
*/
|
||||
private int minimax(Board board, int depth, boolean isWhiteTurn) {
|
||||
if (depth == 0) {
|
||||
return evaluateBoard(board);
|
||||
}
|
||||
|
||||
ArrayList<Move> possibleMoves = generatePossibleMoves(board);
|
||||
|
||||
if (possibleMoves.isEmpty()) {
|
||||
return evaluateBoard(board);
|
||||
}
|
||||
|
||||
int bestScore = isWhiteTurn ? Integer.MIN_VALUE : Integer.MAX_VALUE;
|
||||
|
||||
for (Move move : possibleMoves) {
|
||||
Board boardCopy = new Board(board);
|
||||
boardCopy.playMove(move);
|
||||
|
||||
int score = minimax(boardCopy, depth - 1, !isWhiteTurn);
|
||||
|
||||
if (isWhiteTurn) {
|
||||
bestScore = Math.max(bestScore, score);
|
||||
} else {
|
||||
bestScore = Math.min(bestScore, score);
|
||||
}
|
||||
}
|
||||
|
||||
return bestScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simplified board evaluation
|
||||
*/
|
||||
private int evaluateBoard(Board board) {
|
||||
int score = 0;
|
||||
|
||||
for (Piece piece : board.getPieces()) {
|
||||
int pieceValue = getPieceValue(piece.getType());
|
||||
score += piece.isWhite() ? pieceValue : -pieceValue;
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic piece values
|
||||
*/
|
||||
private int getPieceValue(PieceType type) {
|
||||
switch (type) {
|
||||
case Pawn: return 100;
|
||||
case Knight: return 300;
|
||||
case Bishop: return 300;
|
||||
case Rook: return 500;
|
||||
case Queen: return 900;
|
||||
case King: return 10000;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates all possible legal moves for the current player
|
||||
*/
|
||||
private ArrayList<Move> generatePossibleMoves(Board board) {
|
||||
ArrayList<Move> possibleMoves = new ArrayList<>();
|
||||
boolean isWhiteTurn = board.isTurnWhite();
|
||||
|
||||
for (Piece piece : board.getPieces()) {
|
||||
if (piece.isWhite() == isWhiteTurn) {
|
||||
possibleMoves.addAll(generateMovesForPiece(board, piece));
|
||||
}
|
||||
}
|
||||
|
||||
return possibleMoves;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates all possible legal moves for a specific piece
|
||||
*/
|
||||
private ArrayList<Move> generateMovesForPiece(Board board, Piece piece) {
|
||||
ArrayList<Move> moves = new ArrayList<>();
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
|
||||
Board tempBoard = new Board(board);
|
||||
tempBoard.userTouch(x, y);
|
||||
|
||||
for (int toX = 0; toX < board.getWidth(); toX++) {
|
||||
for (int toY = 0; toY < board.getHeight(); toY++) {
|
||||
if (tempBoard.isHighlighted(toX, toY)) {
|
||||
Piece capturedPiece = null;
|
||||
|
||||
for (Piece boardPiece : board.getPieces()) {
|
||||
if (boardPiece.getX() == toX && boardPiece.getY() == toY) {
|
||||
capturedPiece = boardPiece;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
moves.add(new Move(x, y, toX, toY, piece, capturedPiece));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue