2026-01-29 13:34:40 -05:00
|
|
|
package fr.iut_fbleau.Bot;
|
|
|
|
|
|
2026-01-30 14:11:13 +01:00
|
|
|
import fr.iut_fbleau.Avalam.*;
|
|
|
|
|
import fr.iut_fbleau.GameAPI.*;
|
2026-01-29 13:34:40 -05:00
|
|
|
import java.util.*;
|
|
|
|
|
|
2026-02-05 23:13:31 +01:00
|
|
|
/**
|
|
|
|
|
* 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é.
|
|
|
|
|
*/
|
2026-01-30 14:11:13 +01:00
|
|
|
public class DivineBot extends AbstractGamePlayer {
|
|
|
|
|
|
2026-02-05 23:13:31 +01:00
|
|
|
/** Joueur contrôlé par le bot. */
|
2026-01-30 13:40:04 +01:00
|
|
|
private final Player me;
|
2026-02-05 23:13:31 +01:00
|
|
|
|
|
|
|
|
/** Profondeur de recherche dans l'arbre des coups. */
|
2026-01-30 13:40:04 +01:00
|
|
|
private final int maxDepth;
|
2026-02-05 23:13:31 +01:00
|
|
|
|
|
|
|
|
/** Générateur aléatoire pour choisir entre des coups d'égale valeur. */
|
2026-01-30 13:40:04 +01:00
|
|
|
private final Random rng = new Random();
|
2026-01-29 13:34:40 -05:00
|
|
|
|
2026-02-05 23:13:31 +01:00
|
|
|
/**
|
|
|
|
|
* Constructeur du DivineBot.
|
|
|
|
|
* @param p le joueur (P1 ou P2).
|
|
|
|
|
* @param maxDepth la profondeur d'anticipation.
|
|
|
|
|
*/
|
2026-01-30 13:40:04 +01:00
|
|
|
public DivineBot(Player p, int maxDepth) {
|
|
|
|
|
super(p);
|
|
|
|
|
this.me = p;
|
2026-02-05 22:35:06 +01:00
|
|
|
this.maxDepth = maxDepth;
|
2026-01-30 13:40:04 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 23:13:31 +01:00
|
|
|
/**
|
|
|
|
|
* Calcule et retourne le meilleur coup possible.
|
|
|
|
|
* @param board l'état actuel du jeu.
|
|
|
|
|
* @return le coup sélectionné.
|
|
|
|
|
*/
|
2026-01-30 13:40:04 +01:00
|
|
|
@Override
|
|
|
|
|
public AbstractPly giveYourMove(IBoard board) {
|
|
|
|
|
if (board == null || board.isGameOver()) return null;
|
|
|
|
|
|
|
|
|
|
List<AbstractPly> moves = listMoves(board);
|
2026-02-05 22:23:07 +01:00
|
|
|
int bestValue = Integer.MIN_VALUE;
|
2026-01-30 13:40:04 +01:00
|
|
|
List<AbstractPly> bestMoves = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
for (AbstractPly m : moves) {
|
|
|
|
|
IBoard next = board.safeCopy();
|
|
|
|
|
next.doPly(m);
|
2026-02-05 22:23:07 +01:00
|
|
|
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);
|
2026-01-30 13:40:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return bestMoves.get(rng.nextInt(bestMoves.size()));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 23:13:31 +01:00
|
|
|
/**
|
|
|
|
|
* Algorithme Alpha-Beta pour optimiser la recherche du meilleur score.
|
|
|
|
|
*/
|
2026-01-30 13:40:04 +01:00
|
|
|
private int alphaBeta(IBoard board, int depth, int alpha, int beta) {
|
2026-02-05 22:23:07 +01:00
|
|
|
if (board.isGameOver()) {
|
|
|
|
|
Result r = board.getResult();
|
|
|
|
|
if (r == Result.DRAW) return 0;
|
2026-02-05 23:13:31 +01:00
|
|
|
return (r == Result.WIN == (me == Player.PLAYER1)) ? 1000000 : -1000000;
|
2026-02-05 22:23:07 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 14:11:13 +01:00
|
|
|
if (depth == 0) return evaluate(board);
|
2026-01-30 13:40:04 +01:00
|
|
|
|
2026-02-05 22:23:07 +01:00
|
|
|
boolean isMax = (board.getCurrentPlayer() == me);
|
|
|
|
|
int best = isMax ? Integer.MIN_VALUE : Integer.MAX_VALUE;
|
2026-01-30 13:40:04 +01:00
|
|
|
|
2026-02-05 22:23:07 +01:00
|
|
|
for (AbstractPly m : listMoves(board)) {
|
|
|
|
|
IBoard next = board.safeCopy();
|
|
|
|
|
next.doPly(m);
|
|
|
|
|
int val = alphaBeta(next, depth - 1, alpha, beta);
|
2026-02-05 19:46:02 +01:00
|
|
|
|
2026-02-05 22:23:07 +01:00
|
|
|
if (isMax) {
|
2026-02-05 19:46:02 +01:00
|
|
|
best = Math.max(best, val);
|
|
|
|
|
alpha = Math.max(alpha, best);
|
2026-02-05 22:23:07 +01:00
|
|
|
} else {
|
2026-02-05 19:46:02 +01:00
|
|
|
best = Math.min(best, val);
|
|
|
|
|
beta = Math.min(beta, best);
|
2026-01-30 13:40:04 +01:00
|
|
|
}
|
2026-02-05 22:23:07 +01:00
|
|
|
if (alpha >= beta) break;
|
2026-01-30 13:40:04 +01:00
|
|
|
}
|
2026-02-05 22:23:07 +01:00
|
|
|
return best;
|
2026-01-30 14:11:13 +01:00
|
|
|
}
|
2026-01-30 13:40:04 +01:00
|
|
|
|
2026-02-05 23:13:31 +01:00
|
|
|
/**
|
|
|
|
|
* Analyse la situation actuelle et attribue un score au plateau.
|
|
|
|
|
* @param board le plateau à analyser.
|
|
|
|
|
* @return le score (positif si avantageux pour le bot).
|
|
|
|
|
*/
|
2026-01-30 13:40:04 +01:00
|
|
|
private int evaluate(IBoard board) {
|
|
|
|
|
if (!(board instanceof AvalamBoard)) return 0;
|
|
|
|
|
AvalamBoard b = (AvalamBoard) board;
|
2026-02-05 23:12:03 +01:00
|
|
|
|
2026-01-30 13:40:04 +01:00
|
|
|
Color myColor = (me == Player.PLAYER1) ? Color.YELLOW : Color.RED;
|
2026-02-05 23:12:03 +01:00
|
|
|
Color oppColor = (myColor == Color.YELLOW) ? Color.RED : Color.YELLOW;
|
2026-02-05 22:23:07 +01:00
|
|
|
|
2026-01-30 13:40:04 +01:00
|
|
|
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);
|
2026-02-05 22:23:07 +01:00
|
|
|
if (t == null || t.getHeight() == 0) continue;
|
2026-01-30 13:40:04 +01:00
|
|
|
|
2026-02-05 23:12:03 +01:00
|
|
|
int h = t.getHeight();
|
|
|
|
|
int weight = 0;
|
|
|
|
|
|
2026-02-05 23:13:31 +01:00
|
|
|
// Points sécurisés (isolation ou hauteur max)
|
2026-02-05 23:12:03 +01:00
|
|
|
if (h == 5 || isIsolated(b, r, c)) {
|
2026-02-05 23:13:31 +01:00
|
|
|
weight = 1000;
|
2026-02-05 22:23:07 +01:00
|
|
|
} else {
|
2026-02-05 23:13:31 +01:00
|
|
|
// Gestion de la vulnérabilité face à l'ennemi
|
2026-02-05 23:12:03 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-05 22:23:07 +01:00
|
|
|
}
|
2026-02-05 23:12:03 +01:00
|
|
|
|
|
|
|
|
if (t.getColor() == myColor) score += weight;
|
|
|
|
|
else score -= weight;
|
2026-01-30 13:40:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 23:13:31 +01:00
|
|
|
/**
|
|
|
|
|
* Vérifie si une tour n'a plus de voisins (point bloqué).
|
|
|
|
|
*/
|
2026-02-05 23:12:03 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 23:13:31 +01:00
|
|
|
/**
|
|
|
|
|
* Vérifie si l'adversaire peut capturer la tour au prochain coup.
|
|
|
|
|
*/
|
2026-02-05 23:12:03 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 23:13:31 +01:00
|
|
|
/**
|
|
|
|
|
* Liste tous les coups possibles sur le plateau actuel.
|
|
|
|
|
*/
|
2026-01-30 13:40:04 +01:00
|
|
|
private List<AbstractPly> listMoves(IBoard board) {
|
|
|
|
|
List<AbstractPly> moves = new ArrayList<>();
|
2026-02-05 22:23:07 +01:00
|
|
|
Iterator<AbstractPly> it = board.iterator();
|
2026-02-05 23:12:03 +01:00
|
|
|
while (it.hasNext()) moves.add(it.next());
|
2026-01-30 13:40:04 +01:00
|
|
|
return moves;
|
|
|
|
|
}
|
2026-01-30 14:11:13 +01:00
|
|
|
}
|