From b60bda7ae079222f4f26fcedf3dd7a0e1c728cd5 Mon Sep 17 00:00:00 2001 From: Felix-Vimalaratnam Date: Wed, 8 Oct 2025 13:43:58 +0200 Subject: [PATCH] mise en relation affichage et jeu et bouton relance --- Makefile | 6 +- out/fr/iut/Projet/Action$1.class | Bin 0 -> 584 bytes out/fr/iut/Projet/Action.class | Bin 0 -> 5331 bytes out/fr/iut/Projet/Display.class | Bin 0 -> 1588 bytes out/fr/iut/Projet/PlayButtonListener.class | Bin 0 -> 544 bytes out/fr/iut/Projet/Random_word.class | Bin 3806 -> 3721 bytes src/fr/iut/Projet/Action.java | 232 +++++++++++++++++++-- src/fr/iut/Projet/Random_word.java | 157 ++++++-------- 8 files changed, 286 insertions(+), 109 deletions(-) create mode 100644 out/fr/iut/Projet/Action$1.class create mode 100644 out/fr/iut/Projet/Action.class create mode 100644 out/fr/iut/Projet/Display.class create mode 100644 out/fr/iut/Projet/PlayButtonListener.class diff --git a/Makefile b/Makefile index c9a06ae..98d213b 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ SRC_DIR = src OUT_DIR = out PACKAGE = fr/iut/Projet -MAIN_CLASS = fr.iut.Projet.Random_word +MAIN_CLASS = fr.iut.Projet.Display # === Règle principale === all: compile run @@ -11,7 +11,9 @@ all: compile run compile: @echo "Compilation du projet..." @mkdir -p $(OUT_DIR) - @javac -d $(OUT_DIR) $(SRC_DIR)/$(PACKAGE)/Random_word.java + # Compilation de tous les fichiers Java du package + @javac -d $(OUT_DIR) $(SRC_DIR)/$(PACKAGE)/*.java + # Copier Word.txt dans le dossier de sortie @cp $(SRC_DIR)/$(PACKAGE)/Word.txt $(OUT_DIR)/$(PACKAGE)/ @echo "Compilation terminée." diff --git a/out/fr/iut/Projet/Action$1.class b/out/fr/iut/Projet/Action$1.class new file mode 100644 index 0000000000000000000000000000000000000000..4256abf285d4313800352c50c7e28b8ecd9c0dcc GIT binary patch literal 584 zcmX^0Z`VEs1_mbvc}@l<24;2!79Ivx1~x_pv9u!n%+eD5fTH}Y)DnHi*%`Qa7`PdD7#Rez63Y_xa}x8?_5G8wQj<#<8Q5$x z^D;~97#Wx~G{d+U_!#)v83cG31VPG0u$jroz@3qpmy(m}UYc55%+4Up$RGWW zO)bgDPYFmYO3Y0yNi8a7WZ+K(dpsbuC@sGzH#LQkL0-cLtKF_3cUXhW=L*g*ElN&x z%LJLp4GB3tkRC<`e%HL@oc!XWep>(l literal 0 HcmV?d00001 diff --git a/out/fr/iut/Projet/Action.class b/out/fr/iut/Projet/Action.class new file mode 100644 index 0000000000000000000000000000000000000000..654fe3bac8da8465356cbfd2ec9504730148a184 GIT binary patch literal 5331 zcmX^0Z`VEs1_pCR11^Rnh9-7~W-bOMh9*viM1~eFhD3%|5YYzWwhJ*ZF!XRSC^Gbd zxP4pFBD#(y&TnvH?lR#>xvop-#V3^6pAj&WcBsiO$ zVGf9%3sN>uh=Gw|AxPOG5O=W<0~5ni5O*0n!*Y;yD?r3-c7~Pg46C>pR2iDM7*rTm zvooyWV$cA&RD)qHCxZsVI(CNjTnsu48$g_mAZKp^u{N_ga4>)Yh_?kqYy}yx4Wwc_ zJHrkRhMgc+Zv)BfVzYk$R?7fV(!|cNn~TAYVGoG2mz`lBi#-Da1IXd~K_Uk@7!I;C z9O7bdV>k@r@8@DT!f=$G;TQ+QaV`cghDq!UC%71V8BT&Yy&z>^pPb@gI1TdA8IS|c zaxqjeoCEp$JcxS%#JUJ#T>?p5=3=#h)-5xSz?8Lad~E5y1o}!)S8ijvpl~j#V0W-H3vfpSk#)4K{PWj zIlrhVHMztmwWK7qs2D84$iS78S(Xap2;_j}Jo5@lOWZP3b5a-?Bv5S(Nv(j$STi!P zfE>oiAnlV@q@P(@q90I{pOspoAC#DvlAjw7a*{P81Dj1|US^3MBLlOBW*8#_i*tTT zDkB5GPi9`KUukYqYEejHQVz&xAZ3|}Ihj?d&iT0o`FW{%CB=*k0y&A5`K2YuLVU?Z zsfi`2A^8OXiFqI&i)m=0Iua~y&B!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d29|KGEeuTC7+BGDD==^{C^GOfC^0ZH2!d@F0ssRrduspy literal 0 HcmV?d00001 diff --git a/out/fr/iut/Projet/Random_word.class b/out/fr/iut/Projet/Random_word.class index fc4c7a0e67ca607fe0c38ed91dd9c5712db0d978..36fc137ab50bdd4bd8bc088b66fdb47ebdc06651 100644 GIT binary patch literal 3721 zcmX^0Z`VEs1_pD6<6I1;4B=c1{0tGC4Ezj{>P*Cqo>F70<vt!ki^Bn%@7U}Nd^%q>u`~2D zGH?~ACKsiagy$EfFfs`FWF?j*>gOcprRxWm6lLb6TQf3nWn`wLq~<|XFhx5vGVtZ3 zmXxFx6}y+F78j?cFf#B%^p%!m=I95fmRK_~a0X|l=Ovbu7Ns&WsG-W*K)7H_oHG)O z5|c|(i>&Re85smJ^OEz6ic*tHd>|GuGO*@kmZcUmGBA2FGO*cX=4F=HF)}b~XofK| zusG+Zq=M|u%uDqv%}q)z3Q0`LNo8c<4K7Jc&h||#fbr8)OM(*fQu1>leh|^nL~;Vu zpX>}1*cm1=GH|DZJO#0zkwH|$8H-v*2Hy145)XK=urpXPG6<)qmUv>gpCdiB#0TO9 zW(`eGMh33TV)w+{RR6NnBCtoI7#UbIi^KEtAch7b#RgY!erZv1s#_+=Qvwhl#Fyt6 zrRafTiHE_F!HI{#jlrFV!HmJ2kwFS13W>X*(!7$)+*H?!P3c2|u3W+&~S0Eb$Ewv~$B`7s91r$`0FbU7R zg3^-UlA_eaT!#|0(U8cJ3GTN9)_h1%Xt`9Fsx)}SjEGznqdtigL+tg zX|Y0LS!$I+%Hfq+hZiWM78fU09$u-C3`)8Rpd11T&9xvmtYcWi!?2!V0}n$mLl7f_ zfKz^cNpVS0Vu5dJNk)E3F+Ur_Mh=EeJPex|wlFgAIAx~hDP-lB9$u-S$ic9cgJBy; zW;-K;IKmr{bn8@_nUj)Q#KW+IVJ8p6E|AYxFzn`G*aLDTBz(c1_D{-6O)f$9EFu_T z5dl^RF&32aG4lv0*RkcK=B1Zp!1RDqf@4up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z`?-y zhe42?!IFU!)Ut$_&X^8%6a&L7Ar2wAZ43(DTE`i9K{lvuV-RKB!05A^K_hZIgO=}Z z2JOh*47!n8+Zpt?GZ^_Xo3ZL{V=#@hVmZd3BFVCi!NQ6KMA^>z|Eh(RQAd<@8-wGl z|Mw(We3t*ep}UR2bvc8C6$^yHW5p`TBFVaq!Ap{5|8fRlzfBChy4x56b};Vf%ju83s0XhVASOmh21~>-3*D5+ZmF5p|qa>hZ&33HincvtX8a&tOD^n7&107Q_;e~xcvX4O$>S480t1Lp+b8bL;E&{aFB7^7*bdk zFz`#VF>hn&+QHB_i-8Yh-;`|(qRam`Lxj!Pbwt^BFihLYFq4U47K5NJf;SVQd_Gut zh?Xe(Him_>8SG%{moPDegIx>q)FK9HEm4kb3|*q^3mCX0SwVgXg}YX0ImiqKd6?lK ztt~A7L0)6o#jq0Weo59H46C%ZF|3`*AOH%fT?`w*;v1L$y8+>Egzy;{py_83g9ZZw zgAoHagE50Jg9(E)gDHbNgE@mbg9U>ggB61rgEfOIgDry>gB?QV(?@*%HYFrmcftVC4)c1M}`1~FATv9e;Gm; zIT=D3MH#{v6&S)9)fqw=bs3@ha~P5ta~V<@3mDQF zYZx-X>CujX?>~bpC?#65Geog7%wlJl#mLUElY!wMg9JOnPDaKb3<`fhEqhC@KMcx@ zTz?qE8Kr(PuyRTNWRPHDU}o42_VXTwy$lQtIt=U#XBZe6_JPR*VDb={JOU< handleGuess()); gameFrame.setVisible(true); } + + // ==================== Initialisation des composants ==================== + + /** + * Initialise tous les composants graphiques : + * - Fenêtre principale + * - Labels pour le mot, les vies et les lettres incorrectes + * - Champ de saisie pour entrer les lettres + */ + private void initializeComponents() { + gameFrame = new JFrame("Hanging Man"); + gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + gameFrame.setSize(600, 400); + + wordLabel = new JLabel(game.getHiddenWord()); + wordLabel.setFont(new Font("Arial", Font.BOLD, 32)); + wordLabel.setHorizontalAlignment(SwingConstants.CENTER); + + livesLabel = new JLabel("Lives: " + game.getLives()); + livesLabel.setFont(new Font("Arial", Font.BOLD, 20)); + livesLabel.setHorizontalAlignment(SwingConstants.CENTER); + + incorrectLettersLabel = new JLabel("Incorrect letters: " + game.getIncorrectLetters()); + incorrectLettersLabel.setFont(new Font("Arial", Font.PLAIN, 20)); + incorrectLettersLabel.setHorizontalAlignment(SwingConstants.CENTER); + + letterInputField = new JTextField(3); + letterInputField.setFont(new Font("Arial", Font.PLAIN, 24)); + } + + // ==================== Layout des composants ==================== + + /** + * Dispose les composants dans la fenêtre principale en utilisant GridBagLayout. + * Sépare la fenêtre en deux parties : le haut (mot et vies) et le bas (lettres incorrectes, saisie, bouton restart) + */ + private void layoutComponents() { + JPanel mainPanel = new JPanel(new GridBagLayout()); + GridBagConstraints mainConstraints = new GridBagConstraints(); + mainConstraints.gridx = 0; + mainConstraints.fill = GridBagConstraints.BOTH; + mainConstraints.insets = new Insets(5, 5, 5, 5); + + JPanel topPanel = createTopPanel(); + mainConstraints.gridy = 0; + mainConstraints.weighty = 3.0; + mainConstraints.weightx = 1.0; + mainPanel.add(topPanel, mainConstraints); + + JPanel bottomPanel = createBottomPanel(); + mainConstraints.gridy = 1; + mainConstraints.weighty = 1.0; + mainPanel.add(bottomPanel, mainConstraints); + + gameFrame.add(mainPanel, BorderLayout.CENTER); + } + + /** + * Crée le panneau supérieur avec : + * - Le label des vies (à gauche) + * - Le label du mot caché (à droite) + * @return JPanel configuré pour la partie supérieure + */ + private JPanel createTopPanel() { + JPanel topPanel = new JPanel(new GridBagLayout()); + + // Panel pour les vies + JPanel livesPanel = new JPanel(new GridBagLayout()); + GridBagConstraints livesConstraints = new GridBagConstraints(); + livesConstraints.anchor = GridBagConstraints.CENTER; + livesConstraints.fill = GridBagConstraints.NONE; + livesPanel.add(livesLabel, livesConstraints); + + // Panel pour le mot + JPanel wordPanel = new JPanel(new GridBagLayout()); + GridBagConstraints wordConstraints = new GridBagConstraints(); + wordConstraints.anchor = GridBagConstraints.CENTER; + wordConstraints.fill = GridBagConstraints.NONE; + wordPanel.add(wordLabel, wordConstraints); + + // Ajout des panels dans topPanel + GridBagConstraints leftConstraints = new GridBagConstraints(); + leftConstraints.gridx = 0; + leftConstraints.weightx = 0.5; + leftConstraints.weighty = 1.0; + leftConstraints.fill = GridBagConstraints.BOTH; + topPanel.add(livesPanel, leftConstraints); + + GridBagConstraints rightConstraints = new GridBagConstraints(); + rightConstraints.gridx = 1; + rightConstraints.weightx = 0.5; + rightConstraints.weighty = 1.0; + rightConstraints.fill = GridBagConstraints.BOTH; + topPanel.add(wordPanel, rightConstraints); + + return topPanel; + } + + /** + * Crée le panneau inférieur avec : + * - Le label des lettres incorrectes + * - Le champ de saisie pour entrer une lettre + * - Le bouton "Restart" en bas à droite + * @return JPanel configuré pour la partie inférieure + */ + private JPanel createBottomPanel() { + JPanel bottomPanel = new JPanel(); + bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS)); + + // Label des lettres incorrectes + incorrectLettersLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + bottomPanel.add(incorrectLettersLabel); + + // Champ de saisie + JLabel promptLabel = new JLabel("Enter a letter:"); + promptLabel.setFont(new Font("Arial", Font.PLAIN, 16)); + promptLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + + JPanel inputRow = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5)); + inputRow.add(promptLabel); + inputRow.add(letterInputField); + inputRow.setAlignmentX(Component.CENTER_ALIGNMENT); + + bottomPanel.add(Box.createVerticalStrut(10)); + bottomPanel.add(inputRow); + + // Bouton "Restart" en bas à droite + JButton restartButton = new JButton("Restart"); + restartButton.addActionListener(new PlayButtonListener(gameFrame)); + JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); + buttonPanel.add(restartButton); + + bottomPanel.add(Box.createVerticalStrut(10)); + bottomPanel.add(buttonPanel); + + return bottomPanel; + } + + // ==================== Gestion du jeu ==================== + + /** + * Gère la saisie d'une lettre par le joueur. + * Vérifie la validité de la lettre, met à jour l'affichage et termine le jeu si nécessaire. + */ + private void handleGuess() { + String inputText = letterInputField.getText(); + letterInputField.setText(""); + + if (!isValidInput(inputText)) return; + + char guessedLetter = inputText.charAt(0); + String message = game.guessLetter(guessedLetter); + updateUI(); + + if (game.isGameOver()) endGame(message); + } + + /** + * Vérifie si la saisie de l'utilisateur est valide : + * - Une seule lettre + * - Caractère alphabétique + * @param inputText texte saisi par l'utilisateur + * @return true si la saisie est valide, false sinon + */ + private boolean isValidInput(String inputText) { + if (inputText.length() != 1 || !Character.isLetter(inputText.charAt(0))) { + JOptionPane.showMessageDialog(gameFrame, "Please enter a single letter!"); + return false; + } + return true; + } + + /** + * Met à jour l'affichage des labels : + * - Mot caché + * - Lettres incorrectes + * - Nombre de vies restantes + */ + private void updateUI() { + wordLabel.setText(game.getHiddenWord()); + incorrectLettersLabel.setText("Incorrect letters: " + game.getIncorrectLetters()); + livesLabel.setText("Lives: " + game.getLives()); + } + + /** + * Termine le jeu et affiche un message. + * Désactive le champ de saisie. + * @param message Message à afficher (victoire ou défaite) + */ + private void endGame(String message) { + JOptionPane.showMessageDialog(gameFrame, message); + letterInputField.setEditable(false); + } } diff --git a/src/fr/iut/Projet/Random_word.java b/src/fr/iut/Projet/Random_word.java index bb59230..bc57dcd 100644 --- a/src/fr/iut/Projet/Random_word.java +++ b/src/fr/iut/Projet/Random_word.java @@ -5,132 +5,107 @@ import java.util.*; public class Random_word { - public static void main(String[] args) { - // Récupère un mot aléatoire depuis le fichier Word.txt - String randomword = getRandomWord(); + private String secretWord; + private char[] hiddenWord; + private Set lettersGuessed; + private Set incorrectLetters; + private int lives; - if (randomword == null) { - System.err.println("Impossible de choisir un mot aléatoire !"); - return; + /** + * Constructeur : sélectionne un mot aléatoire et initialise le jeu. + */ + public Random_word() { + this.secretWord = getRandomWord(); + if (this.secretWord == null) { + throw new RuntimeException("Impossible de choisir un mot aléatoire !"); } - - // Démarre le jeu avec le mot choisi - play(randomword); + this.lives = 8; + this.hiddenWord = new char[secretWord.length()]; + Arrays.fill(this.hiddenWord, '_'); + this.lettersGuessed = new HashSet<>(); + this.incorrectLetters = new HashSet<>(); } /** * Lit les mots dans "Word.txt" et retourne un mot aléatoire. - * @return un mot aléatoire en minuscules, ou null si le fichier est introuvable ou vide */ - public static String getRandomWord() { - // Charge le fichier Word.txt depuis le package + private String getRandomWord() { InputStream is = Random_word.class.getResourceAsStream("Word.txt"); - - if (is == null) { - System.err.println("Le fichier 'Word.txt' est introuvable dans le package !"); - return null; - } + if (is == null) return null; String randomWord = null; Random random = new Random(); - - try (BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(is))) { + try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) { String line; int count = 0; - // Parcourt chaque ligne du fichier - while ((line = bufferedreader.readLine()) != null) { + while ((line = br.readLine()) != null) { line = line.trim(); if (!line.isEmpty()) { count++; - // Sélection aléatoire d'un mot (méthode reservoir sampling) if (random.nextInt(count) == 0) { randomWord = line; } } } } catch (IOException e) { - System.err.println("Erreur lors de la lecture du fichier : " + e.getMessage()); + e.printStackTrace(); } - return randomWord != null ? randomWord.toLowerCase() : null; } /** - * Affiche l'état actuel du mot et les lettres incorrectes. - * @param hiddenWord le mot masqué avec les lettres découvertes - * @param incorrectLetters les lettres déjà essayées mais incorrectes + * Tente une lettre, met à jour l'état du jeu et retourne un message. */ - private static void showwordstatus(char[] hiddenWord, Set incorrectLetters) { - System.out.print("\nMot actuel : "); - for (char Character : hiddenWord) { - System.out.print(Character + " "); + public String guessLetter(char letter) { + letter = Character.toLowerCase(letter); + if (lettersGuessed.contains(letter) || incorrectLetters.contains(letter)) { + return "Vous avez déjà essayé cette lettre !"; + } + + if (secretWord.indexOf(letter) >= 0) { + lettersGuessed.add(letter); + for (int i = 0; i < secretWord.length(); i++) { + if (secretWord.charAt(i) == letter) { + hiddenWord[i] = letter; + } + } + if (isWon()) { + return "Félicitations ! Vous avez trouvé le mot : " + secretWord; + } + return "Bien joué !"; + } else { + incorrectLetters.add(letter); + lives--; + if (lives <= 0) { + return "Vous avez perdu ! Le mot était : " + secretWord; + } + return "Mauvaise lettre ! Il vous reste " + lives + " vies."; } - System.out.println(); - System.out.println("Lettres incorrectes : " + incorrectLetters); } - /** - * Logique principale du jeu : gère les essais, met à jour l'état du mot, - * suit le nombre de vies restantes, et détermine la victoire ou la défaite. - * @param secretWord le mot que le joueur doit deviner - */ - public static void play(String secretWord) { - Scanner scanner = new Scanner(System.in); - Set lettersGuessed = new HashSet<>(); - Set incorrectLetters = new HashSet<>(); - int lives = 8; // nombre d'essais + public String getHiddenWord() { + StringBuilder sb = new StringBuilder(); + for (char c : hiddenWord) sb.append(c).append(' '); + return sb.toString().trim(); + } - char[] hiddenWord = new char[secretWord.length()]; - Arrays.fill(hiddenWord, '_'); + public String getIncorrectLetters() { + return incorrectLetters.toString(); + } - System.out.println("Bienvenue dans le jeu du mot mystère !"); - System.out.println("Le mot contient " + secretWord.length() + " lettres."); + public int getLives() { + return lives; + } - while (lives > 0) { - // Affiche le mot actuel et les lettres incorrectes - showwordstatus(hiddenWord, incorrectLetters); + public boolean isGameOver() { + return lives <= 0 || isWon(); + } - System.out.print("Entrez une lettre : "); - String input = scanner.nextLine().toLowerCase(); + public boolean isWon() { + return secretWord.equals(String.valueOf(hiddenWord)); + } - // Vérifie que l'entrée est valide - if (input.length() != 1 || !Character.isLetter(input.charAt(0))) { - System.out.println("Entrez une seule lettre !"); - continue; - } - - char letter = input.charAt(0); - - // Vérifie si la lettre a déjà été essayée - if (lettersGuessed.contains(letter) || incorrectLetters.contains(letter)) { - System.out.println("Vous avez déjà essayé cette lettre !"); - continue; - } - - // Vérifie si la lettre fait partie du mot secret - if (secretWord.indexOf(letter) >= 0) { - lettersGuessed.add(letter); - for (int i = 0; i < secretWord.length(); i++) { - if (secretWord.charAt(i) == letter) { - hiddenWord[i] = letter; - } - } - System.out.println("Bien joué !"); - } else { - // Lettre incorrecte : décrémente le nombre de vies - incorrectLetters.add(letter); - lives--; - System.out.println("Mauvaise lettre ! Il vous reste " + lives + " vies."); - } - - // Vérifie si le mot complet a été trouvé - if (String.valueOf(hiddenWord).equals(secretWord)) { - System.out.println("\nFélicitations ! Vous avez trouvé le mot : " + secretWord); - return; - } - } - - // Le joueur a perdu - System.out.println("\nVous avez perdu ! Le mot était : " + secretWord); + public String getSecretWord() { + return secretWord; } }