Jeu jouable avec fin de parti

This commit is contained in:
2025-11-25 16:02:23 -05:00
parent d23aeb266f
commit 1921b523c6
11 changed files with 537 additions and 913 deletions

View File

@@ -1,42 +1,139 @@
package fr.iut_fbleau.Avalam;
import fr.iut_fbleau.Avalam.logic.*;
import fr.iut_fbleau.Avalam.ui.*;
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 - Plateau Graphique");
super("Avalam");
setSize(750, 800);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
Tower[][] grid = BoardLoader.loadFromFile("fr/iut_fbleau/Res/Plateau.txt");
GameState gs = new GameState(grid);
ScoreManager sm = new ScoreManager();
// ----------------------------------------------------------
// Chargement du plateau initial depuis Plateau.txt
// ----------------------------------------------------------
Tower[][] initialGrid = BoardLoader.loadFromFile("fr/iut_fbleau/Res/Plateau.txt");
board = new AvalamBoard(initialGrid); // PLAYER1 commence
int y = sm.count(Color.COLOR1, grid);
int r = sm.count(Color.COLOR2, grid);
// ----------------------------------------------------------
// PANNEAU SCORE + TOUR
// ----------------------------------------------------------
JPanel topPanel = new JPanel(new GridLayout(2, 1));
topPanel.setBackground(new java.awt.Color(200, 200, 200));
ScoreView scoreView = new ScoreView(y, r);
TurnView turnView = new TurnView("Tour du joueur : Jaune");
scoreView = new ScoreView(
computeScore(Color.YELLOW),
computeScore(Color.RED)
);
BoardView boardView = new BoardView(gs);
turnView = new TurnView(turnMessage());
JPanel top = new JPanel(new GridLayout(2,1));
top.add(scoreView);
top.add(turnView);
topPanel.add(scoreView);
topPanel.add(turnView);
add(topPanel, BorderLayout.NORTH);
// ----------------------------------------------------------
// PLATEAU (BoardView)
// ----------------------------------------------------------
boardView = new BoardView(board, this::onBoardUpdated);
add(top, BorderLayout.NORTH);
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");
}
}