forked from menault/TD3_DEV51_Qualite_Algo
234 lines
7.8 KiB
Java
234 lines
7.8 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 pendu avec niveaux :
|
||
* - facile : mots < 8 lettres
|
||
* - moyen : mots ≥ 8 lettres
|
||
* - difficile : deux mots (score + chrono cumulés)
|
||
* Boutons : Essayer / Nouvelle partie / Menu / Quitter.
|
||
* (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, newBtn, menuBtn, quitBtn;
|
||
|
||
private Game game;
|
||
private List<String> words;
|
||
private int index = 0;
|
||
private final int level;
|
||
private String currentWord = "";
|
||
private Timer timer;
|
||
|
||
// Cumul de session (niveau difficile)
|
||
private long sessionStartNano = -1L;
|
||
private int sessionScore = 0;
|
||
|
||
/** Reçoit la difficulté (facile, moyen, difficile). */
|
||
public GameUI(int level) {
|
||
this.level = level;
|
||
}
|
||
|
||
/** Affiche la fenêtre et lance la session. */
|
||
public void show() {
|
||
setupWindow();
|
||
setupLayout();
|
||
setupActions();
|
||
startNewSession();
|
||
frame.setVisible(true);
|
||
}
|
||
|
||
/** Démarre une nouvelle session (nouveaux mots, reset chrono/score session). */
|
||
private void startNewSession() {
|
||
sessionStartNano = System.nanoTime();
|
||
sessionScore = 0;
|
||
index = 0;
|
||
prepareWords();
|
||
startRound();
|
||
}
|
||
|
||
/** Prépare la liste des mots selon le niveau (aléatoire géré dans Words). */
|
||
private void prepareWords() {
|
||
words = new ArrayList<>();
|
||
if (level == 1) {
|
||
words.add(Words.randomShortWord());
|
||
} else if (level == 2) {
|
||
words.add(Words.randomLongWord());
|
||
} else if (level == 3) {
|
||
words = Words.randomPair(); // [court, long] aléatoires
|
||
}
|
||
if (words.isEmpty()) words.add(Words.random()); // filet de sécurité
|
||
}
|
||
|
||
/** 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));
|
||
}
|
||
|
||
/** Layout + 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");
|
||
|
||
JLabel titleLabel = new JLabel("Sauver Michel!!", SwingConstants.CENTER);
|
||
titleLabel.setFont(new Font("Arial", Font.BOLD, 22));
|
||
titleLabel.setForeground(Color.RED);
|
||
|
||
JPanel top = new JPanel(new BorderLayout());
|
||
JPanel infoPanel = new JPanel(new GridLayout(2, 1));
|
||
infoPanel.add(buildTopLine(wordLabel, scoreLabel));
|
||
infoPanel.add(buildTopLine(triedLabel, timeLabel));
|
||
|
||
top.add(titleLabel, BorderLayout.NORTH);
|
||
top.add(infoPanel, BorderLayout.CENTER);
|
||
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");
|
||
newBtn = new JButton("Nouvelle partie");
|
||
inputPanel.add(new JLabel("Lettre :"));
|
||
inputPanel.add(input);
|
||
inputPanel.add(tryBtn);
|
||
inputPanel.add(newBtn);
|
||
|
||
JPanel actionPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
||
menuBtn = new JButton("Menu");
|
||
quitBtn = new JButton("Quitter");
|
||
actionPanel.add(menuBtn);
|
||
actionPanel.add(quitBtn);
|
||
|
||
bottom.add(inputPanel, BorderLayout.WEST);
|
||
bottom.add(actionPanel, BorderLayout.EAST);
|
||
frame.add(bottom, BorderLayout.SOUTH);
|
||
}
|
||
|
||
/** Ligne d’infos (gauche/droite). */
|
||
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;
|
||
}
|
||
|
||
/** Actions + timer. */
|
||
private void setupActions() {
|
||
tryBtn.addActionListener(this::onTry);
|
||
input.addActionListener(this::onTry);
|
||
newBtn.addActionListener(e -> startNewSession());
|
||
menuBtn.addActionListener(e -> returnToMenu());
|
||
quitBtn.addActionListener(e -> frame.dispose());
|
||
timer = new Timer(1000, e -> refreshStatsOnly());
|
||
}
|
||
|
||
/** Retour vers le menu de sélection. */
|
||
private void returnToMenu() {
|
||
timer.stop();
|
||
frame.dispose();
|
||
MenuUI menu = new MenuUI();
|
||
menu.show();
|
||
}
|
||
|
||
/** Démarre un nouveau mot (ou termine la session si plus de mots). */
|
||
private void startRound() {
|
||
if (index >= words.size()) {
|
||
int total = (level == 3) ? sessionScore : game.getScore();
|
||
long secs = (level == 3) ? getSessionSeconds() : game.getElapsedSeconds();
|
||
showMsg("Niveau terminé !\nScore total : " + total + "\nTemps : " + secs + "s");
|
||
return;
|
||
}
|
||
currentWord = words.get(index++);
|
||
game = new Game(currentWord, 7);
|
||
input.setText("");
|
||
input.requestFocusInWindow();
|
||
if (!timer.isRunning()) timer.start();
|
||
refreshUI();
|
||
}
|
||
|
||
/** Tente une lettre (bouton/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();
|
||
}
|
||
|
||
/** Fin du mot → affiche popup et enchaîne (lvl 3 cumule score/chrono). */
|
||
private void checkEnd() {
|
||
if (!(game.isWin() || game.isLose())) return;
|
||
|
||
game.end(game.isWin());
|
||
int displayScore = (level == 3) ? sessionScore + game.getScore() : game.getScore();
|
||
long displaySecs = (level == 3) ? getSessionSeconds() : game.getElapsedSeconds();
|
||
|
||
showMsg((game.isWin() ? "Bravo !" : "Perdu !")
|
||
+ " Le mot était : " + currentWord
|
||
+ "\nScore : " + displayScore
|
||
+ "\nTemps : " + displaySecs + "s");
|
||
|
||
if (level == 3 && index < words.size()) {
|
||
sessionScore += game.getScore();
|
||
startRound();
|
||
} else {
|
||
timer.stop();
|
||
}
|
||
}
|
||
|
||
/** Refresh complet. */
|
||
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();
|
||
}
|
||
|
||
/** Refresh stats (score/chrono). */
|
||
private void refreshStatsOnly() {
|
||
int s = (level == 3) ? sessionScore + game.getScore() : game.getScore();
|
||
long t = (level == 3) ? getSessionSeconds() : game.getElapsedSeconds();
|
||
scoreLabel.setText("Score : " + s);
|
||
timeLabel.setText("Temps : " + t + "s");
|
||
}
|
||
|
||
/** Secondes écoulées depuis le début de la session (niveau 3). */
|
||
private long getSessionSeconds() {
|
||
long now = System.nanoTime();
|
||
long delta = now - sessionStartNano;
|
||
if (delta < 0) delta = 0;
|
||
return delta / 1_000_000_000L;
|
||
}
|
||
|
||
/** Popup info. */
|
||
private void showMsg(String msg) {
|
||
JOptionPane.showMessageDialog(frame, msg);
|
||
}
|
||
} |