Files
TD3_DEV51_DUCREUX_JANNAIRE/Jeu_pendu/Front/GameUI.java

207 lines
6.7 KiB
Java
Raw Normal View History

2025-10-08 11:57:29 +02:00
package front;
import back.*;
2025-10-08 15:26:45 +02:00
2025-10-08 11:57:29 +02:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
2025-10-08 16:15:19 +02:00
import java.util.ArrayList;
import java.util.List;
2025-10-08 11:57:29 +02:00
/**
2025-10-08 16:15:19 +02:00
* Interface graphique du jeu du pendu avec gestion des difficultés.
2025-10-08 16:20:13 +02:00
* - Niveau 1 : mots de moins de 8 lettres
* - Niveau 2 : mots de 8 lettres ou plus
* - Niveau 3 : deux mots à deviner à la suite (un court + un long)
2025-10-08 16:15:19 +02:00
* Toutes les méthodes 50 lignes.
2025-10-08 11:57:29 +02:00
*/
public class GameUI {
private JFrame frame;
2025-10-08 15:26:45 +02:00
private JLabel imgLabel, wordLabel, triedLabel, scoreLabel, timeLabel;
2025-10-08 11:57:29 +02:00
private JTextField input;
2025-10-08 16:20:13 +02:00
private JButton tryBtn, quitBtn, menuBtn;
2025-10-08 16:15:19 +02:00
2025-10-08 11:57:29 +02:00
private Game game;
2025-10-08 16:15:19 +02:00
private List<String> words;
private int index = 0;
private int level;
private String currentWord = "";
2025-10-08 15:26:45 +02:00
private Timer timer;
2025-10-08 11:57:29 +02:00
2025-10-08 16:15:19 +02:00
/** Constructeur : reçoit la difficulté (1, 2 ou 3) */
public GameUI(int level) {
this.level = level;
}
/** Affiche la fenêtre et lance la partie (ou séquence) */
2025-10-08 11:57:29 +02:00
public void show() {
2025-10-08 15:26:45 +02:00
setupWindow();
setupLayout();
setupActions();
2025-10-08 16:15:19 +02:00
prepareWords();
startRound();
2025-10-08 11:57:29 +02:00
frame.setVisible(true);
}
2025-10-08 16:15:19 +02:00
/** Prépare la liste des mots selon la difficulté choisie */
private void prepareWords() {
words = new ArrayList<String>();
List<String> all = Words.all();
if (level == 1) { // mots courts
for (String w : all) if (w.length() < 8) words.add(w);
} else if (level == 2) { // mots longs
for (String w : all) if (w.length() >= 8) words.add(w);
} else if (level == 3) { // un court + un long
String shortW = null, longW = null;
for (String w : all) {
if (shortW == null && w.length() < 8) shortW = w;
if (longW == null && w.length() >= 8) longW = w;
if (shortW != null && longW != null) break;
}
if (shortW != null) words.add(shortW);
if (longW != null) words.add(longW);
}
2025-10-08 16:20:13 +02:00
if (words.isEmpty()) words.add(Words.random());
2025-10-08 16:15:19 +02:00
}
2025-10-08 15:26:45 +02:00
/** Crée la fenêtre principale */
private void setupWindow() {
2025-10-08 11:57:29 +02:00
frame = new JFrame("Jeu du Pendu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2025-10-08 15:26:45 +02:00
frame.setSize(560, 560);
2025-10-08 11:57:29 +02:00
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout(12, 12));
2025-10-08 15:26:45 +02:00
}
2025-10-08 11:57:29 +02:00
2025-10-08 16:15:19 +02:00
/** Construit la mise en page et les composants */
2025-10-08 15:26:45 +02:00
private void setupLayout() {
2025-10-08 11:57:29 +02:00
imgLabel = new JLabel("", SwingConstants.CENTER);
frame.add(imgLabel, BorderLayout.CENTER);
2025-10-08 15:26:45 +02:00
wordLabel = new JLabel("Mot : ");
triedLabel = new JLabel("Lettres essayées : ");
scoreLabel = new JLabel("Score : 0");
timeLabel = new JLabel("Temps : 0s");
JPanel top = new JPanel(new GridLayout(2, 1));
top.add(buildTopLine(wordLabel, scoreLabel));
top.add(buildTopLine(triedLabel, timeLabel));
frame.add(top, BorderLayout.NORTH);
2025-10-08 16:20:13 +02:00
// --- Bas : boutons + saisie
2025-10-08 11:57:29 +02:00
JPanel bottom = new JPanel(new BorderLayout(8, 8));
JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
input = new JTextField(5);
tryBtn = new JButton("Essayer");
inputPanel.add(new JLabel("Lettre :"));
inputPanel.add(input);
inputPanel.add(tryBtn);
2025-10-08 16:20:13 +02:00
JPanel actionPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
menuBtn = new JButton("Menu");
quitBtn = new JButton("Quitter");
actionPanel.add(menuBtn);
actionPanel.add(quitBtn);
2025-10-08 11:57:29 +02:00
bottom.add(inputPanel, BorderLayout.WEST);
2025-10-08 16:20:13 +02:00
bottom.add(actionPanel, BorderLayout.EAST);
2025-10-08 11:57:29 +02:00
frame.add(bottom, BorderLayout.SOUTH);
2025-10-08 15:26:45 +02:00
}
2025-10-08 11:57:29 +02:00
2025-10-08 16:15:19 +02:00
/** Construit une ligne (label gauche + espace + label droit) */
2025-10-08 15:26:45 +02:00
private JPanel buildTopLine(JLabel left, JLabel right) {
2025-10-08 16:15:19 +02:00
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(left);
p.add(Box.createHorizontalStrut(24));
p.add(right);
return p;
2025-10-08 15:26:45 +02:00
}
2025-10-08 11:57:29 +02:00
2025-10-08 16:15:19 +02:00
/** Branche les actions + timer */
2025-10-08 15:26:45 +02:00
private void setupActions() {
2025-10-08 11:57:29 +02:00
tryBtn.addActionListener(this::onTry);
input.addActionListener(this::onTry);
2025-10-08 16:15:19 +02:00
quitBtn.addActionListener(e -> frame.dispose());
2025-10-08 16:20:13 +02:00
menuBtn.addActionListener(e -> returnToMenu());
2025-10-08 15:26:45 +02:00
timer = new Timer(1000, e -> refreshStatsOnly());
2025-10-08 11:57:29 +02:00
}
2025-10-08 16:20:13 +02:00
/** Retour au menu principal */
private void returnToMenu() {
timer.stop();
frame.dispose();
MenuUI menu = new MenuUI();
menu.show();
}
2025-10-08 16:15:19 +02:00
/** Démarre un nouveau mot (ou termine au niveau 3) */
private void startRound() {
if (index >= words.size()) {
2025-10-08 16:20:13 +02:00
showMsg("🎉 Niveau terminé !");
2025-10-08 16:15:19 +02:00
frame.dispose();
return;
}
currentWord = words.get(index++);
2025-10-08 11:57:29 +02:00
game = new Game(currentWord, 7);
input.setText("");
input.requestFocusInWindow();
2025-10-08 15:26:45 +02:00
if (!timer.isRunning()) timer.start();
2025-10-08 11:57:29 +02:00
refreshUI();
}
2025-10-08 16:15:19 +02:00
/** Tente une lettre (clic bouton ou Entrée) */
2025-10-08 11:57:29 +02:00
private void onTry(ActionEvent e) {
String text = input.getText();
if (!Check.isLetter(text)) {
2025-10-08 15:26:45 +02:00
showMsg("Tape une seule lettre (A-Z).");
2025-10-08 11:57:29 +02:00
input.requestFocusInWindow();
input.selectAll();
return;
}
2025-10-08 15:26:45 +02:00
Result res = game.play(Character.toLowerCase(text.charAt(0)));
2025-10-08 16:15:19 +02:00
if (res == Result.ALREADY) showMsg("Lettre déjà utilisée.");
2025-10-08 15:26:45 +02:00
input.setText("");
refreshUI();
checkEnd();
}
2025-10-08 11:57:29 +02:00
2025-10-08 16:15:19 +02:00
/** Vérifie la fin du mot et enchaîne si besoin (niveau 3) */
2025-10-08 15:26:45 +02:00
private void checkEnd() {
2025-10-08 16:15:19 +02:00
if (!(game.isWin() || game.isLose())) return;
timer.stop();
game.end(game.isWin());
String verdict = game.isWin() ? "Bravo !" : "Perdu !";
String msg = verdict + " Le mot était : " + currentWord
+ "\nScore : " + game.getScore()
+ "\nTemps : " + game.getElapsedSeconds() + "s";
showMsg(msg);
if (level == 3 && index < words.size()) {
startRound();
2025-10-08 11:57:29 +02:00
}
}
2025-10-08 16:15:19 +02:00
/** Rafraîchit image + textes + stats */
2025-10-08 11:57:29 +02:00
private void refreshUI() {
imgLabel.setIcon(Gallows.icon(game.getErrors()));
wordLabel.setText("Mot : " + game.maskedWord());
triedLabel.setText("Lettres essayées : " + String.join(", ", game.triedLetters()));
2025-10-08 15:26:45 +02:00
refreshStatsOnly();
2025-10-08 11:57:29 +02:00
frame.repaint();
}
2025-10-08 15:26:45 +02:00
2025-10-08 16:15:19 +02:00
/** Rafraîchit uniquement score + chrono */
2025-10-08 15:26:45 +02:00
private void refreshStatsOnly() {
scoreLabel.setText("Score : " + game.getScore());
timeLabel.setText("Temps : " + game.getElapsedSeconds() + "s");
}
2025-10-08 16:15:19 +02:00
/** Affiche un message */
2025-10-08 15:26:45 +02:00
private void showMsg(String msg) {
JOptionPane.showMessageDialog(frame, msg);
}
2025-10-08 16:20:13 +02:00
}