Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b115b88b5a | |||
| 98ae6dc754 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +0,0 @@
|
|||||||
# Ignorer le répertoire bin
|
|
||||||
/bin
|
|
||||||
BIN
Affichage/Affiche.class
Normal file
BIN
Affichage/Affiche.class
Normal file
Binary file not shown.
106
Affichage/Affiche.java
Normal file
106
Affichage/Affiche.java
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
|
||||||
|
public class Affiche extends JPanel {
|
||||||
|
|
||||||
|
int tabJeu[][] = {
|
||||||
|
{0,0,1,2,0,0,0,0,0},
|
||||||
|
{0,1,2,1,2,0,0,0,0},
|
||||||
|
{0,2,1,2,1,2,1,0,0},
|
||||||
|
{0,1,2,1,2,1,2,1,2},
|
||||||
|
{1,2,1,2,0,2,1,2,1},
|
||||||
|
{2,1,2,1,2,1,2,1,0},
|
||||||
|
{0,0,1,2,1,2,1,2,0},
|
||||||
|
{0,0,0,0,2,1,2,1,0},
|
||||||
|
{0,0,0,0,0,2,1,0,0}
|
||||||
|
};
|
||||||
|
|
||||||
|
int pionTaille = 50;
|
||||||
|
int pas = 70;
|
||||||
|
int yBase = 60;
|
||||||
|
int xBase = 60;
|
||||||
|
|
||||||
|
public Affiche() {
|
||||||
|
setLayout(null);
|
||||||
|
|
||||||
|
for (int i = 0; i < tabJeu.length; i++) {
|
||||||
|
for (int j = 0; j < tabJeu[i].length; j++) {
|
||||||
|
int valeur = tabJeu[i][j];
|
||||||
|
if (valeur != 0) {
|
||||||
|
Color couleur = (valeur == 1) ? Color.BLUE : Color.RED;
|
||||||
|
JButton bouton = creerBoutonRond(couleur);
|
||||||
|
|
||||||
|
int x = xBase + j * pas;
|
||||||
|
int y = yBase + i * pas;
|
||||||
|
bouton.setBounds(x, y, pionTaille, pionTaille);
|
||||||
|
|
||||||
|
add(bouton);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private JButton creerBoutonRond(Color couleurBase) {
|
||||||
|
JButton bouton = new JButton() {
|
||||||
|
private boolean hover = false;
|
||||||
|
|
||||||
|
{
|
||||||
|
setBorderPainted(false);
|
||||||
|
setContentAreaFilled(false);
|
||||||
|
setFocusPainted(false);
|
||||||
|
setOpaque(false);
|
||||||
|
|
||||||
|
addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseEntered(MouseEvent e) {
|
||||||
|
hover = true;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseExited(MouseEvent e) {
|
||||||
|
hover = false;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
JOptionPane.showMessageDialog(null,
|
||||||
|
"Tu as cliqué sur un pion " + (couleurBase == Color.BLUE ? "bleu" : "rouge") + " !");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
Graphics2D g2 = (Graphics2D) g.create();
|
||||||
|
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
Color couleur = hover ? couleurBase.darker() : couleurBase;
|
||||||
|
g2.setColor(couleur);
|
||||||
|
g2.fillOval(0, 0, getWidth(), getHeight());
|
||||||
|
|
||||||
|
g2.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean contains(int x, int y) {
|
||||||
|
double dx = x - getWidth() / 2.0;
|
||||||
|
double dy = y - getHeight() / 2.0;
|
||||||
|
return dx * dx + dy * dy <= (getWidth() / 2.0) * (getWidth() / 2.0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bouton.setPreferredSize(new Dimension(pionTaille, pionTaille));
|
||||||
|
return bouton;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame fenetre = new JFrame("Pions interactifs sans bord bleu");
|
||||||
|
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
fenetre.setSize(900, 800);
|
||||||
|
fenetre.add(new Affiche());
|
||||||
|
fenetre.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
Affichage/Test.class
Normal file
BIN
Affichage/Test.class
Normal file
Binary file not shown.
15
Affichage/Test.java
Normal file
15
Affichage/Test.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame fenetre = new JFrame();
|
||||||
|
fenetre.setSize(1500, 1050);
|
||||||
|
fenetre.setLocation(150, 30);
|
||||||
|
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
Affiche aff = new Affiche();
|
||||||
|
fenetre.add(aff);
|
||||||
|
|
||||||
|
fenetre.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,218 +0,0 @@
|
|||||||
---
|
|
||||||
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
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
---
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
---
|
|
||||||
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
|
|
||||||
}
|
|
||||||
61
Makefile
61
Makefile
@@ -1,61 +0,0 @@
|
|||||||
# === Répertoires ===
|
|
||||||
SRC_DIR = fr
|
|
||||||
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 ===
|
|
||||||
SOURCES := $(shell find $(SRC_DIR) -name "*.java")
|
|
||||||
|
|
||||||
# === Classe principale ===
|
|
||||||
MAIN_CLASS = fr.iut_fbleau.Avalam.Main
|
|
||||||
|
|
||||||
# === Commandes Java ===
|
|
||||||
JC = javac
|
|
||||||
JCFLAGS = -d $(BIN_DIR)
|
|
||||||
|
|
||||||
JAVA = java
|
|
||||||
JAVAFLAGS = -cp $(BIN_DIR)
|
|
||||||
|
|
||||||
# === Règle par défaut ===
|
|
||||||
all: build
|
|
||||||
|
|
||||||
# === Compilation ===
|
|
||||||
build: compile resources
|
|
||||||
@echo "✔ Compilation terminée."
|
|
||||||
|
|
||||||
compile:
|
|
||||||
@echo "===> Compilation du projet Avalam..."
|
|
||||||
@mkdir -p $(BIN_DIR)
|
|
||||||
@$(JC) $(JCFLAGS) $(SOURCES)
|
|
||||||
|
|
||||||
# === 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 ===
|
|
||||||
run:
|
|
||||||
@echo "===> Lancement du jeu Avalam..."
|
|
||||||
@$(JAVA) $(JAVAFLAGS) $(MAIN_CLASS)
|
|
||||||
|
|
||||||
# === Nettoyage ===
|
|
||||||
clean:
|
|
||||||
@echo "===> Suppression des fichiers compilés..."
|
|
||||||
@rm -rf $(BIN_DIR)
|
|
||||||
@echo "✔ Nettoyage complet."
|
|
||||||
|
|
||||||
# === Recompile + run ===
|
|
||||||
re: clean build run
|
|
||||||
|
|
||||||
# === Génération de la Javadoc ===
|
|
||||||
DOC_DIR = doc
|
|
||||||
javadoc:
|
|
||||||
@echo "===> Génération de la Javadoc..."
|
|
||||||
@mkdir -p $(DOC_DIR)
|
|
||||||
@javadoc -d $(DOC_DIR) -sourcepath $(SRC_DIR) -subpackages fr.iut_fbleau.Avalam
|
|
||||||
@echo "✔ Javadoc générée dans $(DOC_DIR)/"
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>Class</code>
|
|
||||||
*
|
|
||||||
* @version
|
|
||||||
* @author
|
|
||||||
* Date :
|
|
||||||
* Licence :
|
|
||||||
*/
|
|
||||||
public class Class {
|
|
||||||
//Attributs
|
|
||||||
|
|
||||||
//Constructeur
|
|
||||||
public Class() {
|
|
||||||
|
|
||||||
}
|
|
||||||
//Méthodes
|
|
||||||
|
|
||||||
//Affichage
|
|
||||||
public String toString() {
|
|
||||||
return "" ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
import javax.swing.*;
|
|
||||||
import java.awt.event.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>Event</code>
|
|
||||||
*
|
|
||||||
* @version
|
|
||||||
* @author
|
|
||||||
* Date :
|
|
||||||
* Licence :
|
|
||||||
*/
|
|
||||||
public class Event implements ActionListener {
|
|
||||||
// Attributs
|
|
||||||
|
|
||||||
// Constructeur de l'évennement
|
|
||||||
public Event() {
|
|
||||||
|
|
||||||
}
|
|
||||||
// Action de l'évennement
|
|
||||||
public void actionPerformed(ActionEvent event){
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
|
|
||||||
/**
|
|
||||||
* L'interface <code>Interface</code>
|
|
||||||
*
|
|
||||||
* @version
|
|
||||||
* @author
|
|
||||||
* Date :
|
|
||||||
* Licence :
|
|
||||||
*/
|
|
||||||
public interface Interface {
|
|
||||||
//Méthodes
|
|
||||||
public void Action() ;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>Main</code>
|
|
||||||
*
|
|
||||||
* @version
|
|
||||||
* @author
|
|
||||||
* Date :
|
|
||||||
* Licence :
|
|
||||||
*/
|
|
||||||
public class Main {
|
|
||||||
public static void main(String[] args){
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
100
README.md
100
README.md
@@ -1,100 +0,0 @@
|
|||||||
# BUT3 Projet Jeu Groupe
|
|
||||||
|
|
||||||
*Auteurs : AMARY Aurelien, DICK Adrien, FELIX-VIMALARATNAM Patrick, RABAN Hugo*
|
|
||||||
|
|
||||||
Date de création : 16/10/25
|
|
||||||
|
|
||||||
Le but est de mettre en oeuvre des jeux plus intéressants que le jeu de Nim, toujours en suivant l'API.
|
|
||||||
|
|
||||||
Dans un second temps, on fera des bots le plus efficace possible (probablement un alpha beta avec cut-off plus fonction d'évaluation qui peut être faite à la main ou par MonteCarlo).
|
|
||||||
|
|
||||||
Le jeu de notre groupe est **Avalam**.
|
|
||||||
|
|
||||||
## Compilation et exécution
|
|
||||||
|
|
||||||
### Compilation
|
|
||||||
```bash
|
|
||||||
make build
|
|
||||||
```
|
|
||||||
|
|
||||||
### Exécution
|
|
||||||
```bash
|
|
||||||
make run
|
|
||||||
```
|
|
||||||
|
|
||||||
### Nettoyage
|
|
||||||
```bash
|
|
||||||
make clean
|
|
||||||
```
|
|
||||||
|
|
||||||
### Recompiler et exécuter
|
|
||||||
```bash
|
|
||||||
make re
|
|
||||||
```
|
|
||||||
|
|
||||||
### Générer la Javadoc
|
|
||||||
```bash
|
|
||||||
make javadoc
|
|
||||||
```
|
|
||||||
|
|
||||||
## Architecture du projet
|
|
||||||
|
|
||||||
### Structure des classes principales
|
|
||||||
|
|
||||||
- **`AvalamBoard`** : Implémentation du plateau de jeu conforme à l'API `AbstractBoard`
|
|
||||||
- Gère l'état du plateau (grille 9x9)
|
|
||||||
- Valide les règles d'Avalam
|
|
||||||
- Applique et annule les coups
|
|
||||||
- Détecte la fin de partie et calcule le résultat
|
|
||||||
|
|
||||||
- **`AvalamPly`** : Représente un coup dans le jeu
|
|
||||||
- Stocke les coordonnées de départ et d'arrivée
|
|
||||||
- Stocke la hauteur de la tour source (pour l'annulation)
|
|
||||||
|
|
||||||
- **`Tower`** : Représente une tour de pions
|
|
||||||
- Stocke la couleur du sommet et la hauteur
|
|
||||||
|
|
||||||
- **`Color`** : Énumération des couleurs des joueurs
|
|
||||||
- COLOR1 (Jaune) et COLOR2 (Rouge)
|
|
||||||
|
|
||||||
### Logique du jeu
|
|
||||||
|
|
||||||
Le plateau est représenté par une grille 9x9 où chaque case contient :
|
|
||||||
- `null` : case vide (trou)
|
|
||||||
- `ArrayList<Integer>` : tour de pions (chaque Integer = 1 pour PLAYER1, 2 pour PLAYER2)
|
|
||||||
|
|
||||||
### Règles implémentées
|
|
||||||
|
|
||||||
- ✅ Validation des limites du plateau
|
|
||||||
- ✅ Vérification que la case source n'est pas vide
|
|
||||||
- ✅ Vérification que la case destination n'est pas vide (pas de trou)
|
|
||||||
- ✅ Vérification que la destination est voisine (horizontal, vertical ou diagonal)
|
|
||||||
- ✅ Vérification que la hauteur totale après déplacement ≤ 5
|
|
||||||
- ✅ Déplacement de toute la pile (pas partiel)
|
|
||||||
- ✅ Détection de fin de partie (plus aucun coup possible)
|
|
||||||
- ✅ Calcul du résultat (nombre de tours possédées par chaque joueur)
|
|
||||||
|
|
||||||
## Présentation [Avalam](https://escaleajeux.fr/?principal=/jeu/avaxl?)
|
|
||||||
|
|
||||||
Sur un plateau de jeu, les joueurs disposent de 24 pions chacun. Le but est de créer de petites tours avec son pion de couleur au-dessus. Pour ce faire, chaque joueur déplace son pion sur une tour ou déplace une tour sur un autre pion. La personne ayant le plus de pions sur le dessus des tours gagne.
|
|
||||||
|
|
||||||
### Régles officiel :
|
|
||||||
**Régle de base** \
|
|
||||||
Chaque joueur choisit sa couleur.
|
|
||||||
Le but du jeu est de constituer un maximum de tours de 1 à 5 pions, jamais plus, surmontées par un pion de sa couleur.
|
|
||||||
Un joueur est le propriétaire d'une tour lorsqu'un pion de sa couleur en occupe le sommet. Un pion isolé constitue également une tour.
|
|
||||||
|
|
||||||
**Deplacements** \
|
|
||||||
Chaque joueur en effectue un seul mouvement, dans n'importe quel sens (horizontal, vertical, diagonal) avec n'importe quel pion (ou pile de pions), quelle qu'en soit la couleur. Ce mouvement consiste à empiler le ou les pions déplacés sur un trou directement voisin déjà occupé par un ou plusieurs pions.
|
|
||||||
|
|
||||||
**Mouvement interdit (1)** \
|
|
||||||
On déplace obligatoirement toute la pile se trouvant sur un trou (elle peut évidemment n'être constituée que d'un seul pion). Autrement dit, une pile de pions ne peut qu'augmenter, jamais diminuer.
|
|
||||||
|
|
||||||
**Mouvement interdit (2)** \
|
|
||||||
On ne peut jamais poser de pions sur un trou inoccupé: il le reste donc définitivement. Un pion (ou une tour) isolé de tous les cotés ne pourra donc plus changer de propriétaire.
|
|
||||||
|
|
||||||
**Fin de partie** \
|
|
||||||
Tans qu'un joueur peut effectuer un mouvement il a l'obligation de jouer, la partie ne s'achevant que lorque plus aucun déplacement n'est possible.
|
|
||||||
On compte alors combien de pions de chaque couleur occupent le sommet des tours restantes, le vainqueur étant évidemment celui qui en totalise le plus.
|
|
||||||
Attention! Qu'une tour comporte 1,2,... ou 5 pions, elle vaut toujours UN point.
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.GameAPI.AbstractGame;
|
|
||||||
import fr.iut_fbleau.GameAPI.AbstractGamePlayer;
|
|
||||||
import fr.iut_fbleau.GameAPI.IBoard;
|
|
||||||
import fr.iut_fbleau.GameAPI.Player;
|
|
||||||
|
|
||||||
import java.util.EnumMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Classe pour jouer une partie entre deux bots sans interface graphique.
|
|
||||||
* Utilisée dans le mode Arène.
|
|
||||||
*
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class ArenaGame extends AbstractGame {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit une partie Arène entre deux bots.
|
|
||||||
*
|
|
||||||
* @param board plateau initial
|
|
||||||
* @param bot1 bot pour PLAYER1
|
|
||||||
* @param bot2 bot pour PLAYER2
|
|
||||||
*/
|
|
||||||
public ArenaGame(IBoard board, AbstractGamePlayer bot1, AbstractGamePlayer bot2) {
|
|
||||||
super(board, createPlayerMap(bot1, bot2));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Crée la map des joueurs pour AbstractGame.
|
|
||||||
*
|
|
||||||
* @param bot1 bot pour PLAYER1
|
|
||||||
* @param bot2 bot pour PLAYER2
|
|
||||||
* @return une EnumMap associant chaque Player à son bot
|
|
||||||
*/
|
|
||||||
private static EnumMap<Player, AbstractGamePlayer> createPlayerMap(
|
|
||||||
AbstractGamePlayer bot1, AbstractGamePlayer bot2) {
|
|
||||||
EnumMap<Player, AbstractGamePlayer> map = new EnumMap<>(Player.class);
|
|
||||||
map.put(Player.PLAYER1, bot1);
|
|
||||||
map.put(Player.PLAYER2, bot2);
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,280 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.Bot.AlphaBetaBot;
|
|
||||||
import fr.iut_fbleau.Bot.IdiotBot;
|
|
||||||
import fr.iut_fbleau.GameAPI.AbstractGamePlayer;
|
|
||||||
import fr.iut_fbleau.GameAPI.Player;
|
|
||||||
import fr.iut_fbleau.GameAPI.Result;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import javax.swing.table.DefaultTableModel;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fenêtre pour le mode Arène.
|
|
||||||
* Permet de sélectionner deux bots, le nombre de parties, et affiche les résultats.
|
|
||||||
*
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class ArenaWindow extends JFrame {
|
|
||||||
|
|
||||||
/** Tableau affichant les résultats des parties. */
|
|
||||||
private JTable resultsTable;
|
|
||||||
|
|
||||||
/** Modèle de données pour le tableau des résultats. */
|
|
||||||
private DefaultTableModel tableModel;
|
|
||||||
|
|
||||||
/** Liste des résultats des parties (non utilisée actuellement). */
|
|
||||||
private List<String> results;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit la fenêtre Arène.
|
|
||||||
*/
|
|
||||||
public ArenaWindow() {
|
|
||||||
super("Arène - Bot vs Bot");
|
|
||||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
||||||
setLayout(new BorderLayout());
|
|
||||||
|
|
||||||
results = new ArrayList<>();
|
|
||||||
|
|
||||||
// Panneau de configuration
|
|
||||||
JPanel configPanel = createConfigPanel();
|
|
||||||
add(configPanel, BorderLayout.NORTH);
|
|
||||||
|
|
||||||
// Tableau des résultats
|
|
||||||
createResultsTable();
|
|
||||||
JScrollPane scrollPane = new JScrollPane(resultsTable);
|
|
||||||
add(scrollPane, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
// Panneau des boutons
|
|
||||||
JPanel buttonPanel = new JPanel(new FlowLayout());
|
|
||||||
JButton startButton = new JButton("Lancer les parties");
|
|
||||||
startButton.addActionListener(e -> showConfigDialog());
|
|
||||||
buttonPanel.add(startButton);
|
|
||||||
|
|
||||||
JButton backButton = new JButton("Retour au menu");
|
|
||||||
backButton.addActionListener(e -> {
|
|
||||||
dispose(); // Ferme la fenêtre Arène
|
|
||||||
Main.showModeSelection(); // Affiche le menu principal
|
|
||||||
});
|
|
||||||
buttonPanel.add(backButton);
|
|
||||||
|
|
||||||
add(buttonPanel, BorderLayout.SOUTH);
|
|
||||||
|
|
||||||
pack();
|
|
||||||
setSize(600, 400);
|
|
||||||
setLocationRelativeTo(null);
|
|
||||||
setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Crée le panneau de configuration (pour l'instant vide, sera rempli par le dialogue).
|
|
||||||
*
|
|
||||||
* @return un JPanel contenant les informations de configuration
|
|
||||||
*/
|
|
||||||
private JPanel createConfigPanel() {
|
|
||||||
JPanel panel = new JPanel();
|
|
||||||
panel.setBorder(BorderFactory.createTitledBorder("Configuration"));
|
|
||||||
panel.add(new JLabel("Utilisez le bouton 'Lancer les parties' pour configurer et démarrer."));
|
|
||||||
return panel;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Crée le tableau des résultats avec les colonnes : Partie, Bot 1, Bot 2, Gagnant.
|
|
||||||
*/
|
|
||||||
private void createResultsTable() {
|
|
||||||
String[] columnNames = {"Partie", "Bot 1", "Bot 2", "Gagnant"};
|
|
||||||
tableModel = new DefaultTableModel(columnNames, 0) {
|
|
||||||
@Override
|
|
||||||
public boolean isCellEditable(int row, int column) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
resultsTable = new JTable(tableModel);
|
|
||||||
resultsTable.setFillsViewportHeight(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Affiche le dialogue de configuration et lance les parties.
|
|
||||||
*/
|
|
||||||
private void showConfigDialog() {
|
|
||||||
// Choix du bot 1
|
|
||||||
String[] botTypes = {"Bot Idiot", "Bot Alpha-Beta", "Bot Divin"};
|
|
||||||
String bot1Choice = (String) JOptionPane.showInputDialog(
|
|
||||||
this,
|
|
||||||
"Choisissez le Bot 1 (Joueur 1) :",
|
|
||||||
"Configuration Arène - Bot 1",
|
|
||||||
JOptionPane.QUESTION_MESSAGE,
|
|
||||||
null,
|
|
||||||
botTypes,
|
|
||||||
botTypes[0]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (bot1Choice == null) return;
|
|
||||||
|
|
||||||
// Choix du bot 2
|
|
||||||
String bot2Choice = (String) JOptionPane.showInputDialog(
|
|
||||||
this,
|
|
||||||
"Choisissez le Bot 2 (Joueur 2) :",
|
|
||||||
"Configuration Arène - Bot 2",
|
|
||||||
JOptionPane.QUESTION_MESSAGE,
|
|
||||||
null,
|
|
||||||
botTypes,
|
|
||||||
botTypes[0]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (bot2Choice == null) return;
|
|
||||||
|
|
||||||
// Profondeur pour Alpha-Beta et Divin
|
|
||||||
int depth = 4;
|
|
||||||
if (bot1Choice.contains("Alpha") || bot1Choice.contains("Divin") ||
|
|
||||||
bot2Choice.contains("Alpha") || bot2Choice.contains("Divin")) {
|
|
||||||
String depthStr = JOptionPane.showInputDialog(
|
|
||||||
this,
|
|
||||||
"Profondeur de recherche pour les bots Alpha-Beta/Divin ?\n(Conseil: 4)",
|
|
||||||
"Profondeur",
|
|
||||||
JOptionPane.QUESTION_MESSAGE
|
|
||||||
);
|
|
||||||
if (depthStr != null) {
|
|
||||||
try {
|
|
||||||
depth = Integer.parseInt(depthStr.trim());
|
|
||||||
if (depth < 1) depth = 1;
|
|
||||||
} catch (Exception e) {
|
|
||||||
depth = 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Nombre de parties
|
|
||||||
String nbPartiesStr = JOptionPane.showInputDialog(
|
|
||||||
this,
|
|
||||||
"Combien de parties voulez-vous jouer ?",
|
|
||||||
"Nombre de parties",
|
|
||||||
JOptionPane.QUESTION_MESSAGE
|
|
||||||
);
|
|
||||||
|
|
||||||
if (nbPartiesStr == null) return;
|
|
||||||
|
|
||||||
int nbParties;
|
|
||||||
try {
|
|
||||||
nbParties = Integer.parseInt(nbPartiesStr.trim());
|
|
||||||
if (nbParties < 1) {
|
|
||||||
JOptionPane.showMessageDialog(this, "Le nombre de parties doit être au moins 1.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
JOptionPane.showMessageDialog(this, "Nombre invalide.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lancer les parties
|
|
||||||
runArena(bot1Choice, bot2Choice, depth, nbParties);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Lance les parties entre les deux bots.
|
|
||||||
*
|
|
||||||
* @param bot1Type type du bot 1 (Joueur 1)
|
|
||||||
* @param bot2Type type du bot 2 (Joueur 2)
|
|
||||||
* @param depth profondeur de recherche pour les bots Alpha-Beta/Divin
|
|
||||||
* @param nbParties nombre de parties à jouer
|
|
||||||
*/
|
|
||||||
private void runArena(String bot1Type, String bot2Type, int depth, int nbParties) {
|
|
||||||
// Créer les bots
|
|
||||||
AbstractGamePlayer bot1 = createBot(bot1Type, Player.PLAYER1, depth);
|
|
||||||
AbstractGamePlayer bot2 = createBot(bot2Type, Player.PLAYER2, depth);
|
|
||||||
|
|
||||||
if (bot1 == null || bot2 == null) {
|
|
||||||
JOptionPane.showMessageDialog(this, "Erreur lors de la création des bots.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Vider le tableau
|
|
||||||
tableModel.setRowCount(0);
|
|
||||||
results.clear();
|
|
||||||
|
|
||||||
// Charger le plateau initial
|
|
||||||
Tower[][] initialGrid = BoardLoader.loadFromFile("fr/iut_fbleau/Res/Plateau.txt");
|
|
||||||
|
|
||||||
// Lancer les parties
|
|
||||||
for (int i = 1; i <= nbParties; i++) {
|
|
||||||
AvalamBoard board = new AvalamBoard(initialGrid, Player.PLAYER1);
|
|
||||||
ArenaGame game = new ArenaGame(board, bot1, bot2);
|
|
||||||
|
|
||||||
try {
|
|
||||||
Result result = game.run();
|
|
||||||
String winner = getWinnerName(result, bot1Type, bot2Type);
|
|
||||||
|
|
||||||
// Ajouter au tableau
|
|
||||||
tableModel.addRow(new Object[]{
|
|
||||||
"Partie " + i,
|
|
||||||
bot1Type,
|
|
||||||
bot2Type,
|
|
||||||
winner
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
|
||||||
tableModel.addRow(new Object[]{
|
|
||||||
"Partie " + i,
|
|
||||||
bot1Type,
|
|
||||||
bot2Type,
|
|
||||||
"Erreur: " + e.getMessage()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Afficher un message de fin
|
|
||||||
JOptionPane.showMessageDialog(
|
|
||||||
this,
|
|
||||||
"Toutes les parties sont terminées !",
|
|
||||||
"Arène terminée",
|
|
||||||
JOptionPane.INFORMATION_MESSAGE
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Crée un bot selon son type.
|
|
||||||
*
|
|
||||||
* @param botType type de bot ("Bot Idiot", "Bot Alpha-Beta", "Bot Divin")
|
|
||||||
* @param player joueur contrôlé par ce bot (PLAYER1 ou PLAYER2)
|
|
||||||
* @param depth profondeur de recherche pour Alpha-Beta/Divin
|
|
||||||
* @return une instance de AbstractGamePlayer correspondant au type, ou null si le type est invalide
|
|
||||||
*/
|
|
||||||
private AbstractGamePlayer createBot(String botType, Player player, int depth) {
|
|
||||||
if (botType.equals("Bot Idiot")) {
|
|
||||||
return new IdiotBot(player);
|
|
||||||
} else if (botType.equals("Bot Alpha-Beta")) {
|
|
||||||
return new AlphaBetaBot(player, depth);
|
|
||||||
} else if (botType.equals("Bot Divin")) {
|
|
||||||
// Pour l'instant, le Bot Divin n'est pas implémenté, on utilise IdiotBot
|
|
||||||
JOptionPane.showMessageDialog(
|
|
||||||
this,
|
|
||||||
"Le Bot Divin n'est pas encore implémenté. Utilisation du Bot Idiot à la place.",
|
|
||||||
"Avertissement",
|
|
||||||
JOptionPane.WARNING_MESSAGE
|
|
||||||
);
|
|
||||||
return new IdiotBot(player);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Détermine le nom du gagnant selon le résultat.
|
|
||||||
*
|
|
||||||
* @param result résultat de la partie (WIN, LOSS, ou DRAW du point de vue de PLAYER1)
|
|
||||||
* @param bot1Type nom du bot 1
|
|
||||||
* @param bot2Type nom du bot 2
|
|
||||||
* @return une chaîne indiquant le gagnant ou "Match nul"
|
|
||||||
*/
|
|
||||||
private String getWinnerName(Result result, String bot1Type, String bot2Type) {
|
|
||||||
if (result == Result.WIN) {
|
|
||||||
return bot1Type + " (Joueur 1)";
|
|
||||||
} else if (result == Result.LOSS) {
|
|
||||||
return bot2Type + " (Joueur 2)";
|
|
||||||
} else {
|
|
||||||
return "Match nul";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,302 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.GameAPI.AbstractBoard;
|
|
||||||
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.ArrayDeque;
|
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>AvalamBoard</code>
|
|
||||||
*
|
|
||||||
* Représente le plateau et les règles du jeu Avalam.
|
|
||||||
* Cette classe étend <code>AbstractBoard</code> (GameAPI) et fournit :
|
|
||||||
* - la génération des coups (iterator)
|
|
||||||
* - le test de légalité (isLegal)
|
|
||||||
* - l’application d’un coup (doPly)
|
|
||||||
* - la détection de fin de partie (isGameOver)
|
|
||||||
* - le calcul du résultat (getResult)
|
|
||||||
*/
|
|
||||||
public class AvalamBoard extends AbstractBoard {
|
|
||||||
|
|
||||||
//Attributs
|
|
||||||
|
|
||||||
/** Taille du plateau Avalam (9x9). */
|
|
||||||
public static final int SIZE = 9;
|
|
||||||
|
|
||||||
/** Hauteur maximale autorisée pour une tour après fusion. */
|
|
||||||
private static final int MAX_HEIGHT = 5;
|
|
||||||
|
|
||||||
/** Grille du plateau : chaque case contient une tour (Tower) ou null si vide. */
|
|
||||||
private final Tower[][] grid;
|
|
||||||
|
|
||||||
/** Indique si la partie est terminée (mémoïsation). */
|
|
||||||
private boolean gameOver = false;
|
|
||||||
|
|
||||||
/** Résultat de la partie si elle est terminée (mémoïsation). */
|
|
||||||
private Result result = null;
|
|
||||||
|
|
||||||
//Constructeur
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit un plateau Avalam à partir d’une grille initiale et d’un joueur qui commence.
|
|
||||||
*
|
|
||||||
* @param initialGrid grille initiale (Tower ou null)
|
|
||||||
* @param startingPlayer joueur qui commence (PLAYER1 ou PLAYER2)
|
|
||||||
*/
|
|
||||||
public AvalamBoard(Tower[][] initialGrid, Player startingPlayer) {
|
|
||||||
super(startingPlayer, new ArrayDeque<>());
|
|
||||||
this.grid = new Tower[SIZE][SIZE];
|
|
||||||
|
|
||||||
for (int r = 0; r < SIZE; r++)
|
|
||||||
for (int c = 0; c < SIZE; c++)
|
|
||||||
this.grid[r][c] = initialGrid[r][c];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit un plateau Avalam à partir d’une grille initiale.
|
|
||||||
* Par défaut, PLAYER1 commence.
|
|
||||||
*
|
|
||||||
* @param initialGrid grille initiale (Tower ou null)
|
|
||||||
*/
|
|
||||||
public AvalamBoard(Tower[][] initialGrid) {
|
|
||||||
this(initialGrid, Player.PLAYER1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructeur interne utilisé par safeCopy().
|
|
||||||
*
|
|
||||||
* @param grid grille à réutiliser
|
|
||||||
* @param current joueur courant
|
|
||||||
* @param gameOver état “partie terminée”
|
|
||||||
* @param result résultat mémorisé
|
|
||||||
*/
|
|
||||||
private AvalamBoard(Tower[][] grid, Player current, boolean gameOver, Result result) {
|
|
||||||
super(current, new ArrayDeque<>());
|
|
||||||
this.grid = grid;
|
|
||||||
this.gameOver = gameOver;
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Méthodes
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne la tour située à la position (row, col), ou null si hors bornes ou vide.
|
|
||||||
*
|
|
||||||
* @param row ligne
|
|
||||||
* @param col colonne
|
|
||||||
* @return tour présente ou null
|
|
||||||
*/
|
|
||||||
public Tower getTowerAt(int row, int col) {
|
|
||||||
return inBounds(row, col) ? grid[row][col] : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Teste si une position est à l’intérieur du plateau.
|
|
||||||
*
|
|
||||||
* @param r ligne
|
|
||||||
* @param c colonne
|
|
||||||
* @return true si (r,c) est dans [0..SIZE-1]
|
|
||||||
*/
|
|
||||||
private boolean inBounds(int r, int c) {
|
|
||||||
return r >= 0 && r < SIZE && c >= 0 && c < SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Teste si deux cases sont adjacentes (8-voisinage).
|
|
||||||
*
|
|
||||||
* @param r1 ligne source
|
|
||||||
* @param c1 colonne source
|
|
||||||
* @param r2 ligne destination
|
|
||||||
* @param c2 colonne destination
|
|
||||||
* @return true si les cases sont voisines et différentes
|
|
||||||
*/
|
|
||||||
private boolean areAdjacent(int r1, int c1, int r2, int c2) {
|
|
||||||
int dr = Math.abs(r1 - r2);
|
|
||||||
int dc = Math.abs(c1 - c2);
|
|
||||||
return (dr <= 1 && dc <= 1 && !(dr == 0 && dc == 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Associe un joueur GameAPI à une couleur Avalam.
|
|
||||||
*
|
|
||||||
* @param p joueur (PLAYER1/PLAYER2)
|
|
||||||
* @return couleur correspondante (YELLOW/RED)
|
|
||||||
*/
|
|
||||||
private Color colorForPlayer(Player p) {
|
|
||||||
return (p == Player.PLAYER1 ? Color.YELLOW : Color.RED);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Indique si la partie est terminée.
|
|
||||||
* Ici : fin lorsque l’itérateur de coups légaux ne produit plus aucun coup.
|
|
||||||
*
|
|
||||||
* @return true si aucun coup n’est possible
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean isGameOver() {
|
|
||||||
if (gameOver) return true;
|
|
||||||
|
|
||||||
Iterator<AbstractPly> it = iterator();
|
|
||||||
if (it.hasNext()) return false;
|
|
||||||
|
|
||||||
gameOver = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne le résultat si la partie est terminée.
|
|
||||||
* Règle utilisée ici : comparaison du nombre de tours contrôlées par chaque couleur.
|
|
||||||
*
|
|
||||||
* @return WIN / LOSS / DRAW ou null si partie non terminée
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Result getResult() {
|
|
||||||
if (!isGameOver()) return null;
|
|
||||||
if (result != null) return result;
|
|
||||||
|
|
||||||
int yellow = 0;
|
|
||||||
int red = 0;
|
|
||||||
|
|
||||||
for (int r = 0; r < SIZE; r++)
|
|
||||||
for (int c = 0; c < SIZE; c++) {
|
|
||||||
Tower t = grid[r][c];
|
|
||||||
if (t == null) continue;
|
|
||||||
|
|
||||||
if (t.getColor() == Color.YELLOW) yellow++;
|
|
||||||
else if (t.getColor() == Color.RED) red++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (yellow > red) result = Result.WIN;
|
|
||||||
else if (yellow < red) result = Result.LOSS;
|
|
||||||
else result = Result.DRAW;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Teste si un coup est légal selon les règles implémentées :
|
|
||||||
* - coup de type AvalamPly
|
|
||||||
* - source/destination dans le plateau et différentes
|
|
||||||
* - source et destination non null
|
|
||||||
* - la tour source appartient au joueur courant (couleur du sommet)
|
|
||||||
* - cases adjacentes
|
|
||||||
* - couleurs différentes entre source et destination (règle de ce projet)
|
|
||||||
* - hauteur finale <= MAX_HEIGHT
|
|
||||||
*
|
|
||||||
* @param c coup à tester
|
|
||||||
* @return true si le coup est légal
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean isLegal(AbstractPly c) {
|
|
||||||
if (!(c instanceof AvalamPly)) return false;
|
|
||||||
AvalamPly p = (AvalamPly) c;
|
|
||||||
|
|
||||||
int xF = p.getXFrom(), yF = p.getYFrom();
|
|
||||||
int xT = p.getXTo(), yT = p.getYTo();
|
|
||||||
|
|
||||||
if (!inBounds(xF, yF) || !inBounds(xT, yT)) return false;
|
|
||||||
if (xF == xT && yF == yT) return false;
|
|
||||||
|
|
||||||
Tower src = grid[xF][yF];
|
|
||||||
Tower dst = grid[xT][yT];
|
|
||||||
if (src == null || dst == null) return false;
|
|
||||||
|
|
||||||
if (src.getColor() != colorForPlayer(getCurrentPlayer())) return false;
|
|
||||||
if (!areAdjacent(xF, yF, xT, yT)) return false;
|
|
||||||
if (src.getColor() == dst.getColor()) return false;
|
|
||||||
if (src.getHeight() + dst.getHeight() > MAX_HEIGHT) return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Applique un coup légal :
|
|
||||||
* - fusion de la tour source sur la destination
|
|
||||||
* - la case source devient vide
|
|
||||||
* - passage au joueur suivant via super.doPly
|
|
||||||
*
|
|
||||||
* @param c coup à jouer
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void doPly(AbstractPly c) {
|
|
||||||
if (!isLegal(c)) throw new IllegalArgumentException("Coup illégal : " + c);
|
|
||||||
|
|
||||||
AvalamPly p = (AvalamPly) c;
|
|
||||||
|
|
||||||
int xF = p.getXFrom(), yF = p.getYFrom();
|
|
||||||
int xT = p.getXTo(), yT = p.getYTo();
|
|
||||||
|
|
||||||
Tower src = grid[xF][yF];
|
|
||||||
Tower dst = grid[xT][yT];
|
|
||||||
|
|
||||||
dst.mergeTower(src);
|
|
||||||
grid[xF][yF] = null;
|
|
||||||
|
|
||||||
super.doPly(c);
|
|
||||||
|
|
||||||
gameOver = false;
|
|
||||||
result = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne un itérateur sur tous les coups légaux du joueur courant.
|
|
||||||
* Génération brute : pour chaque case et chaque voisin (8 directions), on teste isLegal().
|
|
||||||
*
|
|
||||||
* @return itérateur de coups possibles (AbstractPly)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Iterator<AbstractPly> iterator() {
|
|
||||||
java.util.List<AbstractPly> moves = new java.util.ArrayList<>();
|
|
||||||
|
|
||||||
Player cur = getCurrentPlayer();
|
|
||||||
|
|
||||||
for (int r = 0; r < SIZE; r++) {
|
|
||||||
for (int c = 0; c < SIZE; c++) {
|
|
||||||
for (int dr = -1; dr <= 1; dr++) {
|
|
||||||
for (int dc = -1; dc <= 1; dc++) {
|
|
||||||
if (dr == 0 && dc == 0) continue;
|
|
||||||
|
|
||||||
int nr = r + dr, nc = c + dc;
|
|
||||||
AvalamPly p = new AvalamPly(cur, r, c, nr, nc);
|
|
||||||
|
|
||||||
if (isLegal(p)) moves.add(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return moves.iterator();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne une copie “sûre” de l’état du plateau.
|
|
||||||
* Ici, la grille est recopiée case par case (copie des références Tower).
|
|
||||||
*
|
|
||||||
* @return copie du plateau (IBoard)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public IBoard safeCopy() {
|
|
||||||
|
|
||||||
Tower[][] newGrid = new Tower[SIZE][SIZE];
|
|
||||||
|
|
||||||
for (int r = 0; r < SIZE; r++) {
|
|
||||||
for (int c = 0; c < SIZE; c++) {
|
|
||||||
|
|
||||||
Tower t = grid[r][c];
|
|
||||||
if (t == null) {
|
|
||||||
newGrid[r][c] = null;
|
|
||||||
} else {
|
|
||||||
// Copie profonde : on recrée une nouvelle Tower indépendante
|
|
||||||
newGrid[r][c] = new Tower(t.getHeight(), t.getColor());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// On conserve le joueur courant
|
|
||||||
return new AvalamBoard(newGrid, getCurrentPlayer());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.GameAPI.AbstractPly;
|
|
||||||
import fr.iut_fbleau.GameAPI.Player;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>AvalamPly</code>
|
|
||||||
*
|
|
||||||
* Représente un coup (ply) dans le jeu Avalam.
|
|
||||||
* Un coup est défini par :
|
|
||||||
* - un joueur (PLAYER1 ou PLAYER2)
|
|
||||||
* - 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 {
|
|
||||||
|
|
||||||
//Attributs
|
|
||||||
|
|
||||||
/** Coordonnée ligne de la case source. */
|
|
||||||
private final int xFrom;
|
|
||||||
|
|
||||||
/** Coordonnée colonne de la case source. */
|
|
||||||
private final int yFrom;
|
|
||||||
|
|
||||||
/** Coordonnée ligne de la case destination. */
|
|
||||||
private final int xTo;
|
|
||||||
|
|
||||||
/** Coordonnée colonne de la case destination. */
|
|
||||||
private final int yTo;
|
|
||||||
|
|
||||||
//Constructeur
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit un coup Avalam.
|
|
||||||
*
|
|
||||||
* @param player joueur qui joue le coup
|
|
||||||
* @param xFrom ligne d'origine
|
|
||||||
* @param yFrom colonne d'origine
|
|
||||||
* @param xTo ligne de destination
|
|
||||||
* @param yTo colonne de destination
|
|
||||||
*/
|
|
||||||
public AvalamPly(Player player, int xFrom, int yFrom, int xTo, int yTo) {
|
|
||||||
super(player);
|
|
||||||
this.xFrom = xFrom;
|
|
||||||
this.yFrom = yFrom;
|
|
||||||
this.xTo = xTo;
|
|
||||||
this.yTo = yTo;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Méthodes
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne la ligne d'origine.
|
|
||||||
*
|
|
||||||
* @return ligne source
|
|
||||||
*/
|
|
||||||
public int getXFrom() {
|
|
||||||
return xFrom;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne la colonne d'origine.
|
|
||||||
*
|
|
||||||
* @return colonne source
|
|
||||||
*/
|
|
||||||
public int getYFrom() {
|
|
||||||
return yFrom;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne la ligne de destination.
|
|
||||||
*
|
|
||||||
* @return ligne destination
|
|
||||||
*/
|
|
||||||
public int getXTo() {
|
|
||||||
return xTo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne la colonne de destination.
|
|
||||||
*
|
|
||||||
* @return colonne destination
|
|
||||||
*/
|
|
||||||
public int getYTo() {
|
|
||||||
return yTo;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Affichage
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne une représentation textuelle du coup.
|
|
||||||
*
|
|
||||||
* @return chaîne décrivant le coup
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "AvalamPly{" +
|
|
||||||
"player=" + getPlayer() +
|
|
||||||
", (" + xFrom + "," + yFrom + ") -> (" + xTo + "," + yTo + ")" +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,318 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.Bot.AlphaBetaBot;
|
|
||||||
// A FAIRE PLUS TARD (PVGOD)
|
|
||||||
import fr.iut_fbleau.Bot.DivineBot;
|
|
||||||
import fr.iut_fbleau.Bot.IdiotBot;
|
|
||||||
import fr.iut_fbleau.GameAPI.AbstractPly;
|
|
||||||
import fr.iut_fbleau.GameAPI.Player;
|
|
||||||
import fr.iut_fbleau.GameAPI.Result;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>AvalamWindow</code>
|
|
||||||
*
|
|
||||||
* Fenêtre principale (interface graphique) du jeu Avalam.
|
|
||||||
* Elle contient :
|
|
||||||
* - le plateau (BoardView)
|
|
||||||
* - l’affichage du score (ScoreView)
|
|
||||||
* - l’affichage du joueur courant (TurnView)
|
|
||||||
*
|
|
||||||
* Elle pilote un objet <code>AvalamBoard</code> (moteur du jeu).
|
|
||||||
* Elle peut fonctionner en mode :
|
|
||||||
* - joueur vs joueur
|
|
||||||
* - joueur vs bot idiot (aléatoire)
|
|
||||||
* - joueur vs bot alpha (cut-off)
|
|
||||||
* - joueur vs bot divin (PVGOD)
|
|
||||||
*
|
|
||||||
* @version 1.0
|
|
||||||
* Date :
|
|
||||||
* Licence :
|
|
||||||
*/
|
|
||||||
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;
|
|
||||||
|
|
||||||
private final DivineBot divineBot;
|
|
||||||
|
|
||||||
// A FAIRE PLUS TARD (PVGOD)
|
|
||||||
// /** Bot Divin (utilisé si mode PVGOD). */
|
|
||||||
// private final DivineBot divineBot;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A FAIRE PLUS TARD (PVGOD)
|
|
||||||
* On garde l'attribut à null pour ne pas casser la compilation,
|
|
||||||
* mais toute la logique PVGOD est désactivée/commentée.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/** Indique si une animation de tour de bot est en cours. */
|
|
||||||
private boolean botAnimating = false;
|
|
||||||
|
|
||||||
//Constructeur
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit la fenêtre en mode joueur vs joueur.
|
|
||||||
*/
|
|
||||||
public AvalamWindow() {
|
|
||||||
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;
|
|
||||||
|
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
setLayout(new BorderLayout());
|
|
||||||
|
|
||||||
// Chargement du plateau initial
|
|
||||||
Tower[][] initialGrid = BoardLoader.loadFromFile("fr/iut_fbleau/Res/Plateau.txt");
|
|
||||||
|
|
||||||
// Initialisation du moteur (PLAYER1 commence)
|
|
||||||
board = new AvalamBoard(initialGrid);
|
|
||||||
|
|
||||||
// Panneau supérieur (score + tour)
|
|
||||||
JPanel topPanel = new JPanel(new GridLayout(2, 1));
|
|
||||||
topPanel.setBackground(new java.awt.Color(200, 200, 200));
|
|
||||||
|
|
||||||
scoreView = new ScoreView(
|
|
||||||
computeScore(Color.YELLOW),
|
|
||||||
computeScore(Color.RED)
|
|
||||||
);
|
|
||||||
|
|
||||||
turnView = new TurnView(turnMessage());
|
|
||||||
|
|
||||||
topPanel.add(scoreView);
|
|
||||||
topPanel.add(turnView);
|
|
||||||
|
|
||||||
add(topPanel, BorderLayout.NORTH);
|
|
||||||
|
|
||||||
// Plateau
|
|
||||||
boardView = new BoardView(board, this::onBoardUpdated);
|
|
||||||
add(boardView, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
pack();
|
|
||||||
setResizable(false);
|
|
||||||
setLocationRelativeTo(null);
|
|
||||||
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.
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,159 +0,0 @@
|
|||||||
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,60 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* L'énumération <code>Color</code>
|
|
||||||
*
|
|
||||||
* Représente les deux couleurs utilisées dans Avalam :
|
|
||||||
* - YELLOW (jaune)
|
|
||||||
* - RED (rouge)
|
|
||||||
*
|
|
||||||
* Chaque valeur est associée à une couleur Swing (<code>java.awt.Color</code>)
|
|
||||||
* pour l’affichage graphique et peut être convertie en <code>Player</code> (GameAPI).
|
|
||||||
*/
|
|
||||||
public enum Color {
|
|
||||||
|
|
||||||
//Attributs
|
|
||||||
|
|
||||||
/** 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;
|
|
||||||
|
|
||||||
//Constructeur
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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() {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
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
|
|
||||||
ARENA // bot vs bot (mode arène)
|
|
||||||
}
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,188 +0,0 @@
|
|||||||
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,82 +0,0 @@
|
|||||||
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 static void main(String[] args) {
|
|
||||||
SwingUtilities.invokeLater(() -> {
|
|
||||||
showModeSelection();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Affiche le menu de sélection du mode de jeu.
|
|
||||||
* Peut être appelé depuis d'autres fenêtres pour revenir au menu.
|
|
||||||
*/
|
|
||||||
public static void showModeSelection() {
|
|
||||||
String[] options = {
|
|
||||||
"joueur vs joueur",
|
|
||||||
"joueur vs botidiot",
|
|
||||||
"joueur vs bot alpha",
|
|
||||||
"joueur vs bot divin (NON IMPLEMENTE)",
|
|
||||||
"Arène"
|
|
||||||
};
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
// Mode Arène
|
|
||||||
if (choice == 4) {
|
|
||||||
new ArenaWindow();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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,107 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>PieceButton</code>
|
|
||||||
*
|
|
||||||
* Représente graphiquement une tour Avalam sous forme d’un bouton rond.
|
|
||||||
* Chaque bouton :
|
|
||||||
* - possède une couleur correspondant au joueur,
|
|
||||||
* - affiche la hauteur de la tour,
|
|
||||||
* - réagit au survol de la souris.
|
|
||||||
*
|
|
||||||
* Ce composant permet l’interaction avec les pièces du plateau.
|
|
||||||
*/
|
|
||||||
public class PieceButton extends JButton {
|
|
||||||
|
|
||||||
//Attributs
|
|
||||||
|
|
||||||
/** Couleur graphique du pion. */
|
|
||||||
private java.awt.Color color;
|
|
||||||
|
|
||||||
/** Hauteur de la tour affichée. */
|
|
||||||
private int height;
|
|
||||||
|
|
||||||
/** Indique si la souris survole le bouton. */
|
|
||||||
private boolean hover = false;
|
|
||||||
|
|
||||||
/** Position de la tour sur la grille (ligne). */
|
|
||||||
public final int row;
|
|
||||||
|
|
||||||
/** Position de la tour sur la grille (colonne). */
|
|
||||||
public final int col;
|
|
||||||
|
|
||||||
//Constructeur
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit un bouton représentant une tour Avalam.
|
|
||||||
*
|
|
||||||
* @param c couleur graphique du pion
|
|
||||||
* @param height hauteur de la tour
|
|
||||||
* @param row ligne du pion
|
|
||||||
* @param col colonne du pion
|
|
||||||
*/
|
|
||||||
public PieceButton(java.awt.Color c, int height, int row, int col) {
|
|
||||||
this.color = c;
|
|
||||||
this.height = height;
|
|
||||||
this.row = row;
|
|
||||||
this.col = col;
|
|
||||||
|
|
||||||
setBorderPainted(false);
|
|
||||||
setContentAreaFilled(false);
|
|
||||||
setFocusPainted(false);
|
|
||||||
setOpaque(false);
|
|
||||||
|
|
||||||
addMouseListener(new java.awt.event.MouseAdapter() {
|
|
||||||
@Override public void mouseEntered(java.awt.event.MouseEvent e) { hover = true; repaint(); }
|
|
||||||
@Override public void mouseExited (java.awt.event.MouseEvent e) { hover = false; repaint(); }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//Méthodes
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dessine le pion rond ainsi que sa hauteur au centre.
|
|
||||||
*
|
|
||||||
* @param g contexte graphique
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void paintComponent(Graphics g) {
|
|
||||||
Graphics2D g2 = (Graphics2D) g.create();
|
|
||||||
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
||||||
|
|
||||||
// Couleur foncée lorsque survolé
|
|
||||||
g2.setColor(hover ? color.darker() : color);
|
|
||||||
g2.fillOval(0, 0, getWidth(), getHeight());
|
|
||||||
|
|
||||||
// Hauteur affichée au centre
|
|
||||||
g2.setColor(java.awt.Color.BLACK);
|
|
||||||
g2.setFont(new Font("Arial", Font.BOLD, 18));
|
|
||||||
|
|
||||||
String txt = String.valueOf(height);
|
|
||||||
FontMetrics fm = g2.getFontMetrics();
|
|
||||||
|
|
||||||
int x = (getWidth() - fm.stringWidth(txt)) / 2;
|
|
||||||
int y = (getHeight() + fm.getAscent()) / 2 - 3;
|
|
||||||
|
|
||||||
g2.drawString(txt, x, y);
|
|
||||||
g2.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
//Affichage
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
public boolean contains(int x, int y) {
|
|
||||||
double dx = x - getWidth()/2.0;
|
|
||||||
double dy = y - getHeight()/2.0;
|
|
||||||
return dx*dx + dy*dy <= (getWidth()/2.0)*(getWidth()/2.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>PieceLayer</code>
|
|
||||||
*
|
|
||||||
* Gère l'affichage des tours du plateau Avalam sous forme de boutons.
|
|
||||||
* Elle :
|
|
||||||
* - affiche les pièces,
|
|
||||||
* - positionne les boutons sur la grille,
|
|
||||||
* - associe une action de clic à chaque pièce.
|
|
||||||
*
|
|
||||||
* Aucune logique de jeu n'est implémentée ici.
|
|
||||||
*/
|
|
||||||
public class PieceLayer extends JPanel {
|
|
||||||
|
|
||||||
//Constructeur
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit la couche graphique des pièces.
|
|
||||||
*/
|
|
||||||
public PieceLayer() {
|
|
||||||
setLayout(null);
|
|
||||||
setOpaque(false);
|
|
||||||
setBounds(0, 0, 800, 800);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Méthodes
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Affiche la grille de tours sous forme de boutons interactifs.
|
|
||||||
*
|
|
||||||
* @param grid grille 9×9 des tours
|
|
||||||
* @param xBase décalage horizontal du plateau
|
|
||||||
* @param yBase décalage vertical du plateau
|
|
||||||
* @param spacing espacement entre cases
|
|
||||||
* @param size taille d'un pion
|
|
||||||
* @param clickCallback fonction appelée lors d’un clic sur un pion
|
|
||||||
*/
|
|
||||||
public void displayGrid(Tower[][] grid, int xBase, int yBase,
|
|
||||||
int spacing, int size,
|
|
||||||
java.util.function.BiConsumer<Integer, Integer> clickCallback) {
|
|
||||||
|
|
||||||
removeAll();
|
|
||||||
|
|
||||||
for (int r = 0; r < 9; r++) {
|
|
||||||
for (int c = 0; c < 9; c++) {
|
|
||||||
|
|
||||||
Tower t = grid[r][c];
|
|
||||||
if (t != null) {
|
|
||||||
|
|
||||||
PieceButton pb = new PieceButton(
|
|
||||||
t.getColor().getSwingColor(),
|
|
||||||
t.getHeight(),
|
|
||||||
r, c
|
|
||||||
);
|
|
||||||
|
|
||||||
pb.setBounds(xBase + c * spacing, yBase + r * spacing, size, size);
|
|
||||||
|
|
||||||
int fr = r, fc = c;
|
|
||||||
pb.addActionListener(e -> clickCallback.accept(fr, fc));
|
|
||||||
|
|
||||||
add(pb);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
revalidate();
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
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,90 +0,0 @@
|
|||||||
package fr.iut_fbleau.Avalam;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La classe <code>Tower</code>
|
|
||||||
*
|
|
||||||
* Représente une tour dans le jeu Avalam.
|
|
||||||
* 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 {
|
|
||||||
//Attributs
|
|
||||||
|
|
||||||
/** Hauteur de la tour (nombre de pions empilés). */
|
|
||||||
private byte height;
|
|
||||||
|
|
||||||
/** Couleur du sommet de la tour (propriétaire actuel). */
|
|
||||||
private Color color;
|
|
||||||
|
|
||||||
//Constructeur
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit une tour avec une hauteur et une couleur données.
|
|
||||||
*
|
|
||||||
* @param height hauteur initiale
|
|
||||||
* @param color couleur du sommet
|
|
||||||
*/
|
|
||||||
public Tower(int height, Color color) {
|
|
||||||
this.height = (byte) height;
|
|
||||||
this.color = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit une tour de hauteur 1 avec la couleur donnée.
|
|
||||||
* (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 + ")";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
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,217 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,185 +0,0 @@
|
|||||||
package fr.iut_fbleau.Bot;
|
|
||||||
|
|
||||||
import fr.iut_fbleau.Avalam.*;
|
|
||||||
import fr.iut_fbleau.GameAPI.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bot "Divin" (alpha-beta + évaluateur pondéré).
|
|
||||||
* * Idée :
|
|
||||||
* - Utilise l'algorithme Alpha-Beta pour anticiper les coups.
|
|
||||||
* - Évalue les plateaux non terminaux en accordant plus d'importance aux tours hautes.
|
|
||||||
*/
|
|
||||||
public class DivineBot extends AbstractGamePlayer {
|
|
||||||
|
|
||||||
// Attributs
|
|
||||||
|
|
||||||
/** Joueur contrôlé par ce bot (PLAYER1 ou PLAYER2). */
|
|
||||||
private final Player me;
|
|
||||||
|
|
||||||
/** Profondeur maximale de recherche avant évaluation. */
|
|
||||||
private final int maxDepth;
|
|
||||||
|
|
||||||
/** Générateur aléatoire pour choisir parmi les meilleurs coups équivalents. */
|
|
||||||
private final Random rng = new Random();
|
|
||||||
|
|
||||||
// Constructeur
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit le bot Divine.
|
|
||||||
*
|
|
||||||
* @param p joueur contrôlé par ce bot
|
|
||||||
* @param maxDepth profondeur de l'arbre de recherche
|
|
||||||
*/
|
|
||||||
public DivineBot(Player p, int maxDepth) {
|
|
||||||
super(p);
|
|
||||||
this.me = p;
|
|
||||||
this.maxDepth = Math.max(1, maxDepth);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Méthodes
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Méthode principale de décision du bot.
|
|
||||||
* Explore le premier niveau de l'arbre et lance les appels Alpha-Beta.
|
|
||||||
* * @param board état actuel du jeu
|
|
||||||
* @return le meilleur coup calculé (AbstractPly)
|
|
||||||
*/
|
|
||||||
@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);
|
|
||||||
|
|
||||||
// Appel récursif pour évaluer la suite du coup
|
|
||||||
int value = alphaBeta(next, maxDepth - 1, alpha, beta);
|
|
||||||
|
|
||||||
if (isMax) {
|
|
||||||
if (value > bestValue) {
|
|
||||||
bestValue = value;
|
|
||||||
bestMoves.clear();
|
|
||||||
bestMoves.add(m);
|
|
||||||
} else if (value == bestValue) {
|
|
||||||
bestMoves.add(m);
|
|
||||||
}
|
|
||||||
alpha = Math.max(alpha, bestValue);
|
|
||||||
} else {
|
|
||||||
if (value < bestValue) {
|
|
||||||
bestValue = value;
|
|
||||||
bestMoves.clear();
|
|
||||||
bestMoves.add(m);
|
|
||||||
} else if (value == bestValue) {
|
|
||||||
bestMoves.add(m);
|
|
||||||
}
|
|
||||||
beta = Math.min(beta, bestValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Retourne un coup au hasard parmi les meilleurs ex-aequo
|
|
||||||
return bestMoves.get(rng.nextInt(bestMoves.size()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Algorithme récursif de recherche avec élagage Alpha-Beta.
|
|
||||||
*/
|
|
||||||
private int alphaBeta(IBoard board, int depth, int alpha, int beta) {
|
|
||||||
|
|
||||||
// Cas de base : fin de partie ou limite de profondeur atteinte
|
|
||||||
if (board.isGameOver()) return terminalValue(board);
|
|
||||||
if (depth == 0) return evaluate(board);
|
|
||||||
|
|
||||||
boolean isMax = board.getCurrentPlayer() == me;
|
|
||||||
|
|
||||||
for (AbstractPly m : listMoves(board)) {
|
|
||||||
IBoard next = board.safeCopy();
|
|
||||||
next.doPly(m);
|
|
||||||
|
|
||||||
int val = alphaBeta(next, depth - 1, alpha, beta);
|
|
||||||
|
|
||||||
if (isMax) {
|
|
||||||
alpha = Math.max(alpha, val);
|
|
||||||
if (alpha >= beta) break; // Coupure Beta
|
|
||||||
} else {
|
|
||||||
beta = Math.min(beta, val);
|
|
||||||
if (alpha >= beta) break; // Coupure Alpha
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return isMax ? alpha : beta;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calcule la valeur de l'état final (Victoire / Défaite).
|
|
||||||
*/
|
|
||||||
private int terminalValue(IBoard board) {
|
|
||||||
Result r = board.getResult();
|
|
||||||
if (r == null) return 0;
|
|
||||||
|
|
||||||
if (r == Result.DRAW) return 0;
|
|
||||||
|
|
||||||
boolean botIsP1 = (me == Player.PLAYER1);
|
|
||||||
// Si le bot gagne, valeur positive élevée, sinon valeur négative
|
|
||||||
return ((r == Result.WIN) == botIsP1) ? 100000 : -100000;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Heuristique évoluée pour Avalam :
|
|
||||||
* Calcule un score basé sur le contrôle des tours et leur hauteur.
|
|
||||||
* Les tours de hauteur 5 sont prioritaires car elles sont bloquées.
|
|
||||||
*/
|
|
||||||
private int evaluate(IBoard board) {
|
|
||||||
|
|
||||||
if (!(board instanceof AvalamBoard)) return 0;
|
|
||||||
AvalamBoard b = (AvalamBoard) board;
|
|
||||||
|
|
||||||
Color myColor = (me == Player.PLAYER1) ? Color.YELLOW : Color.RED;
|
|
||||||
Color oppColor = (myColor == Color.YELLOW) ? Color.RED : Color.YELLOW;
|
|
||||||
|
|
||||||
int score = 0;
|
|
||||||
|
|
||||||
for (int r = 0; r < AvalamBoard.SIZE; r++) {
|
|
||||||
for (int c = 0; c < AvalamBoard.SIZE; c++) {
|
|
||||||
|
|
||||||
Tower t = b.getTowerAt(r, c);
|
|
||||||
if (t == null) continue;
|
|
||||||
|
|
||||||
int h = t.getHeight();
|
|
||||||
|
|
||||||
// Pondération selon la hauteur (heuristique "Divine")
|
|
||||||
int value =
|
|
||||||
(h == 5) ? 1000 :
|
|
||||||
(h == 4) ? 300 :
|
|
||||||
(h == 3) ? 120 :
|
|
||||||
(h == 2) ? 40 : 10;
|
|
||||||
|
|
||||||
if (t.getColor() == myColor) score += value;
|
|
||||||
else score -= value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return score;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Génère la liste de tous les coups possibles sur le plateau donné.
|
|
||||||
*/
|
|
||||||
private List<AbstractPly> listMoves(IBoard board) {
|
|
||||||
List<AbstractPly> moves = new ArrayList<>();
|
|
||||||
board.iterator().forEachRemaining(moves::add);
|
|
||||||
return moves;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
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()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 31 KiB |
@@ -1,9 +0,0 @@
|
|||||||
0,0,1,2,0,0,0,0,0
|
|
||||||
0,1,2,1,2,0,0,0,0
|
|
||||||
0,2,1,2,1,2,1,0,0
|
|
||||||
0,1,2,1,2,1,2,1,2
|
|
||||||
1,2,1,2,0,2,1,2,1
|
|
||||||
2,1,2,1,2,1,2,1,0
|
|
||||||
0,0,1,2,1,2,1,2,0
|
|
||||||
0,0,0,0,2,1,2,1,0
|
|
||||||
0,0,0,0,0,2,1,0,0
|
|
||||||
Reference in New Issue
Block a user