Compare commits
	
		
			15 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 5b167c761f | |||
| 94da5d2099 | |||
| 4f38105062 | |||
| 92aa82e7c7 | |||
| 7d2a0a12f9 | |||
| aef5c7f70c | |||
| dcac91d944 | |||
| b60bda7ae0 | |||
| 1037a9ff92 | |||
| 0d074ad1b6 | |||
| 3ab03c6b3e | |||
| 3bc6530305 | |||
| 6a8bc81e01 | |||
| ea1e590d8f | |||
| 976ed5e4f9 | 
							
								
								
									
										31
									
								
								Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								Makefile
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| # === Configuration === | ||||
| SRC_DIR = src | ||||
| OUT_DIR = out | ||||
| Dictionary = fr/iut/Projet/Dictionary | ||||
| PACKAGE = fr/iut/Projet | ||||
| MAIN_CLASS = fr.iut.Projet.Display | ||||
|  | ||||
| # === Règle principale === | ||||
| all: compile run | ||||
|  | ||||
| # === Compilation === | ||||
| compile: | ||||
| 	@echo "Compilation du projet..." | ||||
| 	@mkdir -p $(OUT_DIR) | ||||
| 	# Compilation de tous les fichiers Java du package | ||||
| 	@javac -d $(OUT_DIR) $(SRC_DIR)/$(PACKAGE)/*.java | ||||
| 	# Copier Word.txt dans le dossier de sortie | ||||
| 	@mkdir -p $(OUT_DIR)/$(Dictionary) | ||||
| 	@cp $(SRC_DIR)/$(Dictionary)/*.txt $(OUT_DIR)/$(Dictionary)/ | ||||
| 	@echo "Compilation terminée." | ||||
|  | ||||
| # === Exécution === | ||||
| run: | ||||
| 	@echo "Exécution du programme..." | ||||
| 	@java -cp $(OUT_DIR) $(MAIN_CLASS) | ||||
|  | ||||
| # === Nettoyage === | ||||
| clean: | ||||
| 	@echo "Suppression des fichiers compilés..." | ||||
| 	@rm -rf $(OUT_DIR) | ||||
| 	@echo "Nettoyage terminé." | ||||
							
								
								
									
										
											BIN
										
									
								
								out/Affiche.class
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								out/Affiche.class
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								out/Mouse.class
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								out/Mouse.class
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								out/fr/iut/Projet/Action.class
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								out/fr/iut/Projet/Action.class
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								out/fr/iut/Projet/Affiche.class
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								out/fr/iut/Projet/Affiche.class
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										10194
									
								
								out/fr/iut/Projet/Dictionary/EASY-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10194
									
								
								out/fr/iut/Projet/Dictionary/EASY-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										999
									
								
								out/fr/iut/Projet/Dictionary/HARD-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										999
									
								
								out/fr/iut/Projet/Dictionary/HARD-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										11090
									
								
								out/fr/iut/Projet/Dictionary/MEDIUM-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11090
									
								
								out/fr/iut/Projet/Dictionary/MEDIUM-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								out/fr/iut/Projet/Display.class
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								out/fr/iut/Projet/Display.class
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										10194
									
								
								out/fr/iut/Projet/EASY-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10194
									
								
								out/fr/iut/Projet/EASY-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								out/fr/iut/Projet/GameTimer$1.class
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								out/fr/iut/Projet/GameTimer$1.class
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								out/fr/iut/Projet/GameTimer.class
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								out/fr/iut/Projet/GameTimer.class
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										999
									
								
								out/fr/iut/Projet/HARD-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										999
									
								
								out/fr/iut/Projet/HARD-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										11090
									
								
								out/fr/iut/Projet/MEDIUM-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11090
									
								
								out/fr/iut/Projet/MEDIUM-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								out/fr/iut/Projet/PlayButtonListener.class
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								out/fr/iut/Projet/PlayButtonListener.class
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								out/fr/iut/Projet/Random_word.class
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								out/fr/iut/Projet/Random_word.class
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								out/fr/iut/Projet/Score.class
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								out/fr/iut/Projet/Score.class
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										273
									
								
								src/fr/iut/Projet/Action.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										273
									
								
								src/fr/iut/Projet/Action.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,273 @@ | ||||
| package fr.iut.Projet; | ||||
|  | ||||
| import javax.swing.*; | ||||
| import java.awt.*; | ||||
|  | ||||
| /** | ||||
|  * Classe principale qui gère l'interface graphique du jeu du pendu. | ||||
|  *  | ||||
|  * Affiche le mot caché, les lettres incorrectes, le dessin du pendu, le timer et le score. | ||||
|  * Gère également les interactions avec le joueur, la fin de partie et le redémarrage. | ||||
|  */ | ||||
| public class Action { | ||||
|  | ||||
|     private Score score; | ||||
|     private JFrame gameFrame; | ||||
|     private JLabel wordLabel; | ||||
|     private JLabel incorrectLettersLabel; | ||||
|     private JTextField letterInputField; | ||||
|     private Random_word game; | ||||
|     private Affiche affiche; | ||||
|     private GameTimer gameTimer; | ||||
|     private String difficulty; | ||||
|  | ||||
|     /** | ||||
|      * Constructeur : initialise le jeu selon la difficulté choisie, | ||||
|      * crée tous les composants graphiques et démarre le timer. | ||||
|      * @param difficulty Niveau de difficulté ("facile", "moyen", "difficile", "hell") | ||||
|      */ | ||||
|     public Action(String difficulty) { | ||||
|         this.difficulty = difficulty; | ||||
|         game = new Random_word(difficulty); | ||||
|  | ||||
|         // Initialisation | ||||
|         initializeComponents(); | ||||
|         layoutComponents(); | ||||
|         setupListeners(); | ||||
|  | ||||
|         // Démarre le timer et affiche la fenêtre | ||||
|         gameTimer.start(); | ||||
|         gameFrame.setVisible(true); | ||||
|     } | ||||
|  | ||||
|     // ==================== Initialisation ==================== | ||||
|  | ||||
|     /** Initialise les composants principaux : fenêtre, jeu, timer et score */ | ||||
|     private void initializeComponents() { | ||||
|         initializeFrameAndPanels(); | ||||
|         initializeGameComponents(); | ||||
|         initializeTimerAndScore(); | ||||
|     } | ||||
|  | ||||
|     /** Initialise la fenêtre principale */ | ||||
|     private void initializeFrameAndPanels() { | ||||
|         gameFrame = new JFrame("Hanging Man - " + difficulty); | ||||
|         gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||||
|         gameFrame.setSize(700, 500); | ||||
|     } | ||||
|  | ||||
|     /** Initialise les composants du jeu : pendu, mot caché, lettres incorrectes, champ de saisie */ | ||||
|     private void initializeGameComponents() { | ||||
|         affiche = new Affiche(); | ||||
|         affiche.setPreferredSize(new Dimension(350, 400)); | ||||
|         affiche.setBackground(Color.WHITE); | ||||
|         affiche.setOpaque(true); | ||||
|  | ||||
|         wordLabel = new JLabel(game.getHiddenWord()); | ||||
|         wordLabel.setFont(new Font("Arial", Font.BOLD, 32)); | ||||
|         wordLabel.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)); | ||||
|     } | ||||
|  | ||||
|     /** Initialise le timer et le score, avec un listener pour diminuer le score chaque seconde */ | ||||
|     private void initializeTimerAndScore() { | ||||
|         gameTimer = new GameTimer(); | ||||
|         score = new Score(); | ||||
|  | ||||
|         // Listener pour diminuer le score chaque seconde et finir après 60s | ||||
|         gameTimer.setTimerListener(() -> { | ||||
|             score.decreaseBySecond(); // comportement normal selon le temps | ||||
|  | ||||
|             // Si 60 secondes écoulées et que la partie n'est pas finie | ||||
|             if (gameTimer.getSecondsElapsed() >= 60 && !game.isGameOver()) { | ||||
|                 score.setScoreToZero(); // on met le score à 0 | ||||
|                 endGame("Temps écoulé ! Vous avez perdu !"); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     /** Ajoute les listeners sur les composants */ | ||||
|     private void setupListeners() { | ||||
|         letterInputField.addActionListener(e -> handleGuess()); | ||||
|     } | ||||
|  | ||||
|     // ==================== Mise en page ==================== | ||||
|  | ||||
|     /** Organise la mise en page globale (BorderLayout : gauche pendu, droite info et saisie) */ | ||||
|     private void layoutComponents() { | ||||
|         JPanel mainPanel = new JPanel(new BorderLayout()); | ||||
|         mainPanel.add(createLeftPanel(), BorderLayout.WEST); | ||||
|         mainPanel.add(createRightPanel(), BorderLayout.CENTER); | ||||
|         gameFrame.add(mainPanel); | ||||
|     } | ||||
|  | ||||
|     /** Crée le panneau gauche avec le dessin du pendu */ | ||||
|     private JPanel createLeftPanel() { | ||||
|         JPanel leftPanel = new JPanel(new BorderLayout()); | ||||
|         leftPanel.add(affiche, BorderLayout.CENTER); | ||||
|         return leftPanel; | ||||
|     } | ||||
|  | ||||
|     /** Crée le panneau droit avec mot, lettres incorrectes, saisie, boutons et timer/score */ | ||||
|     private JPanel createRightPanel() { | ||||
|         JPanel rightPanel = new JPanel(new GridBagLayout()); | ||||
|         GridBagConstraints gbc = new GridBagConstraints(); | ||||
|         gbc.gridx = 0; | ||||
|         gbc.fill = GridBagConstraints.BOTH; | ||||
|         gbc.insets = new Insets(5, 5, 5, 5); | ||||
|  | ||||
|         addWordAndIncorrectLetters(rightPanel, gbc); | ||||
|         addInputRow(rightPanel, gbc); | ||||
|         addButtons(rightPanel, gbc); | ||||
|         addTimerAndScore(rightPanel, gbc); | ||||
|  | ||||
|         return rightPanel; | ||||
|     } | ||||
|  | ||||
|     /** Ajoute le mot caché et les lettres incorrectes dans le panneau */ | ||||
|     private void addWordAndIncorrectLetters(JPanel panel, GridBagConstraints gbc) { | ||||
|         gbc.gridy = 0; | ||||
|         gbc.weighty = 1.0; | ||||
|         panel.add(wordLabel, gbc); | ||||
|  | ||||
|         gbc.gridy = 1; | ||||
|         panel.add(incorrectLettersLabel, gbc); | ||||
|     } | ||||
|  | ||||
|     /** Ajoute le champ de saisie pour les lettres */ | ||||
|     private void addInputRow(JPanel panel, GridBagConstraints gbc) { | ||||
|         gbc.gridy = 2; | ||||
|         JPanel inputRow = new JPanel(); | ||||
|         inputRow.add(new JLabel("Enter a letter:")); | ||||
|         inputRow.add(letterInputField); | ||||
|         panel.add(inputRow, gbc); | ||||
|     } | ||||
|  | ||||
|     /** Ajoute les boutons restart et changement de difficulté */ | ||||
|     private void addButtons(JPanel panel, GridBagConstraints gbc) { | ||||
|         gbc.gridy = 3; | ||||
|         JButton restartButton = new JButton("Restart"); | ||||
|         restartButton.addActionListener(e -> restartGame()); | ||||
|         panel.add(restartButton, gbc); | ||||
|  | ||||
|         gbc.gridy = 4; | ||||
|         JButton changeDifficultyButton = new JButton("Changer la difficulté"); | ||||
|         changeDifficultyButton.addActionListener(e -> showDifficultyDialog()); | ||||
|         panel.add(changeDifficultyButton, gbc); | ||||
|     } | ||||
|  | ||||
|     /** Ajoute le timer et le score en haut à droite */ | ||||
|     private void addTimerAndScore(JPanel panel, GridBagConstraints gbc) { | ||||
|         gbc.gridy = 5; | ||||
|         JPanel topRightPanel = new JPanel(new BorderLayout()); | ||||
|         topRightPanel.add(gameTimer.getTimerLabel(), BorderLayout.WEST); | ||||
|         topRightPanel.add(score.getScoreLabel(), BorderLayout.EAST); | ||||
|         panel.add(topRightPanel, gbc); | ||||
|     } | ||||
|  | ||||
|     // ==================== Gestion des actions ==================== | ||||
|  | ||||
|     /** Traite la saisie d'une lettre par le joueur */ | ||||
|     private void handleGuess() { | ||||
|         String inputText = letterInputField.getText(); | ||||
|         letterInputField.setText(""); | ||||
|  | ||||
|         if (!isValidInput(inputText)) return; | ||||
|  | ||||
|         char guessedLetter = inputText.charAt(0); | ||||
|         String message = game.guessLetter(guessedLetter); | ||||
|  | ||||
|         updateScore(message); | ||||
|         updateUI(); | ||||
|  | ||||
|         if (game.isGameOver()) { | ||||
|             if (game.isWon()) { | ||||
|                 score.increaseByWordFound(); | ||||
|             } | ||||
|             endGame(message); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** Met à jour le score selon le résultat de la lettre */ | ||||
|     private void updateScore(String message) { | ||||
|         if (message.contains("Bien joué")) { | ||||
|             score.increaseByCorrectLetter(); | ||||
|         } else if (message.contains("Mauvaise lettre")) { | ||||
|             score.decreaseByWrongLetter(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** Vérifie que la saisie est une seule lettre */ | ||||
|     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 du mot, des lettres incorrectes et des vies du pendu */ | ||||
|     private void updateUI() { | ||||
|         wordLabel.setText(game.getHiddenWord()); | ||||
|         incorrectLettersLabel.setText("Incorrect letters: " + game.getIncorrectLetters()); | ||||
|         affiche.setLives(game.getLives()); | ||||
|     } | ||||
|  | ||||
|     /** Termine la partie, arrête le timer et affiche le message */ | ||||
|     private void endGame(String message) { | ||||
|         gameTimer.stop(); | ||||
|  | ||||
|         if (game.isWon()) { | ||||
|             affiche.setYouWin(true); | ||||
|         } else { | ||||
|             affiche.setLives(0); | ||||
|             score.setScoreToZero(); // <- on met le score à 0 en cas de défaite | ||||
|         } | ||||
|  | ||||
|         JOptionPane.showMessageDialog(gameFrame, message); | ||||
|         letterInputField.setEditable(false); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /** Redémarre le jeu avec la même difficulté */ | ||||
|     private void restartGame() { | ||||
|         gameFrame.dispose(); | ||||
|         JFrame tempFrame = new JFrame(); | ||||
|         new PlayButtonListener(tempFrame, difficulty).actionPerformed(null); | ||||
|     } | ||||
|  | ||||
|     /** Affiche le dialogue pour changer la difficulté et relance le jeu */ | ||||
|     private void showDifficultyDialog() { | ||||
|         String[] options = {"Facile", "Moyen", "Difficile", "Hell"}; | ||||
|         int choice = JOptionPane.showOptionDialog( | ||||
|                 gameFrame, | ||||
|                 "Choisissez la difficulté :", | ||||
|                 "Changer la difficulté", | ||||
|                 JOptionPane.DEFAULT_OPTION, | ||||
|                 JOptionPane.QUESTION_MESSAGE, | ||||
|                 null, | ||||
|                 options, | ||||
|                 options[0] | ||||
|         ); | ||||
|  | ||||
|         if (choice >= 0) { | ||||
|             String newDifficulty; | ||||
|             switch (choice) { | ||||
|                 case 0: newDifficulty = "facile"; break; | ||||
|                 case 1: newDifficulty = "moyen"; break; | ||||
|                 case 2: newDifficulty = "difficile"; break; | ||||
|                 case 3: newDifficulty = "hell"; break; | ||||
|                 default: newDifficulty = "moyen"; break; | ||||
|             } | ||||
|             gameFrame.dispose(); | ||||
|             JFrame tempFrame = new JFrame(); | ||||
|             new PlayButtonListener(tempFrame, newDifficulty).actionPerformed(null); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										177
									
								
								src/fr/iut/Projet/Affiche.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										177
									
								
								src/fr/iut/Projet/Affiche.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,177 @@ | ||||
| package fr.iut.Projet; | ||||
|  | ||||
| import javax.swing.*; | ||||
| import java.awt.*; | ||||
|  | ||||
| /** | ||||
|  * Classe graphique responsable du dessin du pendu dans le jeu. | ||||
|  * <p> | ||||
|  * Cette classe hérite de {@link JComponent} et redéfinit la méthode | ||||
|  * {@link #paintComponent(Graphics)} pour afficher le pendu selon le nombre | ||||
|  * de vies restantes. Elle affiche également un message de victoire ou de défaite. | ||||
|  * </p> | ||||
|  * | ||||
|  * @author  | ||||
|  * @version 1.0 | ||||
|  */ | ||||
| class Affiche extends JComponent { | ||||
|  | ||||
|     /** Nombre de vies restantes (entre 0 et 8). */ | ||||
|     private int lives = 8; | ||||
|  | ||||
|     /** Indique si le joueur a gagné. */ | ||||
|     private boolean youWin = false; | ||||
|  | ||||
|     /** | ||||
|      * Définit le nombre de vies restantes et redessine le composant. | ||||
|      * | ||||
|      * @param lives nombre de vies restantes | ||||
|      */ | ||||
|     public void setLives(int lives) { | ||||
|         this.lives = lives; | ||||
|         repaint(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Définit si le joueur a gagné et redessine le composant. | ||||
|      * | ||||
|      * @param value {@code true} si le joueur a gagné, sinon {@code false} | ||||
|      */ | ||||
|     public void setYouWin(boolean value) { | ||||
|         this.youWin = value; | ||||
|         repaint(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Redessine le pendu en fonction du nombre de vies restantes. | ||||
|      * <ul> | ||||
|      *     <li>Affiche la potence dès la première erreur</li> | ||||
|      *     <li>Ajoute progressivement les membres du pendu</li> | ||||
|      *     <li>Affiche une tête de mort si toutes les vies sont perdues</li> | ||||
|      *     <li>Affiche un message "YOU WIN" si la partie est gagnée</li> | ||||
|      * </ul> | ||||
|      * | ||||
|      * @param g contexte graphique utilisé pour dessiner | ||||
|      */ | ||||
|     @Override | ||||
|     protected void paintComponent(Graphics g) { | ||||
|         super.paintComponent(g); | ||||
|  | ||||
|         if (isOpaque()) { | ||||
|             g.setColor(getBackground()); | ||||
|             g.fillRect(0, 0, getWidth(), getHeight()); | ||||
|         } | ||||
|  | ||||
|         Graphics2D g2 = (Graphics2D) g.create(); | ||||
|         g2.setStroke(new BasicStroke(3)); | ||||
|         g2.setColor(Color.BLACK); | ||||
|  | ||||
|         if (lives <= 7) drawGallows(g2); | ||||
|         if (lives <= 6) drawHead(g2); | ||||
|         if (lives <= 5) drawBody(g2); | ||||
|         if (lives <= 4) drawLeftArm(g2); | ||||
|         if (lives <= 3) drawRightArm(g2); | ||||
|         if (lives <= 2) drawLeftLeg(g2); | ||||
|         if (lives <= 1) drawRightLeg(g2); | ||||
|         if (lives == 1) drawAura(g2); | ||||
|         if (lives == 0) drawSkull(g2); | ||||
|         if (youWin) drawYouWinMessage(g2); | ||||
|  | ||||
|         g2.dispose(); | ||||
|     } | ||||
|  | ||||
|     // ==================== Méthodes de dessin ==================== | ||||
|  | ||||
|     /** Dessine la potence. */ | ||||
|     private void drawGallows(Graphics2D g2) { | ||||
|         g2.drawLine(50, 350, 200, 350); | ||||
|         g2.drawLine(100, 350, 100, 50); | ||||
|         g2.drawLine(100, 50, 250, 50); | ||||
|         g2.drawLine(250, 50, 250, 100); | ||||
|         g2.drawLine(100, 100, 180, 50); | ||||
|     } | ||||
|  | ||||
|     // Coordonnées et tailles du pendu | ||||
|     private final int headX = 225, headY = 100, headDiam = 50; | ||||
|     private final int bodyX = headX + headDiam / 2; | ||||
|     private final int bodyYStart = headY + headDiam; | ||||
|     private final int bodyYEnd = bodyYStart + 100; | ||||
|     private final int armLength = 60; | ||||
|     private final int legLength = 70; | ||||
|  | ||||
|     /** Dessine la tête du pendu. */ | ||||
|     private void drawHead(Graphics2D g2) { g2.drawOval(headX, headY, headDiam, headDiam); } | ||||
|  | ||||
|     /** Dessine le corps du pendu. */ | ||||
|     private void drawBody(Graphics2D g2) { g2.drawLine(bodyX, bodyYStart, bodyX, bodyYEnd); } | ||||
|  | ||||
|     /** Dessine le bras gauche. */ | ||||
|     private void drawLeftArm(Graphics2D g2) { | ||||
|         g2.drawLine(bodyX, bodyYStart + 20, bodyX - armLength, bodyYStart + 20); | ||||
|     } | ||||
|  | ||||
|     /** Dessine le bras droit. */ | ||||
|     private void drawRightArm(Graphics2D g2) { | ||||
|         g2.drawLine(bodyX, bodyYStart + 20, bodyX + armLength, bodyYStart + 20); | ||||
|     } | ||||
|  | ||||
|     /** Dessine la jambe gauche. */ | ||||
|     private void drawLeftLeg(Graphics2D g2) { | ||||
|         g2.drawLine(bodyX, bodyYEnd, bodyX - 5, bodyYEnd + legLength); | ||||
|     } | ||||
|  | ||||
|     /** Dessine la jambe droite. */ | ||||
|     private void drawRightLeg(Graphics2D g2) { | ||||
|         g2.drawLine(bodyX, bodyYEnd, bodyX + 5, bodyYEnd + legLength); | ||||
|     } | ||||
|  | ||||
|     /** Dessine une aura bleue lorsque le joueur est sur sa dernière vie. */ | ||||
|     private void drawAura(Graphics2D g2) { | ||||
|         g2.setColor(new Color(0, 0, 255, 100)); | ||||
|         g2.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); | ||||
|         g2.drawOval(headX, headY, headDiam, headDiam); | ||||
|         g2.drawLine(bodyX, bodyYStart, bodyX, bodyYEnd); | ||||
|         g2.drawLine(bodyX, bodyYStart + 20, bodyX - armLength, bodyYStart + 20); | ||||
|         g2.drawLine(bodyX, bodyYStart + 20, bodyX + armLength, bodyYStart + 20); | ||||
|         g2.drawLine(bodyX, bodyYEnd, bodyX - 5, bodyYEnd + legLength); | ||||
|         g2.drawLine(bodyX, bodyYEnd, bodyX + 5, bodyYEnd + legLength); | ||||
|     } | ||||
|  | ||||
|     /** Dessine la tête de mort et le message "GAME OVER". */ | ||||
|     private void drawSkull(Graphics2D g2) { | ||||
|         int skullX = 225, skullY = 100, skullDiam = 50; | ||||
|  | ||||
|         g2.setColor(Color.BLACK); | ||||
|         g2.fillOval(skullX, skullY, skullDiam, skullDiam); | ||||
|  | ||||
|         g2.setColor(Color.WHITE); | ||||
|         g2.fillOval(skullX + skullDiam / 5, skullY + skullDiam / 6, skullDiam / 5, skullDiam / 5); | ||||
|         g2.fillOval(skullX + 3 * skullDiam / 5, skullY + skullDiam / 6, skullDiam / 5, skullDiam / 5); | ||||
|         g2.fillOval(skullX + skullDiam / 2 - skullDiam / 12, skullY + skullDiam / 2 - skullDiam / 12, skullDiam / 6, skullDiam / 6); | ||||
|  | ||||
|         g2.setStroke(new BasicStroke(2)); | ||||
|         g2.drawLine(skullX + skullDiam / 5, skullY + 2 * skullDiam / 3, | ||||
|                     skullX + 4 * skullDiam / 5, skullY + 2 * skullDiam / 3); | ||||
|  | ||||
|         g2.setColor(Color.RED); | ||||
|         g2.setFont(new Font("Arial", Font.BOLD, 36)); | ||||
|         String message = "GAME OVER"; | ||||
|         FontMetrics fm = g2.getFontMetrics(); | ||||
|         int textWidth = fm.stringWidth(message); | ||||
|         int xText = (getWidth() - textWidth) / 2; | ||||
|         int yText = skullY - 20; | ||||
|         g2.drawString(message, xText, yText); | ||||
|     } | ||||
|  | ||||
|     /** Dessine le message "YOU WIN" en vert en cas de victoire. */ | ||||
|     private void drawYouWinMessage(Graphics2D g2) { | ||||
|         g2.setColor(Color.GREEN); | ||||
|         g2.setFont(new Font("Arial", Font.BOLD, 36)); | ||||
|         String message = "YOU WIN"; | ||||
|         FontMetrics fm = g2.getFontMetrics(); | ||||
|         int textWidth = fm.stringWidth(message); | ||||
|         int xText = (getWidth() - textWidth) / 2; | ||||
|         int yText = 50; | ||||
|         g2.drawString(message, xText, yText); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										10194
									
								
								src/fr/iut/Projet/Dictionary/EASY-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10194
									
								
								src/fr/iut/Projet/Dictionary/EASY-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										999
									
								
								src/fr/iut/Projet/Dictionary/HARD-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										999
									
								
								src/fr/iut/Projet/Dictionary/HARD-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										11090
									
								
								src/fr/iut/Projet/Dictionary/MEDIUM-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11090
									
								
								src/fr/iut/Projet/Dictionary/MEDIUM-FRENCH.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										65
									
								
								src/fr/iut/Projet/Display.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								src/fr/iut/Projet/Display.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | ||||
| package fr.iut.Projet; | ||||
|  | ||||
| import javax.swing.*; | ||||
| import java.awt.*; | ||||
|  | ||||
| /** | ||||
|  * Classe Display représentant le menu principal du jeu "Hanging Man". | ||||
|  * Elle affiche le titre et trois boutons de difficulté : Facile, Moyen et Difficile. | ||||
|  */ | ||||
| public class Display { | ||||
|  | ||||
|     /** | ||||
|      * Méthode principale. Crée et affiche la fenêtre du menu principal. | ||||
|      * @param args Arguments de la ligne de commande (non utilisés) | ||||
|      */ | ||||
|     public static void main(String[] args) { | ||||
|         // Création de la fenêtre principale | ||||
|         JFrame frame = new JFrame("Hanging Man"); | ||||
|         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||||
|         frame.setSize(1040, 1000); | ||||
|         frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); | ||||
|  | ||||
|         // Titre du jeu | ||||
|         JLabel text = new JLabel("Hanging Man"); | ||||
|         text.setAlignmentX(Component.CENTER_ALIGNMENT); | ||||
|         text.setFont(new Font("Arial", Font.BOLD, 70)); | ||||
|  | ||||
|         // Création des boutons de difficulté | ||||
|         JButton easyButton = new JButton("Facile"); | ||||
|         JButton mediumButton = new JButton("Moyen"); | ||||
|         JButton hardButton = new JButton("Difficile"); | ||||
|         JButton hellButton = new JButton("Hell"); | ||||
|  | ||||
|         // Mise en forme des boutons | ||||
|         JButton[] buttons = { easyButton, mediumButton, hardButton, hellButton}; | ||||
|         for (JButton button : buttons) { | ||||
|             button.setAlignmentX(Component.CENTER_ALIGNMENT); | ||||
|             button.setPreferredSize(new Dimension(300, 100)); | ||||
|             button.setMaximumSize(new Dimension(300, 150)); | ||||
|             button.setFont(new Font("Arial", Font.PLAIN, 36)); | ||||
|         } | ||||
|  | ||||
|         // Ajout d'écouteurs (listeners) | ||||
|         easyButton.addActionListener(new PlayButtonListener(frame, "facile")); | ||||
|         mediumButton.addActionListener(new PlayButtonListener(frame, "moyen")); | ||||
|         hardButton.addActionListener(new PlayButtonListener(frame, "difficile")); | ||||
|         hellButton.addActionListener(new PlayButtonListener(frame, "hell")); | ||||
|  | ||||
|         // Ajout des composants à la fenêtre | ||||
|         frame.add(Box.createVerticalGlue()); | ||||
|         frame.add(text); | ||||
|         frame.add(Box.createVerticalStrut(50)); | ||||
|         frame.add(easyButton); | ||||
|         frame.add(Box.createVerticalStrut(20)); | ||||
|         frame.add(mediumButton); | ||||
|         frame.add(Box.createVerticalStrut(20)); | ||||
|         frame.add(hardButton); | ||||
|         frame.add(Box.createVerticalStrut(20)); | ||||
|         frame.add(hellButton); | ||||
|         frame.add(Box.createVerticalGlue()); | ||||
|  | ||||
|         // Affichage de la fenêtre | ||||
|         frame.setVisible(true); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										79
									
								
								src/fr/iut/Projet/GameTimer.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								src/fr/iut/Projet/GameTimer.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,79 @@ | ||||
| package fr.iut.Projet; | ||||
|  | ||||
| import javax.swing.*; | ||||
| import java.awt.event.ActionEvent; | ||||
| import java.awt.event.ActionListener; | ||||
|  | ||||
| /** | ||||
|  * Classe GameTimer qui gère le temps écoulé sur un mot. | ||||
|  *  | ||||
|  * Affiche le temps en secondes dans un JLabel et peut déclencher un listener | ||||
|  * à chaque seconde (par exemple pour réduire le score ou vérifier la limite de temps). | ||||
|  */ | ||||
| public class GameTimer { | ||||
|  | ||||
|     /** Label affichant le temps écoulé */ | ||||
|     private JLabel timerLabel; | ||||
|  | ||||
|     /** Timer Swing qui incrémente le temps chaque seconde */ | ||||
|     private Timer timer; | ||||
|  | ||||
|     /** Compteur de secondes écoulées */ | ||||
|     private int secondsElapsed; | ||||
|  | ||||
|     /** Listener appelé à chaque tick (chaque seconde) */ | ||||
|     private Runnable timerListener; | ||||
|  | ||||
|     /** Constructeur : crée le label et initialise le timer */ | ||||
|     public GameTimer() { | ||||
|         this.secondsElapsed = 0; | ||||
|         this.timerLabel = new JLabel("Temps : 0 s"); | ||||
|         timerLabel.setHorizontalAlignment(SwingConstants.RIGHT); | ||||
|  | ||||
|         // Timer qui déclenche l'action toutes les 1000ms (1 seconde) | ||||
|         this.timer = new Timer(1000, new ActionListener() { | ||||
|             @Override | ||||
|             public void actionPerformed(ActionEvent e) { | ||||
|                 secondsElapsed++; | ||||
|                 timerLabel.setText("Temps : " + secondsElapsed + " s"); | ||||
|  | ||||
|                 // Appel du listener pour gérer le score ou la fin de partie | ||||
|                 if (timerListener != null) { | ||||
|                     timerListener.run(); | ||||
|                 } | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     /** Définit le listener appelé à chaque seconde */ | ||||
|     public void setTimerListener(Runnable listener) { | ||||
|         this.timerListener = listener; | ||||
|     } | ||||
|  | ||||
|     /** Démarre le timer */ | ||||
|     public void start() { | ||||
|         secondsElapsed = 0; | ||||
|         timer.start(); | ||||
|     } | ||||
|  | ||||
|     /** Arrête le timer */ | ||||
|     public void stop() { | ||||
|         timer.stop(); | ||||
|     } | ||||
|  | ||||
|     /** Remet le compteur à zéro */ | ||||
|     public void reset() { | ||||
|         secondsElapsed = 0; | ||||
|         timerLabel.setText("Temps : 0 s"); | ||||
|     } | ||||
|  | ||||
|     /** @return le JLabel contenant le temps écoulé */ | ||||
|     public JLabel getTimerLabel() { | ||||
|         return timerLabel; | ||||
|     } | ||||
|  | ||||
|     /** @return le temps écoulé en secondes */ | ||||
|     public int getSecondsElapsed() { | ||||
|         return secondsElapsed; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										42
									
								
								src/fr/iut/Projet/PlayButtonListener.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/fr/iut/Projet/PlayButtonListener.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| package fr.iut.Projet; | ||||
|  | ||||
| import javax.swing.*; | ||||
| import java.awt.event.ActionEvent; | ||||
| import java.awt.event.ActionListener; | ||||
|  | ||||
| /** | ||||
|  * Classe PlayButtonListener qui gère le clic sur les boutons de difficulté. | ||||
|  * Lorsqu'on clique sur un bouton, elle ouvre la fenêtre du jeu avec le niveau choisi | ||||
|  * et ferme la fenêtre principale. | ||||
|  */ | ||||
| public class PlayButtonListener implements ActionListener { | ||||
|  | ||||
|     /** Référence à la fenêtre principale du menu */ | ||||
|     private JFrame parentFrame; | ||||
|  | ||||
|     /** Niveau de difficulté choisi */ | ||||
|     private String difficulty; | ||||
|  | ||||
|     /** | ||||
|      * Constructeur de PlayButtonListener. | ||||
|      * @param frame La fenêtre principale à fermer après ouverture du jeu | ||||
|      * @param difficulty Le niveau de difficulté choisi ("facile", "moyen" ou "difficile") | ||||
|      */ | ||||
|     public PlayButtonListener(JFrame frame, String difficulty) { | ||||
|         this.parentFrame = frame; | ||||
|         this.difficulty = difficulty; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Méthode appelée lorsque le bouton est cliqué. | ||||
|      * Ouvre la fenêtre du jeu avec la difficulté sélectionnée | ||||
|      * et ferme la fenêtre principale. | ||||
|      * @param e L'événement déclenché par le clic | ||||
|      */ | ||||
|     @Override | ||||
|     public void actionPerformed(ActionEvent e) { | ||||
|         // Passe la difficulté à la classe Action | ||||
|         new Action(difficulty);   | ||||
|         parentFrame.dispose(); // Ferme le menu principal | ||||
|     } | ||||
| } | ||||
							
								
								
									
										205
									
								
								src/fr/iut/Projet/Random_word.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										205
									
								
								src/fr/iut/Projet/Random_word.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,205 @@ | ||||
| package fr.iut.Projet; | ||||
|  | ||||
| import java.io.*; | ||||
| import java.util.*; | ||||
|  | ||||
| /** | ||||
|  * Classe gérant la logique du jeu du pendu. | ||||
|  *  | ||||
|  * Cette classe sélectionne un mot (ou une combinaison de mots) aléatoire | ||||
|  * selon la difficulté, conserve l'état du jeu (mot secret, lettres devinées, | ||||
|  * lettres incorrectes, nombre de vies) et fournit des méthodes pour deviner | ||||
|  * des lettres et vérifier l'état du jeu. | ||||
|  */ | ||||
| public class Random_word { | ||||
|  | ||||
|     /** Mot secret que le joueur doit deviner */ | ||||
|     private String secretWord; | ||||
|  | ||||
|     /** Tableau représentant le mot caché avec les lettres découvertes et espaces */ | ||||
|     private char[] hiddenWord; | ||||
|  | ||||
|     /** Lettres correctement devinées par le joueur */ | ||||
|     private Set<Character> lettersGuessed; | ||||
|  | ||||
|     /** Lettres incorrectes devinées par le joueur */ | ||||
|     private Set<Character> incorrectLetters; | ||||
|  | ||||
|     /** Nombre de vies restantes */ | ||||
|     private int lives; | ||||
|  | ||||
|     /** | ||||
|      * Constructeur : sélectionne un mot aléatoire selon la difficulté | ||||
|      * et initialise les structures du jeu. | ||||
|      *  | ||||
|      * @param difficulty Niveau de difficulté ("facile", "moyen", "difficile", "hell") | ||||
|      * @throws RuntimeException si aucun mot n'a pu être choisi | ||||
|      */ | ||||
|     public Random_word(String difficulty) { | ||||
|         if (difficulty.equalsIgnoreCase("hell")) { | ||||
|             // En mode Hell, on choisit deux mots faciles et on les concatène avec un espace | ||||
|             String firstWord = getRandomWord("facile"); | ||||
|             String secondWord = getRandomWord("facile"); | ||||
|  | ||||
|             if (firstWord == null || secondWord == null) { | ||||
|                 throw new RuntimeException("Impossible de choisir deux mots pour le mode Hell !"); | ||||
|             } | ||||
|  | ||||
|             this.secretWord = firstWord + " " + secondWord; | ||||
|         } else { | ||||
|             // Pour les autres difficultés, on choisit un seul mot correspondant | ||||
|             this.secretWord = getRandomWord(difficulty); | ||||
|         } | ||||
|  | ||||
|         if (this.secretWord == null) { | ||||
|             throw new RuntimeException("Impossible de choisir un mot aléatoire !"); | ||||
|         } | ||||
|  | ||||
|         this.lives = 8; // nombre de vies par défaut | ||||
|         this.hiddenWord = new char[secretWord.length()]; | ||||
|  | ||||
|         // Initialise le mot caché : underscore pour les lettres, espace conservé | ||||
|         for (int i = 0; i < secretWord.length(); i++) { | ||||
|             hiddenWord[i] = (secretWord.charAt(i) == ' ') ? ' ' : '_'; | ||||
|         } | ||||
|  | ||||
|         this.lettersGuessed = new HashSet<>(); | ||||
|         this.incorrectLetters = new HashSet<>(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Lit un fichier de mots correspondant à la difficulté et retourne un mot aléatoire. | ||||
|      *  | ||||
|      * @param difficulty Niveau de difficulté | ||||
|      * @return mot aléatoire en minuscules | ||||
|      */ | ||||
|     private String getRandomWord(String difficulty) { | ||||
|         String fileName; | ||||
|  | ||||
|         // Sélection du fichier selon la difficulté | ||||
|         switch (difficulty.toLowerCase()) { | ||||
|             case "facile": | ||||
|                 fileName = "/fr/iut/Projet/Dictionary/EASY-FRENCH.txt"; | ||||
|                 break; | ||||
|             case "moyen": | ||||
|                 fileName = "/fr/iut/Projet/Dictionary/MEDIUM-FRENCH.txt"; | ||||
|                 break; | ||||
|             case "difficile": | ||||
|                 fileName = "/fr/iut/Projet/Dictionary/HARD-FRENCH.txt"; | ||||
|                 break; | ||||
|             default: | ||||
|                 System.out.println("Difficulté inconnue, mode 'moyen' utilisé par défaut."); | ||||
|                 fileName = "/fr/iut/Projet/Dictionary/MEDIUM-FRENCH.txt"; | ||||
|         } | ||||
|  | ||||
|         // Récupère le fichier dans les ressources | ||||
|         InputStream is = Random_word.class.getResourceAsStream(fileName); | ||||
|         if (is == null) { | ||||
|             System.err.println("Fichier introuvable : " + fileName); | ||||
|             return null; | ||||
|         } | ||||
|  | ||||
|         String randomWord = null; | ||||
|         Random random = new Random(); | ||||
|  | ||||
|         try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) { | ||||
|             String line; | ||||
|             int count = 0; | ||||
|             while ((line = br.readLine()) != null) { | ||||
|                 line = line.trim(); | ||||
|                 if (!line.isEmpty()) { | ||||
|                     count++; | ||||
|                     // Choix aléatoire d'une ligne avec Reservoir Sampling | ||||
|                     if (random.nextInt(count) == 0) { | ||||
|                         randomWord = line; | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } catch (IOException e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|  | ||||
|         return randomWord != null ? randomWord.toLowerCase() : null; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Tente une lettre proposée par le joueur. | ||||
|      * Met à jour l'état du jeu (mot caché, lettres incorrectes, vies restantes). | ||||
|      *  | ||||
|      * @param letter lettre proposée | ||||
|      * @return message d'information pour le joueur (succès, erreur ou fin de partie) | ||||
|      */ | ||||
|     public String guessLetter(char letter) { | ||||
|         letter = Character.toLowerCase(letter); | ||||
|  | ||||
|         // Vérifie si la lettre a déjà été essayée | ||||
|         if (lettersGuessed.contains(letter) || incorrectLetters.contains(letter)) { | ||||
|             return "Vous avez déjà essayé cette lettre !"; | ||||
|         } | ||||
|  | ||||
|         // Lettre correcte | ||||
|         if (secretWord.indexOf(letter) >= 0) { | ||||
|             lettersGuessed.add(letter); | ||||
|  | ||||
|             // Remplace les underscores par la lettre devinée | ||||
|             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 { | ||||
|             // Lettre incorrecte | ||||
|             incorrectLetters.add(letter); | ||||
|             lives--; | ||||
|  | ||||
|             if (lives <= 0) { | ||||
|                 return "Vous avez perdu ! Le mot était : " + secretWord; | ||||
|             } | ||||
|  | ||||
|             return "Mauvaise lettre ! Il vous reste " + lives + " vies."; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** @return le mot caché avec les lettres découvertes séparées par des espaces */ | ||||
|     public String getHiddenWord() { | ||||
|         StringBuilder sb = new StringBuilder(); | ||||
|         for (char c : hiddenWord) sb.append(c).append(' '); | ||||
|         return sb.toString().trim(); | ||||
|     } | ||||
|  | ||||
|     /** @return la liste des lettres incorrectes devinées */ | ||||
|     public String getIncorrectLetters() { | ||||
|         return incorrectLetters.toString(); | ||||
|     } | ||||
|  | ||||
|     /** @return nombre de vies restantes */ | ||||
|     public int getLives() { | ||||
|         return lives; | ||||
|     } | ||||
|  | ||||
|     /** @return true si le jeu est terminé (victoire ou défaite) */ | ||||
|     public boolean isGameOver() { | ||||
|         return lives <= 0 || isWon(); | ||||
|     } | ||||
|  | ||||
|     /** @return true si le mot est entièrement deviné */ | ||||
|     public boolean isWon() { | ||||
|         for (char c : hiddenWord) { | ||||
|             if (c == '_') { | ||||
|                 return false; | ||||
|             } | ||||
|         } | ||||
|         return true; | ||||
|     } | ||||
|  | ||||
|     /** @return le mot secret */ | ||||
|     public String getSecretWord() { | ||||
|         return secretWord; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										70
									
								
								src/fr/iut/Projet/Score.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								src/fr/iut/Projet/Score.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,70 @@ | ||||
| package fr.iut.Projet; | ||||
|  | ||||
| import javax.swing.*; | ||||
| import java.awt.*; | ||||
|  | ||||
| /** | ||||
|  * Classe Score qui gère le score du joueur. | ||||
|  *  | ||||
|  * Commence à 100, diminue à chaque seconde et à chaque erreur, | ||||
|  * augmente à chaque bonne lettre et ajoute un bonus si le mot est trouvé. | ||||
|  * Si le joueur perd, le score devient 0. | ||||
|  */ | ||||
| public class Score { | ||||
|  | ||||
|     private int currentScore; | ||||
|     private JLabel scoreLabel; | ||||
|  | ||||
|     /** Constructeur : initialise le score à 100 */ | ||||
|     public Score() { | ||||
|         this.currentScore = 100; | ||||
|         this.scoreLabel = new JLabel("Score : " + currentScore); | ||||
|         this.scoreLabel.setFont(new Font("Arial", Font.BOLD, 16)); | ||||
|         this.scoreLabel.setHorizontalAlignment(SwingConstants.RIGHT); | ||||
|     } | ||||
|  | ||||
|     /** Décrémente le score de 1 point chaque seconde */ | ||||
|     public void decreaseBySecond() { | ||||
|         currentScore = Math.max(0, currentScore - 1); | ||||
|         updateLabel(); | ||||
|     } | ||||
|  | ||||
|     /** Décrémente le score de 5 points pour une mauvaise lettre */ | ||||
|     public void decreaseByWrongLetter() { | ||||
|         currentScore = Math.max(0, currentScore - 5); | ||||
|         updateLabel(); | ||||
|     } | ||||
|  | ||||
|     /** Incrémente le score de 10 points pour une bonne lettre */ | ||||
|     public void increaseByCorrectLetter() { | ||||
|         currentScore += 10; | ||||
|         updateLabel(); | ||||
|     } | ||||
|  | ||||
|     /** Incrémente le score de 50 points si le mot est trouvé */ | ||||
|     public void increaseByWordFound() { | ||||
|         currentScore += 50; | ||||
|         updateLabel(); | ||||
|     } | ||||
|  | ||||
|     /** Met le score à zéro (utilisé si le joueur perd) */ | ||||
|     public void setScoreToZero() { | ||||
|         currentScore = 0; | ||||
|         updateLabel(); | ||||
|     } | ||||
|  | ||||
|     /** Met à jour le texte du JLabel */ | ||||
|     private void updateLabel() { | ||||
|         scoreLabel.setText("Score : " + currentScore); | ||||
|     } | ||||
|  | ||||
|     /** @return le JLabel contenant le score */ | ||||
|     public JLabel getScoreLabel() { | ||||
|         return scoreLabel; | ||||
|     } | ||||
|  | ||||
|     /** @return le score actuel */ | ||||
|     public int getCurrentScore() { | ||||
|         return currentScore; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user