From 9f5b27769fae5469c316297b786e11dc9a26127d Mon Sep 17 00:00:00 2001 From: Guillaume VALLENET Date: Fri, 25 Apr 2025 15:40:41 +0200 Subject: [PATCH] Actualiser src/backend/AutoPlayer.java --- src/backend/AutoPlayer.java | 163 +++++++++++++++++++++++++++++++++++- 1 file changed, 159 insertions(+), 4 deletions(-) diff --git a/src/backend/AutoPlayer.java b/src/backend/AutoPlayer.java index 3e67f52..9b753af 100644 --- a/src/backend/AutoPlayer.java +++ b/src/backend/AutoPlayer.java @@ -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 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())); } -} \ No newline at end of file + + /** + * Simplified minimax algorithm without alpha-beta pruning + */ + private int minimax(Board board, int depth, boolean isWhiteTurn) { + if (depth == 0) { + return evaluateBoard(board); + } + + ArrayList 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 generatePossibleMoves(Board board) { + ArrayList 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 generateMovesForPiece(Board board, Piece piece) { + ArrayList 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; + } +}