forked from menault/TD3_DEV51_Qualite_Algo
niveau
This commit is contained in:
@@ -82,4 +82,15 @@ public class Words {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Retourne la liste complète des mots disponibles */
|
||||||
|
public static List<String> 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(' ', '_');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,28 +5,67 @@ import back.*;
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
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 {
|
public class GameUI {
|
||||||
private JFrame frame;
|
private JFrame frame;
|
||||||
private JLabel imgLabel, wordLabel, triedLabel, scoreLabel, timeLabel;
|
private JLabel imgLabel, wordLabel, triedLabel, scoreLabel, timeLabel;
|
||||||
private JTextField input;
|
private JTextField input;
|
||||||
private JButton tryBtn, newGameBtn;
|
private JButton tryBtn, quitBtn;
|
||||||
|
|
||||||
private Game game;
|
private Game game;
|
||||||
private String currentWord;
|
private List<String> words;
|
||||||
|
private int index = 0;
|
||||||
|
private int level;
|
||||||
|
private String currentWord = "";
|
||||||
private Timer timer;
|
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() {
|
public void show() {
|
||||||
setupWindow();
|
setupWindow();
|
||||||
setupLayout();
|
setupLayout();
|
||||||
setupActions();
|
setupActions();
|
||||||
startNewGame();
|
prepareWords();
|
||||||
|
startRound();
|
||||||
frame.setVisible(true);
|
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 */
|
/** Crée la fenêtre principale */
|
||||||
private void setupWindow() {
|
private void setupWindow() {
|
||||||
frame = new JFrame("Jeu du Pendu");
|
frame = new JFrame("Jeu du Pendu");
|
||||||
@@ -36,7 +75,7 @@ public class GameUI {
|
|||||||
frame.setLayout(new BorderLayout(12, 12));
|
frame.setLayout(new BorderLayout(12, 12));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Construit les composants et le layout */
|
/** Construit la mise en page et les composants */
|
||||||
private void setupLayout() {
|
private void setupLayout() {
|
||||||
imgLabel = new JLabel("", SwingConstants.CENTER);
|
imgLabel = new JLabel("", SwingConstants.CENTER);
|
||||||
frame.add(imgLabel, BorderLayout.CENTER);
|
frame.add(imgLabel, BorderLayout.CENTER);
|
||||||
@@ -55,35 +94,40 @@ public class GameUI {
|
|||||||
JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
||||||
input = new JTextField(5);
|
input = new JTextField(5);
|
||||||
tryBtn = new JButton("Essayer");
|
tryBtn = new JButton("Essayer");
|
||||||
newGameBtn = new JButton("Nouvelle partie");
|
quitBtn = new JButton("Quitter");
|
||||||
inputPanel.add(new JLabel("Lettre :"));
|
inputPanel.add(new JLabel("Lettre :"));
|
||||||
inputPanel.add(input);
|
inputPanel.add(input);
|
||||||
inputPanel.add(tryBtn);
|
inputPanel.add(tryBtn);
|
||||||
bottom.add(inputPanel, BorderLayout.WEST);
|
bottom.add(inputPanel, BorderLayout.WEST);
|
||||||
bottom.add(newGameBtn, BorderLayout.EAST);
|
bottom.add(quitBtn, BorderLayout.EAST);
|
||||||
frame.add(bottom, BorderLayout.SOUTH);
|
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) {
|
private JPanel buildTopLine(JLabel left, JLabel right) {
|
||||||
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
||||||
panel.add(left);
|
p.add(left);
|
||||||
panel.add(Box.createHorizontalStrut(24));
|
p.add(Box.createHorizontalStrut(24));
|
||||||
panel.add(right);
|
p.add(right);
|
||||||
return panel;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ajoute les actions et le timer */
|
/** Branche les actions + timer */
|
||||||
private void setupActions() {
|
private void setupActions() {
|
||||||
tryBtn.addActionListener(this::onTry);
|
tryBtn.addActionListener(this::onTry);
|
||||||
input.addActionListener(this::onTry);
|
input.addActionListener(this::onTry);
|
||||||
newGameBtn.addActionListener(e -> startNewGame());
|
quitBtn.addActionListener(e -> frame.dispose());
|
||||||
timer = new Timer(1000, e -> refreshStatsOnly());
|
timer = new Timer(1000, e -> refreshStatsOnly());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Démarre une nouvelle partie */
|
/** Démarre un nouveau mot (ou termine au niveau 3) */
|
||||||
private void startNewGame() {
|
private void startRound() {
|
||||||
currentWord = Words.random();
|
if (index >= words.size()) {
|
||||||
|
showMsg("Niveau terminé !");
|
||||||
|
frame.dispose();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
currentWord = words.get(index++);
|
||||||
game = new Game(currentWord, 7);
|
game = new Game(currentWord, 7);
|
||||||
input.setText("");
|
input.setText("");
|
||||||
input.requestFocusInWindow();
|
input.requestFocusInWindow();
|
||||||
@@ -91,7 +135,7 @@ public class GameUI {
|
|||||||
refreshUI();
|
refreshUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gère le clic ou l'appui sur Entrée */
|
/** Tente une lettre (clic bouton ou Entrée) */
|
||||||
private void onTry(ActionEvent e) {
|
private void onTry(ActionEvent e) {
|
||||||
String text = input.getText();
|
String text = input.getText();
|
||||||
if (!Check.isLetter(text)) {
|
if (!Check.isLetter(text)) {
|
||||||
@@ -101,38 +145,30 @@ public class GameUI {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Result res = game.play(Character.toLowerCase(text.charAt(0)));
|
Result res = game.play(Character.toLowerCase(text.charAt(0)));
|
||||||
handleResult(res);
|
if (res == Result.ALREADY) showMsg("Lettre déjà utilisée.");
|
||||||
input.setText("");
|
input.setText("");
|
||||||
refreshUI();
|
refreshUI();
|
||||||
checkEnd();
|
checkEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Réagit selon le résultat d'une tentative */
|
/** Vérifie la fin du mot et enchaîne si besoin (niveau 3) */
|
||||||
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 */
|
|
||||||
private void checkEnd() {
|
private void checkEnd() {
|
||||||
if (game.isWin() || game.isLose()) {
|
if (!(game.isWin() || game.isLose())) return;
|
||||||
timer.stop();
|
|
||||||
game.end(game.isWin());
|
timer.stop();
|
||||||
String msg = (game.isWin() ? "Bravo !" : "Perdu !")
|
game.end(game.isWin());
|
||||||
+ " Le mot était : " + currentWord
|
String verdict = game.isWin() ? "Bravo !" : "Perdu !";
|
||||||
+ "\nScore final : " + game.getScore()
|
String msg = verdict + " Le mot était : " + currentWord
|
||||||
+ "\nTemps : " + game.getElapsedSeconds() + "s";
|
+ "\nScore : " + game.getScore()
|
||||||
showMsg(msg);
|
+ "\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() {
|
private void refreshUI() {
|
||||||
imgLabel.setIcon(Gallows.icon(game.getErrors()));
|
imgLabel.setIcon(Gallows.icon(game.getErrors()));
|
||||||
wordLabel.setText("Mot : " + game.maskedWord());
|
wordLabel.setText("Mot : " + game.maskedWord());
|
||||||
@@ -141,13 +177,13 @@ public class GameUI {
|
|||||||
frame.repaint();
|
frame.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Met à jour uniquement score + chrono */
|
/** Rafraîchit uniquement score + chrono */
|
||||||
private void refreshStatsOnly() {
|
private void refreshStatsOnly() {
|
||||||
scoreLabel.setText("Score : " + game.getScore());
|
scoreLabel.setText("Score : " + game.getScore());
|
||||||
timeLabel.setText("Temps : " + game.getElapsedSeconds() + "s");
|
timeLabel.setText("Temps : " + game.getElapsedSeconds() + "s");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Affiche une boîte de message */
|
/** Affiche un message */
|
||||||
private void showMsg(String msg) {
|
private void showMsg(String msg) {
|
||||||
JOptionPane.showMessageDialog(frame, msg);
|
JOptionPane.showMessageDialog(frame, msg);
|
||||||
}
|
}
|
||||||
|
|||||||
50
Jeu_pendu/Front/MenuUI.java
Normal file
50
Jeu_pendu/Front/MenuUI.java
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,16 @@
|
|||||||
package main;
|
package main;
|
||||||
|
|
||||||
import front.GameUI;
|
import front.MenuUI;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Point d'entrée du programme.
|
* Point d'entrée du programme.
|
||||||
* Lance l'interface graphique du jeu du pendu.
|
* Affiche le menu de sélection avant de lancer le jeu.
|
||||||
*/
|
*/
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Démarre l'UI Swing sur le thread de l'EDT
|
|
||||||
javax.swing.SwingUtilities.invokeLater(() -> {
|
javax.swing.SwingUtilities.invokeLater(() -> {
|
||||||
GameUI ui = new GameUI();
|
MenuUI menu = new MenuUI();
|
||||||
ui.show();
|
menu.show();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user