Files
BUT3ProjetJeuGroupe/fr/iut_fbleau/Avalam/AvalamWindow.java

141 lines
4.2 KiB
Java
Raw Normal View History

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.Avalam.logic.BoardLoader;
import fr.iut_fbleau.Avalam.ui.BoardView;
import fr.iut_fbleau.Avalam.ui.ScoreView;
import fr.iut_fbleau.Avalam.ui.TurnView;
import fr.iut_fbleau.GameAPI.Player;
import fr.iut_fbleau.GameAPI.Result;
2025-11-20 13:25:09 -05:00
import javax.swing.*;
import java.awt.*;
2025-11-20 13:25:09 -05:00
/**
* Fenêtre principale du jeu Avalam.
2025-11-25 16:02:23 -05:00
*
* Elle contient :
* - le plateau (BoardView)
* - l'affichage du score
* - l'affichage du joueur courant
*
* Elle interagit directement avec AvalamBoard (moteur du jeu).
2025-11-20 13:25:09 -05:00
*/
public class AvalamWindow extends JFrame {
2025-11-25 16:02:23 -05:00
/** Moteur du jeu (API GameAPI) */
private AvalamBoard board;
/** Vues graphiques */
private ScoreView scoreView;
private TurnView turnView;
private BoardView boardView;
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
2025-11-25 16:02:23 -05:00
board = new AvalamBoard(initialGrid); // PLAYER1 commence
2025-11-25 16:02:23 -05:00
// ----------------------------------------------------------
// PANNEAU SCORE + TOUR
// ----------------------------------------------------------
JPanel topPanel = new JPanel(new GridLayout(2, 1));
topPanel.setBackground(new java.awt.Color(200, 200, 200));
2025-11-25 16:02:23 -05:00
scoreView = new ScoreView(
computeScore(Color.YELLOW),
computeScore(Color.RED)
);
2025-11-25 16:02:23 -05:00
turnView = new TurnView(turnMessage());
2025-11-25 16:02:23 -05:00
topPanel.add(scoreView);
topPanel.add(turnView);
add(topPanel, BorderLayout.NORTH);
// ----------------------------------------------------------
// PLATEAU (BoardView)
// ----------------------------------------------------------
boardView = new BoardView(board, this::onBoardUpdated);
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
/* ================================================================
* MISES À JOUR DAPRÈS LE MOTEUR
* ================================================================ */
/**
* Appelé automatiquement après chaque coup via BoardView controller board.
*/
public void onBoardUpdated() {
// Mise à jour du score et du joueur courant
scoreView.updateScores(
computeScore(Color.YELLOW),
computeScore(Color.RED)
);
turnView.setTurn(turnMessage());
// Détection de fin de partie
if (board.isGameOver()) {
Result res = board.getResult();
String msg;
switch (res) {
2025-11-27 13:06:14 +01:00
case WIN : msg = "Le joueur jaune a gagné !";
case LOSS : msg = "Le joueur rouge a gagné !";
case DRAW : msg = "Égalité !";
default : msg = "Fin de partie.";
2025-11-25 16:02:23 -05:00
}
JOptionPane.showMessageDialog(this, msg, "Partie terminée",
JOptionPane.INFORMATION_MESSAGE);
}
}
/* ================================================================
* OUTILS
* ================================================================ */
/**
* Calcule le score d'une couleur : nombre de tours contrôlées.
*/
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;
}
/**
* Message du joueur dont c'est le tour.
*/
private String turnMessage() {
return "Tour du joueur : " +
(board.getCurrentPlayer() == Player.PLAYER1 ? "Jaune" : "Rouge");
}
2025-11-20 13:25:09 -05:00
}