Files
BUT3ProjetJeuGroupe/fr/iut_fbleau/Bot/DivineBot.java

180 lines
5.9 KiB
Java

package fr.iut_fbleau.Bot;
import fr.iut_fbleau.Avalam.*;
import fr.iut_fbleau.GameAPI.*;
import java.util.*;
/**
* Bot expert pour le jeu Avalam utilisant l'algorithme Alpha-Beta.
* * Ce bot évalue le plateau en fonction du contrôle des tours,
* de leur isolation (points sécurisés) et de leur vulnérabilité.
*/
public class DivineBot extends AbstractGamePlayer {
/** Joueur contrôlé par le bot. */
private final Player me;
/** Profondeur de recherche dans l'arbre des coups. */
private final int maxDepth;
/** Générateur aléatoire pour choisir entre des coups d'égale valeur. */
private final Random rng = new Random();
/**
* Constructeur du DivineBot.
* @param p le joueur (P1 ou P2).
* @param maxDepth la profondeur d'anticipation.
*/
public DivineBot(Player p, int maxDepth) {
super(p);
this.me = p;
this.maxDepth = maxDepth;
}
/**
* Calcule et retourne le meilleur coup possible.
* @param board l'état actuel du jeu.
* @return le coup sélectionné.
*/
@Override
public AbstractPly giveYourMove(IBoard board) {
if (board == null || board.isGameOver()) return null;
List<AbstractPly> moves = listMoves(board);
int bestValue = Integer.MIN_VALUE;
List<AbstractPly> bestMoves = new ArrayList<>();
for (AbstractPly m : moves) {
IBoard next = board.safeCopy();
next.doPly(m);
int value = alphaBeta(next, maxDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
if (value > bestValue) {
bestValue = value;
bestMoves.clear();
bestMoves.add(m);
} else if (value == bestValue) {
bestMoves.add(m);
}
}
return bestMoves.get(rng.nextInt(bestMoves.size()));
}
/**
* Algorithme Alpha-Beta pour optimiser la recherche du meilleur score.
*/
private int alphaBeta(IBoard board, int depth, int alpha, int beta) {
if (board.isGameOver()) {
Result r = board.getResult();
if (r == Result.DRAW) return 0;
return (r == Result.WIN == (me == Player.PLAYER1)) ? 1000000 : -1000000;
}
if (depth == 0) return evaluate(board);
boolean isMax = (board.getCurrentPlayer() == me);
int best = isMax ? Integer.MIN_VALUE : Integer.MAX_VALUE;
for (AbstractPly m : listMoves(board)) {
IBoard next = board.safeCopy();
next.doPly(m);
int val = alphaBeta(next, depth - 1, alpha, beta);
if (isMax) {
best = Math.max(best, val);
alpha = Math.max(alpha, best);
} else {
best = Math.min(best, val);
beta = Math.min(beta, best);
}
if (alpha >= beta) break;
}
return best;
}
/**
* Analyse la situation actuelle et attribue un score au plateau.
* @param board le plateau à analyser.
* @return le score (positif si avantageux pour le bot).
*/
private int evaluate(IBoard board) {
if (!(board instanceof AvalamBoard)) return 0;
AvalamBoard b = (AvalamBoard) board;
Color myColor = (me == Player.PLAYER1) ? Color.YELLOW : Color.RED;
Color oppColor = (myColor == Color.YELLOW) ? Color.RED : Color.YELLOW;
int score = 0;
for (int r = 0; r < AvalamBoard.SIZE; r++) {
for (int c = 0; c < AvalamBoard.SIZE; c++) {
Tower t = b.getTowerAt(r, c);
if (t == null || t.getHeight() == 0) continue;
int h = t.getHeight();
int weight = 0;
// Points sécurisés (isolation ou hauteur max)
if (h == 5 || isIsolated(b, r, c)) {
weight = 1000;
} else {
// Gestion de la vulnérabilité face à l'ennemi
if (isVulnerable(b, r, c, oppColor)) {
weight = -200;
} else {
switch (h) {
case 4: weight = 400; break;
case 3: weight = 150; break;
case 2: weight = 60; break;
default: weight = 10; break;
}
}
}
if (t.getColor() == myColor) score += weight;
else score -= weight;
}
}
return score;
}
/**
* Vérifie si une tour n'a plus de voisins (point bloqué).
*/
private boolean isIsolated(AvalamBoard b, int r, int c) {
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
if (dr == 0 && dc == 0) continue;
Tower n = b.getTowerAt(r + dr, c + dc);
if (n != null && n.getHeight() > 0) return false;
}
}
return true;
}
/**
* Vérifie si l'adversaire peut capturer la tour au prochain coup.
*/
private boolean isVulnerable(AvalamBoard b, int r, int c, Color enemyColor) {
int myHeight = b.getTowerAt(r, c).getHeight();
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
if (dr == 0 && dc == 0) continue;
Tower n = b.getTowerAt(r + dr, c + dc);
if (n != null && n.getColor() == enemyColor && (n.getHeight() + myHeight <= 5)) {
return true;
}
}
}
return false;
}
/**
* Liste tous les coups possibles sur le plateau actuel.
*/
private List<AbstractPly> listMoves(IBoard board) {
List<AbstractPly> moves = new ArrayList<>();
Iterator<AbstractPly> it = board.iterator();
while (it.hasNext()) moves.add(it.next());
return moves;
}
}