2025-11-20 13:25:09 -05:00
|
|
|
|
package fr.iut_fbleau.Avalam;
|
|
|
|
|
|
|
2025-11-25 16:02:23 -05:00
|
|
|
|
import fr.iut_fbleau.GameAPI.Player;
|
|
|
|
|
|
import fr.iut_fbleau.GameAPI.Result;
|
2025-11-22 11:56:51 -05:00
|
|
|
|
|
2025-11-20 13:25:09 -05:00
|
|
|
|
import javax.swing.*;
|
2025-11-22 11:56:51 -05:00
|
|
|
|
import java.awt.*;
|
2025-11-20 13:25:09 -05:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-26 06:39:49 -05:00
|
|
|
|
* 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).
|
|
|
|
|
|
*/
|
2025-11-20 13:25:09 -05:00
|
|
|
|
public class AvalamWindow extends JFrame {
|
|
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
//Attributs
|
|
|
|
|
|
|
|
|
|
|
|
/** Moteur du jeu (état + règles). */
|
2025-11-25 16:02:23 -05:00
|
|
|
|
private AvalamBoard board;
|
|
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
/** Vue affichant le score des deux couleurs. */
|
2025-11-25 16:02:23 -05:00
|
|
|
|
private ScoreView scoreView;
|
2026-01-26 06:39:49 -05:00
|
|
|
|
|
|
|
|
|
|
/** Vue affichant le joueur dont c'est le tour. */
|
2025-11-25 16:02:23 -05:00
|
|
|
|
private TurnView turnView;
|
2026-01-26 06:39:49 -05:00
|
|
|
|
|
|
|
|
|
|
/** Vue affichant le plateau et gérant les interactions de jeu. */
|
2025-11-25 16:02:23 -05:00
|
|
|
|
private BoardView boardView;
|
|
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
//Constructeur
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Construit la fenêtre et initialise l’interface :
|
|
|
|
|
|
* - charge le plateau initial depuis Plateau.txt
|
|
|
|
|
|
* - construit les vues (score, tour, plateau)
|
|
|
|
|
|
* - affiche la fenêtre
|
|
|
|
|
|
*/
|
2025-11-20 13:25:09 -05:00
|
|
|
|
public AvalamWindow() {
|
2025-11-25 16:02:23 -05:00
|
|
|
|
super("Avalam");
|
2025-11-20 13:25:09 -05:00
|
|
|
|
|
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
2025-11-25 16:02:23 -05:00
|
|
|
|
setLayout(new BorderLayout());
|
2025-11-20 13:25:09 -05:00
|
|
|
|
|
2025-11-25 16:02:23 -05:00
|
|
|
|
// Chargement du plateau initial depuis Plateau.txt
|
|
|
|
|
|
Tower[][] initialGrid = BoardLoader.loadFromFile("fr/iut_fbleau/Res/Plateau.txt");
|
2025-11-27 13:10:03 +01:00
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
// Initialisation du moteur (PLAYER1 commence)
|
|
|
|
|
|
board = new AvalamBoard(initialGrid);
|
2025-11-22 11:56:51 -05:00
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
// Création du panneau supérieur (score + tour)
|
2025-11-25 16:02:23 -05:00
|
|
|
|
JPanel topPanel = new JPanel(new GridLayout(2, 1));
|
|
|
|
|
|
topPanel.setBackground(new java.awt.Color(200, 200, 200));
|
2025-11-22 11:56:51 -05:00
|
|
|
|
|
2025-11-25 16:02:23 -05:00
|
|
|
|
scoreView = new ScoreView(
|
|
|
|
|
|
computeScore(Color.YELLOW),
|
|
|
|
|
|
computeScore(Color.RED)
|
|
|
|
|
|
);
|
2025-11-22 11:56:51 -05:00
|
|
|
|
|
2025-11-25 16:02:23 -05:00
|
|
|
|
turnView = new TurnView(turnMessage());
|
2025-11-22 11:56:51 -05:00
|
|
|
|
|
2025-11-25 16:02:23 -05:00
|
|
|
|
topPanel.add(scoreView);
|
|
|
|
|
|
topPanel.add(turnView);
|
|
|
|
|
|
|
|
|
|
|
|
add(topPanel, BorderLayout.NORTH);
|
|
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
// Création de la vue plateau (avec callback de mise à jour)
|
2025-11-25 16:02:23 -05:00
|
|
|
|
boardView = new BoardView(board, this::onBoardUpdated);
|
2025-11-22 11:56:51 -05:00
|
|
|
|
add(boardView, BorderLayout.CENTER);
|
2025-11-20 13:25:09 -05:00
|
|
|
|
|
2025-11-25 16:02:23 -05:00
|
|
|
|
pack();
|
|
|
|
|
|
setResizable(false);
|
|
|
|
|
|
setLocationRelativeTo(null);
|
2025-11-20 13:25:09 -05:00
|
|
|
|
setVisible(true);
|
|
|
|
|
|
}
|
2025-11-25 16:02:23 -05:00
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
//Méthodes
|
2025-11-25 16:02:23 -05:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-26 06:39:49 -05:00
|
|
|
|
* Méthode appelée automatiquement après chaque coup (via BoardView).
|
|
|
|
|
|
* Elle rafraîchit :
|
|
|
|
|
|
* - les scores
|
|
|
|
|
|
* - le joueur courant
|
|
|
|
|
|
* et affiche un message si la partie est terminée.
|
|
|
|
|
|
*/
|
2025-11-25 16:02:23 -05:00
|
|
|
|
public void onBoardUpdated() {
|
|
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
// Mise à jour du score
|
2025-11-25 16:02:23 -05:00
|
|
|
|
scoreView.updateScores(
|
|
|
|
|
|
computeScore(Color.YELLOW),
|
|
|
|
|
|
computeScore(Color.RED)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
// Mise à jour du joueur courant
|
2025-11-25 16:02:23 -05:00
|
|
|
|
turnView.setTurn(turnMessage());
|
|
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
// Détection de fin de partie
|
2025-11-25 16:02:23 -05:00
|
|
|
|
if (board.isGameOver()) {
|
|
|
|
|
|
Result res = board.getResult();
|
|
|
|
|
|
|
|
|
|
|
|
String msg;
|
|
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
// Correction : ajout des "break" pour éviter le fall-through.
|
2025-11-25 16:02:23 -05:00
|
|
|
|
switch (res) {
|
2026-01-26 06:39:49 -05:00
|
|
|
|
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;
|
2025-11-25 16:02:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 06:39:49 -05:00
|
|
|
|
JOptionPane.showMessageDialog(
|
|
|
|
|
|
this,
|
|
|
|
|
|
msg,
|
|
|
|
|
|
"Partie terminée",
|
|
|
|
|
|
JOptionPane.INFORMATION_MESSAGE
|
|
|
|
|
|
);
|
2025-11-25 16:02:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-26 06:39:49 -05:00
|
|
|
|
* Calcule le score d'une couleur : nombre de tours contrôlées (sommet de la tour).
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param c couleur à compter
|
|
|
|
|
|
* @return nombre de tours appartenant à la couleur c
|
|
|
|
|
|
*/
|
2025-11-25 16:02:23 -05:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-26 06:39:49 -05:00
|
|
|
|
* Retourne le message correspondant au joueur dont c'est le tour.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return message d’affichage du tour
|
|
|
|
|
|
*/
|
2025-11-25 16:02:23 -05:00
|
|
|
|
private String turnMessage() {
|
2026-01-26 06:39:49 -05:00
|
|
|
|
return "Tour du joueur : " +
|
2025-11-25 16:02:23 -05:00
|
|
|
|
(board.getCurrentPlayer() == Player.PLAYER1 ? "Jaune" : "Rouge");
|
|
|
|
|
|
}
|
2025-11-20 13:25:09 -05:00
|
|
|
|
}
|