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

142 lines
4.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package fr.iut_fbleau.Avalam;
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;
import javax.swing.*;
import java.awt.*;
/**
* Fenêtre principale du jeu Avalam.
*
* Elle contient :
* - le plateau (BoardView)
* - l'affichage du score
* - l'affichage du joueur courant
*
* Elle interagit directement avec AvalamBoard (moteur du jeu).
*/
public class AvalamWindow extends JFrame {
/** Moteur du jeu (API GameAPI) */
private AvalamBoard board;
/** Vues graphiques */
private ScoreView scoreView;
private TurnView turnView;
private BoardView boardView;
public AvalamWindow() {
super("Avalam");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// ----------------------------------------------------------
// Chargement du plateau initial depuis Plateau.txt
// ----------------------------------------------------------
Tower[][] initialGrid = BoardLoader.loadFromFile("fr/iut_fbleau/Res/Plateau.txt");
// debug TEMP !!!!!!!!!
System.out.println("DEBUG Plateau: Grid[0][0] = " + initialGrid[0][0]);
board = new AvalamBoard(initialGrid); // PLAYER1 commence
// ----------------------------------------------------------
// PANNEAU 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)
// ----------------------------------------------------------
boardView = new BoardView(board, this::onBoardUpdated);
add(boardView, BorderLayout.CENTER);
pack();
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
/* ================================================================
* 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) {
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.";
}
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");
}
}