forked from menault/TD3_DEV51_Qualite_Algo
chameauuuuuu
This commit is contained in:
@@ -4,12 +4,11 @@ import java.awt.event.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Interface graphique du jeu du Pendu.
|
||||
* Noms de variables/fonctions en anglais (exigence),
|
||||
* affichages et commentaires en français.
|
||||
* Hangman game GUI.
|
||||
* Variables/methods and comments in English.
|
||||
* User-facing texts remain in French.
|
||||
*/
|
||||
public class HangmanGUI extends JFrame {
|
||||
// --- Données du jeu ---
|
||||
private final String[] words = {
|
||||
"ordinateur","voiture","maison","soleil","lumiere",
|
||||
"fromage","chocolat","montagne","riviere","plage",
|
||||
@@ -20,14 +19,14 @@ public class HangmanGUI extends JFrame {
|
||||
"brouillard","village","histoire","cerise","pomme",
|
||||
"banane","poisson","arbre","cascade","cheval"
|
||||
};
|
||||
|
||||
private String secretWord;
|
||||
private final Set<Character> found = new HashSet<>();
|
||||
private final Set<Character> tried = new HashSet<>();
|
||||
private int errors = 0;
|
||||
private static final int MAX_ERRORS = 8; // 8 étapes : base, poteau, poutre, corde, tête, corps, bras, jambes
|
||||
private boolean gameOver = false; // indique si la partie est terminée
|
||||
private static final int MAX_ERRORS = 8; // 8 steps: base, post, beam, rope, head, body, arms, legs
|
||||
private boolean gameOver = false; // indicates if the game is finished
|
||||
|
||||
// --- Éléments UI ---
|
||||
private final JLabel wordLbl = new JLabel("", SwingConstants.CENTER);
|
||||
private final JLabel triedLbl = new JLabel("", SwingConstants.CENTER);
|
||||
private final JLabel infoLbl = new JLabel("Entrez une lettre (a-z)", SwingConstants.CENTER);
|
||||
@@ -35,7 +34,7 @@ public class HangmanGUI extends JFrame {
|
||||
private final JButton guessBtn = new JButton("Proposer");
|
||||
private final HangPanel hangPanel = new HangPanel();
|
||||
|
||||
/** Panneau de dessin du pendu, progressif selon le nombre d'erreurs. */
|
||||
/** Drawing panel for the hangman, progressive according to error count. */
|
||||
private static class HangPanel extends JPanel {
|
||||
private int errors = 0;
|
||||
|
||||
@@ -51,41 +50,33 @@ public class HangmanGUI extends JFrame {
|
||||
gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
int w = getWidth(), h = getHeight();
|
||||
int baseY = h - 40; // niveau du sol
|
||||
int baseY = h - 40; // ground level
|
||||
|
||||
// 1) base
|
||||
if (errors >= 1) gg.drawLine(50, baseY, w - 50, baseY);
|
||||
|
||||
// 2) poteau vertical
|
||||
if (errors >= 2) gg.drawLine(100, baseY, 100, 60);
|
||||
|
||||
// 3) poutre horizontale
|
||||
if (errors >= 3) gg.drawLine(100, 60, 220, 60);
|
||||
|
||||
// 4) corde
|
||||
if (errors >= 4) gg.drawLine(220, 60, 220, 90);
|
||||
|
||||
// 5) tête
|
||||
if (errors >= 5) gg.drawOval(200, 90, 40, 40);
|
||||
|
||||
// 6) corps
|
||||
if (errors >= 6) gg.drawLine(220, 130, 220, 200);
|
||||
|
||||
// 7) bras
|
||||
if (errors >= 7) {
|
||||
gg.drawLine(220, 145, 190, 165); // bras gauche
|
||||
gg.drawLine(220, 145, 250, 165); // bras droit
|
||||
gg.drawLine(220, 145, 190, 165);
|
||||
gg.drawLine(220, 145, 250, 165);
|
||||
}
|
||||
|
||||
// 8) jambes
|
||||
if (errors >= 8) {
|
||||
gg.drawLine(220, 200, 200, 240); // jambe gauche
|
||||
gg.drawLine(220, 200, 240, 240); // jambe droite
|
||||
gg.drawLine(220, 200, 200, 240);
|
||||
gg.drawLine(220, 200, 240, 240);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Point d'entrée : crée et affiche la fenêtre du jeu. */
|
||||
/** Entry point: create and show the game window. */
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
HangmanGUI app = new HangmanGUI();
|
||||
@@ -93,7 +84,7 @@ public class HangmanGUI extends JFrame {
|
||||
});
|
||||
}
|
||||
|
||||
/** Constructeur : configure la fenêtre, l'UI et lance la partie. */
|
||||
/** Constructor: window setup, UI setup, and start the game. */
|
||||
public HangmanGUI() {
|
||||
super("Jeu du Pendu");
|
||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
@@ -103,7 +94,7 @@ public class HangmanGUI extends JFrame {
|
||||
startGame();
|
||||
}
|
||||
|
||||
/** Prépare la mise en page et les actions de l'interface. */
|
||||
/** Prepare the layout and interactions. */
|
||||
private void setupUI() {
|
||||
wordLbl.setFont(new Font(Font.MONOSPACED, Font.BOLD, 28));
|
||||
triedLbl.setFont(new Font("SansSerif", Font.PLAIN, 14));
|
||||
@@ -130,15 +121,14 @@ public class HangmanGUI extends JFrame {
|
||||
|
||||
setContentPane(main);
|
||||
|
||||
// Actions
|
||||
guessBtn.addActionListener(e -> onGuess());
|
||||
input.addActionListener(e -> onGuess()); // touche Entrée
|
||||
input.addActionListener(e -> onGuess());
|
||||
addWindowListener(new WindowAdapter() {
|
||||
@Override public void windowOpened(WindowEvent e) { input.requestFocusInWindow(); }
|
||||
});
|
||||
}
|
||||
|
||||
/** Démarre une partie (unique). */
|
||||
/** Start a game. */
|
||||
private void startGame() {
|
||||
secretWord = pickWord();
|
||||
found.clear();
|
||||
@@ -153,9 +143,9 @@ public class HangmanGUI extends JFrame {
|
||||
updateUIState();
|
||||
}
|
||||
|
||||
/** Traite une proposition : valide, met à jour et vérifie fin de partie. */
|
||||
/** Handle a guess: validate input, update state, check for end of game. */
|
||||
private void onGuess() {
|
||||
if (gameOver) return; // bloque toute saisie après fin
|
||||
if (gameOver) return; // block input after game end
|
||||
|
||||
String s = input.getText().trim().toLowerCase(Locale.ROOT);
|
||||
if (s.length() != 1 || s.charAt(0) < 'a' || s.charAt(0) > 'z') {
|
||||
@@ -192,7 +182,7 @@ public class HangmanGUI extends JFrame {
|
||||
input.requestFocusInWindow();
|
||||
}
|
||||
|
||||
/** Met à jour les libellés, le dessin, et gère la fin de partie. */
|
||||
/** Refresh labels/drawing and handle end-of-game. */
|
||||
private void updateUIState() {
|
||||
wordLbl.setText(maskWord(secretWord, found));
|
||||
triedLbl.setText("Lettres essayées : " + joinChars(tried));
|
||||
@@ -200,7 +190,7 @@ public class HangmanGUI extends JFrame {
|
||||
|
||||
if (isWin(secretWord, found)) {
|
||||
infoLbl.setText("Bravo ! Vous avez trouvé le mot : " + secretWord);
|
||||
wordLbl.setText(spaced(secretWord)); // affichage propre du mot
|
||||
wordLbl.setText(spaced(secretWord)); // pretty display of the word
|
||||
gameOver = true;
|
||||
disableInput();
|
||||
} else if (errors >= MAX_ERRORS) {
|
||||
@@ -210,24 +200,24 @@ public class HangmanGUI extends JFrame {
|
||||
disableInput();
|
||||
}
|
||||
|
||||
// force le rafraîchissement de l'interface
|
||||
// force UI refresh
|
||||
revalidate();
|
||||
repaint();
|
||||
}
|
||||
|
||||
/** Désactive les contrôles après la fin du jeu. */
|
||||
/** Disable controls after the game ends. */
|
||||
private void disableInput() {
|
||||
input.setEditable(false);
|
||||
guessBtn.setEnabled(false);
|
||||
}
|
||||
|
||||
/** Sélectionne un mot aléatoire. */
|
||||
/** Pick a random word. */
|
||||
private String pickWord() {
|
||||
Random rnd = new Random();
|
||||
return words[rnd.nextInt(words.length)].toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
/** Construit le mot masqué selon les lettres trouvées. */
|
||||
/** Build the masked word based on found letters. */
|
||||
private String maskWord(String secret, Set<Character> foundSet) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < secret.length(); i++) {
|
||||
@@ -240,7 +230,7 @@ public class HangmanGUI extends JFrame {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** Vrai si toutes les lettres du mot ont été trouvées. */
|
||||
/** True if all letters have been found. */
|
||||
private boolean isWin(String secret, Set<Character> foundSet) {
|
||||
for (int i = 0; i < secret.length(); i++) {
|
||||
char lc = Character.toLowerCase(secret.charAt(i));
|
||||
@@ -249,7 +239,7 @@ public class HangmanGUI extends JFrame {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Trie et joint les lettres pour affichage. */
|
||||
/** Sort and join letters for display. */
|
||||
private String joinChars(Set<Character> set) {
|
||||
java.util.List<Character> list = new ArrayList<>(set);
|
||||
Collections.sort(list);
|
||||
@@ -261,7 +251,7 @@ public class HangmanGUI extends JFrame {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** Ajoute des espaces entre les lettres pour une lecture plus claire. */
|
||||
/** Add spaces between letters for nicer reading. */
|
||||
private String spaced(String s) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
|
||||
Reference in New Issue
Block a user