forked from menault/TD3_DEV51_Qualite_Algo
190 lines
6.3 KiB
Java
190 lines
6.3 KiB
Java
package front;
|
|
|
|
import back.*;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Interface graphique du jeu du pendu avec gestion des difficultés.
|
|
* - Niveau Facile : mots de moins de 8 lettres
|
|
* - Niveau Moyen : mots de 8 lettres ou plus
|
|
* - Niveau Difficile : deux mots à deviner à la suite (un court + un long)
|
|
* Toutes les méthodes ≤ 50 lignes.
|
|
*/
|
|
public class GameUI {
|
|
private JFrame frame;
|
|
private JLabel imgLabel, wordLabel, triedLabel, scoreLabel, timeLabel;
|
|
private JTextField input;
|
|
private JButton tryBtn, quitBtn;
|
|
|
|
private Game game;
|
|
private List<String> words;
|
|
private int index = 0;
|
|
private int level;
|
|
private String currentWord = "";
|
|
private Timer timer;
|
|
|
|
/** 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) */
|
|
public void show() {
|
|
setupWindow();
|
|
setupLayout();
|
|
setupActions();
|
|
prepareWords();
|
|
startRound();
|
|
frame.setVisible(true);
|
|
}
|
|
|
|
/** 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);
|
|
}
|
|
|
|
if (words.isEmpty()) words.add(Words.random()); // fallback si rien
|
|
}
|
|
|
|
/** Crée la fenêtre principale */
|
|
private void setupWindow() {
|
|
frame = new JFrame("Jeu du Pendu");
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
frame.setSize(560, 560);
|
|
frame.setLocationRelativeTo(null);
|
|
frame.setLayout(new BorderLayout(12, 12));
|
|
}
|
|
|
|
/** Construit la mise en page et les composants */
|
|
private void setupLayout() {
|
|
imgLabel = new JLabel("", SwingConstants.CENTER);
|
|
frame.add(imgLabel, BorderLayout.CENTER);
|
|
|
|
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);
|
|
|
|
JPanel bottom = new JPanel(new BorderLayout(8, 8));
|
|
JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
|
input = new JTextField(5);
|
|
tryBtn = new JButton("Essayer");
|
|
quitBtn = new JButton("Quitter");
|
|
inputPanel.add(new JLabel("Lettre :"));
|
|
inputPanel.add(input);
|
|
inputPanel.add(tryBtn);
|
|
bottom.add(inputPanel, BorderLayout.WEST);
|
|
bottom.add(quitBtn, BorderLayout.EAST);
|
|
frame.add(bottom, BorderLayout.SOUTH);
|
|
}
|
|
|
|
/** Construit une ligne (label gauche + espace + label droit) */
|
|
private JPanel buildTopLine(JLabel left, JLabel right) {
|
|
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
|
p.add(left);
|
|
p.add(Box.createHorizontalStrut(24));
|
|
p.add(right);
|
|
return p;
|
|
}
|
|
|
|
/** Branche les actions + timer */
|
|
private void setupActions() {
|
|
tryBtn.addActionListener(this::onTry);
|
|
input.addActionListener(this::onTry);
|
|
quitBtn.addActionListener(e -> frame.dispose());
|
|
timer = new Timer(1000, e -> refreshStatsOnly());
|
|
}
|
|
|
|
/** Démarre un nouveau mot (ou termine au niveau 3) */
|
|
private void startRound() {
|
|
if (index >= words.size()) {
|
|
showMsg("Niveau terminé !");
|
|
frame.dispose();
|
|
return;
|
|
}
|
|
currentWord = words.get(index++);
|
|
game = new Game(currentWord, 7);
|
|
input.setText("");
|
|
input.requestFocusInWindow();
|
|
if (!timer.isRunning()) timer.start();
|
|
refreshUI();
|
|
}
|
|
|
|
/** Tente une lettre (clic bouton ou Entrée) */
|
|
private void onTry(ActionEvent e) {
|
|
String text = input.getText();
|
|
if (!Check.isLetter(text)) {
|
|
showMsg("Tape une seule lettre (A-Z).");
|
|
input.requestFocusInWindow();
|
|
input.selectAll();
|
|
return;
|
|
}
|
|
Result res = game.play(Character.toLowerCase(text.charAt(0)));
|
|
if (res == Result.ALREADY) showMsg("Lettre déjà utilisée.");
|
|
input.setText("");
|
|
refreshUI();
|
|
checkEnd();
|
|
}
|
|
|
|
/** Vérifie la fin du mot et enchaîne si besoin (niveau 3) */
|
|
private void checkEnd() {
|
|
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();
|
|
}
|
|
}
|
|
|
|
/** Rafraîchit image + textes + stats */
|
|
private void refreshUI() {
|
|
imgLabel.setIcon(Gallows.icon(game.getErrors()));
|
|
wordLabel.setText("Mot : " + game.maskedWord());
|
|
triedLabel.setText("Lettres essayées : " + String.join(", ", game.triedLetters()));
|
|
refreshStatsOnly();
|
|
frame.repaint();
|
|
}
|
|
|
|
/** Rafraîchit uniquement score + chrono */
|
|
private void refreshStatsOnly() {
|
|
scoreLabel.setText("Score : " + game.getScore());
|
|
timeLabel.setText("Temps : " + game.getElapsedSeconds() + "s");
|
|
}
|
|
|
|
/** Affiche un message */
|
|
private void showMsg(String msg) {
|
|
JOptionPane.showMessageDialog(frame, msg);
|
|
}
|
|
} |