diff --git a/Jeu_pendu/Back/Words.java b/Jeu_pendu/Back/Words.java index 060a08a..7632427 100644 --- a/Jeu_pendu/Back/Words.java +++ b/Jeu_pendu/Back/Words.java @@ -82,4 +82,15 @@ public class Words { } return result; } + + /* Retourne la liste complète des mots disponibles */ + public static List all() { + ensureLoaded(); + return new ArrayList<>(CACHE); + } + + /* Petit utilitaire pour afficher un mot masqué dans les messages */ + public static String hiddenWord(Game g) { + return g.maskedWord().replace(' ', '_'); + } } \ No newline at end of file diff --git a/Jeu_pendu/Front/GameUI.java b/Jeu_pendu/Front/GameUI.java index 4f66674..145e483 100644 --- a/Jeu_pendu/Front/GameUI.java +++ b/Jeu_pendu/Front/GameUI.java @@ -5,28 +5,67 @@ 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. + * 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, newGameBtn; + private JButton tryBtn, quitBtn; + private Game game; - private String currentWord; + private List words; + private int index = 0; + private int level; + private String currentWord = ""; private Timer timer; - /** Lance la fenêtre et démarre une partie */ + /** 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(); - startNewGame(); + prepareWords(); + startRound(); frame.setVisible(true); } + /** Prépare la liste des mots selon la difficulté choisie */ + private void prepareWords() { + words = new ArrayList(); + List 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"); @@ -36,7 +75,7 @@ public class GameUI { frame.setLayout(new BorderLayout(12, 12)); } - /** Construit les composants et le layout */ + /** Construit la mise en page et les composants */ private void setupLayout() { imgLabel = new JLabel("", SwingConstants.CENTER); frame.add(imgLabel, BorderLayout.CENTER); @@ -55,35 +94,40 @@ public class GameUI { JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); input = new JTextField(5); tryBtn = new JButton("Essayer"); - newGameBtn = new JButton("Nouvelle partie"); + quitBtn = new JButton("Quitter"); inputPanel.add(new JLabel("Lettre :")); inputPanel.add(input); inputPanel.add(tryBtn); bottom.add(inputPanel, BorderLayout.WEST); - bottom.add(newGameBtn, BorderLayout.EAST); + bottom.add(quitBtn, BorderLayout.EAST); frame.add(bottom, BorderLayout.SOUTH); } - /** Crée une ligne du haut avec 2 labels */ + /** Construit une ligne (label gauche + espace + label droit) */ private JPanel buildTopLine(JLabel left, JLabel right) { - JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); - panel.add(left); - panel.add(Box.createHorizontalStrut(24)); - panel.add(right); - return panel; + JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT)); + p.add(left); + p.add(Box.createHorizontalStrut(24)); + p.add(right); + return p; } - /** Ajoute les actions et le timer */ + /** Branche les actions + timer */ private void setupActions() { tryBtn.addActionListener(this::onTry); input.addActionListener(this::onTry); - newGameBtn.addActionListener(e -> startNewGame()); + quitBtn.addActionListener(e -> frame.dispose()); timer = new Timer(1000, e -> refreshStatsOnly()); } - /** Démarre une nouvelle partie */ - private void startNewGame() { - currentWord = Words.random(); + /** 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(); @@ -91,7 +135,7 @@ public class GameUI { refreshUI(); } - /** Gère le clic ou l'appui sur Entrée */ + /** Tente une lettre (clic bouton ou Entrée) */ private void onTry(ActionEvent e) { String text = input.getText(); if (!Check.isLetter(text)) { @@ -101,38 +145,30 @@ public class GameUI { return; } Result res = game.play(Character.toLowerCase(text.charAt(0))); - handleResult(res); + if (res == Result.ALREADY) showMsg("Lettre déjà utilisée."); input.setText(""); refreshUI(); checkEnd(); } - /** Réagit selon le résultat d'une tentative */ - private void handleResult(Result res) { - switch (res) { - case ALREADY: - showMsg("Lettre déjà utilisée."); - break; - case HIT: - case MISS: - break; // rien, juste refresh - } - } - - /** Vérifie si la partie est finie */ + /** Vérifie la fin du mot et enchaîne si besoin (niveau 3) */ private void checkEnd() { - if (game.isWin() || game.isLose()) { - timer.stop(); - game.end(game.isWin()); - String msg = (game.isWin() ? "Bravo !" : "Perdu !") - + " Le mot était : " + currentWord - + "\nScore final : " + game.getScore() - + "\nTemps : " + game.getElapsedSeconds() + "s"; - showMsg(msg); + 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(); } } - /** Met à jour tout l'affichage */ + /** Rafraîchit image + textes + stats */ private void refreshUI() { imgLabel.setIcon(Gallows.icon(game.getErrors())); wordLabel.setText("Mot : " + game.maskedWord()); @@ -141,14 +177,14 @@ public class GameUI { frame.repaint(); } - /** Met à jour uniquement score + chrono */ + /** Rafraîchit uniquement score + chrono */ private void refreshStatsOnly() { scoreLabel.setText("Score : " + game.getScore()); timeLabel.setText("Temps : " + game.getElapsedSeconds() + "s"); } - /** Affiche une boîte de message */ + /** Affiche un message */ private void showMsg(String msg) { JOptionPane.showMessageDialog(frame, msg); } -} +} \ No newline at end of file diff --git a/Jeu_pendu/Front/MenuUI.java b/Jeu_pendu/Front/MenuUI.java new file mode 100644 index 0000000..8e66416 --- /dev/null +++ b/Jeu_pendu/Front/MenuUI.java @@ -0,0 +1,50 @@ +package front; + +import javax.swing.*; +import java.awt.*; + +/* +* Menu de démarrage du jeu du pendu. +* Permet de choisir la difficulté (facile, moyen ou difficile). +*/ +public class MenuUI { + private JFrame frame; + + /** + * Interface graphique de la page d'accueil du jeu du pendu. + */ + public void show() { + frame = new JFrame("Jeu du Pendu - Sélection de la difficulté"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(400, 300); + frame.setLocationRelativeTo(null); + frame.setLayout(new BorderLayout(12, 12)); + + JLabel title = new JLabel("Choisis une difficulté", SwingConstants.CENTER); + title.setFont(new Font("Arial", Font.BOLD, 20)); + frame.add(title, BorderLayout.NORTH); + + JPanel buttons = new JPanel(new GridLayout(3, 1, 10, 10)); + JButton easyBtn = new JButton("Niveau Facile"); + JButton mediumBtn = new JButton("Niveau Moyen"); + JButton hardBtn = new JButton("Niveau Difficile"); + + buttons.add(easyBtn); + buttons.add(mediumBtn); + buttons.add(hardBtn); + frame.add(buttons, BorderLayout.CENTER); + + easyBtn.addActionListener(e -> startGame(1)); + mediumBtn.addActionListener(e -> startGame(2)); + hardBtn.addActionListener(e -> startGame(3)); + + frame.setVisible(true); + } + + /* Lance le jeu avec le niveau choisi */ + private void startGame(int level) { + frame.dispose(); // ferme le menu + GameUI ui = new GameUI(level); + ui.show(); + } +} \ No newline at end of file diff --git a/Jeu_pendu/Main/Main.java b/Jeu_pendu/Main/Main.java index 42ac4ae..874575d 100644 --- a/Jeu_pendu/Main/Main.java +++ b/Jeu_pendu/Main/Main.java @@ -1,17 +1,16 @@ package main; -import front.GameUI; +import front.MenuUI; /** - * Point d'entrée du programme. - * Lance l'interface graphique du jeu du pendu. - */ +* Point d'entrée du programme. +* Affiche le menu de sélection avant de lancer le jeu. +*/ public class Main { public static void main(String[] args) { - // Démarre l'UI Swing sur le thread de l'EDT javax.swing.SwingUtilities.invokeLater(() -> { - GameUI ui = new GameUI(); - ui.show(); + MenuUI menu = new MenuUI(); + menu.show(); }); } } \ No newline at end of file