Compare commits
19 Commits
Regle_coup
...
tests
| Author | SHA1 | Date | |
|---|---|---|---|
| 14e5df4332 | |||
| 6eb63cacaa | |||
| fdba5f0f2f | |||
| 6269404e7d | |||
| 4e8528fd58 | |||
| fa493f2fc1 | |||
| aba891e060 | |||
| 27663cd583 | |||
| 031b23c7c5 | |||
| b97b9cef69 | |||
| 25f8dcdc76 | |||
| 823b0fcae0 | |||
| 9f78dad6e5 | |||
| 830729ca21 | |||
| d95d52785e | |||
| ddf9b00c0a | |||
| c1e5de9ed2 | |||
| 1921b523c6 | |||
| d23aeb266f |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# Ignorer le répertoire bin
|
||||||
|
/bin
|
||||||
218
Diagrammes/Diagramme_Avalam.mmd
Normal file
218
Diagrammes/Diagramme_Avalam.mmd
Normal file
@@ -0,0 +1,218 @@
|
|||||||
|
---
|
||||||
|
title: Avalam - Diagramme de classes (complet)
|
||||||
|
---
|
||||||
|
classDiagram
|
||||||
|
|
||||||
|
class AvalamBoard{
|
||||||
|
+SIZE: int
|
||||||
|
-MAX_HEIGHT: int
|
||||||
|
-grid: Tower[][]
|
||||||
|
-gameOver: boolean
|
||||||
|
-result: Result
|
||||||
|
|
||||||
|
+AvalamBoard(Tower[][] initialGrid, Player startingPlayer)
|
||||||
|
+AvalamBoard(Tower[][] initialGrid)
|
||||||
|
-AvalamBoard(Tower[][] grid, Player current, boolean gameOver, Result result)
|
||||||
|
+getTowerAt(int row, int col): Tower
|
||||||
|
-inBounds(int r, int c): boolean
|
||||||
|
-areAdjacent(int r1, int c1, int r2, int c2): boolean
|
||||||
|
-colorForPlayer(Player p): Color
|
||||||
|
+isGameOver(): boolean
|
||||||
|
+getResult(): Result
|
||||||
|
+isLegal(AbstractPly c): boolean
|
||||||
|
+doPly(AbstractPly c): void
|
||||||
|
+iterator(): Iterator<AbstractPly>
|
||||||
|
+safeCopy(): IBoard
|
||||||
|
}
|
||||||
|
|
||||||
|
AvalamBoard "1" *-- "many" Tower
|
||||||
|
AvalamBoard ..> AvalamPly
|
||||||
|
|
||||||
|
class AvalamPly{
|
||||||
|
-xFrom : int
|
||||||
|
-yFrom : int
|
||||||
|
-xTo : int
|
||||||
|
-yTo : int
|
||||||
|
|
||||||
|
+AvalamPly(Player player, int xFrom, int yFrom, int xTo, int yTo)
|
||||||
|
+getXFrom(): int
|
||||||
|
+getXFrom(): int
|
||||||
|
+getXFrom(): int
|
||||||
|
+getXFrom(): int
|
||||||
|
|
||||||
|
+toString(): String
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class AvalamWindow{
|
||||||
|
-board : AvalamBoard
|
||||||
|
-scoreView : ScoreView
|
||||||
|
-turnView : TurnView
|
||||||
|
-boardView : BoardView
|
||||||
|
-mode: GameMode
|
||||||
|
-botPlayer: Player
|
||||||
|
-idiotBot: IdiotBot
|
||||||
|
-alphaBot: AlphaBetaBot
|
||||||
|
-divineBot: Object
|
||||||
|
-botAnimating: boolean
|
||||||
|
|
||||||
|
+AvalamWindow()
|
||||||
|
+AvalamWindow(GameMode mode)
|
||||||
|
+AvalamWindow(GameMode mode, int alphaDepth)
|
||||||
|
+onBoardUpdated(): void
|
||||||
|
-maybePlayBotTurn(): void
|
||||||
|
-computeScore(Color c): int
|
||||||
|
-turnMessage(): String
|
||||||
|
}
|
||||||
|
|
||||||
|
AvalamWindow *-- AvalamBoard
|
||||||
|
AvalamWindow *-- BoardView
|
||||||
|
AvalamWindow *-- ScoreView
|
||||||
|
AvalamWindow *-- TurnView
|
||||||
|
AvalamWindow --> GameMode
|
||||||
|
|
||||||
|
class BoardLoader{
|
||||||
|
+loadFromFile(String resourcePath): Tower[][]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class BoardView{
|
||||||
|
-board: AvalamBoard
|
||||||
|
-backgroundLayer: BackgroundLayer
|
||||||
|
-highlightLayer: HighlightLayer
|
||||||
|
-pieceLayer: PieceLayer
|
||||||
|
-controller: InteractionController
|
||||||
|
-size: int
|
||||||
|
-spacing: int
|
||||||
|
-xBase: int
|
||||||
|
-yBase: int
|
||||||
|
-boardUpdateCallback: Runnable
|
||||||
|
|
||||||
|
+BoardView(AvalamBoard board, Runnable boardUpdateCallback)
|
||||||
|
+getController(): InteractionController
|
||||||
|
+setInteractionEnabled(boolean enabled): void
|
||||||
|
+onBoardUpdated(): void
|
||||||
|
+refresh(): void
|
||||||
|
-boardGrid(): Tower[][]
|
||||||
|
-boardGrid(): Tower[][]
|
||||||
|
}
|
||||||
|
|
||||||
|
BoardView *-- BackgroundLayer
|
||||||
|
BoardView *-- HighlightLayer
|
||||||
|
BoardView *-- PieceLayer
|
||||||
|
BoardView *-- InteractionController
|
||||||
|
BoardView --> AvalamBoard
|
||||||
|
|
||||||
|
class BackgroundLayer{
|
||||||
|
-img: Image
|
||||||
|
+BackgroundLayer(String resourcePath)
|
||||||
|
#paintComponent(Graphics g): void
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Color{
|
||||||
|
-YELLOW(int r, int g, int b)
|
||||||
|
-RED(int r, int g, int b)
|
||||||
|
-swingColor: java.awt.Color
|
||||||
|
+Color(int r, int g, int b)
|
||||||
|
+getSwingColor(): java.awt.Color
|
||||||
|
+toPlayer(): fr.iut_fbleau.GameAPI.Player
|
||||||
|
}
|
||||||
|
|
||||||
|
class GameMode{
|
||||||
|
PVP
|
||||||
|
PVBOT
|
||||||
|
PVALPHA
|
||||||
|
PVGOD
|
||||||
|
}
|
||||||
|
|
||||||
|
class HighlightLayer{
|
||||||
|
-xBase: int
|
||||||
|
-yBase: int
|
||||||
|
-spacing: int
|
||||||
|
-size: int
|
||||||
|
-legalMoves: List<Point>
|
||||||
|
|
||||||
|
+HighlightLayer(int xBase, int yBase, int spacing, int size)
|
||||||
|
+setLegalMoves(List<Point> moves): void
|
||||||
|
#paintComponent(Graphics g): void
|
||||||
|
}
|
||||||
|
|
||||||
|
class InteractionController{
|
||||||
|
-board: AvalamBoard
|
||||||
|
-selectedRow: int
|
||||||
|
-selectedCol: int
|
||||||
|
-legalMoves: List<Point>
|
||||||
|
-view: BoardView
|
||||||
|
|
||||||
|
+InteractionController(AvalamBoard board, BoardView view)
|
||||||
|
+onPieceClicked(int r, int c): void
|
||||||
|
+selectTower(int r, int c): void
|
||||||
|
-clearSelection(): void
|
||||||
|
-computeLegalMoves(): void
|
||||||
|
-tryMove(int r, int c): void
|
||||||
|
}
|
||||||
|
|
||||||
|
InteractionController --> AvalamBoard
|
||||||
|
InteractionController --> BoardView
|
||||||
|
|
||||||
|
class Main{
|
||||||
|
+main(String[] args): void
|
||||||
|
}
|
||||||
|
|
||||||
|
Main ..> AvalamWindow
|
||||||
|
Main ..> GameMode
|
||||||
|
|
||||||
|
class PieceButton{
|
||||||
|
-color: java.awt.Color
|
||||||
|
-height: int
|
||||||
|
-hover: boolean
|
||||||
|
+row: int
|
||||||
|
+col: int
|
||||||
|
|
||||||
|
+PieceButton(java.awt.Color c, int height, int row, int col)
|
||||||
|
#paintComponent(Graphics g): void
|
||||||
|
+contains(int x, int y): boolean
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class PieceLayer{
|
||||||
|
+PieceLayer()
|
||||||
|
+displayGrid(Tower[][] grid, int xBase, int yBase, int spacing, int size, java.util.function.BiConsumer<Integer, Integer> clickCallback): void
|
||||||
|
}
|
||||||
|
|
||||||
|
class ScoreView{
|
||||||
|
-scoreY: JLabel
|
||||||
|
-scoreR: JLabel
|
||||||
|
|
||||||
|
+ScoreView(int y, int r)
|
||||||
|
+updateScores(int y, int r): void
|
||||||
|
}
|
||||||
|
|
||||||
|
class Tower{
|
||||||
|
-height: byte
|
||||||
|
-color: Color
|
||||||
|
|
||||||
|
+Tower(int height, Color color)
|
||||||
|
+createTower(Color color): Tower
|
||||||
|
+getHeight(): int
|
||||||
|
+getColor(): Color
|
||||||
|
+mergeTower(Tower src): void
|
||||||
|
+toString(): String
|
||||||
|
}
|
||||||
|
|
||||||
|
Tower --> Color
|
||||||
|
|
||||||
|
class TurnView{
|
||||||
|
-text: JLabel
|
||||||
|
|
||||||
|
+TurnView(String initial)
|
||||||
|
+setTurn(String s): void
|
||||||
|
}
|
||||||
|
|
||||||
|
BoardLoader ..> Tower
|
||||||
|
PieceLayer ..> Tower
|
||||||
|
PieceButton ..> Tower
|
||||||
27
Diagrammes/Diagramme_Bot.mmd
Normal file
27
Diagrammes/Diagramme_Bot.mmd
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
---
|
||||||
|
title: Bot - Diagramme de classes (complet)
|
||||||
|
---
|
||||||
|
classDiagram
|
||||||
|
|
||||||
|
class AlphaBetaBot{
|
||||||
|
-me: Player
|
||||||
|
-maxDepth: int
|
||||||
|
-rng: Random
|
||||||
|
|
||||||
|
+AlphaBetaBot(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 DivineBot{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class IdiotBot{
|
||||||
|
-rng: Random
|
||||||
|
+IdiotBot(Player p)
|
||||||
|
+giveYourMove(IBoard board): AbstractPly
|
||||||
|
}
|
||||||
73
Diagrammes/Diagramme_GameAPI
Normal file
73
Diagrammes/Diagramme_GameAPI
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
---
|
||||||
|
title: GameAPI - Diagramme de classes (complet)
|
||||||
|
---
|
||||||
|
classDiagram
|
||||||
|
|
||||||
|
class AbstractBoard{
|
||||||
|
<<abstract>>
|
||||||
|
-currentPlayer: Player
|
||||||
|
-history: Deque<AbstractPly>
|
||||||
|
|
||||||
|
+AbstractBoard(Player p, Deque<AbstractPly> h)
|
||||||
|
#setNextPlayer(): void
|
||||||
|
#addPlyToHistory(AbstractPly c): void
|
||||||
|
#removePlyFromHistory(): AbstractPly
|
||||||
|
#getLastPlyFromHistory(): AbstractPly
|
||||||
|
+getCurrentPlayer()
|
||||||
|
+isGameOver(): boolean
|
||||||
|
+getResult(): Result
|
||||||
|
+isLegal(AbstractPly c): boolean
|
||||||
|
+iterator(): Iterator<AbstractPly>
|
||||||
|
+doPly(AbstractPly c): void
|
||||||
|
+undoPly(): void
|
||||||
|
+safeCopy(): IBoard
|
||||||
|
}
|
||||||
|
|
||||||
|
class AbstractGame{
|
||||||
|
<<abstract>>
|
||||||
|
-currentBoard: IBoard
|
||||||
|
-mapPlayers: EnumMap<Player, AbstractGamePlayer>
|
||||||
|
|
||||||
|
+AbstractGame(IBoard b, EnumMap<Player,AbstractGamePlayer> m)
|
||||||
|
+run(): Result
|
||||||
|
}
|
||||||
|
|
||||||
|
class AbstractGamePlayer{
|
||||||
|
<<abstract>>
|
||||||
|
-iAm: Player
|
||||||
|
|
||||||
|
+AbstractGamePlayer(Player p)
|
||||||
|
+giveYourMove(IBoard p): AbstractPly
|
||||||
|
}
|
||||||
|
|
||||||
|
class AbstractPly{
|
||||||
|
<<abstract>>
|
||||||
|
-joueur: Player
|
||||||
|
|
||||||
|
+AbstractPly(Player j)
|
||||||
|
+getPlayer(): Player
|
||||||
|
}
|
||||||
|
|
||||||
|
class IBoard{
|
||||||
|
+getCurrentPlayer(): Player
|
||||||
|
+isGameOver(): boolean
|
||||||
|
+getResult(): Result
|
||||||
|
+isLegal(AbstractPly c): boolean
|
||||||
|
+iterator(): Iterator<AbstractPly>
|
||||||
|
+doPly(AbstractPly c): void
|
||||||
|
+undoPly(): void
|
||||||
|
+safeCopy(): IBoard
|
||||||
|
}
|
||||||
|
|
||||||
|
class Player{
|
||||||
|
<<enumerate>>
|
||||||
|
PLAYER1
|
||||||
|
PLAYER2
|
||||||
|
}
|
||||||
|
|
||||||
|
class Result{
|
||||||
|
<<enumerate>>
|
||||||
|
WIN
|
||||||
|
DRAW
|
||||||
|
LOSS
|
||||||
|
}
|
||||||
32
Makefile
32
Makefile
@@ -1,35 +1,61 @@
|
|||||||
|
# === Environnements ===
|
||||||
|
TEST_ENV = "bin:/usr/share/java/junit.jar:/usr/share/java/hamcrest-core.jar"
|
||||||
|
|
||||||
# === Répertoires ===
|
# === Répertoires ===
|
||||||
SRC_DIR = fr
|
SRC_DIR = fr
|
||||||
BIN_DIR = bin
|
BIN_DIR = bin
|
||||||
|
|
||||||
|
# === Répertoires des ressources ===
|
||||||
|
RES_SRC = fr/iut_fbleau/Res
|
||||||
|
RES_BIN = bin/fr/iut_fbleau/Res
|
||||||
|
|
||||||
# === Recherche automatique des fichiers .java dans tous les sous-dossiers ===
|
# === Recherche automatique des fichiers .java dans tous les sous-dossiers ===
|
||||||
SOURCES := $(shell find $(SRC_DIR) -name "*.java")
|
SOURCES := $(shell find $(SRC_DIR) -name "*.java")
|
||||||
|
|
||||||
# === Classe principale ===
|
# === Classe principale ===
|
||||||
MAIN_CLASS = fr.iut_fbleau.Avalam.Main
|
MAIN_CLASS = fr.iut_fbleau.Avalam.Main
|
||||||
|
|
||||||
|
# === Classe de test ===
|
||||||
|
TEST_CLASS = fr.iut_fbleau.Tests.AvalamBoardTest
|
||||||
|
|
||||||
# === Commandes Java ===
|
# === Commandes Java ===
|
||||||
JC = javac
|
JC = javac
|
||||||
JCFLAGS = -d $(BIN_DIR)
|
JCFLAGS = -d $(BIN_DIR) -cp $(TEST_ENV)
|
||||||
|
|
||||||
JAVA = java
|
JAVA = java
|
||||||
JAVAFLAGS = -cp $(BIN_DIR)
|
JAVAFLAGS = -cp $(BIN_DIR)
|
||||||
|
JAVAFLAGS_TESTS = -cp $(TEST_ENV)
|
||||||
|
|
||||||
# === Règle par défaut ===
|
# === Règle par défaut ===
|
||||||
all: build
|
all: build
|
||||||
|
|
||||||
# === Compilation ===
|
# === Compilation ===
|
||||||
build:
|
build: compile resources
|
||||||
|
@echo "✔ Compilation terminée."
|
||||||
|
|
||||||
|
compile:
|
||||||
@echo "===> Compilation du projet Avalam..."
|
@echo "===> Compilation du projet Avalam..."
|
||||||
@mkdir -p $(BIN_DIR)
|
@mkdir -p $(BIN_DIR)
|
||||||
@$(JC) $(JCFLAGS) $(SOURCES)
|
@$(JC) $(JCFLAGS) $(SOURCES)
|
||||||
@echo "✔ Compilation terminée."
|
|
||||||
|
# === Copie des ressources (.txt) dans bin ===
|
||||||
|
resources:
|
||||||
|
@echo "===> Copie des ressources..."
|
||||||
|
@mkdir -p $(RES_BIN)
|
||||||
|
@cp $(RES_SRC)/* $(RES_BIN)/
|
||||||
|
@echo "✔ Ressources copiées."
|
||||||
|
|
||||||
# === Exécution ===
|
# === Exécution ===
|
||||||
run:
|
run:
|
||||||
@echo "===> Lancement du jeu Avalam..."
|
@echo "===> Lancement du jeu Avalam..."
|
||||||
@$(JAVA) $(JAVAFLAGS) $(MAIN_CLASS)
|
@$(JAVA) $(JAVAFLAGS) $(MAIN_CLASS)
|
||||||
|
|
||||||
|
# === Tests ===
|
||||||
|
test:
|
||||||
|
@echo "===> Lancement des tests..."
|
||||||
|
@$(JAVA) $(JAVAFLAGS_TESTS) org.junit.runner.JUnitCore $(TEST_CLASS)
|
||||||
|
@echo "... Fin des tests."
|
||||||
|
|
||||||
# === Nettoyage ===
|
# === Nettoyage ===
|
||||||
clean:
|
clean:
|
||||||
@echo "===> Suppression des fichiers compilés..."
|
@echo "===> Suppression des fichiers compilés..."
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -4,126 +4,101 @@ import fr.iut_fbleau.GameAPI.AbstractPly;
|
|||||||
import fr.iut_fbleau.GameAPI.Player;
|
import fr.iut_fbleau.GameAPI.Player;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Représente un coup dans le jeu Avalam.
|
* La classe <code>AvalamPly</code>
|
||||||
* Un coup consiste à déplacer une tour de la position (xFrom, yFrom)
|
*
|
||||||
* vers la position (xTo, yTo).
|
* Représente un coup (ply) dans le jeu Avalam.
|
||||||
*
|
* Un coup est défini par :
|
||||||
* @author AMARY Aurelien, DICK Adrien, FELIX-VIMALARATNAM Patrick, RABAN Hugo
|
* - un joueur (PLAYER1 ou PLAYER2)
|
||||||
* @version 1.0
|
* - une position source (xFrom, yFrom)
|
||||||
*/
|
* - une position destination (xTo, yTo)
|
||||||
|
*
|
||||||
|
* Cette classe ne vérifie pas la légalité : tout est délégué à <code>AvalamBoard</code>.
|
||||||
|
*/
|
||||||
public class AvalamPly extends AbstractPly {
|
public class AvalamPly extends AbstractPly {
|
||||||
/** Coordonnée X de la case source */
|
|
||||||
private int xFrom;
|
//Attributs
|
||||||
/** Coordonnée Y de la case source */
|
|
||||||
private int yFrom;
|
/** Coordonnée ligne de la case source. */
|
||||||
/** Coordonnée X de la case destination */
|
private final int xFrom;
|
||||||
private int xTo;
|
|
||||||
/** Coordonnée Y de la case destination */
|
/** Coordonnée colonne de la case source. */
|
||||||
private int yTo;
|
private final int yFrom;
|
||||||
/** Hauteur de la tour source (utilisée pour l'annulation de coup) */
|
|
||||||
private int sourceHeight;
|
/** Coordonnée ligne de la case destination. */
|
||||||
|
private final int xTo;
|
||||||
|
|
||||||
|
/** Coordonnée colonne de la case destination. */
|
||||||
|
private final int yTo;
|
||||||
|
|
||||||
|
//Constructeur
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructeur par défaut d'un coup Avalam.
|
* Construit un coup Avalam.
|
||||||
* La hauteur de la source sera définie automatiquement lors de l'application du coup.
|
*
|
||||||
*
|
* @param player joueur qui joue le coup
|
||||||
* @param joueur Le joueur qui effectue le coup
|
* @param xFrom ligne d'origine
|
||||||
* @param xFrom Coordonnée X de la case source
|
* @param yFrom colonne d'origine
|
||||||
* @param yFrom Coordonnée Y de la case source
|
* @param xTo ligne de destination
|
||||||
* @param xTo Coordonnée X de la case destination
|
* @param yTo colonne de destination
|
||||||
* @param yTo Coordonnée Y de la case destination
|
*/
|
||||||
*/
|
public AvalamPly(Player player, int xFrom, int yFrom, int xTo, int yTo) {
|
||||||
public AvalamPly(Player joueur, int xFrom, int yFrom, int xTo, int yTo) {
|
super(player);
|
||||||
super(joueur);
|
|
||||||
this.xFrom = xFrom;
|
this.xFrom = xFrom;
|
||||||
this.yFrom = yFrom;
|
this.yFrom = yFrom;
|
||||||
this.xTo = xTo;
|
this.xTo = xTo;
|
||||||
this.yTo = yTo;
|
this.yTo = yTo;
|
||||||
this.sourceHeight = -1; // Sera défini lors du doPly
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//Méthodes
|
||||||
* Constructeur avec hauteur de la source spécifiée.
|
|
||||||
* Utilisé principalement pour l'annulation de coups.
|
|
||||||
*
|
|
||||||
* @param joueur Le joueur qui effectue le coup
|
|
||||||
* @param xFrom Coordonnée X de la case source
|
|
||||||
* @param yFrom Coordonnée Y de la case source
|
|
||||||
* @param xTo Coordonnée X de la case destination
|
|
||||||
* @param yTo Coordonnée Y de la case destination
|
|
||||||
* @param sourceHeight Hauteur de la tour source avant le déplacement
|
|
||||||
*/
|
|
||||||
public AvalamPly(Player joueur, int xFrom, int yFrom, int xTo, int yTo, int sourceHeight) {
|
|
||||||
super(joueur);
|
|
||||||
this.xFrom = xFrom;
|
|
||||||
this.yFrom = yFrom;
|
|
||||||
this.xTo = xTo;
|
|
||||||
this.yTo = yTo;
|
|
||||||
this.sourceHeight = sourceHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retourne la coordonnée X de la case source.
|
* Retourne la ligne d'origine.
|
||||||
*
|
*
|
||||||
* @return La coordonnée X de la case source
|
* @return ligne source
|
||||||
*/
|
*/
|
||||||
public int getXFrom() {
|
public int getXFrom() {
|
||||||
return xFrom;
|
return xFrom;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retourne la coordonnée Y de la case source.
|
* Retourne la colonne d'origine.
|
||||||
*
|
*
|
||||||
* @return La coordonnée Y de la case source
|
* @return colonne source
|
||||||
*/
|
*/
|
||||||
public int getYFrom() {
|
public int getYFrom() {
|
||||||
return yFrom;
|
return yFrom;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retourne la coordonnée X de la case destination.
|
* Retourne la ligne de destination.
|
||||||
*
|
*
|
||||||
* @return La coordonnée X de la case destination
|
* @return ligne destination
|
||||||
*/
|
*/
|
||||||
public int getXTo() {
|
public int getXTo() {
|
||||||
return xTo;
|
return xTo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retourne la coordonnée Y de la case destination.
|
* Retourne la colonne de destination.
|
||||||
*
|
*
|
||||||
* @return La coordonnée Y de la case destination
|
* @return colonne destination
|
||||||
*/
|
*/
|
||||||
public int getYTo() {
|
public int getYTo() {
|
||||||
return yTo;
|
return yTo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//Affichage
|
||||||
* Retourne la hauteur de la tour source.
|
|
||||||
*
|
|
||||||
* @return La hauteur de la tour source, ou -1 si non définie
|
|
||||||
*/
|
|
||||||
public int getSourceHeight() {
|
|
||||||
return sourceHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Définit la hauteur de la tour source.
|
* Retourne une représentation textuelle du coup.
|
||||||
* Cette méthode est appelée automatiquement lors de l'application du coup.
|
*
|
||||||
*
|
* @return chaîne décrivant le coup
|
||||||
* @param height La hauteur de la tour source
|
*/
|
||||||
*/
|
|
||||||
public void setSourceHeight(int height) {
|
|
||||||
this.sourceHeight = height;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "AvalamPly{" +
|
return "AvalamPly{" +
|
||||||
"joueur=" + getPlayer() +
|
"player=" + getPlayer() +
|
||||||
", from=(" + xFrom + "," + yFrom + ")" +
|
", (" + xFrom + "," + yFrom + ") -> (" + xTo + "," + yTo + ")" +
|
||||||
", to=(" + xTo + "," + yTo + ")" +
|
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,42 +1,319 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
import fr.iut_fbleau.Avalam.logic.*;
|
import fr.iut_fbleau.Bot.AlphaBetaBot;
|
||||||
import fr.iut_fbleau.Avalam.ui.*;
|
// A FAIRE PLUS TARD (PVGOD)
|
||||||
|
// import fr.iut_fbleau.Bot.DivineBot;
|
||||||
|
import fr.iut_fbleau.Bot.IdiotBot;
|
||||||
|
import fr.iut_fbleau.GameAPI.AbstractPly;
|
||||||
|
import fr.iut_fbleau.GameAPI.Player;
|
||||||
|
import fr.iut_fbleau.GameAPI.Result;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fenêtre principale du jeu Avalam.
|
* 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)
|
||||||
|
* - joueur vs bot divin (PVGOD)
|
||||||
|
*
|
||||||
|
* @version 1.0
|
||||||
|
* Date :
|
||||||
|
* Licence :
|
||||||
*/
|
*/
|
||||||
public class AvalamWindow extends JFrame {
|
public class AvalamWindow extends JFrame {
|
||||||
|
|
||||||
|
//Attributs
|
||||||
|
|
||||||
|
/** Moteur du jeu (état + règles). */
|
||||||
|
private AvalamBoard board;
|
||||||
|
|
||||||
|
/** Vue affichant le score. */
|
||||||
|
private ScoreView scoreView;
|
||||||
|
|
||||||
|
/** Vue affichant le joueur courant. */
|
||||||
|
private TurnView turnView;
|
||||||
|
|
||||||
|
/** Vue affichant le plateau. */
|
||||||
|
private BoardView boardView;
|
||||||
|
|
||||||
|
/** Mode de jeu sélectionné. */
|
||||||
|
private final GameMode mode;
|
||||||
|
|
||||||
|
/** Joueur contrôlé par le bot (si actif). */
|
||||||
|
private final Player botPlayer = Player.PLAYER2;
|
||||||
|
|
||||||
|
/** Bot idiot (utilisé si mode PVBOT). */
|
||||||
|
private final IdiotBot idiotBot;
|
||||||
|
|
||||||
|
/** Bot Alpha-Beta (utilisé si mode PVALPHA). */
|
||||||
|
private final AlphaBetaBot alphaBot;
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
*/
|
||||||
|
private final Object divineBot = null;
|
||||||
|
|
||||||
|
/** Indique si une animation de tour de bot est en cours. */
|
||||||
|
private boolean botAnimating = false;
|
||||||
|
|
||||||
|
//Constructeur
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit la fenêtre en mode joueur vs joueur.
|
||||||
|
*/
|
||||||
public AvalamWindow() {
|
public AvalamWindow() {
|
||||||
super("Avalam - Plateau Graphique");
|
this(GameMode.PVP, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit la fenêtre en fonction du mode choisi.
|
||||||
|
* Pour PVALPHA/PVGOD, la profondeur par défaut est 4.
|
||||||
|
*
|
||||||
|
* @param mode mode de jeu
|
||||||
|
*/
|
||||||
|
public AvalamWindow(GameMode mode) {
|
||||||
|
this(mode, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit la fenêtre en fonction du mode choisi.
|
||||||
|
* Si le mode est PVALPHA ou PVGOD, la profondeur est utilisée comme cut-off.
|
||||||
|
*
|
||||||
|
* @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.idiotBot = (mode == GameMode.PVBOT) ? new IdiotBot(botPlayer) : null;
|
||||||
|
|
||||||
|
int depth = Math.max(1, alphaDepth);
|
||||||
|
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;
|
||||||
|
|
||||||
setSize(750, 800);
|
|
||||||
setLocationRelativeTo(null);
|
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
Tower[][] grid = BoardLoader.loadFromFile("fr/iut_fbleau/Res/Plateau.txt");
|
// Chargement du plateau initial
|
||||||
GameState gs = new GameState(grid);
|
Tower[][] initialGrid = BoardLoader.loadFromFile("fr/iut_fbleau/Res/Plateau.txt");
|
||||||
ScoreManager sm = new ScoreManager();
|
|
||||||
|
|
||||||
int y = sm.count(Color.COLOR1, grid);
|
// Initialisation du moteur (PLAYER1 commence)
|
||||||
int r = sm.count(Color.COLOR2, grid);
|
board = new AvalamBoard(initialGrid);
|
||||||
|
|
||||||
ScoreView scoreView = new ScoreView(y, r);
|
// Panneau supérieur (score + tour)
|
||||||
TurnView turnView = new TurnView("Tour du joueur : Jaune");
|
JPanel topPanel = new JPanel(new GridLayout(2, 1));
|
||||||
|
topPanel.setBackground(new java.awt.Color(200, 200, 200));
|
||||||
|
|
||||||
BoardView boardView = new BoardView(gs);
|
scoreView = new ScoreView(
|
||||||
|
computeScore(Color.YELLOW),
|
||||||
|
computeScore(Color.RED)
|
||||||
|
);
|
||||||
|
|
||||||
JPanel top = new JPanel(new GridLayout(2,1));
|
turnView = new TurnView(turnMessage());
|
||||||
top.add(scoreView);
|
|
||||||
top.add(turnView);
|
|
||||||
|
|
||||||
add(top, BorderLayout.NORTH);
|
topPanel.add(scoreView);
|
||||||
|
topPanel.add(turnView);
|
||||||
|
|
||||||
|
add(topPanel, BorderLayout.NORTH);
|
||||||
|
|
||||||
|
// Plateau
|
||||||
|
boardView = new BoardView(board, this::onBoardUpdated);
|
||||||
add(boardView, BorderLayout.CENTER);
|
add(boardView, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
pack();
|
||||||
|
setResizable(false);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
|
|
||||||
|
// Si un jour le bot devait commencer (pas le cas ici), on le ferait jouer ici.
|
||||||
|
maybePlayBotTurn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appelée après chaque coup (humain ou bot).
|
||||||
|
* Met à jour score, tour, et affiche la fin de partie.
|
||||||
|
*/
|
||||||
|
public void onBoardUpdated() {
|
||||||
|
|
||||||
|
scoreView.updateScores(
|
||||||
|
computeScore(Color.YELLOW),
|
||||||
|
computeScore(Color.RED)
|
||||||
|
);
|
||||||
|
|
||||||
|
turnView.setTurn(turnMessage());
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
JOptionPane.showMessageDialog(
|
||||||
|
this,
|
||||||
|
msg,
|
||||||
|
"Partie terminée",
|
||||||
|
JOptionPane.INFORMATION_MESSAGE
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si on est contre un bot et que c’est son tour, on déclenche son animation.
|
||||||
|
maybePlayBotTurn();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fait jouer le bot (idiot / alpha / divin) en deux étapes visibles :
|
||||||
|
* 1) sélection de la tour (affiche les coups légaux)
|
||||||
|
* 2) attente 1 seconde
|
||||||
|
* 3) déplacement vers la destination
|
||||||
|
*
|
||||||
|
* Le tout sans bloquer l'interface (Timer Swing).
|
||||||
|
*/
|
||||||
|
private void maybePlayBotTurn() {
|
||||||
|
|
||||||
|
// Mode joueur vs joueur : aucun bot
|
||||||
|
if (mode == GameMode.PVP) return;
|
||||||
|
|
||||||
|
// Sécurité
|
||||||
|
if (board.isGameOver()) return;
|
||||||
|
if (board.getCurrentPlayer() != botPlayer) return;
|
||||||
|
if (botAnimating) return;
|
||||||
|
|
||||||
|
// Vérifier qu'on a bien le bot correspondant
|
||||||
|
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.
|
||||||
|
if (mode == GameMode.PVGOD) return;
|
||||||
|
|
||||||
|
botAnimating = true;
|
||||||
|
|
||||||
|
// Désactiver les interactions du joueur pendant le tour du bot.
|
||||||
|
boardView.setInteractionEnabled(false);
|
||||||
|
|
||||||
|
// Choix d’un coup sur une copie sûre
|
||||||
|
AbstractPly botMove;
|
||||||
|
if (mode == GameMode.PVBOT) {
|
||||||
|
botMove = idiotBot.giveYourMove(board.safeCopy());
|
||||||
|
} else if (mode == GameMode.PVALPHA) {
|
||||||
|
botMove = alphaBot.giveYourMove(board.safeCopy());
|
||||||
|
} else {
|
||||||
|
// A FAIRE PLUS TARD (PVGOD)
|
||||||
|
// botMove = divineBot.giveYourMove(board.safeCopy());
|
||||||
|
botMove = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (botMove == null) {
|
||||||
|
botAnimating = false;
|
||||||
|
boardView.setInteractionEnabled(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(botMove instanceof AvalamPly)) {
|
||||||
|
botAnimating = false;
|
||||||
|
boardView.setInteractionEnabled(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
AvalamPly ap = (AvalamPly) botMove;
|
||||||
|
|
||||||
|
// Étape 1 : sélection (comme un clic humain)
|
||||||
|
InteractionController ctrl = boardView.getController();
|
||||||
|
ctrl.onPieceClicked(ap.getXFrom(), ap.getYFrom());
|
||||||
|
boardView.refresh();
|
||||||
|
|
||||||
|
// Étape 2 : attendre puis cliquer la destination
|
||||||
|
javax.swing.Timer t = new javax.swing.Timer(1000, e -> {
|
||||||
|
|
||||||
|
// Sécurité : si la partie a changé entre temps
|
||||||
|
if (board.isGameOver() || board.getCurrentPlayer() != botPlayer) {
|
||||||
|
botAnimating = false;
|
||||||
|
boardView.setInteractionEnabled(true);
|
||||||
|
((javax.swing.Timer) e.getSource()).stop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrl.onPieceClicked(ap.getXTo(), ap.getYTo());
|
||||||
|
boardView.refresh();
|
||||||
|
|
||||||
|
botAnimating = false;
|
||||||
|
boardView.setInteractionEnabled(true);
|
||||||
|
|
||||||
|
((javax.swing.Timer) e.getSource()).stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
t.setRepeats(false);
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calcule le score d'une couleur : nombre de tours contrôlées.
|
||||||
|
*
|
||||||
|
* @param c couleur à compter
|
||||||
|
* @return nombre de tours appartenant à la couleur c
|
||||||
|
*/
|
||||||
|
private int computeScore(Color c) {
|
||||||
|
int score = 0;
|
||||||
|
for (int r = 0; r < AvalamBoard.SIZE; r++) {
|
||||||
|
for (int col = 0; col < AvalamBoard.SIZE; col++) {
|
||||||
|
Tower t = board.getTowerAt(r, col);
|
||||||
|
if (t != null && t.getColor() == c) {
|
||||||
|
score++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne le message affiché pour le joueur courant.
|
||||||
|
*
|
||||||
|
* @return message du tour
|
||||||
|
*/
|
||||||
|
private String turnMessage() {
|
||||||
|
return "Tour du joueur : " +
|
||||||
|
(board.getCurrentPlayer() == Player.PLAYER1 ? "Jaune" : "Rouge");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Affichage
|
||||||
}
|
}
|
||||||
|
|||||||
37
fr/iut_fbleau/Avalam/BackgroundLayer.java
Normal file
37
fr/iut_fbleau/Avalam/BackgroundLayer.java
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La classe <code>BackgroundLayer</code>
|
||||||
|
*
|
||||||
|
* Sous composant de BoardView affichant l’image de fond.
|
||||||
|
*/
|
||||||
|
public class BackgroundLayer extends JComponent {
|
||||||
|
private Image img;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit une couche de fond.
|
||||||
|
*
|
||||||
|
* @param resourcePath chemin de l'image de fond
|
||||||
|
*/
|
||||||
|
public BackgroundLayer(String resourcePath) {
|
||||||
|
img = Toolkit.getDefaultToolkit().getImage(
|
||||||
|
getClass().getClassLoader().getResource(resourcePath)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dessine l'image de fond.
|
||||||
|
*
|
||||||
|
* @param g contexte graphique
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
if (img != null) {
|
||||||
|
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
84
fr/iut_fbleau/Avalam/BoardLoader.java
Normal file
84
fr/iut_fbleau/Avalam/BoardLoader.java
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.Avalam.Color;
|
||||||
|
import fr.iut_fbleau.Avalam.Tower;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La classe <code>BoardLoader</code>
|
||||||
|
*
|
||||||
|
* Permet de charger un plateau Avalam à partir d’un fichier texte.
|
||||||
|
* Le fichier contient une grille 9x9 de valeurs numériques :
|
||||||
|
* - 0 : case vide
|
||||||
|
* - 1 : tour jaune
|
||||||
|
* - 2 : tour rouge
|
||||||
|
*
|
||||||
|
* Cette classe fournit une méthode statique pour construire
|
||||||
|
* une grille de tours (<code>Tower[][]</code>) à partir d’une ressource.
|
||||||
|
*/
|
||||||
|
public class BoardLoader {
|
||||||
|
|
||||||
|
//Attributs
|
||||||
|
|
||||||
|
//Constructeur
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Charge un plateau Avalam depuis un fichier de ressources.
|
||||||
|
*
|
||||||
|
* @param resourcePath chemin du fichier de plateau (dans les ressources)
|
||||||
|
* @return une grille 9x9 de tours (Tower) ou null pour les cases vides
|
||||||
|
*/
|
||||||
|
public static Tower[][] loadFromFile(String resourcePath) {
|
||||||
|
|
||||||
|
Tower[][] grid = new Tower[9][9];
|
||||||
|
|
||||||
|
InputStream in = BoardLoader.class.getResourceAsStream("/" + resourcePath);
|
||||||
|
|
||||||
|
if (in == null) {
|
||||||
|
System.err.println("Ressource introuvable : /" + resourcePath);
|
||||||
|
return grid;
|
||||||
|
}
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
|
||||||
|
|
||||||
|
String line;
|
||||||
|
int row = 0;
|
||||||
|
|
||||||
|
while ((line = reader.readLine()) != null && row < 9) {
|
||||||
|
|
||||||
|
// Accepte SOIT les espaces, SOIT les virgules
|
||||||
|
line = line.replace(",", " ");
|
||||||
|
String[] parts = line.trim().split("\\s+");
|
||||||
|
|
||||||
|
for (int col = 0; col < 9; col++) {
|
||||||
|
int value = Integer.parseInt(parts[col]);
|
||||||
|
|
||||||
|
switch (value) {
|
||||||
|
case 1:
|
||||||
|
grid[row][col] = Tower.createTower(Color.YELLOW);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
grid[row][col] = Tower.createTower(Color.RED);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
grid[row][col] = null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
row++;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return grid;
|
||||||
|
}
|
||||||
|
}
|
||||||
159
fr/iut_fbleau/Avalam/BoardView.java
Normal file
159
fr/iut_fbleau/Avalam/BoardView.java
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La classe <code>BoardView</code>
|
||||||
|
*
|
||||||
|
* Représente la vue principale du plateau Avalam.
|
||||||
|
* Elle gère :
|
||||||
|
* - l’affichage des tours (PieceLayer)
|
||||||
|
* - l’affichage des coups possibles (HighlightLayer)
|
||||||
|
* - l’affichage du fond graphique (BackgroundLayer)
|
||||||
|
* - les clics via InteractionController
|
||||||
|
*
|
||||||
|
* Cette classe ne contient aucune logique de règles du jeu.
|
||||||
|
*
|
||||||
|
* @version 1.0
|
||||||
|
* Date :
|
||||||
|
* Licence :
|
||||||
|
*/
|
||||||
|
public class BoardView extends JLayeredPane {
|
||||||
|
|
||||||
|
//Attributs
|
||||||
|
|
||||||
|
/** Référence au moteur Avalam. */
|
||||||
|
private AvalamBoard board;
|
||||||
|
|
||||||
|
/** Couche d’affichage du fond. */
|
||||||
|
private BackgroundLayer backgroundLayer;
|
||||||
|
|
||||||
|
/** Couche d’affichage des coups possibles. */
|
||||||
|
private HighlightLayer highlightLayer;
|
||||||
|
|
||||||
|
/** Couche d’affichage des pièces. */
|
||||||
|
private PieceLayer pieceLayer;
|
||||||
|
|
||||||
|
/** Contrôleur des interactions. */
|
||||||
|
private InteractionController controller;
|
||||||
|
|
||||||
|
/** Taille d’un pion en pixels. */
|
||||||
|
private final int size = 50;
|
||||||
|
|
||||||
|
/** Espacement entre les cases. */
|
||||||
|
private final int spacing = 70;
|
||||||
|
|
||||||
|
/** Décalage horizontal du plateau. */
|
||||||
|
private final int xBase = 60;
|
||||||
|
|
||||||
|
/** Décalage vertical du plateau. */
|
||||||
|
private final int yBase = 60;
|
||||||
|
|
||||||
|
/** Callback vers AvalamWindow pour mise à jour (score, tour, fin). */
|
||||||
|
private Runnable boardUpdateCallback;
|
||||||
|
|
||||||
|
//Constructeur
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit la vue du plateau.
|
||||||
|
*
|
||||||
|
* @param board moteur du jeu
|
||||||
|
* @param boardUpdateCallback callback à appeler après un coup
|
||||||
|
*/
|
||||||
|
public BoardView(AvalamBoard board, Runnable boardUpdateCallback) {
|
||||||
|
this.board = board;
|
||||||
|
this.boardUpdateCallback = boardUpdateCallback;
|
||||||
|
|
||||||
|
setLayout(null);
|
||||||
|
|
||||||
|
// Contrôleur
|
||||||
|
this.controller = new InteractionController(board, this);
|
||||||
|
|
||||||
|
// Couche fond
|
||||||
|
backgroundLayer = new BackgroundLayer("fr/iut_fbleau/Res/BackgroundAvalam.png");
|
||||||
|
backgroundLayer.setBounds(0, 0, 725, 725);
|
||||||
|
add(backgroundLayer, JLayeredPane.FRAME_CONTENT_LAYER);
|
||||||
|
|
||||||
|
// Couche highlight
|
||||||
|
highlightLayer = new HighlightLayer(xBase, yBase, spacing, size);
|
||||||
|
add(highlightLayer, JLayeredPane.DEFAULT_LAYER);
|
||||||
|
|
||||||
|
// Couche pièces
|
||||||
|
pieceLayer = new PieceLayer();
|
||||||
|
add(pieceLayer, JLayeredPane.PALETTE_LAYER);
|
||||||
|
|
||||||
|
setPreferredSize(new Dimension(725, 725));
|
||||||
|
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne le contrôleur d'interactions (utile pour simuler les clics du bot).
|
||||||
|
*
|
||||||
|
* @return contrôleur
|
||||||
|
*/
|
||||||
|
public InteractionController getController() {
|
||||||
|
return controller;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Active/désactive les interactions utilisateur sur le plateau.
|
||||||
|
* Utile pendant l'animation du bot pour éviter des clics concurrents.
|
||||||
|
*
|
||||||
|
* @param enabled true pour activer, false pour désactiver
|
||||||
|
*/
|
||||||
|
public void setInteractionEnabled(boolean enabled) {
|
||||||
|
// Désactive la couche des pièces (boutons) principalement
|
||||||
|
pieceLayer.setEnabled(enabled);
|
||||||
|
for (Component c : pieceLayer.getComponents()) {
|
||||||
|
c.setEnabled(enabled);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appelé par le contrôleur après un coup.
|
||||||
|
*/
|
||||||
|
public void onBoardUpdated() {
|
||||||
|
if (boardUpdateCallback != null) {
|
||||||
|
boardUpdateCallback.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rafraîchit les couches visuelles.
|
||||||
|
*/
|
||||||
|
public void refresh() {
|
||||||
|
|
||||||
|
pieceLayer.displayGrid(
|
||||||
|
boardGrid(),
|
||||||
|
xBase, yBase, spacing, size,
|
||||||
|
(r, c) -> controller.onPieceClicked(r, c)
|
||||||
|
);
|
||||||
|
|
||||||
|
highlightLayer.setLegalMoves(controller.getLegalMoves());
|
||||||
|
|
||||||
|
backgroundLayer.repaint();
|
||||||
|
highlightLayer.repaint();
|
||||||
|
pieceLayer.repaint();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Récupère la grille depuis le moteur.
|
||||||
|
*
|
||||||
|
* @return grille 9x9 de tours
|
||||||
|
*/
|
||||||
|
private Tower[][] boardGrid() {
|
||||||
|
Tower[][] grid = new Tower[AvalamBoard.SIZE][AvalamBoard.SIZE];
|
||||||
|
|
||||||
|
for (int r = 0; r < AvalamBoard.SIZE; r++) {
|
||||||
|
for (int c = 0; c < AvalamBoard.SIZE; c++) {
|
||||||
|
grid[r][c] = board.getTowerAt(r, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return grid;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,28 +1,60 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* L'énumération <code>Color</code> représente la couleur du sommet d'une tour Avalam.
|
* L'énumération <code>Color</code>
|
||||||
*
|
*
|
||||||
* COLOR1 : couleur du Joueur Jaune
|
* Représente les deux couleurs utilisées dans Avalam :
|
||||||
* COLOR2 : couleur du Joueur Rouge
|
* - YELLOW (jaune)
|
||||||
*
|
* - RED (rouge)
|
||||||
* Les couleurs Swing associées peuvent être modifiées ici pour changer tout le jeu.
|
*
|
||||||
*
|
* Chaque valeur est associée à une couleur Swing (<code>java.awt.Color</code>)
|
||||||
* @version 3.0
|
* pour l’affichage graphique et peut être convertie en <code>Player</code> (GameAPI).
|
||||||
*/
|
*/
|
||||||
public enum Color {
|
public enum Color {
|
||||||
|
|
||||||
COLOR1(java.awt.Color.YELLOW), // Joueur Jaune
|
//Attributs
|
||||||
COLOR2(java.awt.Color.RED); // Joueur Rouge
|
|
||||||
|
|
||||||
/** Couleur Swing associée */
|
/** Couleur jaune (associée à PLAYER1). */
|
||||||
|
YELLOW(255, 220, 0),
|
||||||
|
|
||||||
|
/** Couleur rouge (associée à PLAYER2). */
|
||||||
|
RED(200, 40, 40);
|
||||||
|
|
||||||
|
/** Couleur Swing utilisée pour l'affichage dans l'interface graphique. */
|
||||||
private final java.awt.Color swingColor;
|
private final java.awt.Color swingColor;
|
||||||
|
|
||||||
Color(java.awt.Color col) {
|
//Constructeur
|
||||||
this.swingColor = col;
|
|
||||||
|
/**
|
||||||
|
* Construit une couleur Avalam en initialisant sa couleur Swing associée.
|
||||||
|
*
|
||||||
|
* @param r composante rouge [0..255]
|
||||||
|
* @param g composante verte [0..255]
|
||||||
|
* @param b composante bleue [0..255]
|
||||||
|
*/
|
||||||
|
Color(int r, int g, int b) {
|
||||||
|
this.swingColor = new java.awt.Color(r, g, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne la couleur Swing associée (pour l'affichage).
|
||||||
|
*
|
||||||
|
* @return java.awt.Color
|
||||||
|
*/
|
||||||
public java.awt.Color getSwingColor() {
|
public java.awt.Color getSwingColor() {
|
||||||
return this.swingColor;
|
return swingColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convertit la couleur Avalam en joueur GameAPI.
|
||||||
|
*
|
||||||
|
* @return PLAYER1 si YELLOW, sinon PLAYER2
|
||||||
|
*/
|
||||||
|
public fr.iut_fbleau.GameAPI.Player toPlayer() {
|
||||||
|
return (this == YELLOW ?
|
||||||
|
fr.iut_fbleau.GameAPI.Player.PLAYER1 :
|
||||||
|
fr.iut_fbleau.GameAPI.Player.PLAYER2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
fr/iut_fbleau/Avalam/GameMode.java
Normal file
11
fr/iut_fbleau/Avalam/GameMode.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mode de jeu au lancement.
|
||||||
|
*/
|
||||||
|
public enum GameMode {
|
||||||
|
PVP, // joueur vs joueur
|
||||||
|
PVBOT, // joueur vs bot idiot
|
||||||
|
PVALPHA, // joueur vs bot alpha
|
||||||
|
PVGOD // joueur vs bot stratégique
|
||||||
|
}
|
||||||
94
fr/iut_fbleau/Avalam/HighlightLayer.java
Normal file
94
fr/iut_fbleau/Avalam/HighlightLayer.java
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La classe <code>HighlightLayer</code>
|
||||||
|
*
|
||||||
|
* Gère l'affichage graphique des cases jouables sur le plateau Avalam.
|
||||||
|
* Les cases autorisées sont représentées par des cercles verts semi-transparents.
|
||||||
|
*
|
||||||
|
* Cette classe ne contient aucune logique de jeu.
|
||||||
|
*/
|
||||||
|
public class HighlightLayer extends JPanel {
|
||||||
|
|
||||||
|
//Attributs
|
||||||
|
|
||||||
|
/** Position X de base du plateau. */
|
||||||
|
private int xBase;
|
||||||
|
|
||||||
|
/** Position Y de base du plateau. */
|
||||||
|
private int yBase;
|
||||||
|
|
||||||
|
/** Espacement entre les cases du plateau. */
|
||||||
|
private int spacing;
|
||||||
|
|
||||||
|
/** Taille d’un pion en pixels. */
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
/** Liste des positions jouables (cases en vert). */
|
||||||
|
private List<Point> legalMoves;
|
||||||
|
|
||||||
|
//Constructeur
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit la couche d'affichage des coups légaux.
|
||||||
|
*
|
||||||
|
* @param xBase position X de base du plateau
|
||||||
|
* @param yBase position Y de base du plateau
|
||||||
|
* @param spacing espacement entre cases
|
||||||
|
* @param size taille des pions
|
||||||
|
*/
|
||||||
|
public HighlightLayer(int xBase, int yBase, int spacing, int size) {
|
||||||
|
this.xBase = xBase;
|
||||||
|
this.yBase = yBase;
|
||||||
|
this.spacing = spacing;
|
||||||
|
this.size = size;
|
||||||
|
|
||||||
|
setOpaque(false);
|
||||||
|
setBounds(0, 0, 800, 800);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Définit la liste des cases légales à afficher.
|
||||||
|
*
|
||||||
|
* @param moves liste des positions jouables
|
||||||
|
*/
|
||||||
|
public void setLegalMoves(List<Point> moves) {
|
||||||
|
this.legalMoves = moves;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Affichage
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dessine les cercles verts autour des cases autorisées.
|
||||||
|
*
|
||||||
|
* @param g contexte graphique
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
|
||||||
|
if (legalMoves == null) return;
|
||||||
|
|
||||||
|
Graphics2D g2 = (Graphics2D) g;
|
||||||
|
g2.setColor(new java.awt.Color(0, 255, 0, 120));
|
||||||
|
|
||||||
|
for (Point p : legalMoves) {
|
||||||
|
|
||||||
|
int r = p.x;
|
||||||
|
int c = p.y;
|
||||||
|
|
||||||
|
int x = xBase + c * spacing;
|
||||||
|
int y = yBase + r * spacing;
|
||||||
|
|
||||||
|
int highlight = size + 20; // Cercle plus grand que le pion
|
||||||
|
g2.fillOval(x - 10, y - 10, highlight, highlight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
188
fr/iut_fbleau/Avalam/InteractionController.java
Normal file
188
fr/iut_fbleau/Avalam/InteractionController.java
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.GameAPI.AbstractPly;
|
||||||
|
import fr.iut_fbleau.GameAPI.Player;
|
||||||
|
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La classe <code>InteractionController</code>
|
||||||
|
*
|
||||||
|
* Gère l'interaction entre l'utilisateur et le moteur du jeu Avalam.
|
||||||
|
* Elle permet :
|
||||||
|
* - la sélection d'une tour
|
||||||
|
* - le calcul des coups légaux
|
||||||
|
* - la validation d'un déplacement
|
||||||
|
* - l'application d'un coup
|
||||||
|
* - la mise à jour de l'affichage via BoardView
|
||||||
|
*
|
||||||
|
* Cette classe ne réalise aucun affichage direct.
|
||||||
|
*/
|
||||||
|
public class InteractionController {
|
||||||
|
|
||||||
|
//Attributs
|
||||||
|
|
||||||
|
/** Référence au moteur du jeu Avalam. */
|
||||||
|
private AvalamBoard board;
|
||||||
|
|
||||||
|
/** Ligne de la tour sélectionnée (-1 si aucune). */
|
||||||
|
private int selectedRow = -1;
|
||||||
|
|
||||||
|
/** Colonne de la tour sélectionnée (-1 si aucune). */
|
||||||
|
private int selectedCol = -1;
|
||||||
|
|
||||||
|
/** Liste des coups légaux (sous forme de points). */
|
||||||
|
private List<Point> legalMoves = new ArrayList<>();
|
||||||
|
|
||||||
|
/** Référence à la vue du plateau pour rafraîchir l'affichage. */
|
||||||
|
private BoardView view;
|
||||||
|
|
||||||
|
//Constructeur
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit le contrôleur d'interactions.
|
||||||
|
*
|
||||||
|
* @param board moteur du jeu Avalam
|
||||||
|
* @param view vue du plateau
|
||||||
|
*/
|
||||||
|
public InteractionController(AvalamBoard board, BoardView view) {
|
||||||
|
this.board = board;
|
||||||
|
this.view = view;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne la liste des cases jouables.
|
||||||
|
*
|
||||||
|
* @return liste des coups légaux
|
||||||
|
*/
|
||||||
|
public List<Point> getLegalMoves() {
|
||||||
|
return legalMoves;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appelé lorsqu’un pion est cliqué dans BoardView.
|
||||||
|
* Gère :
|
||||||
|
* - la sélection d’une tour
|
||||||
|
* - la désélection
|
||||||
|
* - la tentative de déplacement
|
||||||
|
*
|
||||||
|
* @param r ligne cliquée
|
||||||
|
* @param c colonne cliquée
|
||||||
|
*/
|
||||||
|
public void onPieceClicked(int r, int c) {
|
||||||
|
|
||||||
|
// Si on clique la même case ⇒ désélection
|
||||||
|
if (r == selectedRow && c == selectedCol) {
|
||||||
|
clearSelection();
|
||||||
|
view.refresh();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si aucune sélection : on sélectionne un pion appartenant au joueur courant
|
||||||
|
Tower t = board.getTowerAt(r, c);
|
||||||
|
|
||||||
|
if (t != null && t.getColor().toPlayer() == board.getCurrentPlayer()) {
|
||||||
|
selectTower(r, c);
|
||||||
|
view.refresh();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sinon : tentons de jouer un coup (déplacement)
|
||||||
|
if (selectedRow != -1 && selectedCol != -1) {
|
||||||
|
tryMove(r, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sélectionne une tour à la position (r,c).
|
||||||
|
*
|
||||||
|
* @param r ligne
|
||||||
|
* @param c colonne
|
||||||
|
*/
|
||||||
|
private void selectTower(int r, int c) {
|
||||||
|
selectedRow = r;
|
||||||
|
selectedCol = c;
|
||||||
|
computeLegalMoves();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annule la sélection actuelle.
|
||||||
|
*/
|
||||||
|
private void clearSelection() {
|
||||||
|
selectedRow = -1;
|
||||||
|
selectedCol = -1;
|
||||||
|
legalMoves.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calcule les coups légaux à partir de la tour sélectionnée.
|
||||||
|
* Utilise l'itérateur fourni par AvalamBoard.
|
||||||
|
*/
|
||||||
|
private void computeLegalMoves() {
|
||||||
|
legalMoves.clear();
|
||||||
|
|
||||||
|
Iterator<AbstractPly> it = board.iterator();
|
||||||
|
|
||||||
|
while (it.hasNext()) {
|
||||||
|
AbstractPly p = it.next();
|
||||||
|
|
||||||
|
if (!(p instanceof AvalamPly)) continue;
|
||||||
|
AvalamPly ap = (AvalamPly) p;
|
||||||
|
|
||||||
|
// On ne garde que les coups dont la source correspond à la sélection
|
||||||
|
if (ap.getXFrom() == selectedRow && ap.getYFrom() == selectedCol) {
|
||||||
|
legalMoves.add(new Point(ap.getXTo(), ap.getYTo()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tente d’exécuter un déplacement vers la case (r,c).
|
||||||
|
*
|
||||||
|
* @param r ligne de destination
|
||||||
|
* @param c colonne de destination
|
||||||
|
*/
|
||||||
|
private void tryMove(int r, int c) {
|
||||||
|
// Vérifier si (r,c) est une destination légale
|
||||||
|
boolean isLegalDest = false;
|
||||||
|
for (Point p : legalMoves) {
|
||||||
|
if (p.x == r && p.y == c) {
|
||||||
|
isLegalDest = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isLegalDest) {
|
||||||
|
clearSelection(); // clic ailleurs = désélection
|
||||||
|
view.refresh();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Construire le coup
|
||||||
|
Player cur = board.getCurrentPlayer();
|
||||||
|
AvalamPly ply = new AvalamPly(cur, selectedRow, selectedCol, r, c);
|
||||||
|
|
||||||
|
// Vérifier via l’API
|
||||||
|
if (board.isLegal(ply)) {
|
||||||
|
|
||||||
|
// Appliquer via l’API
|
||||||
|
board.doPly(ply);
|
||||||
|
|
||||||
|
// Réinitialiser la sélection
|
||||||
|
clearSelection();
|
||||||
|
|
||||||
|
// Mise à jour de l'interface
|
||||||
|
view.onBoardUpdated();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Coup impossible
|
||||||
|
clearSelection();
|
||||||
|
}
|
||||||
|
|
||||||
|
view.refresh();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,69 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Point d’entrée : propose un menu de sélection de mode, puis lance la fenêtre Avalam.
|
||||||
|
*/
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new AvalamWindow();
|
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
|
||||||
|
String[] options = {
|
||||||
|
"joueur vs joueur",
|
||||||
|
"joueur vs botidiot",
|
||||||
|
"joueur vs bot alpha",
|
||||||
|
"joueur vs bot divin (NON IMPLEMENTE)"
|
||||||
|
};
|
||||||
|
|
||||||
|
int choice = JOptionPane.showOptionDialog(
|
||||||
|
null,
|
||||||
|
"Choisissez un mode de jeu :",
|
||||||
|
"Avalam - Mode de jeu",
|
||||||
|
JOptionPane.DEFAULT_OPTION,
|
||||||
|
JOptionPane.QUESTION_MESSAGE,
|
||||||
|
null,
|
||||||
|
options,
|
||||||
|
options[0]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (choice == -1) System.exit(0);
|
||||||
|
|
||||||
|
GameMode mode;
|
||||||
|
if (choice == 1) mode = GameMode.PVBOT;
|
||||||
|
else if (choice == 2) mode = GameMode.PVALPHA;
|
||||||
|
else if (choice == 3) mode = GameMode.PVGOD;
|
||||||
|
else mode = GameMode.PVP;
|
||||||
|
|
||||||
|
// Pour ALPHA et GOD : demander une profondeur
|
||||||
|
if (mode == GameMode.PVALPHA || mode == GameMode.PVGOD) {
|
||||||
|
|
||||||
|
String s = JOptionPane.showInputDialog(
|
||||||
|
null,
|
||||||
|
"Profondeur de recherche ?\n(Conseil 4)",
|
||||||
|
(mode == GameMode.PVGOD ? "Bot Divin (PVGOD)" : "Bot Alpha-Beta"),
|
||||||
|
JOptionPane.QUESTION_MESSAGE
|
||||||
|
);
|
||||||
|
|
||||||
|
int depth = 4; // défaut
|
||||||
|
if (s != null) {
|
||||||
|
try { depth = Integer.parseInt(s.trim()); }
|
||||||
|
catch (Exception ignored) { depth = 4; }
|
||||||
|
} else {
|
||||||
|
// Annulation : on revient en PVP
|
||||||
|
new AvalamWindow(GameMode.PVP);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (depth < 1) depth = 1;
|
||||||
|
|
||||||
|
new AvalamWindow(mode, depth);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
new AvalamWindow(mode);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +1,47 @@
|
|||||||
package fr.iut_fbleau.Avalam.ui;
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La classe <code>PieceButton</code> représente graphiquement une tour Avalam.
|
* La classe <code>PieceButton</code>
|
||||||
* Il s'agit d'un bouton rond :
|
*
|
||||||
*
|
* Représente graphiquement une tour Avalam sous forme d’un bouton rond.
|
||||||
* <ul>
|
* Chaque bouton :
|
||||||
* <li>coloré selon le joueur</li>
|
* - possède une couleur correspondant au joueur,
|
||||||
* <li>affichant sa hauteur</li>
|
* - affiche la hauteur de la tour,
|
||||||
* <li>avec un effet de survol visuel</li>
|
* - réagit au survol de la souris.
|
||||||
* </ul>
|
*
|
||||||
*
|
* Ce composant permet l’interaction avec les pièces du plateau.
|
||||||
* Ce composant sert d'interaction principale pour cliquer les pions.
|
*/
|
||||||
*
|
|
||||||
* @author
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class PieceButton extends JButton {
|
public class PieceButton extends JButton {
|
||||||
|
|
||||||
private Color color;
|
//Attributs
|
||||||
|
|
||||||
|
/** Couleur graphique du pion. */
|
||||||
|
private java.awt.Color color;
|
||||||
|
|
||||||
|
/** Hauteur de la tour affichée. */
|
||||||
private int height;
|
private int height;
|
||||||
|
|
||||||
|
/** Indique si la souris survole le bouton. */
|
||||||
private boolean hover = false;
|
private boolean hover = false;
|
||||||
|
|
||||||
/** Position de la tour sur la grille. */
|
/** Position de la tour sur la grille (ligne). */
|
||||||
public final int row, col;
|
public final int row;
|
||||||
|
|
||||||
|
/** Position de la tour sur la grille (colonne). */
|
||||||
|
public final int col;
|
||||||
|
|
||||||
|
//Constructeur
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructeur.
|
* Construit un bouton représentant une tour Avalam.
|
||||||
*
|
*
|
||||||
* @param c couleur graphique du pion
|
* @param c couleur graphique du pion
|
||||||
* @param height hauteur de la tour
|
* @param height hauteur de la tour
|
||||||
* @param row ligne du pion
|
* @param row ligne du pion
|
||||||
* @param col colonne du pion
|
* @param col colonne du pion
|
||||||
*/
|
*/
|
||||||
public PieceButton(java.awt.Color c, int height, int row, int col) {
|
public PieceButton(java.awt.Color c, int height, int row, int col) {
|
||||||
this.color = c;
|
this.color = c;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
@@ -52,9 +59,13 @@ public class PieceButton extends JButton {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dessine le pion rond ainsi que son chiffre au centre.
|
* Dessine le pion rond ainsi que sa hauteur au centre.
|
||||||
*/
|
*
|
||||||
|
* @param g contexte graphique
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g) {
|
protected void paintComponent(Graphics g) {
|
||||||
Graphics2D g2 = (Graphics2D) g.create();
|
Graphics2D g2 = (Graphics2D) g.create();
|
||||||
@@ -78,9 +89,15 @@ public class PieceButton extends JButton {
|
|||||||
g2.dispose();
|
g2.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Affichage
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rend le bouton réellement rond (zone cliquable circulaire).
|
* Rend le bouton réellement rond (zone cliquable circulaire).
|
||||||
*/
|
*
|
||||||
|
* @param x coordonnée X
|
||||||
|
* @param y coordonnée Y
|
||||||
|
* @return true si le point est dans le cercle
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(int x, int y) {
|
public boolean contains(int x, int y) {
|
||||||
double dx = x - getWidth()/2.0;
|
double dx = x - getWidth()/2.0;
|
||||||
@@ -1,48 +1,44 @@
|
|||||||
package fr.iut_fbleau.Avalam.ui;
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
import fr.iut_fbleau.Avalam.Tower;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La classe <code>PieceLayer</code> gère l'affichage des pions
|
* La classe <code>PieceLayer</code>
|
||||||
* (sous forme de <code>PieceButton</code>) sur la grille.
|
*
|
||||||
*
|
* Gère l'affichage des tours du plateau Avalam sous forme de boutons.
|
||||||
* Elle s'occupe uniquement :
|
* Elle :
|
||||||
* <ul>
|
* - affiche les pièces,
|
||||||
* <li>d'afficher les pièces</li>
|
* - positionne les boutons sur la grille,
|
||||||
* <li>de positionner correctement les boutons</li>
|
* - associe une action de clic à chaque pièce.
|
||||||
* <li>d'attacher un callback à chaque clic</li>
|
*
|
||||||
* </ul>
|
* Aucune logique de jeu n'est implémentée ici.
|
||||||
*
|
*/
|
||||||
* Aucune logique de jeu n'est réalisée ici.
|
|
||||||
*
|
|
||||||
* @author
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class PieceLayer extends JPanel {
|
public class PieceLayer extends JPanel {
|
||||||
|
|
||||||
|
//Constructeur
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructeur.
|
* Construit la couche graphique des pièces.
|
||||||
* Initialise un panneau transparent prêt à recevoir des pions.
|
*/
|
||||||
*/
|
|
||||||
public PieceLayer() {
|
public PieceLayer() {
|
||||||
setLayout(null);
|
setLayout(null);
|
||||||
setOpaque(false);
|
setOpaque(false);
|
||||||
setBounds(0, 0, 800, 800);
|
setBounds(0, 0, 800, 800);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Affiche la grille de tours sous forme de boutons.
|
* Affiche la grille de tours sous forme de boutons interactifs.
|
||||||
*
|
*
|
||||||
* @param grid grille 9×9 des tours
|
* @param grid grille 9×9 des tours
|
||||||
* @param xBase offset X du plateau
|
* @param xBase décalage horizontal du plateau
|
||||||
* @param yBase offset Y du plateau
|
* @param yBase décalage vertical du plateau
|
||||||
* @param spacing espacement entre cases
|
* @param spacing espacement entre cases
|
||||||
* @param size taille d'un pion
|
* @param size taille d'un pion
|
||||||
* @param clickCallback fonction appelée lors d’un clic sur un pion
|
* @param clickCallback fonction appelée lors d’un clic sur un pion
|
||||||
*/
|
*/
|
||||||
public void displayGrid(Tower[][] grid, int xBase, int yBase,
|
public void displayGrid(Tower[][] grid, int xBase, int yBase,
|
||||||
int spacing, int size,
|
int spacing, int size,
|
||||||
java.util.function.BiConsumer<Integer, Integer> clickCallback) {
|
java.util.function.BiConsumer<Integer, Integer> clickCallback) {
|
||||||
57
fr/iut_fbleau/Avalam/ScoreView.java
Normal file
57
fr/iut_fbleau/Avalam/ScoreView.java
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La classe <code>ScoreView</code>
|
||||||
|
*
|
||||||
|
* Affiche les scores des deux joueurs du jeu Avalam.
|
||||||
|
* Cette classe est purement graphique : elle ne calcule pas les scores.
|
||||||
|
*/
|
||||||
|
public class ScoreView extends JPanel {
|
||||||
|
|
||||||
|
//Attributs
|
||||||
|
|
||||||
|
/** Label affichant le score du joueur jaune. */
|
||||||
|
private JLabel scoreY;
|
||||||
|
|
||||||
|
/** Label affichant le score du joueur rouge. */
|
||||||
|
private JLabel scoreR;
|
||||||
|
|
||||||
|
//Constructeur
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit la vue des scores.
|
||||||
|
*
|
||||||
|
* @param y score initial du joueur jaune
|
||||||
|
* @param r score initial du joueur rouge
|
||||||
|
*/
|
||||||
|
public ScoreView(int y, int r) {
|
||||||
|
setBackground(new java.awt.Color(200,200,200));
|
||||||
|
setLayout(new FlowLayout());
|
||||||
|
|
||||||
|
scoreY = new JLabel("Score Jaune : " + y);
|
||||||
|
scoreR = new JLabel("Score Rouge : " + r);
|
||||||
|
|
||||||
|
scoreY.setFont(new Font("Arial", Font.BOLD, 18));
|
||||||
|
scoreR.setFont(new Font("Arial", Font.BOLD, 18));
|
||||||
|
|
||||||
|
add(scoreY);
|
||||||
|
add(new JLabel(" | "));
|
||||||
|
add(scoreR);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Met à jour l'affichage des scores.
|
||||||
|
*
|
||||||
|
* @param y nouveau score du joueur jaune
|
||||||
|
* @param r nouveau score du joueur rouge
|
||||||
|
*/
|
||||||
|
public void updateScores(int y, int r) {
|
||||||
|
scoreY.setText("Score Jaune : " + y);
|
||||||
|
scoreR.setText("Score Rouge : " + r);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,41 +1,90 @@
|
|||||||
package fr.iut_fbleau.Avalam ;
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La classe <code>Tower</code> stocke la couleur de son pion haut et la hauteur de la tour.
|
* La classe <code>Tower</code>
|
||||||
*
|
*
|
||||||
* @version 1.0
|
* Représente une tour dans le jeu Avalam.
|
||||||
* @author Aurélien
|
* Une tour est caractérisée par :
|
||||||
|
* - une couleur (couleur du sommet)
|
||||||
|
* - une hauteur (nombre de pions empilés)
|
||||||
|
*
|
||||||
|
* Cette version est volontairement compatible avec le reste du projet :
|
||||||
|
* - constructeur Tower(int, Color) utilisé dans d'autres parties possibles
|
||||||
|
* - usine createTower(Color) utilisé par BoardLoader
|
||||||
|
* - méthode mergeTower(Tower) utilisée par AvalamBoard
|
||||||
*/
|
*/
|
||||||
public class Tower {
|
public class Tower {
|
||||||
//Attributs
|
//Attributs
|
||||||
private Color color ;
|
|
||||||
private byte height = 1 ;
|
|
||||||
|
|
||||||
//Constructeur
|
/** Hauteur de la tour (nombre de pions empilés). */
|
||||||
public Tower(Color color) {
|
private byte height;
|
||||||
this.color = color ;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Méthodes
|
/** Couleur du sommet de la tour (propriétaire actuel). */
|
||||||
public Color getColor() {
|
private Color color;
|
||||||
return this.color ;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte getHeight() {
|
//Constructeur
|
||||||
return this.height ;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Méthode qui empile une autre tour sur l'objet sur lequel le méthode est appelée.
|
* Construit une tour avec une hauteur et une couleur données.
|
||||||
* Aucune vérification de hauteur n'est effectuée.
|
*
|
||||||
|
* @param height hauteur initiale
|
||||||
|
* @param color couleur du sommet
|
||||||
*/
|
*/
|
||||||
public void mergeTower(Tower tower) {
|
public Tower(int height, Color color) {
|
||||||
this.color = tower.getColor();
|
this.height = (byte) height;
|
||||||
this.height += tower.getHeight();
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public String toString() {
|
* Construit une tour de hauteur 1 avec la couleur donnée.
|
||||||
return "" ;
|
* (Constructeur attendu par BoardLoader dans le projet.)
|
||||||
}
|
*
|
||||||
|
* @param color couleur du sommet
|
||||||
|
*/
|
||||||
|
public static Tower createTower(Color color) {
|
||||||
|
return new Tower(1, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne la hauteur de la tour.
|
||||||
|
*
|
||||||
|
* @return hauteur
|
||||||
|
*/
|
||||||
|
public int getHeight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne la couleur du sommet.
|
||||||
|
*
|
||||||
|
* @return couleur
|
||||||
|
*/
|
||||||
|
public Color getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fusionne la tour <code>src</code> sur la tour courante (destination).
|
||||||
|
* La couleur du sommet devient celle de <code>src</code>.
|
||||||
|
*
|
||||||
|
* @param src tour source empilée sur la destination
|
||||||
|
*/
|
||||||
|
public void mergeTower(Tower src) {
|
||||||
|
this.height += src.height;
|
||||||
|
this.color = src.color;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Affichage
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Représentation textuelle de la tour.
|
||||||
|
*
|
||||||
|
* @return chaîne représentant la tour
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return color + "(" + height + ")";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
45
fr/iut_fbleau/Avalam/TurnView.java
Normal file
45
fr/iut_fbleau/Avalam/TurnView.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package fr.iut_fbleau.Avalam;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La classe <code>TurnView</code>
|
||||||
|
*
|
||||||
|
* Affiche le joueur dont c'est le tour dans le jeu Avalam.
|
||||||
|
* Cette classe est uniquement graphique.
|
||||||
|
*/
|
||||||
|
public class TurnView extends JPanel {
|
||||||
|
|
||||||
|
//Attributs
|
||||||
|
|
||||||
|
/** Label affichant le joueur courant. */
|
||||||
|
private JLabel text;
|
||||||
|
|
||||||
|
//Constructeur
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit la vue du tour de jeu.
|
||||||
|
*
|
||||||
|
* @param initial message initial à afficher
|
||||||
|
*/
|
||||||
|
public TurnView(String initial) {
|
||||||
|
setBackground(new java.awt.Color(220,220,220));
|
||||||
|
|
||||||
|
text = new JLabel(initial);
|
||||||
|
text.setFont(new Font("Arial", Font.BOLD, 20));
|
||||||
|
|
||||||
|
add(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Met à jour le texte affichant le joueur courant.
|
||||||
|
*
|
||||||
|
* @param s message à afficher
|
||||||
|
*/
|
||||||
|
public void setTurn(String s) {
|
||||||
|
text.setText(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam.logic;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.Avalam.Color;
|
|
||||||
import fr.iut_fbleau.Avalam.Tower;
|
|
||||||
import java.io.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>BoardLoader</code> est responsable du chargement d'un plateau Avalam
|
|
||||||
* depuis un fichier texte externe (généralement Plateau.txt).
|
|
||||||
*
|
|
||||||
* Le fichier doit contenir une matrice 9×9 de valeurs numériques séparées par des virgules :
|
|
||||||
* - 0 : case vide (trou)
|
|
||||||
* - 1 : pion appartenant au joueur COLOR1 (Jaune)
|
|
||||||
* - 2 : pion appartenant au joueur COLOR2 (Rouge)
|
|
||||||
*
|
|
||||||
* Aucun contrôle de cohérence avancé n'est effectué, le chargeur se contente
|
|
||||||
* d’interpréter les valeurs comme demandé.
|
|
||||||
*
|
|
||||||
* @author
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class BoardLoader {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Charge un plateau Avalam depuis un fichier.
|
|
||||||
*
|
|
||||||
* @param path chemin du fichier plateau.
|
|
||||||
* @return un tableau 9×9 contenant des objets <code>Tower</code> ou <code>null</code>.
|
|
||||||
*/
|
|
||||||
public static Tower[][] loadFromFile(String path) {
|
|
||||||
|
|
||||||
Tower[][] grid = new Tower[9][9];
|
|
||||||
|
|
||||||
try (BufferedReader br = new BufferedReader(new FileReader(new File(path)))) {
|
|
||||||
|
|
||||||
String line;
|
|
||||||
int row = 0;
|
|
||||||
|
|
||||||
while ((line = br.readLine()) != null && row < 9) {
|
|
||||||
|
|
||||||
String[] parts = line.split(",");
|
|
||||||
|
|
||||||
for (int col = 0; col < 9; col++) {
|
|
||||||
int v = Integer.parseInt(parts[col]);
|
|
||||||
|
|
||||||
// Interprétation des valeurs
|
|
||||||
if (v == 1) grid[row][col] = new Tower(Color.COLOR1);
|
|
||||||
else if (v == 2) grid[row][col] = new Tower(Color.COLOR2);
|
|
||||||
else grid[row][col] = null; // Case vide
|
|
||||||
}
|
|
||||||
row++;
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return grid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam.logic;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.Avalam.Color;
|
|
||||||
import fr.iut_fbleau.Avalam.Tower;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>GameState</code> représente l'état courant du jeu Avalam :
|
|
||||||
* - la grille 9×9
|
|
||||||
* - le joueur dont c'est le tour
|
|
||||||
*
|
|
||||||
* Elle ne contient aucune logique de déplacement ni de vérification des règles ;
|
|
||||||
* son rôle est uniquement de stocker et de fournir l'état du jeu.
|
|
||||||
*
|
|
||||||
* @author
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class GameState {
|
|
||||||
|
|
||||||
/** Grille du plateau : 9×9 tours ou cases vides. */
|
|
||||||
private Tower[][] grid;
|
|
||||||
|
|
||||||
/** Joueur dont c’est le tour (COLOR1 ou COLOR2). */
|
|
||||||
private Color currentPlayer = Color.COLOR1;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructeur.
|
|
||||||
*
|
|
||||||
* @param grid la grille initiale du plateau.
|
|
||||||
*/
|
|
||||||
public GameState(Tower[][] grid) {
|
|
||||||
this.grid = grid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne la tour présente à la position (r,c), ou null.
|
|
||||||
*/
|
|
||||||
public Tower get(int r, int c) {
|
|
||||||
return grid[r][c];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne la grille complète.
|
|
||||||
*/
|
|
||||||
public Tower[][] getGrid() {
|
|
||||||
return grid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne le joueur dont c'est le tour.
|
|
||||||
*/
|
|
||||||
public Color getCurrentPlayer() {
|
|
||||||
return currentPlayer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Change le joueur courant : COLOR1 → COLOR2 → COLOR1.
|
|
||||||
*/
|
|
||||||
public void switchPlayer() {
|
|
||||||
currentPlayer = (currentPlayer == Color.COLOR1) ? Color.COLOR2 : Color.COLOR1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Vérifie si une position (r,c) est dans les limites du plateau.
|
|
||||||
*
|
|
||||||
* @return vrai si la position est comprise dans un plateau 9×9.
|
|
||||||
*/
|
|
||||||
public boolean isInside(int r, int c) {
|
|
||||||
return r >= 0 && r < 9 && c >= 0 && c < 9;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam.logic;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.Avalam.Color;
|
|
||||||
import fr.iut_fbleau.Avalam.Tower;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>ScoreManager</code> gère le calcul des scores
|
|
||||||
* selon les règles officielles d’Avalam :
|
|
||||||
*
|
|
||||||
* Un joueur gagne 1 point par tour dont le sommet (couleur) lui appartient,
|
|
||||||
* indépendamment de la hauteur de la tour.
|
|
||||||
*
|
|
||||||
* Cette classe n’a aucune responsabilité autre que compter.
|
|
||||||
*
|
|
||||||
* @author
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class ScoreManager {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compte le nombre de tours dont le sommet appartient à la couleur donnée.
|
|
||||||
*
|
|
||||||
* @param c couleur du joueur (COLOR1 ou COLOR2)
|
|
||||||
* @param grid grille 9×9 contenant des tours ou null
|
|
||||||
* @return score du joueur
|
|
||||||
*/
|
|
||||||
public static int count(Color c, Tower[][] grid) {
|
|
||||||
int score = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < 9; i++)
|
|
||||||
for (int j = 0; j < 9; j++)
|
|
||||||
if (grid[i][j] != null && grid[i][j].getColor() == c)
|
|
||||||
score++;
|
|
||||||
|
|
||||||
return score;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam.logic;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.Avalam.Color;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>TurnManager</code> gère le déroulement des tours d’Avalam.
|
|
||||||
*
|
|
||||||
* Son rôle est simple :
|
|
||||||
* - identifier le joueur dont c’est le tour
|
|
||||||
* - passer au joueur suivant
|
|
||||||
*
|
|
||||||
* Elle ne contient pas de logique de mouvement, ni de validation.
|
|
||||||
*
|
|
||||||
* @author
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class TurnManager {
|
|
||||||
|
|
||||||
/** Joueur dont c'est le tour (COLOR1 commence la partie). */
|
|
||||||
private Color current = Color.COLOR1;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne le joueur actuel.
|
|
||||||
*/
|
|
||||||
public Color getPlayer() {
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Passe au joueur suivant.
|
|
||||||
*/
|
|
||||||
public void next() {
|
|
||||||
current = (current == Color.COLOR1) ? Color.COLOR2 : Color.COLOR1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam.ui;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.Avalam.logic.GameState;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>BoardView</code> représente l'affichage complet du plateau Avalam.
|
|
||||||
* Elle s’appuie sur une architecture en couches (layered pane) pour séparer proprement :
|
|
||||||
*
|
|
||||||
* <ul>
|
|
||||||
* <li><b>HighlightLayer</b> : les cases jouables mises en surbrillance</li>
|
|
||||||
* <li><b>PieceLayer</b> : les pions affichés sous forme de boutons ronds</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* La vue ne contient pas la logique de jeu : elle la délègue entièrement
|
|
||||||
* à un <code>InteractionController</code>.
|
|
||||||
*
|
|
||||||
* @author
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class BoardView extends JLayeredPane {
|
|
||||||
|
|
||||||
/** Taille des pions affichés. */
|
|
||||||
private final int size = 50;
|
|
||||||
|
|
||||||
/** Distance entre deux cases de la grille. */
|
|
||||||
private final int spacing = 70;
|
|
||||||
|
|
||||||
/** Décalages pour centrer l'affichage. */
|
|
||||||
private final int xBase = 60, yBase = 60;
|
|
||||||
|
|
||||||
/** Accès à l'état du jeu. */
|
|
||||||
private GameState state;
|
|
||||||
|
|
||||||
/** Couche d'affichage des ronds verts. */
|
|
||||||
private HighlightLayer highlightLayer;
|
|
||||||
|
|
||||||
/** Couche d'affichage des pions. */
|
|
||||||
private PieceLayer pieceLayer;
|
|
||||||
|
|
||||||
/** Gestionnaire d'interactions utilisateur. */
|
|
||||||
private InteractionController controller;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructeur de la vue du plateau.
|
|
||||||
*
|
|
||||||
* @param state l'état du jeu Avalam
|
|
||||||
*/
|
|
||||||
public BoardView(GameState state) {
|
|
||||||
this.state = state;
|
|
||||||
setLayout(null);
|
|
||||||
|
|
||||||
controller = new InteractionController(state);
|
|
||||||
|
|
||||||
highlightLayer = new HighlightLayer(xBase, yBase, spacing, size);
|
|
||||||
pieceLayer = new PieceLayer();
|
|
||||||
|
|
||||||
add(highlightLayer, JLayeredPane.DEFAULT_LAYER);
|
|
||||||
add(pieceLayer, JLayeredPane.PALETTE_LAYER);
|
|
||||||
|
|
||||||
refresh();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Met à jour l'affichage des couches en fonction de l'état actuel du jeu.
|
|
||||||
*/
|
|
||||||
public void refresh() {
|
|
||||||
|
|
||||||
pieceLayer.displayGrid(
|
|
||||||
state.getGrid(),
|
|
||||||
xBase, yBase, spacing, size,
|
|
||||||
|
|
||||||
// Callback appelé lorsqu’un pion est cliqué
|
|
||||||
(r, c) -> {
|
|
||||||
controller.onPieceClicked(r, c);
|
|
||||||
highlightLayer.setLegalMoves(controller.getLegalMoves());
|
|
||||||
highlightLayer.repaint();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
highlightLayer.repaint();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam.ui;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>HighlightLayer</code> est responsable de l'affichage des
|
|
||||||
* cases de déplacement autorisées sous forme de cercles verts.
|
|
||||||
*
|
|
||||||
* Elle n'interagit pas directement avec les pièces, mais se contente
|
|
||||||
* de dessiner en arrière-plan selon une liste de positions légales.
|
|
||||||
*
|
|
||||||
* @author
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class HighlightLayer extends JPanel {
|
|
||||||
|
|
||||||
private int xBase, yBase, spacing, size;
|
|
||||||
|
|
||||||
/** Liste des positions jouables (cases en vert). */
|
|
||||||
private List<Point> legalMoves;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructeur.
|
|
||||||
*
|
|
||||||
* @param xBase position X de base du plateau
|
|
||||||
* @param yBase position Y de base du plateau
|
|
||||||
* @param spacing espacement entre cases
|
|
||||||
* @param size taille des pions
|
|
||||||
*/
|
|
||||||
public HighlightLayer(int xBase, int yBase, int spacing, int size) {
|
|
||||||
this.xBase = xBase;
|
|
||||||
this.yBase = yBase;
|
|
||||||
this.spacing = spacing;
|
|
||||||
this.size = size;
|
|
||||||
|
|
||||||
setOpaque(false);
|
|
||||||
setBounds(0, 0, 800, 800);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Définit la liste des cases légales à afficher.
|
|
||||||
*/
|
|
||||||
public void setLegalMoves(List<Point> moves) {
|
|
||||||
this.legalMoves = moves;
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dessine les cercles verts autour des cases autorisées.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void paintComponent(Graphics g) {
|
|
||||||
super.paintComponent(g);
|
|
||||||
|
|
||||||
if (legalMoves == null) return;
|
|
||||||
|
|
||||||
Graphics2D g2 = (Graphics2D) g;
|
|
||||||
g2.setColor(new Color(0, 255, 0, 120));
|
|
||||||
|
|
||||||
for (Point p : legalMoves) {
|
|
||||||
|
|
||||||
int r = p.x;
|
|
||||||
int c = p.y;
|
|
||||||
|
|
||||||
int x = xBase + c * spacing;
|
|
||||||
int y = yBase + r * spacing;
|
|
||||||
|
|
||||||
int highlight = size + 20; // Cercle plus grand que le pion
|
|
||||||
g2.fillOval(x - 10, y - 10, highlight, highlight);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam.ui;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.Avalam.Tower;
|
|
||||||
import fr.iut_fbleau.Avalam.logic.GameState;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>InteractionController</code> gère entièrement
|
|
||||||
* la logique d'interaction de l'utilisateur :
|
|
||||||
*
|
|
||||||
* <ul>
|
|
||||||
* <li>Sélection d'un pion</li>
|
|
||||||
* <li>Annulation de la sélection</li>
|
|
||||||
* <li>Calcul des cases de déplacement légales</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* Aucun affichage n'est réalisé ici ; la vue (BoardView) récupère
|
|
||||||
* simplement les données calculées pour les afficher.
|
|
||||||
*
|
|
||||||
* @author
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class InteractionController {
|
|
||||||
|
|
||||||
private GameState state;
|
|
||||||
|
|
||||||
/** Position du pion sélectionné (ou -1 s'il n'y en a pas). */
|
|
||||||
private int selectedRow = -1, selectedCol = -1;
|
|
||||||
|
|
||||||
/** Liste des déplacements possibles depuis la sélection. */
|
|
||||||
private List<Point> legalMoves = new ArrayList<>();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructeur.
|
|
||||||
*
|
|
||||||
* @param state état du jeu à manipuler
|
|
||||||
*/
|
|
||||||
public InteractionController(GameState state) {
|
|
||||||
this.state = state;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne les cases jouables calculées.
|
|
||||||
*/
|
|
||||||
public List<Point> getLegalMoves() {
|
|
||||||
return legalMoves;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gère le clic sur un pion donné.
|
|
||||||
*
|
|
||||||
* @param r ligne de la case cliquée
|
|
||||||
* @param c colonne de la case cliquée
|
|
||||||
*/
|
|
||||||
public void onPieceClicked(int r, int c) {
|
|
||||||
|
|
||||||
Tower t = state.get(r, c);
|
|
||||||
if (t == null) return;
|
|
||||||
|
|
||||||
// Annulation si on reclique la sélection
|
|
||||||
if (r == selectedRow && c == selectedCol) {
|
|
||||||
selectedRow = -1;
|
|
||||||
selectedCol = -1;
|
|
||||||
legalMoves.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Interdiction de jouer le pion adverse
|
|
||||||
if (t.getColor() != state.getCurrentPlayer()) {
|
|
||||||
selectedRow = -1;
|
|
||||||
selectedCol = -1;
|
|
||||||
legalMoves.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
selectedRow = r;
|
|
||||||
selectedCol = c;
|
|
||||||
|
|
||||||
computeLegalMoves();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calcule toutes les cases accessibles à partir du pion sélectionné.
|
|
||||||
*/
|
|
||||||
private void computeLegalMoves() {
|
|
||||||
|
|
||||||
legalMoves.clear();
|
|
||||||
|
|
||||||
Tower[][] grid = state.getGrid();
|
|
||||||
Tower src = grid[selectedRow][selectedCol];
|
|
||||||
int h = src.getHeight();
|
|
||||||
|
|
||||||
int[] d = {-1, 0, 1};
|
|
||||||
|
|
||||||
for (int dr : d) {
|
|
||||||
for (int dc : d) {
|
|
||||||
|
|
||||||
if (dr == 0 && dc == 0) continue;
|
|
||||||
|
|
||||||
int nr = selectedRow + dr;
|
|
||||||
int nc = selectedCol + dc;
|
|
||||||
|
|
||||||
if (!state.isInside(nr, nc)) continue;
|
|
||||||
|
|
||||||
Tower dest = grid[nr][nc];
|
|
||||||
if (dest == null) continue;
|
|
||||||
if (h + dest.getHeight() > 5) continue;
|
|
||||||
|
|
||||||
legalMoves.add(new Point(nr, nc));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam.ui;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>ScoreView</code> affiche les scores actuels des deux joueurs.
|
|
||||||
*
|
|
||||||
* Elle est purement graphique : aucune logique de calcul n'est présente.
|
|
||||||
*
|
|
||||||
* @author
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class ScoreView extends JPanel {
|
|
||||||
|
|
||||||
private JLabel scoreY, scoreR;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructeur.
|
|
||||||
*
|
|
||||||
* @param y score initial du joueur jaune
|
|
||||||
* @param r score initial du joueur rouge
|
|
||||||
*/
|
|
||||||
public ScoreView(int y, int r) {
|
|
||||||
setBackground(new Color(200,200,200));
|
|
||||||
setLayout(new FlowLayout());
|
|
||||||
|
|
||||||
scoreY = new JLabel("Score Jaune : " + y);
|
|
||||||
scoreR = new JLabel("Score Rouge : " + r);
|
|
||||||
|
|
||||||
scoreY.setFont(new Font("Arial", Font.BOLD, 18));
|
|
||||||
scoreR.setFont(new Font("Arial", Font.BOLD, 18));
|
|
||||||
|
|
||||||
add(scoreY);
|
|
||||||
add(new JLabel(" | "));
|
|
||||||
add(scoreR);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Met à jour l'affichage des scores.
|
|
||||||
*/
|
|
||||||
public void updateScores(int y, int r) {
|
|
||||||
scoreY.setText("Score Jaune : " + y);
|
|
||||||
scoreR.setText("Score Rouge : " + r);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam.ui;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>TurnView</code> affiche le joueur à qui c'est le tour.
|
|
||||||
*
|
|
||||||
* Elle agit comme une simple bannière d’information,
|
|
||||||
* mise à jour par la logique du jeu.
|
|
||||||
*
|
|
||||||
* @author
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class TurnView extends JPanel {
|
|
||||||
|
|
||||||
private JLabel text;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructeur.
|
|
||||||
*
|
|
||||||
* @param initial message initial à afficher
|
|
||||||
*/
|
|
||||||
public TurnView(String initial) {
|
|
||||||
setBackground(new Color(220,220,220));
|
|
||||||
|
|
||||||
text = new JLabel(initial);
|
|
||||||
text.setFont(new Font("Arial", Font.BOLD, 20));
|
|
||||||
|
|
||||||
add(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Met à jour le texte affichant le joueur courant.
|
|
||||||
*
|
|
||||||
* @param s message à afficher
|
|
||||||
*/
|
|
||||||
public void setTurn(String s) {
|
|
||||||
text.setText(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
217
fr/iut_fbleau/Bot/AlphaBetaBot.java
Normal file
217
fr/iut_fbleau/Bot/AlphaBetaBot.java
Normal file
@@ -0,0 +1,217 @@
|
|||||||
|
package fr.iut_fbleau.Bot;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.Avalam.AvalamBoard;
|
||||||
|
import fr.iut_fbleau.Avalam.Color;
|
||||||
|
import fr.iut_fbleau.Avalam.Tower;
|
||||||
|
import fr.iut_fbleau.GameAPI.AbstractGamePlayer;
|
||||||
|
import fr.iut_fbleau.GameAPI.AbstractPly;
|
||||||
|
import fr.iut_fbleau.GameAPI.IBoard;
|
||||||
|
import fr.iut_fbleau.GameAPI.Player;
|
||||||
|
import fr.iut_fbleau.GameAPI.Result;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bot Alpha-Beta avec cut-off (profondeur maximale).
|
||||||
|
*
|
||||||
|
* Idée :
|
||||||
|
* - si on atteint la profondeur limite, on évalue la position (heuristique).
|
||||||
|
* - sinon, on explore les coups avec Alpha-Beta (minimax optimisé).
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class AlphaBetaBot extends AbstractGamePlayer {
|
||||||
|
|
||||||
|
//Attributs
|
||||||
|
|
||||||
|
/** Joueur contrôlé par ce bot (PLAYER1 ou PLAYER2). */
|
||||||
|
private final Player me;
|
||||||
|
|
||||||
|
/** Profondeur maximale de recherche (cut-off). */
|
||||||
|
private final int maxDepth;
|
||||||
|
|
||||||
|
/** Générateur aléatoire pour départager des coups de même valeur. */
|
||||||
|
private final Random rng = new Random();
|
||||||
|
|
||||||
|
//Constructeur
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit un bot Alpha-Beta.
|
||||||
|
*
|
||||||
|
* @param p joueur contrôlé par ce bot
|
||||||
|
* @param maxDepth profondeur maximale de recherche
|
||||||
|
*/
|
||||||
|
public AlphaBetaBot(Player p, int maxDepth) {
|
||||||
|
super(p);
|
||||||
|
this.me = p;
|
||||||
|
this.maxDepth = Math.max(1, maxDepth);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Méthodes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Méthode appelée par GameAPI : le bot doit choisir un coup.
|
||||||
|
*
|
||||||
|
* @param board copie sûre de l'état de jeu (IBoard)
|
||||||
|
* @return un coup (AbstractPly) ou null si aucun coup possible
|
||||||
|
*/
|
||||||
|
@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;
|
||||||
|
|
||||||
|
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, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bestMoves.get(rng.nextInt(bestMoves.size()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fonction récursive Alpha-Beta.
|
||||||
|
*/
|
||||||
|
private int alphaBeta(IBoard board, int depth, int alpha, int beta) {
|
||||||
|
|
||||||
|
if (board.isGameOver()) {
|
||||||
|
return terminalValue(board);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (depth == 0) {
|
||||||
|
return evaluate(board);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isMax = (board.getCurrentPlayer() == me);
|
||||||
|
|
||||||
|
List<AbstractPly> moves = listMoves(board);
|
||||||
|
if (moves.isEmpty()) {
|
||||||
|
return evaluate(board);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convertit le résultat final en valeur très grande (victoire/défaite).
|
||||||
|
* Result est du point de vue de PLAYER1.
|
||||||
|
*/
|
||||||
|
private int terminalValue(IBoard board) {
|
||||||
|
Result r = board.getResult();
|
||||||
|
if (r == null) return 0;
|
||||||
|
|
||||||
|
boolean botIsP1 = (me == Player.PLAYER1);
|
||||||
|
|
||||||
|
if (r == Result.DRAW) return 0;
|
||||||
|
|
||||||
|
if (botIsP1) {
|
||||||
|
return (r == Result.WIN) ? 100000 : -100000;
|
||||||
|
} else {
|
||||||
|
return (r == Result.LOSS) ? 100000 : -100000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Heuristique simple Avalam :
|
||||||
|
* score = (tours du bot) - (tours adverses)
|
||||||
|
*/
|
||||||
|
private int evaluate(IBoard board) {
|
||||||
|
|
||||||
|
if (!(board instanceof AvalamBoard)) return 0;
|
||||||
|
AvalamBoard b = (AvalamBoard) board;
|
||||||
|
|
||||||
|
Color botColor = (me == Player.PLAYER1) ? Color.YELLOW : Color.RED;
|
||||||
|
Color oppColor = (me == Player.PLAYER1) ? Color.RED : Color.YELLOW;
|
||||||
|
|
||||||
|
int botTowers = 0;
|
||||||
|
int oppTowers = 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.getColor() == botColor) botTowers++;
|
||||||
|
else if (t.getColor() == oppColor) oppTowers++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return botTowers - oppTowers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Récupère tous les coups légaux via iterator().
|
||||||
|
*/
|
||||||
|
private List<AbstractPly> listMoves(IBoard board) {
|
||||||
|
List<AbstractPly> moves = new ArrayList<>();
|
||||||
|
Iterator<AbstractPly> it = board.iterator();
|
||||||
|
while (it.hasNext()) moves.add(it.next());
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Affichage
|
||||||
|
}
|
||||||
22
fr/iut_fbleau/Bot/DivineBot.java
Normal file
22
fr/iut_fbleau/Bot/DivineBot.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package fr.iut_fbleau.Bot;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.Avalam.AvalamBoard;
|
||||||
|
import fr.iut_fbleau.Avalam.AvalamPly;
|
||||||
|
import fr.iut_fbleau.Avalam.Color;
|
||||||
|
import fr.iut_fbleau.Avalam.Tower;
|
||||||
|
import fr.iut_fbleau.GameAPI.AbstractGamePlayer;
|
||||||
|
import fr.iut_fbleau.GameAPI.AbstractPly;
|
||||||
|
import fr.iut_fbleau.GameAPI.IBoard;
|
||||||
|
import fr.iut_fbleau.GameAPI.Player;
|
||||||
|
import fr.iut_fbleau.GameAPI.Result;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bot "Divin" (fort) pour Avalam.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Objectif : trop fort. */
|
||||||
|
public class DivineBot{
|
||||||
|
|
||||||
|
}
|
||||||
43
fr/iut_fbleau/Bot/IdiotBot.java
Normal file
43
fr/iut_fbleau/Bot/IdiotBot.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package fr.iut_fbleau.Bot;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.GameAPI.AbstractGamePlayer;
|
||||||
|
import fr.iut_fbleau.GameAPI.AbstractPly;
|
||||||
|
import fr.iut_fbleau.GameAPI.IBoard;
|
||||||
|
import fr.iut_fbleau.GameAPI.Player;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bot idiot : choisit un coup légal au hasard parmi ceux retournés par IBoard.iterator().
|
||||||
|
* Compatible avec n'importe quel jeu respectant GameAPI (dont AvalamBoard).
|
||||||
|
*/
|
||||||
|
public class IdiotBot extends AbstractGamePlayer {
|
||||||
|
|
||||||
|
private final Random rng;
|
||||||
|
|
||||||
|
public IdiotBot(Player p) {
|
||||||
|
super(p);
|
||||||
|
this.rng = new Random();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AbstractPly giveYourMove(IBoard board) {
|
||||||
|
|
||||||
|
// Si la partie est terminée ou qu'il n'y a pas de coups, on ne joue rien.
|
||||||
|
if (board == null || board.isGameOver()) return null;
|
||||||
|
|
||||||
|
Iterator<AbstractPly> it = board.iterator();
|
||||||
|
List<AbstractPly> moves = new ArrayList<>();
|
||||||
|
|
||||||
|
while (it.hasNext()) {
|
||||||
|
moves.add(it.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (moves.isEmpty()) return null;
|
||||||
|
|
||||||
|
return moves.get(rng.nextInt(moves.size()));
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
fr/iut_fbleau/Res/BackgroundAvalam.png
Normal file
BIN
fr/iut_fbleau/Res/BackgroundAvalam.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 31 KiB |
122
fr/iut_fbleau/Tests/AvalamBoardTest.java
Normal file
122
fr/iut_fbleau/Tests/AvalamBoardTest.java
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
package fr.iut_fbleau.Tests;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.GameAPI.AbstractPly;
|
||||||
|
import fr.iut_fbleau.GameAPI.Player;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.Avalam.AvalamBoard;
|
||||||
|
import fr.iut_fbleau.Avalam.AvalamPly;
|
||||||
|
import fr.iut_fbleau.Avalam.Tower;
|
||||||
|
import fr.iut_fbleau.Avalam.Color;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
//import org.mockito.Mockito; //À retirer si Mockito absent
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La classe <code>AvalamBoardTest</code> test si la méthode isLegal() fonctionne comme prévu.
|
||||||
|
*/
|
||||||
|
public class AvalamBoardTest {
|
||||||
|
|
||||||
|
private Tower[][] grid;
|
||||||
|
private AvalamBoard board;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
grid = new Tower[AvalamBoard.SIZE][AvalamBoard.SIZE];
|
||||||
|
// Par défaut, current player sera PLAYER1 via constructeur sans joueur
|
||||||
|
board = new AvalamBoard(grid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
@Test //À retirer si Mockito absent
|
||||||
|
public void nonAvalamPly_returnsFalse() {
|
||||||
|
AbstractPly fake = Mockito.mock(AbstractPly.class); // instance non-AvalamPly
|
||||||
|
assertFalse(board.isLegal(fake));
|
||||||
|
}*/
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void outOfBounds_returnsFalse() {
|
||||||
|
grid[2][2] = new Tower(1, Color.YELLOW);
|
||||||
|
grid[2][3] = new Tower(1, Color.RED);
|
||||||
|
AvalamPly p = new AvalamPly(Player.PLAYER1, -1, 2, 2, 3);
|
||||||
|
assertFalse(board.isLegal(p));
|
||||||
|
|
||||||
|
AvalamPly p2 = new AvalamPly(Player.PLAYER1, 2, 2, 9, 3);
|
||||||
|
assertFalse(board.isLegal(p2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sameCell_returnsFalse() {
|
||||||
|
grid[4][4] = new Tower(1, Color.YELLOW);
|
||||||
|
AvalamPly p = new AvalamPly(Player.PLAYER1, 4, 4, 4, 4);
|
||||||
|
assertFalse(board.isLegal(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void emptySourceOrDest_returnsFalse() {
|
||||||
|
// source null
|
||||||
|
grid[3][3] = null;
|
||||||
|
grid[3][4] = new Tower(1, Color.RED);
|
||||||
|
AvalamPly p1 = new AvalamPly(Player.PLAYER1, 3, 3, 3, 4);
|
||||||
|
assertFalse(board.isLegal(p1));
|
||||||
|
|
||||||
|
// dest null
|
||||||
|
grid[5][5] = new Tower(1, Color.YELLOW);
|
||||||
|
grid[5][6] = null;
|
||||||
|
AvalamPly p2 = new AvalamPly(Player.PLAYER1, 5, 5, 5, 6);
|
||||||
|
assertFalse(board.isLegal(p2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sourceNotOwned_returnsFalse() {
|
||||||
|
// current player = PLAYER1 -> color must be YELLOW
|
||||||
|
grid[2][2] = new Tower(1, Color.RED); // not owned
|
||||||
|
grid[2][3] = new Tower(1, Color.YELLOW);
|
||||||
|
AvalamPly p = new AvalamPly(Player.PLAYER1, 2, 2, 2, 3);
|
||||||
|
assertFalse(board.isLegal(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void notAdjacent_returnsFalse() {
|
||||||
|
grid[0][0] = new Tower(1, Color.YELLOW);
|
||||||
|
grid[0][2] = new Tower(1, Color.RED);
|
||||||
|
AvalamPly p = new AvalamPly(Player.PLAYER1, 0, 0, 0, 2);
|
||||||
|
assertFalse(board.isLegal(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sameColor_returnsFalse() {
|
||||||
|
grid[6][6] = new Tower(1, Color.YELLOW);
|
||||||
|
grid[6][7] = new Tower(1, Color.YELLOW); // same color as source
|
||||||
|
AvalamPly p = new AvalamPly(Player.PLAYER1, 6, 6, 6, 7);
|
||||||
|
assertFalse(board.isLegal(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void tooTallAfterMerge_returnsFalse() {
|
||||||
|
grid[1][1] = new Tower(3, Color.YELLOW);
|
||||||
|
grid[1][2] = new Tower(3, Color.RED); // 3+3 = 6 > MAX_HEIGHT (5)
|
||||||
|
AvalamPly p = new AvalamPly(Player.PLAYER1, 1, 1, 1, 2);
|
||||||
|
assertFalse(board.isLegal(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void validMove_returnsTrue() {
|
||||||
|
grid[4][4] = new Tower(2, Color.YELLOW); // owned by PLAYER1
|
||||||
|
grid[4][5] = new Tower(2, Color.RED); // opposite color
|
||||||
|
AvalamPly p = new AvalamPly(Player.PLAYER1, 4, 4, 4, 5);
|
||||||
|
assertTrue(board.isLegal(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void currentPlayerMismatchInPlyDoesNotAffectOwnershipCheck() {
|
||||||
|
// Even if AvalamPly is constructed with a player value, isLegal uses board.getCurrentPlayer()
|
||||||
|
grid[7][7] = new Tower(1, Color.YELLOW); // owned by PLAYER1 (board default)
|
||||||
|
grid[7][8] = new Tower(1, Color.RED);
|
||||||
|
// Construct ply with PLAYER2 explicitly — ownership check should still compare to board.getCurrentPlayer()
|
||||||
|
AvalamPly p = new AvalamPly(Player.PLAYER2, 7, 7, 7, 8);
|
||||||
|
assertFalse(board.isLegal(p));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user