Actualiser fr/iut_fbleau/Bot/DivineBot.java

This commit is contained in:
2026-02-05 23:13:31 +01:00
parent f40f3daa1d
commit c5d01dd2bb

View File

@@ -4,18 +4,38 @@ import fr.iut_fbleau.Avalam.*;
import fr.iut_fbleau.GameAPI.*; import fr.iut_fbleau.GameAPI.*;
import java.util.*; 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 { public class DivineBot extends AbstractGamePlayer {
/** Joueur contrôlé par le bot. */
private final Player me; private final Player me;
/** Profondeur de recherche dans l'arbre des coups. */
private final int maxDepth; private final int maxDepth;
/** Générateur aléatoire pour choisir entre des coups d'égale valeur. */
private final Random rng = new Random(); 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) { public DivineBot(Player p, int maxDepth) {
super(p); super(p);
this.me = p; this.me = p;
this.maxDepth = maxDepth; this.maxDepth = maxDepth;
} }
/**
* Calcule et retourne le meilleur coup possible.
* @param board l'état actuel du jeu.
* @return le coup sélectionné.
*/
@Override @Override
public AbstractPly giveYourMove(IBoard board) { public AbstractPly giveYourMove(IBoard board) {
if (board == null || board.isGameOver()) return null; if (board == null || board.isGameOver()) return null;
@@ -27,7 +47,6 @@ public class DivineBot extends AbstractGamePlayer {
for (AbstractPly m : moves) { for (AbstractPly m : moves) {
IBoard next = board.safeCopy(); IBoard next = board.safeCopy();
next.doPly(m); next.doPly(m);
// On regarde l'avenir
int value = alphaBeta(next, maxDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE); int value = alphaBeta(next, maxDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
if (value > bestValue) { if (value > bestValue) {
@@ -41,14 +60,14 @@ public class DivineBot extends AbstractGamePlayer {
return bestMoves.get(rng.nextInt(bestMoves.size())); 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) { private int alphaBeta(IBoard board, int depth, int alpha, int beta) {
if (board.isGameOver()) { if (board.isGameOver()) {
Result r = board.getResult(); Result r = board.getResult();
if (r == Result.DRAW) return 0; if (r == Result.DRAW) return 0;
// On vérifie strictement si MON joueur a gagné return (r == Result.WIN == (me == Player.PLAYER1)) ? 1000000 : -1000000;
boolean winP1 = (r == Result.WIN);
boolean amIP1 = (me == Player.PLAYER1);
return (winP1 == amIP1) ? 1000000 : -1000000;
} }
if (depth == 0) return evaluate(board); if (depth == 0) return evaluate(board);
@@ -73,11 +92,15 @@ public class DivineBot extends AbstractGamePlayer {
return best; 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) { private int evaluate(IBoard board) {
if (!(board instanceof AvalamBoard)) return 0; if (!(board instanceof AvalamBoard)) return 0;
AvalamBoard b = (AvalamBoard) board; AvalamBoard b = (AvalamBoard) board;
// On définit clairement les couleurs
Color myColor = (me == Player.PLAYER1) ? Color.YELLOW : Color.RED; Color myColor = (me == Player.PLAYER1) ? Color.YELLOW : Color.RED;
Color oppColor = (myColor == Color.YELLOW) ? Color.RED : Color.YELLOW; Color oppColor = (myColor == Color.YELLOW) ? Color.RED : Color.YELLOW;
@@ -90,17 +113,14 @@ public class DivineBot extends AbstractGamePlayer {
int h = t.getHeight(); int h = t.getHeight();
int weight = 0; int weight = 0;
// Étape 1 : Est-ce que cette tour est "finie" ou isolée ? // Points sécurisés (isolation ou hauteur max)
if (h == 5 || isIsolated(b, r, c)) { if (h == 5 || isIsolated(b, r, c)) {
weight = 1000; // Point sécurisé weight = 1000;
} else { } else {
// Étape 2 : Si la tour est prenable par l'ennemi // Gestion de la vulnérabilité face à l'ennemi
if (isVulnerable(b, r, c, oppColor)) { if (isVulnerable(b, r, c, oppColor)) {
// Si c'est ma tour, c'est un danger (-200)
// Si c'est la sienne, c'est une opportunité (+50)
weight = -200; weight = -200;
} else { } else {
// Étape 3 : Barème de progression sécurisée
switch (h) { switch (h) {
case 4: weight = 400; break; case 4: weight = 400; break;
case 3: weight = 150; break; case 3: weight = 150; break;
@@ -117,6 +137,9 @@ public class DivineBot extends AbstractGamePlayer {
return score; return score;
} }
/**
* Vérifie si une tour n'a plus de voisins (point bloqué).
*/
private boolean isIsolated(AvalamBoard b, int r, int c) { private boolean isIsolated(AvalamBoard b, int r, int c) {
for (int dr = -1; dr <= 1; dr++) { for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) { for (int dc = -1; dc <= 1; dc++) {
@@ -128,13 +151,15 @@ public class DivineBot extends AbstractGamePlayer {
return true; 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) { private boolean isVulnerable(AvalamBoard b, int r, int c, Color enemyColor) {
int myHeight = b.getTowerAt(r, c).getHeight(); int myHeight = b.getTowerAt(r, c).getHeight();
for (int dr = -1; dr <= 1; dr++) { for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) { for (int dc = -1; dc <= 1; dc++) {
if (dr == 0 && dc == 0) continue; if (dr == 0 && dc == 0) continue;
Tower n = b.getTowerAt(r + dr, c + dc); Tower n = b.getTowerAt(r + dr, c + dc);
// Si un ennemi peut sauter dessus pour faire une tour <= 5
if (n != null && n.getColor() == enemyColor && (n.getHeight() + myHeight <= 5)) { if (n != null && n.getColor() == enemyColor && (n.getHeight() + myHeight <= 5)) {
return true; return true;
} }
@@ -143,6 +168,9 @@ public class DivineBot extends AbstractGamePlayer {
return false; return false;
} }
/**
* Liste tous les coups possibles sur le plateau actuel.
*/
private List<AbstractPly> listMoves(IBoard board) { private List<AbstractPly> listMoves(IBoard board) {
List<AbstractPly> moves = new ArrayList<>(); List<AbstractPly> moves = new ArrayList<>();
Iterator<AbstractPly> it = board.iterator(); Iterator<AbstractPly> it = board.iterator();