Compare commits
10 Commits
tests
...
makefile_c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d94b95fa36 | ||
|
|
2db0212b31 | ||
|
|
43e44bf7d2 | ||
|
|
ee374a9221 | ||
| 1eddda2605 | |||
|
|
b52badad31 | ||
|
|
88c65bc194 | ||
| 8bbe17ebd8 | |||
| ad2f0c63cf | |||
|
|
6226f4254a |
47
Makefile
47
Makefile
@@ -1,5 +1,7 @@
|
||||
# === Environnements ===
|
||||
TEST_ENV = "bin:/usr/share/java/junit.jar:/usr/share/java/hamcrest-core.jar"
|
||||
JUNIT_JAR = /usr/share/java/junit.jar
|
||||
HAMCREST_JAR = /usr/share/java/hamcrest-core.jar
|
||||
TEST_ENV = "bin:$(JUNIT_JAR):$(HAMCREST_JAR)"
|
||||
|
||||
# === Répertoires ===
|
||||
SRC_DIR = fr
|
||||
@@ -9,8 +11,11 @@ BIN_DIR = bin
|
||||
RES_SRC = fr/iut_fbleau/Res
|
||||
RES_BIN = bin/fr/iut_fbleau/Res
|
||||
|
||||
# === Recherche automatique des fichiers .java dans tous les sous-dossiers ===
|
||||
SOURCES := $(shell find $(SRC_DIR) -name "*.java")
|
||||
# === Recherche automatique des fichiers .java ===
|
||||
# SOURCES : uniquement le code de l'application (sans les fichiers de tests)
|
||||
SOURCES := $(shell find $(SRC_DIR) -name "*.java" -not -path "$(SRC_DIR)/iut_fbleau/Tests/*")
|
||||
# TEST_SOURCES : uniquement les fichiers de tests
|
||||
TEST_SOURCES := $(shell find $(SRC_DIR)/iut_fbleau/Tests -name "*.java" 2>/dev/null)
|
||||
|
||||
# === Classe principale ===
|
||||
MAIN_CLASS = fr.iut_fbleau.Avalam.Main
|
||||
@@ -20,7 +25,10 @@ TEST_CLASS = fr.iut_fbleau.Tests.AvalamBoardTest
|
||||
|
||||
# === Commandes Java ===
|
||||
JC = javac
|
||||
JCFLAGS = -d $(BIN_DIR) -cp $(TEST_ENV)
|
||||
# Compilation normale (application uniquement)
|
||||
JCFLAGS = -d $(BIN_DIR)
|
||||
# Compilation des tests (application + JUnit)
|
||||
JCFLAGS_TESTS = -d $(BIN_DIR) -cp $(TEST_ENV)
|
||||
|
||||
JAVA = java
|
||||
JAVAFLAGS = -cp $(BIN_DIR)
|
||||
@@ -38,6 +46,35 @@ compile:
|
||||
@mkdir -p $(BIN_DIR)
|
||||
@$(JC) $(JCFLAGS) $(SOURCES)
|
||||
|
||||
compile_tests: compile
|
||||
@echo "===> Compilation des tests..."
|
||||
@mkdir -p $(BIN_DIR)
|
||||
ifneq ($(TEST_SOURCES),)
|
||||
@$(JC) $(JCFLAGS_TESTS) $(TEST_SOURCES)
|
||||
else
|
||||
@echo "Aucun fichier de test trouvé dans $(SRC_DIR)/iut_fbleau/Tests"
|
||||
endif
|
||||
|
||||
# === Vérification / installation des dépendances de tests ===
|
||||
check_test_deps:
|
||||
@echo "===> Vérification des dépendances de tests (JUnit / Hamcrest)..."
|
||||
@if [ ! -f "$(JUNIT_JAR)" ] || [ ! -f "$(HAMCREST_JAR)" ]; then \
|
||||
echo " JUnit ou Hamcrest manquant, tentative d'installation (sudo requis)..."; \
|
||||
if command -v sudo >/dev/null 2>&1; then \
|
||||
sudo apt-get update && sudo apt-get install -y junit4 libhamcrest-java; \
|
||||
else \
|
||||
apt-get update && apt-get install -y junit4 libhamcrest-java; \
|
||||
fi; \
|
||||
if [ ! -f "$(JUNIT_JAR)" ] || [ ! -f "$(HAMCREST_JAR)" ]; then \
|
||||
echo "✖ Impossible de trouver/installer $(JUNIT_JAR) ou $(HAMCREST_JAR). Vérifiez manuellement vos paquets JUnit/Hamcrest."; \
|
||||
exit 1; \
|
||||
else \
|
||||
echo "✔ Dépendances de tests installées."; \
|
||||
fi; \
|
||||
else \
|
||||
echo "✔ JUnit et Hamcrest trouvés."; \
|
||||
fi
|
||||
|
||||
# === Copie des ressources (.txt) dans bin ===
|
||||
resources:
|
||||
@echo "===> Copie des ressources..."
|
||||
@@ -51,7 +88,7 @@ run:
|
||||
@$(JAVA) $(JAVAFLAGS) $(MAIN_CLASS)
|
||||
|
||||
# === Tests ===
|
||||
test:
|
||||
test: check_test_deps compile_tests
|
||||
@echo "===> Lancement des tests..."
|
||||
@$(JAVA) $(JAVAFLAGS_TESTS) org.junit.runner.JUnitCore $(TEST_CLASS)
|
||||
@echo "... Fin des tests."
|
||||
|
||||
32
README.md
32
README.md
@@ -12,37 +12,55 @@ Dans un second temps, nous développerons des bots les plus efficaces possible,
|
||||
|
||||
Le jeu de notre groupe est **Avalam**.
|
||||
|
||||
## Compilation et exécution
|
||||
## Compilation, exécution et tests
|
||||
|
||||
### Compilation
|
||||
### Compilation (sans tests)
|
||||
```bash
|
||||
make build
|
||||
```
|
||||
|
||||
### Tests
|
||||
ou simplement :
|
||||
```bash
|
||||
make test
|
||||
make
|
||||
```
|
||||
Cette commande :
|
||||
- compile uniquement le code de l'application (sans les fichiers du dossier `fr/iut_fbleau/Tests`) ;
|
||||
- copie les ressources dans `bin/`.
|
||||
|
||||
### Exécution
|
||||
### Exécution du jeu
|
||||
```bash
|
||||
make run
|
||||
```
|
||||
Lance la fenêtre de jeu Avalam après compilation.
|
||||
|
||||
### Lancer les tests
|
||||
```bash
|
||||
make test
|
||||
```
|
||||
Cette commande :
|
||||
- vérifie d'abord la présence de **JUnit** et **Hamcrest** dans `/usr/share/java` ;
|
||||
- si nécessaire, tente de les installer automatiquement via `apt-get` (sudo requis sur Debian/Ubuntu) ;
|
||||
- compile ensuite les fichiers de tests (`fr/iut_fbleau/Tests`) ;
|
||||
- lance enfin la suite de tests JUnit (`AvalamBoardTest`).
|
||||
|
||||
Si l'installation automatique échoue (autre OS, pas de droits sudo, pas d'accès réseau, etc.), un message l'indiquera et il faudra installer JUnit/Hamcrest manuellement.
|
||||
|
||||
### Nettoyage
|
||||
```bash
|
||||
make clean
|
||||
```
|
||||
Supprime le répertoire `bin/` (classes compilées et ressources copiées).
|
||||
|
||||
### Recompiler et exécuter
|
||||
### Recompiler puis exécuter
|
||||
```bash
|
||||
make re
|
||||
```
|
||||
Équivaut à `make clean` puis `make build` puis `make run`.
|
||||
|
||||
### Générer la Javadoc
|
||||
```bash
|
||||
make javadoc
|
||||
```
|
||||
Génère la documentation dans le dossier `doc/`.
|
||||
|
||||
## Architecture du projet
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package fr.iut_fbleau.Avalam;
|
||||
|
||||
import fr.iut_fbleau.Bot.AlphaBetaBot;
|
||||
import fr.iut_fbleau.Bot.DivineBot;
|
||||
import fr.iut_fbleau.Bot.IdiotBot;
|
||||
import fr.iut_fbleau.GameAPI.AbstractGamePlayer;
|
||||
import fr.iut_fbleau.GameAPI.Player;
|
||||
@@ -13,10 +14,14 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Fenêtre pour le mode Arène.
|
||||
* Permet de sélectionner deux bots, le nombre de parties, et affiche les résultats.
|
||||
*
|
||||
* @version 1.0
|
||||
* Fenêtre pour le mode Arène (bots vs bots).
|
||||
*
|
||||
* Elle permet :
|
||||
* - de sélectionner deux bots parmi Idiot, Alpha-Beta et Divin ;
|
||||
* - de configurer la profondeur de recherche pour les bots intelligents ;
|
||||
* - de choisir le nombre de parties à jouer ;
|
||||
* - d’afficher, dans un tableau, le résultat de chaque partie (gagnant ou erreur) ;
|
||||
* - de revenir au menu principal ou de quitter entièrement le jeu.
|
||||
*/
|
||||
public class ArenaWindow extends JFrame {
|
||||
|
||||
@@ -53,14 +58,19 @@ public class ArenaWindow extends JFrame {
|
||||
JButton startButton = new JButton("Lancer les parties");
|
||||
startButton.addActionListener(e -> showConfigDialog());
|
||||
buttonPanel.add(startButton);
|
||||
|
||||
|
||||
JButton backButton = new JButton("Retour au menu");
|
||||
backButton.addActionListener(e -> {
|
||||
dispose(); // Ferme la fenêtre Arène
|
||||
Main.showModeSelection(); // Affiche le menu principal
|
||||
});
|
||||
buttonPanel.add(backButton);
|
||||
|
||||
|
||||
// Nouveau bouton pour quitter entièrement le jeu
|
||||
JButton quitButton = new JButton("Quitter");
|
||||
quitButton.addActionListener(e -> System.exit(0));
|
||||
buttonPanel.add(quitButton);
|
||||
|
||||
add(buttonPanel, BorderLayout.SOUTH);
|
||||
|
||||
pack();
|
||||
@@ -182,55 +192,135 @@ public class ArenaWindow extends JFrame {
|
||||
* @param nbParties nombre de parties à jouer
|
||||
*/
|
||||
private void runArena(String bot1Type, String bot2Type, int depth, int nbParties) {
|
||||
// Créer les bots
|
||||
AbstractGamePlayer bot1 = createBot(bot1Type, Player.PLAYER1, depth);
|
||||
AbstractGamePlayer bot2 = createBot(bot2Type, Player.PLAYER2, depth);
|
||||
|
||||
if (bot1 == null || bot2 == null) {
|
||||
JOptionPane.showMessageDialog(this, "Erreur lors de la création des bots.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Vider le tableau
|
||||
tableModel.setRowCount(0);
|
||||
results.clear();
|
||||
|
||||
// Charger le plateau initial
|
||||
Tower[][] initialGrid = BoardLoader.loadFromFile("fr/iut_fbleau/Res/Plateau.txt");
|
||||
// Créer un dialogue de progression
|
||||
JDialog progressDialog = new JDialog(this, "Parties en cours...", true);
|
||||
JLabel progressLabel = new JLabel("Préparation des parties...", JLabel.CENTER);
|
||||
progressLabel.setBorder(BorderFactory.createEmptyBorder(20, 40, 20, 40));
|
||||
progressDialog.add(progressLabel);
|
||||
progressDialog.setSize(300, 100);
|
||||
progressDialog.setLocationRelativeTo(this);
|
||||
progressDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
|
||||
|
||||
// Lancer les parties
|
||||
for (int i = 1; i <= nbParties; i++) {
|
||||
AvalamBoard board = new AvalamBoard(initialGrid, Player.PLAYER1);
|
||||
ArenaGame game = new ArenaGame(board, bot1, bot2);
|
||||
// Exécuter les parties dans un thread séparé pour ne pas bloquer l'interface
|
||||
Thread arenaThread = new Thread(() -> {
|
||||
SwingUtilities.invokeLater(() -> progressDialog.setVisible(true));
|
||||
|
||||
try {
|
||||
Result result = game.run();
|
||||
String winner = getWinnerName(result, bot1Type, bot2Type);
|
||||
|
||||
// Ajouter au tableau
|
||||
tableModel.addRow(new Object[]{
|
||||
"Partie " + i,
|
||||
bot1Type,
|
||||
bot2Type,
|
||||
winner
|
||||
});
|
||||
} catch (Exception e) {
|
||||
tableModel.addRow(new Object[]{
|
||||
"Partie " + i,
|
||||
bot1Type,
|
||||
bot2Type,
|
||||
"Erreur: " + e.getMessage()
|
||||
// Statistiques pour déboguer
|
||||
int bot1Wins = 0;
|
||||
int bot2Wins = 0;
|
||||
int draws = 0;
|
||||
int errors = 0;
|
||||
|
||||
for (int i = 1; i <= nbParties; i++) {
|
||||
final int partieNum = i;
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
progressLabel.setText("Partie " + partieNum + " / " + nbParties + " en cours...");
|
||||
});
|
||||
|
||||
// Recréer les bots à chaque partie pour garantir l'indépendance complète
|
||||
// (notamment pour réinitialiser les générateurs aléatoires)
|
||||
AbstractGamePlayer bot1 = createBot(bot1Type, Player.PLAYER1, depth);
|
||||
AbstractGamePlayer bot2 = createBot(bot2Type, Player.PLAYER2, depth);
|
||||
|
||||
if (bot1 == null || bot2 == null) {
|
||||
errors++;
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
tableModel.addRow(new Object[]{
|
||||
"Partie " + partieNum,
|
||||
bot1Type,
|
||||
bot2Type,
|
||||
"Erreur: Impossible de créer les bots"
|
||||
});
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// Recharger le plateau à chaque partie pour garantir l'indépendance complète
|
||||
Tower[][] initialGrid = BoardLoader.loadFromFile("fr/iut_fbleau/Res/Plateau.txt");
|
||||
AvalamBoard board = new AvalamBoard(initialGrid, Player.PLAYER1);
|
||||
ArenaGame game = new ArenaGame(board, bot1, bot2);
|
||||
|
||||
try {
|
||||
Result result = game.run();
|
||||
String winner = getWinnerName(result, bot1Type, bot2Type);
|
||||
|
||||
// Mettre à jour les statistiques
|
||||
if (result == Result.WIN) {
|
||||
bot1Wins++;
|
||||
} else if (result == Result.LOSS) {
|
||||
bot2Wins++;
|
||||
} else if (result == Result.DRAW) {
|
||||
draws++;
|
||||
}
|
||||
|
||||
// Ajouter au tableau dans le thread EDT
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
tableModel.addRow(new Object[]{
|
||||
"Partie " + partieNum,
|
||||
bot1Type,
|
||||
bot2Type,
|
||||
winner
|
||||
});
|
||||
});
|
||||
} catch (Exception e) {
|
||||
errors++;
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
tableModel.addRow(new Object[]{
|
||||
"Partie " + partieNum,
|
||||
bot1Type,
|
||||
bot2Type,
|
||||
"Erreur: " + e.getMessage()
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Afficher les statistiques finales
|
||||
final int finalBot1Wins = bot1Wins;
|
||||
final int finalBot2Wins = bot2Wins;
|
||||
final int finalDraws = draws;
|
||||
final int finalErrors = errors;
|
||||
|
||||
// Afficher un message de fin
|
||||
JOptionPane.showMessageDialog(
|
||||
this,
|
||||
"Toutes les parties sont terminées !",
|
||||
"Arène terminée",
|
||||
JOptionPane.INFORMATION_MESSAGE
|
||||
);
|
||||
// Fermer le dialogue et afficher le message de fin avec statistiques
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
progressDialog.dispose();
|
||||
|
||||
String statsMessage = String.format(
|
||||
"Toutes les parties sont terminées !\n\n" +
|
||||
"Statistiques :\n" +
|
||||
"- %s (Bot 1) : %d victoires\n" +
|
||||
"- %s (Bot 2) : %d victoires\n" +
|
||||
"- Matchs nuls : %d\n" +
|
||||
"- Erreurs : %d",
|
||||
bot1Type, finalBot1Wins,
|
||||
bot2Type, finalBot2Wins,
|
||||
finalDraws,
|
||||
finalErrors
|
||||
);
|
||||
|
||||
Object[] options = {"OK", "Quitter le jeu"};
|
||||
int choice = JOptionPane.showOptionDialog(
|
||||
this,
|
||||
statsMessage,
|
||||
"Arène terminée",
|
||||
JOptionPane.DEFAULT_OPTION,
|
||||
JOptionPane.INFORMATION_MESSAGE,
|
||||
null,
|
||||
options,
|
||||
options[0]
|
||||
);
|
||||
|
||||
if (choice == 1) {
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
arenaThread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -247,14 +337,7 @@ public class ArenaWindow extends JFrame {
|
||||
} else if (botType.equals("Bot Alpha-Beta")) {
|
||||
return new AlphaBetaBot(player, depth);
|
||||
} else if (botType.equals("Bot Divin")) {
|
||||
// Pour l'instant, le Bot Divin n'est pas implémenté, on utilise IdiotBot
|
||||
JOptionPane.showMessageDialog(
|
||||
this,
|
||||
"Le Bot Divin n'est pas encore implémenté. Utilisation du Bot Idiot à la place.",
|
||||
"Avertissement",
|
||||
JOptionPane.WARNING_MESSAGE
|
||||
);
|
||||
return new IdiotBot(player);
|
||||
return new DivineBot(player, depth);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -51,9 +51,16 @@ public class AvalamBoard extends AbstractBoard {
|
||||
super(startingPlayer, new ArrayDeque<>());
|
||||
this.grid = new Tower[SIZE][SIZE];
|
||||
|
||||
// Copie profonde : créer de nouvelles tours pour éviter que toutes les parties partagent les mêmes objets
|
||||
for (int r = 0; r < SIZE; r++)
|
||||
for (int c = 0; c < SIZE; c++)
|
||||
this.grid[r][c] = initialGrid[r][c];
|
||||
for (int c = 0; c < SIZE; c++) {
|
||||
Tower t = initialGrid[r][c];
|
||||
if (t == null) {
|
||||
this.grid[r][c] = null;
|
||||
} else {
|
||||
this.grid[r][c] = new Tower(t.getHeight(), t.getColor());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package fr.iut_fbleau.Avalam;
|
||||
|
||||
import fr.iut_fbleau.Bot.AlphaBetaBot;
|
||||
// A FAIRE PLUS TARD (PVGOD)
|
||||
import fr.iut_fbleau.Bot.DivineBot;
|
||||
import fr.iut_fbleau.Bot.IdiotBot;
|
||||
import fr.iut_fbleau.GameAPI.AbstractPly;
|
||||
@@ -14,22 +13,25 @@ import java.awt.*;
|
||||
/**
|
||||
* La classe <code>AvalamWindow</code>
|
||||
*
|
||||
* Fenêtre principale (interface graphique) du jeu Avalam.
|
||||
* Elle contient :
|
||||
* - le plateau (BoardView)
|
||||
* - l’affichage du score (ScoreView)
|
||||
* - l’affichage du joueur courant (TurnView)
|
||||
*
|
||||
* Elle pilote un objet <code>AvalamBoard</code> (moteur du jeu).
|
||||
* Elle peut fonctionner en mode :
|
||||
* - joueur vs joueur
|
||||
* - joueur vs bot idiot (aléatoire)
|
||||
* - joueur vs bot alpha (cut-off)
|
||||
* Fenêtre principale (interface graphique) du jeu Avalam pour tous les modes
|
||||
* hors Arène :
|
||||
* - joueur vs joueur (PVP)
|
||||
* - joueur vs bot idiot (PVBOT)
|
||||
* - joueur vs bot alpha (PVALPHA)
|
||||
* - joueur vs bot divin (PVGOD)
|
||||
*
|
||||
* @version 1.0
|
||||
* Date :
|
||||
* Licence :
|
||||
* Elle contient :
|
||||
* - le plateau (<code>BoardView</code>)
|
||||
* - l’affichage du score (<code>ScoreView</code>)
|
||||
* - l’affichage du joueur courant (<code>TurnView</code>)
|
||||
*
|
||||
* Elle pilote un objet <code>AvalamBoard</code> (moteur du jeu) et,
|
||||
* en fonction du {@link GameMode}, instancie le bot approprié
|
||||
* (idiot, alpha-bêta ou divin).
|
||||
*
|
||||
* En fin de partie, elle ouvre une fenêtre de fin (<code>EndGameDialog</code>)
|
||||
* affichant le gagnant, les scores et proposant les actions :
|
||||
* « Rejouer », « Menu principal » ou « Quitter le jeu ».
|
||||
*/
|
||||
public class AvalamWindow extends JFrame {
|
||||
|
||||
@@ -50,6 +52,9 @@ public class AvalamWindow extends JFrame {
|
||||
/** Mode de jeu sélectionné. */
|
||||
private final GameMode mode;
|
||||
|
||||
/** Profondeur de recherche utilisée (utile pour les modes avec bot intelligent et pour rejouer). */
|
||||
private final int searchDepth;
|
||||
|
||||
/** Joueur contrôlé par le bot (si actif). */
|
||||
private final Player botPlayer = Player.PLAYER2;
|
||||
|
||||
@@ -59,18 +64,9 @@ public class AvalamWindow extends JFrame {
|
||||
/** Bot Alpha-Beta (utilisé si mode PVALPHA). */
|
||||
private final AlphaBetaBot alphaBot;
|
||||
|
||||
/** Bot Divin (utilisé si mode PVGOD). */
|
||||
private final DivineBot divineBot;
|
||||
|
||||
// A FAIRE PLUS TARD (PVGOD)
|
||||
// /** Bot Divin (utilisé si mode PVGOD). */
|
||||
// private final DivineBot divineBot;
|
||||
|
||||
/**
|
||||
* A FAIRE PLUS TARD (PVGOD)
|
||||
* On garde l'attribut à null pour ne pas casser la compilation,
|
||||
* mais toute la logique PVGOD est désactivée/commentée.
|
||||
*/
|
||||
|
||||
/** Indique si une animation de tour de bot est en cours. */
|
||||
private boolean botAnimating = false;
|
||||
|
||||
@@ -87,7 +83,7 @@ public class AvalamWindow extends JFrame {
|
||||
* Construit la fenêtre en fonction du mode choisi.
|
||||
* Pour PVALPHA/PVGOD, la profondeur par défaut est 4.
|
||||
*
|
||||
* @param mode mode de jeu
|
||||
* @param mode mode de jeu (PVP, PVBOT, PVALPHA ou PVGOD)
|
||||
*/
|
||||
public AvalamWindow(GameMode mode) {
|
||||
this(mode, 4);
|
||||
@@ -95,22 +91,23 @@ public class AvalamWindow extends JFrame {
|
||||
|
||||
/**
|
||||
* Construit la fenêtre en fonction du mode choisi.
|
||||
* Si le mode est PVALPHA ou PVGOD, la profondeur est utilisée comme cut-off.
|
||||
* Si le mode est PVALPHA ou PVGOD, la profondeur est utilisée comme cut-off
|
||||
* pour les bots Alpha-Beta et Divin.
|
||||
*
|
||||
* @param mode mode de jeu
|
||||
* @param mode mode de jeu
|
||||
* @param alphaDepth profondeur de recherche pour Alpha-Beta / Bot Divin
|
||||
*/
|
||||
public AvalamWindow(GameMode mode, int alphaDepth) {
|
||||
super("Avalam");
|
||||
|
||||
this.mode = mode;
|
||||
this.searchDepth = Math.max(1, alphaDepth);
|
||||
|
||||
this.idiotBot = (mode == GameMode.PVBOT) ? new IdiotBot(botPlayer) : null;
|
||||
|
||||
int depth = Math.max(1, alphaDepth);
|
||||
int depth = this.searchDepth;
|
||||
this.alphaBot = (mode == GameMode.PVALPHA) ? new AlphaBetaBot(botPlayer, depth) : null;
|
||||
|
||||
// A FAIRE PLUS TARD (PVGOD)
|
||||
this.divineBot = (mode == GameMode.PVGOD) ? new DivineBot(botPlayer, depth) : null;
|
||||
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
@@ -169,28 +166,30 @@ public class AvalamWindow extends JFrame {
|
||||
if (board.isGameOver()) {
|
||||
Result res = board.getResult();
|
||||
|
||||
String msg;
|
||||
switch (res) {
|
||||
case WIN:
|
||||
msg = "Le joueur jaune a gagné !";
|
||||
break;
|
||||
case LOSS:
|
||||
msg = "Le joueur rouge a gagné !";
|
||||
break;
|
||||
case DRAW:
|
||||
msg = "Égalité !";
|
||||
break;
|
||||
default:
|
||||
msg = "Fin de partie.";
|
||||
break;
|
||||
}
|
||||
int scoreJaune = computeScore(Color.YELLOW);
|
||||
int scoreRouge = computeScore(Color.RED);
|
||||
|
||||
JOptionPane.showMessageDialog(
|
||||
EndGameDialog dialog = new EndGameDialog(
|
||||
this,
|
||||
msg,
|
||||
"Partie terminée",
|
||||
JOptionPane.INFORMATION_MESSAGE
|
||||
res,
|
||||
scoreJaune,
|
||||
scoreRouge,
|
||||
mode,
|
||||
searchDepth,
|
||||
// Rejouer : on ferme la fenêtre actuelle et on relance une nouvelle partie avec le même mode/profondeur
|
||||
() -> SwingUtilities.invokeLater(() -> {
|
||||
dispose();
|
||||
new AvalamWindow(mode, searchDepth);
|
||||
}),
|
||||
// Menu principal : on ferme la fenêtre actuelle et on réaffiche le menu de mode de jeu
|
||||
() -> SwingUtilities.invokeLater(() -> {
|
||||
dispose();
|
||||
Main.showModeSelection();
|
||||
}),
|
||||
// Quitter complètement l'application
|
||||
() -> System.exit(0)
|
||||
);
|
||||
dialog.setVisible(true);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -220,12 +219,8 @@ public class AvalamWindow extends JFrame {
|
||||
if (mode == GameMode.PVBOT && idiotBot == null) return;
|
||||
if (mode == GameMode.PVALPHA && alphaBot == null) return;
|
||||
|
||||
// A FAIRE PLUS TARD (PVGOD)
|
||||
if (mode == GameMode.PVGOD && divineBot == null) return;
|
||||
|
||||
// A FAIRE PLUS TARD (PVGOD)
|
||||
// Pour l'instant, si PVGOD est sélectionné, on ne joue pas de coup bot.
|
||||
|
||||
botAnimating = true;
|
||||
|
||||
// Désactiver les interactions du joueur pendant le tour du bot.
|
||||
@@ -238,7 +233,6 @@ public class AvalamWindow extends JFrame {
|
||||
} else if (mode == GameMode.PVALPHA) {
|
||||
botMove = alphaBot.giveYourMove(board.safeCopy());
|
||||
} else {
|
||||
// A FAIRE PLUS TARD (PVGOD)
|
||||
botMove = divineBot.giveYourMove(board.safeCopy());
|
||||
}
|
||||
|
||||
|
||||
160
fr/iut_fbleau/Avalam/EndGameDialog.java
Normal file
160
fr/iut_fbleau/Avalam/EndGameDialog.java
Normal file
@@ -0,0 +1,160 @@
|
||||
package fr.iut_fbleau.Avalam;
|
||||
|
||||
import fr.iut_fbleau.GameAPI.Result;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* Fenêtre de fin de partie.
|
||||
*
|
||||
* Elle est ouverte par {@link AvalamWindow} lorsque le moteur signale
|
||||
* que la partie est terminée. Elle affiche :
|
||||
* - le résultat (gagnant ou égalité) à partir du {@link Result} ;
|
||||
* - le score détaillé (tours contrôlées par Jaune et Rouge) ;
|
||||
* - le mode de jeu courant (PVP, PVBOT, PVALPHA, PVGOD, avec profondeur pour les bots intelligents).
|
||||
*
|
||||
* Elle propose également trois actions sous forme de boutons :
|
||||
* - « Rejouer » : relancer une partie avec la même configuration ;
|
||||
* - « Menu principal » : retourner au menu de sélection de mode ;
|
||||
* - « Quitter » : fermer complètement l’application.
|
||||
*/
|
||||
public class EndGameDialog extends JDialog {
|
||||
|
||||
/**
|
||||
* Construit la fenêtre de fin de partie.
|
||||
*
|
||||
* @param parent fenêtre principale (généralement une {@link AvalamWindow})
|
||||
* @param result résultat de la partie (WIN / LOSS / DRAW du point de vue de PLAYER1 / Jaune)
|
||||
* @param scoreJaune score du joueur jaune (nombre de tours contrôlées)
|
||||
* @param scoreRouge score du joueur rouge (nombre de tours contrôlées)
|
||||
* @param mode mode de jeu courant (pour l’information et le « Rejouer »)
|
||||
* @param depth profondeur utilisée (pour les modes avec bot intelligent)
|
||||
* @param onReplay action à exécuter lorsque l’utilisateur clique sur « Rejouer »
|
||||
* @param onMenu action à exécuter lorsque l’utilisateur clique sur « Menu principal »
|
||||
* @param onQuit action à exécuter lorsque l’utilisateur clique sur « Quitter »
|
||||
*/
|
||||
public EndGameDialog(
|
||||
JFrame parent,
|
||||
Result result,
|
||||
int scoreJaune,
|
||||
int scoreRouge,
|
||||
GameMode mode,
|
||||
int depth,
|
||||
Runnable onReplay,
|
||||
Runnable onMenu,
|
||||
Runnable onQuit
|
||||
) {
|
||||
super(parent, "Fin de partie", true);
|
||||
|
||||
setLayout(new BorderLayout(10, 10));
|
||||
|
||||
// Panel principal d'information
|
||||
JPanel infoPanel = new JPanel();
|
||||
infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
|
||||
infoPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
||||
|
||||
JLabel titre = new JLabel("Partie terminée");
|
||||
titre.setFont(titre.getFont().deriveFont(Font.BOLD, 18f));
|
||||
titre.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
String gagnant;
|
||||
switch (result) {
|
||||
case WIN:
|
||||
gagnant = "Le joueur JAUNE a gagné !";
|
||||
break;
|
||||
case LOSS:
|
||||
gagnant = "Le joueur ROUGE a gagné !";
|
||||
break;
|
||||
case DRAW:
|
||||
gagnant = "Égalité parfaite !";
|
||||
break;
|
||||
default:
|
||||
gagnant = "Fin de partie.";
|
||||
}
|
||||
|
||||
JLabel gagnantLabel = new JLabel(gagnant);
|
||||
gagnantLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
JLabel scoreLabel = new JLabel(
|
||||
"Score - Jaune : " + scoreJaune + " | Rouge : " + scoreRouge
|
||||
);
|
||||
scoreLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
JLabel modeLabel = new JLabel("Mode : " + modeToString(mode, depth));
|
||||
modeLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
infoPanel.add(titre);
|
||||
infoPanel.add(Box.createVerticalStrut(10));
|
||||
infoPanel.add(gagnantLabel);
|
||||
infoPanel.add(Box.createVerticalStrut(5));
|
||||
infoPanel.add(scoreLabel);
|
||||
infoPanel.add(Box.createVerticalStrut(5));
|
||||
infoPanel.add(modeLabel);
|
||||
|
||||
add(infoPanel, BorderLayout.CENTER);
|
||||
|
||||
// Panel des boutons d'action
|
||||
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 10));
|
||||
|
||||
JButton replayButton = new JButton("Rejouer");
|
||||
replayButton.addActionListener(e -> {
|
||||
if (onReplay != null) {
|
||||
onReplay.run();
|
||||
}
|
||||
dispose();
|
||||
});
|
||||
|
||||
JButton menuButton = new JButton("Menu principal");
|
||||
menuButton.addActionListener(e -> {
|
||||
if (onMenu != null) {
|
||||
onMenu.run();
|
||||
}
|
||||
dispose();
|
||||
});
|
||||
|
||||
JButton quitButton = new JButton("Quitter");
|
||||
quitButton.addActionListener(e -> {
|
||||
if (onQuit != null) {
|
||||
onQuit.run();
|
||||
}
|
||||
dispose();
|
||||
});
|
||||
|
||||
buttonPanel.add(replayButton);
|
||||
buttonPanel.add(menuButton);
|
||||
buttonPanel.add(quitButton);
|
||||
|
||||
add(buttonPanel, BorderLayout.SOUTH);
|
||||
|
||||
pack();
|
||||
setResizable(false);
|
||||
setLocationRelativeTo(parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne une description lisible du mode de jeu courant.
|
||||
*
|
||||
* @param mode mode de jeu
|
||||
* @param depth profondeur (pour les bots intelligents)
|
||||
* @return chaîne décrivant le mode
|
||||
*/
|
||||
private String modeToString(GameMode mode, int depth) {
|
||||
switch (mode) {
|
||||
case PVP:
|
||||
return "Joueur vs Joueur";
|
||||
case PVBOT:
|
||||
return "Joueur vs Bot idiot";
|
||||
case PVALPHA:
|
||||
return "Joueur vs Bot Alpha (profondeur " + depth + ")";
|
||||
case PVGOD:
|
||||
return "Joueur vs Bot Divin (profondeur " + depth + ")";
|
||||
case ARENA:
|
||||
return "Arène (bots)"; // normalement non utilisé ici
|
||||
default:
|
||||
return mode.name();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ public class Main {
|
||||
"joueur vs joueur",
|
||||
"joueur vs botidiot",
|
||||
"joueur vs bot alpha",
|
||||
"joueur vs bot divin (NON IMPLEMENTE)",
|
||||
"joueur vs bot divin",
|
||||
"Arène"
|
||||
};
|
||||
|
||||
|
||||
@@ -105,22 +105,38 @@ public class DivineBot extends AbstractGamePlayer {
|
||||
|
||||
boolean isMax = board.getCurrentPlayer() == me;
|
||||
|
||||
for (AbstractPly m : listMoves(board)) {
|
||||
IBoard next = board.safeCopy();
|
||||
next.doPly(m);
|
||||
|
||||
int val = alphaBeta(next, depth - 1, alpha, beta);
|
||||
|
||||
if (isMax) {
|
||||
alpha = Math.max(alpha, val);
|
||||
if (alpha >= beta) break; // Coupure Beta
|
||||
} else {
|
||||
beta = Math.min(beta, val);
|
||||
if (alpha >= beta) break; // Coupure Alpha
|
||||
}
|
||||
List<AbstractPly> moves = listMoves(board);
|
||||
if (moves.isEmpty()) {
|
||||
return evaluate(board);
|
||||
}
|
||||
|
||||
return isMax ? 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);
|
||||
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);
|
||||
best = Math.min(best, val);
|
||||
beta = Math.min(beta, best);
|
||||
|
||||
if (alpha >= beta) break; // Coupure Alpha
|
||||
}
|
||||
return best;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user