5 Commits

7 changed files with 124 additions and 125 deletions

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 128 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 22 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 40 KiB

View File

@@ -3,6 +3,28 @@ title: Avalam - Diagramme de classes (complet)
--- ---
classDiagram classDiagram
class ArenaGame{
+ArenaGame(IBoard board, AbstractGamePlayer bot1, AbstractGamePlayer bot2)
-createPlayerMap(AbstractGamePlayer bot1, AbstractGamePlayer bot2): EnumMap<Player, AbstractGamePlayer>
}
class ArenaWindow{
-resultsTable: JTable
-tableModel: DefaultTableModel
-results: List<string>
+ArenaWindows()
-createConfigPanel(): JPanel
-createResultsTable()
-showConfigDialog()
-runArena(String bot1Type, String bot2Type, int depth, int nbParties)
-createBot(String botType, Player player, int depth): AbstractGamePlayer
-getWinnerName(Result result, String bot1Type, String bot2Type): String
}
ArenaWindow *-- AvalamBoard
ArenaWindow *-- ArenaGame
class AvalamBoard{ class AvalamBoard{
+SIZE: int +SIZE: int
-MAX_HEIGHT: int -MAX_HEIGHT: int
@@ -42,8 +64,6 @@ classDiagram
+toString(): String +toString(): String
} }
class AvalamWindow{ class AvalamWindow{
-board : AvalamBoard -board : AvalamBoard
@@ -70,13 +90,18 @@ classDiagram
AvalamWindow *-- BoardView AvalamWindow *-- BoardView
AvalamWindow *-- ScoreView AvalamWindow *-- ScoreView
AvalamWindow *-- TurnView AvalamWindow *-- TurnView
AvalamWindow *-- EndGameDialog
AvalamWindow --> GameMode AvalamWindow --> GameMode
class BackgroundLayer{
-img: Image
+BackgroundLayer(String resourcePath)
#paintComponent(Graphics g): void
}
class BoardLoader{ class BoardLoader{
+loadFromFile(String resourcePath): Tower[][] +loadFromFile(String resourcePath): Tower[][]
} }
class BoardView{ class BoardView{
-board: AvalamBoard -board: AvalamBoard
@@ -105,14 +130,6 @@ classDiagram
BoardView *-- InteractionController BoardView *-- InteractionController
BoardView --> AvalamBoard BoardView --> AvalamBoard
class BackgroundLayer{
-img: Image
+BackgroundLayer(String resourcePath)
#paintComponent(Graphics g): void
}
class Color{ class Color{
-YELLOW(int r, int g, int b) -YELLOW(int r, int g, int b)
-RED(int r, int g, int b) -RED(int r, int g, int b)
@@ -122,11 +139,17 @@ classDiagram
+toPlayer(): fr.iut_fbleau.GameAPI.Player +toPlayer(): fr.iut_fbleau.GameAPI.Player
} }
class EndGameDialog{
+EndGameDialog(JFrame parent, Result result, int scoreJaune, int scoreRouge, GameMode mode, int depth, Runnable onReplay, Runnable onMenu, Runnable onQuit)
-modeToString(GameMode mode, int depth): String
}
class GameMode{ class GameMode{
PVP PVP
PVBOT PVBOT
PVALPHA PVALPHA
PVGOD PVGOD
ARENA
} }
class HighlightLayer{ class HighlightLayer{
@@ -165,6 +188,7 @@ classDiagram
Main ..> AvalamWindow Main ..> AvalamWindow
Main ..> GameMode Main ..> GameMode
Main ..> ArenaWindow
class PieceButton{ class PieceButton{
-color: java.awt.Color -color: java.awt.Color

View File

@@ -17,7 +17,16 @@ classDiagram
} }
class DivineBot{ 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>
} }
class IdiotBot{ class IdiotBot{

View File

@@ -2,14 +2,14 @@ package fr.iut_fbleau.Bot;
import fr.iut_fbleau.Avalam.*; import fr.iut_fbleau.Avalam.*;
import fr.iut_fbleau.GameAPI.*; import fr.iut_fbleau.GameAPI.*;
import java.util.*; import java.util.*;
/** /**
* Bot "Divin" (alpha-beta + évaluateur pondéré). * Bot expert utilisant l'algorithme Alpha-Beta pour le jeu Avalam.
* * Idée : * * Idée :
* - Utilise l'algorithme Alpha-Beta pour anticiper les coups. * - Explore l'arbre des coups possibles jusqu'à une profondeur donnée.
* - Évalue les plateaux non terminaux en accordant plus d'importance aux tours hautes. * - Utilise l'élagage Alpha-Beta pour optimiser la recherche.
* - Évalue les positions selon le contrôle des tours et leur hauteur.
*/ */
public class DivineBot extends AbstractGamePlayer { public class DivineBot extends AbstractGamePlayer {
@@ -18,184 +18,141 @@ public class DivineBot extends AbstractGamePlayer {
/** Joueur contrôlé par ce bot (PLAYER1 ou PLAYER2). */ /** Joueur contrôlé par ce bot (PLAYER1 ou PLAYER2). */
private final Player me; private final Player me;
/** Profondeur maximale de recherche avant évaluation. */ /** Profondeur maximale de recherche (cut-off). */
private final int maxDepth; private final int maxDepth;
/** Générateur aléatoire pour choisir parmi les meilleurs coups équivalents. */ /** Générateur aléatoire pour départager les coups de même valeur. */
private final Random rng = new Random(); private final Random rng = new Random();
// Constructeur // Constructeur
/** /**
* Construit le bot Divine. * Construit un bot DivineBot.
* * * @param p joueur contrôlé par ce bot
* @param p joueur contrôlé par ce bot * @param maxDepth profondeur maximale de recherche
* @param maxDepth profondeur de l'arbre de recherche
*/ */
public DivineBot(Player p, int maxDepth) { public DivineBot(Player p, int maxDepth) {
super(p); super(p);
this.me = p; this.me = p;
this.maxDepth = Math.max(1, maxDepth); this.maxDepth = maxDepth;
} }
// Méthodes // Méthodes
/** /**
* Méthode principale de décision du bot. * Méthode appelée par GameAPI : le bot doit choisir le meilleur coup possible.
* Explore le premier niveau de l'arbre et lance les appels Alpha-Beta. * * @param board copie sûre de l'état de jeu (IBoard)
* * @param board état actuel du jeu * @return le coup choisi (AbstractPly) ou null si aucun coup n'est possible
* @return le meilleur coup calculé (AbstractPly)
*/ */
@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;
List<AbstractPly> moves = listMoves(board); List<AbstractPly> moves = listMoves(board);
if (moves.isEmpty()) return null; int bestValue = Integer.MIN_VALUE;
boolean isMax = board.getCurrentPlayer() == me;
int bestValue = isMax ? Integer.MIN_VALUE : Integer.MAX_VALUE;
List<AbstractPly> bestMoves = new ArrayList<>(); List<AbstractPly> bestMoves = new ArrayList<>();
int alpha = Integer.MIN_VALUE;
int beta = Integer.MAX_VALUE;
for (AbstractPly m : moves) { for (AbstractPly m : moves) {
IBoard next = board.safeCopy(); IBoard next = board.safeCopy();
next.doPly(m); next.doPly(m);
// On calcule la valeur du plateau après ce coup
int value = alphaBeta(next, maxDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
// Appel récursif pour évaluer la suite du coup if (value > bestValue) {
int value = alphaBeta(next, maxDepth - 1, alpha, beta); bestValue = value;
bestMoves.clear();
if (isMax) { bestMoves.add(m);
if (value > bestValue) { } else if (value == bestValue) {
bestValue = value; bestMoves.add(m);
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);
} }
} }
// Retourne un coup au hasard parmi les meilleurs ex-aequo
return bestMoves.get(rng.nextInt(bestMoves.size())); return bestMoves.get(rng.nextInt(bestMoves.size()));
} }
/** /**
* Algorithme récursif de recherche avec élagage Alpha-Beta. * Fonction récursive Alpha-Beta pour évaluer l'arbre de décision.
* * @param board état actuel du plateau
* @param depth profondeur restante à explorer
* @param alpha borne inférieure
* @param beta borne supérieure
* @return la valeur de l'évaluation
*/ */
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()) {
// Cas de base : fin de partie ou limite de profondeur atteinte Result r = board.getResult();
if (board.isGameOver()) return terminalValue(board); if (r == Result.DRAW) return 0;
boolean p1Wins = (r == Result.WIN);
boolean amIP1 = (me == Player.PLAYER1);
return (p1Wins == amIP1) ? 10000 : -10000;
}
if (depth == 0) return evaluate(board); if (depth == 0) return evaluate(board);
boolean isMax = board.getCurrentPlayer() == me; // Si c'est à moi de jouer, je maximise. Sinon, je minimise.
boolean isMax = (board.getCurrentPlayer() == me);
int best = isMax ? Integer.MIN_VALUE : Integer.MAX_VALUE;
List<AbstractPly> moves = listMoves(board); for (AbstractPly m : listMoves(board)) {
if (moves.isEmpty()) { IBoard next = board.safeCopy();
return evaluate(board); next.doPly(m);
} int val = alphaBeta(next, depth - 1, alpha, beta);
if (isMax) { 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);
best = Math.max(best, val); best = Math.max(best, val);
alpha = Math.max(alpha, best); alpha = Math.max(alpha, best);
} else {
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);
best = Math.min(best, val); best = Math.min(best, val);
beta = Math.min(beta, best); 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). * Heuristique spécifique à Avalam.
*/ * Valorise le contrôle des tours, avec un bonus pour les tours de hauteur 5 (verrouillées).
private int terminalValue(IBoard board) { * * @param board plateau à évaluer
Result r = board.getResult(); * @return score numérique de la position (positif si avantageux)
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.
*/ */
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;
// Configuration des 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;
int score = 0; int score = 0;
for (int r = 0; r < AvalamBoard.SIZE; r++) { for (int r = 0; r < AvalamBoard.SIZE; r++) {
for (int c = 0; c < AvalamBoard.SIZE; c++) { for (int c = 0; c < AvalamBoard.SIZE; c++) {
Tower t = b.getTowerAt(r, c); Tower t = b.getTowerAt(r, c);
if (t == null) continue; if (t == null || t.getHeight() == 0) continue;
int h = t.getHeight(); // Une tour de 5 vaut beaucoup plus car elle est "verrouillée".
int val = (t.getHeight() == 5) ? 10 : 1;
// Pondération selon la hauteur (heuristique "Divine") if (t.getColor() == myColor) {
int value = score += val;
(h == 5) ? 1000 : } else {
(h == 4) ? 300 : score -= val;
(h == 3) ? 120 : }
(h == 2) ? 40 : 10;
if (t.getColor() == myColor) score += value;
else score -= value;
} }
} }
return score; return score;
} }
/** /**
* Génère la liste de tous les coups possibles sur le plateau donné. * Récupère la liste de tous les coups légaux disponibles sur le plateau.
* * @param board plateau actuel
* @return liste des coups possibles
*/ */
private List<AbstractPly> listMoves(IBoard board) { private List<AbstractPly> listMoves(IBoard board) {
List<AbstractPly> moves = new ArrayList<>(); List<AbstractPly> moves = new ArrayList<>();
board.iterator().forEachRemaining(moves::add); Iterator<AbstractPly> it = board.iterator();
while (it.hasNext()) {
moves.add(it.next());
}
return moves; return moves;
} }
} }