Compare commits
8 Commits
af70986edd
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 3aba1312fb | |||
| c44e7495a4 | |||
| 86a86f598c | |||
| 2766fa7632 | |||
| c5d01dd2bb | |||
| f40f3daa1d | |||
| ebfc9d7d78 | |||
| 88df11dc03 |
3
Diagrammes/Diagramme_Avalam_(simplifié).svg
Normal file
3
Diagrammes/Diagramme_Avalam_(simplifié).svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 100 KiB |
@@ -16,17 +16,18 @@ classDiagram
|
||||
-listMoves(IBoard board): List<AbstractPly>
|
||||
}
|
||||
|
||||
class DivineBot{
|
||||
-me: Player
|
||||
-maxDepth: int
|
||||
-rng: Random
|
||||
|
||||
class DivineBot {
|
||||
-me : Player
|
||||
-maxDepth : int
|
||||
-rng : Random
|
||||
|
||||
+DivineBot(Player p, int maxDepth)
|
||||
+giveYourMove(IBoard board): AbstractPly
|
||||
-alphaBeta(IBoard board, int depth, int alpha, int beta): int
|
||||
-terminalValue(IBoard board): int
|
||||
-evaluate(IBoard board): int
|
||||
-listMoves(IBoard board): List<AbstractPly>
|
||||
+giveYourMove(IBoard board) : AbstractPly
|
||||
-alphaBeta(IBoard board, int depth, int alpha, int beta) : int
|
||||
-evaluate(IBoard board) : int
|
||||
-isIsolated(AvalamBoard b, int r, int c) : boolean
|
||||
-isVulnerable(AvalamBoard b, int r, int c, Color enemyColor) : boolean
|
||||
-listMoves(IBoard board) : List~AbstractPly~
|
||||
}
|
||||
|
||||
class IdiotBot{
|
||||
|
||||
@@ -123,3 +123,8 @@ Tans qu'un joueur peut effectuer un mouvement il a l'obligation de jouer, la par
|
||||
On compte alors combien de pions de chaque couleur occupent le sommet des tours restantes, le vainqueur étant évidemment celui qui en totalise le plus.
|
||||
Attention! Qu'une tour comporte 1,2,... ou 5 pions, elle vaut toujours UN point.
|
||||
|
||||
## Arène de bots
|
||||
Dans ce mode de jeu, deux bots s'affrontent pour certains nombre de partie choisit, afin de voir quel bot gagne le plus.
|
||||
|
||||
### Lancer parties
|
||||
Lancer parties permet de faire fonctionner l'arène. On choisit le premier bot pour le joueur1, et le deuxième pour le joueur deux. On choisit la profondeurs de recherches pour les bots intelligent, puis enfin le nombre de partie que doivent effectués les bots. Enfin il suffit pour voir l'entièreté des résultats, si y a beaucoup de parties, sinon, on peut voir les parties se déroulé pendant la session.
|
||||
BIN
Rapport_Avalam.pdf
Normal file
BIN
Rapport_Avalam.pdf
Normal file
Binary file not shown.
@@ -2,200 +2,179 @@ package fr.iut_fbleau.Bot;
|
||||
|
||||
import fr.iut_fbleau.Avalam.*;
|
||||
import fr.iut_fbleau.GameAPI.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Bot "Divin" (alpha-beta + évaluateur pondéré).
|
||||
* * Idée :
|
||||
* - Utilise l'algorithme Alpha-Beta pour anticiper les coups.
|
||||
* - Évalue les plateaux non terminaux en accordant plus d'importance aux tours hautes.
|
||||
* 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 {
|
||||
|
||||
// Attributs
|
||||
|
||||
/** Joueur contrôlé par ce bot (PLAYER1 ou PLAYER2). */
|
||||
/** Joueur contrôlé par le bot. */
|
||||
private final Player me;
|
||||
|
||||
/** Profondeur maximale de recherche avant évaluation. */
|
||||
/** Profondeur de recherche dans l'arbre des coups. */
|
||||
private final int maxDepth;
|
||||
|
||||
/** Générateur aléatoire pour choisir parmi les meilleurs coups équivalents. */
|
||||
/** Générateur aléatoire pour choisir entre des coups d'égale valeur. */
|
||||
private final Random rng = new Random();
|
||||
|
||||
// Constructeur
|
||||
|
||||
/**
|
||||
* Construit le bot Divine.
|
||||
*
|
||||
* @param p joueur contrôlé par ce bot
|
||||
* @param maxDepth profondeur de l'arbre de recherche
|
||||
* 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 = Math.max(1, maxDepth);
|
||||
this.maxDepth = maxDepth;
|
||||
}
|
||||
|
||||
// Méthodes
|
||||
|
||||
/**
|
||||
* Méthode principale de décision du bot.
|
||||
* Explore le premier niveau de l'arbre et lance les appels Alpha-Beta.
|
||||
* * @param board état actuel du jeu
|
||||
* @return le meilleur coup calculé (AbstractPly)
|
||||
* 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);
|
||||
if (moves.isEmpty()) return null;
|
||||
|
||||
boolean isMax = board.getCurrentPlayer() == me;
|
||||
|
||||
int bestValue = isMax ? Integer.MIN_VALUE : Integer.MAX_VALUE;
|
||||
int bestValue = Integer.MIN_VALUE;
|
||||
List<AbstractPly> bestMoves = new ArrayList<>();
|
||||
|
||||
int alpha = Integer.MIN_VALUE;
|
||||
int beta = Integer.MAX_VALUE;
|
||||
|
||||
for (AbstractPly m : moves) {
|
||||
IBoard next = board.safeCopy();
|
||||
next.doPly(m);
|
||||
int value = alphaBeta(next, maxDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||
|
||||
// Appel récursif pour évaluer la suite du coup
|
||||
int value = alphaBeta(next, maxDepth - 1, alpha, beta);
|
||||
|
||||
if (isMax) {
|
||||
if (value > bestValue) {
|
||||
bestValue = value;
|
||||
bestMoves.clear();
|
||||
bestMoves.add(m);
|
||||
} else if (value == bestValue) {
|
||||
bestMoves.add(m);
|
||||
}
|
||||
alpha = Math.max(alpha, bestValue);
|
||||
} else {
|
||||
if (value < bestValue) {
|
||||
bestValue = value;
|
||||
bestMoves.clear();
|
||||
bestMoves.add(m);
|
||||
} else if (value == bestValue) {
|
||||
bestMoves.add(m);
|
||||
}
|
||||
beta = Math.min(beta, bestValue);
|
||||
if (value > bestValue) {
|
||||
bestValue = value;
|
||||
bestMoves.clear();
|
||||
bestMoves.add(m);
|
||||
} else if (value == bestValue) {
|
||||
bestMoves.add(m);
|
||||
}
|
||||
}
|
||||
|
||||
// Retourne un coup au hasard parmi les meilleurs ex-aequo
|
||||
return bestMoves.get(rng.nextInt(bestMoves.size()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Algorithme récursif de recherche avec élagage Alpha-Beta.
|
||||
* Algorithme Alpha-Beta pour optimiser la recherche du meilleur score.
|
||||
*/
|
||||
private int alphaBeta(IBoard board, int depth, int alpha, int beta) {
|
||||
|
||||
// Cas de base : fin de partie ou limite de profondeur atteinte
|
||||
if (board.isGameOver()) return terminalValue(board);
|
||||
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;
|
||||
boolean isMax = (board.getCurrentPlayer() == me);
|
||||
int best = isMax ? Integer.MIN_VALUE : Integer.MAX_VALUE;
|
||||
|
||||
List<AbstractPly> moves = listMoves(board);
|
||||
if (moves.isEmpty()) {
|
||||
return evaluate(board);
|
||||
}
|
||||
for (AbstractPly m : listMoves(board)) {
|
||||
IBoard next = board.safeCopy();
|
||||
next.doPly(m);
|
||||
int val = alphaBeta(next, depth - 1, alpha, beta);
|
||||
|
||||
if (isMax) {
|
||||
int best = Integer.MIN_VALUE;
|
||||
for (AbstractPly m : moves) {
|
||||
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);
|
||||
|
||||
if (alpha >= beta) break; // Coupure Beta
|
||||
}
|
||||
return best;
|
||||
} else {
|
||||
int best = Integer.MAX_VALUE;
|
||||
for (AbstractPly m : moves) {
|
||||
IBoard next = board.safeCopy();
|
||||
next.doPly(m);
|
||||
|
||||
int val = alphaBeta(next, depth - 1, alpha, beta);
|
||||
} else {
|
||||
best = Math.min(best, val);
|
||||
beta = Math.min(beta, best);
|
||||
|
||||
if (alpha >= beta) break; // Coupure Alpha
|
||||
}
|
||||
return best;
|
||||
if (alpha >= beta) break;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcule la valeur de l'état final (Victoire / Défaite).
|
||||
*/
|
||||
private int terminalValue(IBoard board) {
|
||||
Result r = board.getResult();
|
||||
if (r == null) return 0;
|
||||
|
||||
if (r == Result.DRAW) return 0;
|
||||
|
||||
boolean botIsP1 = (me == Player.PLAYER1);
|
||||
// Si le bot gagne, valeur positive élevée, sinon valeur négative
|
||||
return ((r == Result.WIN) == botIsP1) ? 100000 : -100000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Heuristique évoluée pour Avalam :
|
||||
* Calcule un score basé sur le contrôle des tours et leur hauteur.
|
||||
* Les tours de hauteur 5 sont prioritaires car elles sont bloquées.
|
||||
* 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) continue;
|
||||
if (t == null || t.getHeight() == 0) continue;
|
||||
|
||||
int h = t.getHeight();
|
||||
|
||||
// Pondération selon la hauteur (heuristique "Divine")
|
||||
int value =
|
||||
(h == 5) ? 1000 :
|
||||
(h == 4) ? 300 :
|
||||
(h == 3) ? 120 :
|
||||
(h == 2) ? 40 : 10;
|
||||
int weight = 0;
|
||||
|
||||
if (t.getColor() == myColor) score += value;
|
||||
else score -= value;
|
||||
// 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Génère la liste de tous les coups possibles sur le plateau donné.
|
||||
* 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<>();
|
||||
board.iterator().forEachRemaining(moves::add);
|
||||
Iterator<AbstractPly> it = board.iterator();
|
||||
while (it.hasNext()) moves.add(it.next());
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user