forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
4 Commits
b60bda7ae0
...
92aa82e7c7
| Author | SHA1 | Date | |
|---|---|---|---|
| 92aa82e7c7 | |||
| 7d2a0a12f9 | |||
| aef5c7f70c | |||
| dcac91d944 |
4
Makefile
4
Makefile
@@ -1,6 +1,7 @@
|
|||||||
# === Configuration ===
|
# === Configuration ===
|
||||||
SRC_DIR = src
|
SRC_DIR = src
|
||||||
OUT_DIR = out
|
OUT_DIR = out
|
||||||
|
Dictionary = fr/iut/Projet/Dictionary
|
||||||
PACKAGE = fr/iut/Projet
|
PACKAGE = fr/iut/Projet
|
||||||
MAIN_CLASS = fr.iut.Projet.Display
|
MAIN_CLASS = fr.iut.Projet.Display
|
||||||
|
|
||||||
@@ -14,7 +15,8 @@ compile:
|
|||||||
# Compilation de tous les fichiers Java du package
|
# Compilation de tous les fichiers Java du package
|
||||||
@javac -d $(OUT_DIR) $(SRC_DIR)/$(PACKAGE)/*.java
|
@javac -d $(OUT_DIR) $(SRC_DIR)/$(PACKAGE)/*.java
|
||||||
# Copier Word.txt dans le dossier de sortie
|
# Copier Word.txt dans le dossier de sortie
|
||||||
@cp $(SRC_DIR)/$(PACKAGE)/Word.txt $(OUT_DIR)/$(PACKAGE)/
|
@mkdir -p $(OUT_DIR)/$(Dictionary)
|
||||||
|
@cp $(SRC_DIR)/$(Dictionary)/*.txt $(OUT_DIR)/$(Dictionary)/
|
||||||
@echo "Compilation terminée."
|
@echo "Compilation terminée."
|
||||||
|
|
||||||
# === Exécution ===
|
# === Exécution ===
|
||||||
|
|||||||
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.
Binary file not shown.
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
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
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
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -4,200 +4,181 @@ import javax.swing.*;
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Classe qui représente l'interface graphique du jeu du pendu.
|
* Classe principale qui gère l'interface graphique du jeu du pendu.
|
||||||
* Elle gère l'affichage du mot caché, des lettres incorrectes, des vies
|
*
|
||||||
* et permet de saisir des lettres. Un bouton permet de relancer le jeu.
|
* Cette classe crée la fenêtre du jeu, affiche :
|
||||||
|
* - le mot caché avec les lettres devinées,
|
||||||
|
* - les lettres incorrectes,
|
||||||
|
* - le dessin du pendu (via {@link Affiche}),
|
||||||
|
* et permet à l'utilisateur de saisir des lettres.
|
||||||
|
* Elle gère également la logique de mise à jour, la fin de partie,
|
||||||
|
* le redémarrage et le changement de difficulté en utilisant {@link PlayButtonListener}.
|
||||||
*/
|
*/
|
||||||
public class Action {
|
public class Action {
|
||||||
|
|
||||||
// Fenêtre principale du jeu
|
/** Fenêtre principale du jeu */
|
||||||
private JFrame gameFrame;
|
private JFrame gameFrame;
|
||||||
|
|
||||||
// Labels pour afficher le mot, les lettres incorrectes et les vies
|
/** Label affichant le mot caché */
|
||||||
private JLabel wordLabel;
|
private JLabel wordLabel;
|
||||||
private JLabel incorrectLettersLabel;
|
|
||||||
private JLabel livesLabel;
|
|
||||||
|
|
||||||
// Champ de saisie pour entrer une lettre
|
/** Label affichant les lettres incorrectes */
|
||||||
|
private JLabel incorrectLettersLabel;
|
||||||
|
|
||||||
|
/** Champ de texte pour saisir une lettre */
|
||||||
private JTextField letterInputField;
|
private JTextField letterInputField;
|
||||||
|
|
||||||
// Instance du jeu
|
/** Instance du jeu avec la logique (mot secret, lettres, vies, etc.) */
|
||||||
private Random_word game;
|
private Random_word game;
|
||||||
|
|
||||||
|
/** Composant graphique qui dessine le pendu */
|
||||||
|
private Affiche affiche;
|
||||||
|
|
||||||
|
/** Niveau de difficulté courant ("facile", "moyen", "difficile") */
|
||||||
|
private String difficulty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructeur de la classe Action.
|
* Constructeur de la classe Action.
|
||||||
* Initialise le jeu, les composants graphiques et affiche la fenêtre.
|
* Initialise le jeu avec la difficulté choisie, crée et dispose les composants.
|
||||||
|
*
|
||||||
|
* @param difficulty Niveau de difficulté pour le mot à deviner.
|
||||||
*/
|
*/
|
||||||
public Action() {
|
public Action(String difficulty) {
|
||||||
game = new Random_word();
|
this.difficulty = difficulty;
|
||||||
|
|
||||||
|
// Création du jeu avec le mot choisi selon la difficulté
|
||||||
|
game = new Random_word(difficulty);
|
||||||
|
|
||||||
|
// Initialisation des composants graphiques
|
||||||
initializeComponents();
|
initializeComponents();
|
||||||
layoutComponents();
|
layoutComponents();
|
||||||
|
|
||||||
|
// Gestion de la saisie utilisateur
|
||||||
letterInputField.addActionListener(e -> handleGuess());
|
letterInputField.addActionListener(e -> handleGuess());
|
||||||
|
|
||||||
|
// Affiche la fenêtre
|
||||||
gameFrame.setVisible(true);
|
gameFrame.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== Initialisation des composants ====================
|
// ==================== Initialisation des composants ====================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialise tous les composants graphiques :
|
* Initialise tous les composants graphiques du jeu :
|
||||||
* - Fenêtre principale
|
* - fenêtre principale
|
||||||
* - Labels pour le mot, les vies et les lettres incorrectes
|
* - dessin du pendu
|
||||||
* - Champ de saisie pour entrer les lettres
|
* - labels pour le mot et les lettres incorrectes
|
||||||
|
* - champ de saisie
|
||||||
*/
|
*/
|
||||||
private void initializeComponents() {
|
private void initializeComponents() {
|
||||||
gameFrame = new JFrame("Hanging Man");
|
gameFrame = new JFrame("Hanging Man - " + difficulty);
|
||||||
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
gameFrame.setSize(600, 400);
|
gameFrame.setSize(700, 500);
|
||||||
|
|
||||||
|
// Composant graphique pour le pendu
|
||||||
|
affiche = new Affiche();
|
||||||
|
affiche.setPreferredSize(new Dimension(350, 400));
|
||||||
|
affiche.setBackground(Color.WHITE);
|
||||||
|
affiche.setOpaque(true);
|
||||||
|
|
||||||
|
// Label affichant le mot caché
|
||||||
wordLabel = new JLabel(game.getHiddenWord());
|
wordLabel = new JLabel(game.getHiddenWord());
|
||||||
wordLabel.setFont(new Font("Arial", Font.BOLD, 32));
|
wordLabel.setFont(new Font("Arial", Font.BOLD, 32));
|
||||||
wordLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
wordLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
|
||||||
livesLabel = new JLabel("Lives: " + game.getLives());
|
// Label affichant les lettres incorrectes
|
||||||
livesLabel.setFont(new Font("Arial", Font.BOLD, 20));
|
|
||||||
livesLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
|
||||||
|
|
||||||
incorrectLettersLabel = new JLabel("Incorrect letters: " + game.getIncorrectLetters());
|
incorrectLettersLabel = new JLabel("Incorrect letters: " + game.getIncorrectLetters());
|
||||||
incorrectLettersLabel.setFont(new Font("Arial", Font.PLAIN, 20));
|
incorrectLettersLabel.setFont(new Font("Arial", Font.PLAIN, 20));
|
||||||
incorrectLettersLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
incorrectLettersLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
|
||||||
|
// Champ pour saisir une lettre
|
||||||
letterInputField = new JTextField(3);
|
letterInputField = new JTextField(3);
|
||||||
letterInputField.setFont(new Font("Arial", Font.PLAIN, 24));
|
letterInputField.setFont(new Font("Arial", Font.PLAIN, 24));
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== Layout des composants ====================
|
// ==================== Mise en page ====================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispose les composants dans la fenêtre principale en utilisant GridBagLayout.
|
* Dispose tous les composants graphiques dans la fenêtre.
|
||||||
* Sépare la fenêtre en deux parties : le haut (mot et vies) et le bas (lettres incorrectes, saisie, bouton restart)
|
* Utilise un BorderLayout principal avec :
|
||||||
|
* - panneau gauche pour le dessin du pendu
|
||||||
|
* - panneau droit pour les interactions utilisateur
|
||||||
*/
|
*/
|
||||||
private void layoutComponents() {
|
private void layoutComponents() {
|
||||||
JPanel mainPanel = new JPanel(new GridBagLayout());
|
JPanel mainPanel = new JPanel(new BorderLayout());
|
||||||
GridBagConstraints mainConstraints = new GridBagConstraints();
|
|
||||||
mainConstraints.gridx = 0;
|
|
||||||
mainConstraints.fill = GridBagConstraints.BOTH;
|
|
||||||
mainConstraints.insets = new Insets(5, 5, 5, 5);
|
|
||||||
|
|
||||||
JPanel topPanel = createTopPanel();
|
// Panneau gauche : dessin du pendu
|
||||||
mainConstraints.gridy = 0;
|
JPanel leftPanel = new JPanel(new BorderLayout());
|
||||||
mainConstraints.weighty = 3.0;
|
leftPanel.add(affiche, BorderLayout.CENTER);
|
||||||
mainConstraints.weightx = 1.0;
|
mainPanel.add(leftPanel, BorderLayout.WEST);
|
||||||
mainPanel.add(topPanel, mainConstraints);
|
|
||||||
|
|
||||||
JPanel bottomPanel = createBottomPanel();
|
// Panneau droit : interface du jeu (mot, lettres, saisie, boutons)
|
||||||
mainConstraints.gridy = 1;
|
JPanel rightPanel = new JPanel(new GridBagLayout());
|
||||||
mainConstraints.weighty = 1.0;
|
GridBagConstraints gbc = new GridBagConstraints();
|
||||||
mainPanel.add(bottomPanel, mainConstraints);
|
gbc.gridx = 0;
|
||||||
|
gbc.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc.insets = new Insets(5, 5, 5, 5);
|
||||||
|
|
||||||
gameFrame.add(mainPanel, BorderLayout.CENTER);
|
// Label du mot caché
|
||||||
}
|
gbc.gridy = 0;
|
||||||
|
gbc.weighty = 1.0;
|
||||||
/**
|
rightPanel.add(wordLabel, gbc);
|
||||||
* 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
|
// Label des lettres incorrectes
|
||||||
incorrectLettersLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
gbc.gridy = 1;
|
||||||
bottomPanel.add(incorrectLettersLabel);
|
rightPanel.add(incorrectLettersLabel, gbc);
|
||||||
|
|
||||||
// Champ de saisie
|
// Champ de saisie
|
||||||
JLabel promptLabel = new JLabel("Enter a letter:");
|
gbc.gridy = 2;
|
||||||
promptLabel.setFont(new Font("Arial", Font.PLAIN, 16));
|
JPanel inputRow = new JPanel();
|
||||||
promptLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
inputRow.add(new JLabel("Enter a letter:"));
|
||||||
|
|
||||||
JPanel inputRow = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
|
|
||||||
inputRow.add(promptLabel);
|
|
||||||
inputRow.add(letterInputField);
|
inputRow.add(letterInputField);
|
||||||
inputRow.setAlignmentX(Component.CENTER_ALIGNMENT);
|
rightPanel.add(inputRow, gbc);
|
||||||
|
|
||||||
bottomPanel.add(Box.createVerticalStrut(10));
|
// Bouton de redémarrage
|
||||||
bottomPanel.add(inputRow);
|
gbc.gridy = 3;
|
||||||
|
|
||||||
// Bouton "Restart" en bas à droite
|
|
||||||
JButton restartButton = new JButton("Restart");
|
JButton restartButton = new JButton("Restart");
|
||||||
restartButton.addActionListener(new PlayButtonListener(gameFrame));
|
restartButton.addActionListener(e -> restartGame());
|
||||||
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
rightPanel.add(restartButton, gbc);
|
||||||
buttonPanel.add(restartButton);
|
|
||||||
|
|
||||||
bottomPanel.add(Box.createVerticalStrut(10));
|
// Bouton pour changer la difficulté
|
||||||
bottomPanel.add(buttonPanel);
|
gbc.gridy = 4;
|
||||||
|
JButton changeDifficultyButton = new JButton("Changer la difficulté");
|
||||||
|
changeDifficultyButton.addActionListener(e -> showDifficultyDialog());
|
||||||
|
rightPanel.add(changeDifficultyButton, gbc);
|
||||||
|
|
||||||
return bottomPanel;
|
mainPanel.add(rightPanel, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
// Ajout du panneau principal à la fenêtre
|
||||||
|
gameFrame.add(mainPanel);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== Gestion du jeu ====================
|
// ==================== Gestion du jeu ====================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gère la saisie d'une lettre par le joueur.
|
* Traite la saisie d'une lettre par l'utilisateur.
|
||||||
* Vérifie la validité de la lettre, met à jour l'affichage et termine le jeu si nécessaire.
|
* Met à jour l'état du jeu et l'affichage.
|
||||||
*/
|
*/
|
||||||
private void handleGuess() {
|
private void handleGuess() {
|
||||||
String inputText = letterInputField.getText();
|
String inputText = letterInputField.getText();
|
||||||
letterInputField.setText("");
|
letterInputField.setText(""); // efface le champ après saisie
|
||||||
|
|
||||||
if (!isValidInput(inputText)) return;
|
if (!isValidInput(inputText)) return;
|
||||||
|
|
||||||
char guessedLetter = inputText.charAt(0);
|
char guessedLetter = inputText.charAt(0);
|
||||||
String message = game.guessLetter(guessedLetter);
|
String message = game.guessLetter(guessedLetter);
|
||||||
|
|
||||||
|
// Mise à jour de l'interface
|
||||||
updateUI();
|
updateUI();
|
||||||
|
|
||||||
|
// Si la partie est terminée, affiche le résultat
|
||||||
if (game.isGameOver()) endGame(message);
|
if (game.isGameOver()) endGame(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vérifie si la saisie de l'utilisateur est valide :
|
* Vérifie que l'utilisateur a saisi une seule lettre valide.
|
||||||
* - Une seule lettre
|
*
|
||||||
* - Caractère alphabétique
|
* @param inputText texte saisi
|
||||||
* @param inputText texte saisi par l'utilisateur
|
* @return true si la saisie est valide
|
||||||
* @return true si la saisie est valide, false sinon
|
|
||||||
*/
|
*/
|
||||||
private boolean isValidInput(String inputText) {
|
private boolean isValidInput(String inputText) {
|
||||||
if (inputText.length() != 1 || !Character.isLetter(inputText.charAt(0))) {
|
if (inputText.length() != 1 || !Character.isLetter(inputText.charAt(0))) {
|
||||||
@@ -208,24 +189,90 @@ public class Action {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Met à jour l'affichage des labels :
|
* Met à jour l'affichage du mot caché, des lettres incorrectes et du dessin du pendu.
|
||||||
* - Mot caché
|
|
||||||
* - Lettres incorrectes
|
|
||||||
* - Nombre de vies restantes
|
|
||||||
*/
|
*/
|
||||||
private void updateUI() {
|
private void updateUI() {
|
||||||
wordLabel.setText(game.getHiddenWord());
|
wordLabel.setText(game.getHiddenWord());
|
||||||
incorrectLettersLabel.setText("Incorrect letters: " + game.getIncorrectLetters());
|
incorrectLettersLabel.setText("Incorrect letters: " + game.getIncorrectLetters());
|
||||||
livesLabel.setText("Lives: " + game.getLives());
|
affiche.setLives(game.getLives());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Termine le jeu et affiche un message.
|
* Termine la partie et affiche le message de résultat.
|
||||||
* Désactive le champ de saisie.
|
* Désactive le champ de saisie.
|
||||||
* @param message Message à afficher (victoire ou défaite)
|
*
|
||||||
|
* @param message message à afficher (victoire/défaite)
|
||||||
*/
|
*/
|
||||||
private void endGame(String message) {
|
private void endGame(String message) {
|
||||||
|
if (game.isWon()) {
|
||||||
|
affiche.setYouWin(true);
|
||||||
|
} else {
|
||||||
|
affiche.setLives(0);
|
||||||
|
}
|
||||||
|
|
||||||
JOptionPane.showMessageDialog(gameFrame, message);
|
JOptionPane.showMessageDialog(gameFrame, message);
|
||||||
letterInputField.setEditable(false);
|
letterInputField.setEditable(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==================== Redémarrage ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Redémarre le jeu avec la même difficulté en utilisant PlayButtonListener.
|
||||||
|
*/
|
||||||
|
private void restartGame() {
|
||||||
|
gameFrame.dispose(); // ferme la fenêtre actuelle
|
||||||
|
|
||||||
|
// Crée un JFrame temporaire pour utiliser PlayButtonListener
|
||||||
|
JFrame tempFrame = new JFrame();
|
||||||
|
new PlayButtonListener(tempFrame, difficulty).actionPerformed(null);
|
||||||
|
// tempFrame ne sera jamais affiché, il sert juste à passer la référence
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== Changement de difficulté ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Affiche une boîte de dialogue pour choisir une nouvelle difficulté
|
||||||
|
* et relance une partie avec cette difficulté en utilisant PlayButtonListener.
|
||||||
|
*/
|
||||||
|
private void showDifficultyDialog() {
|
||||||
|
String[] options = {"Facile", "Moyen", "Difficile"};
|
||||||
|
|
||||||
|
// Boîte de dialogue avec trois options
|
||||||
|
int choice = JOptionPane.showOptionDialog(
|
||||||
|
gameFrame,
|
||||||
|
"Choisissez la difficulté :",
|
||||||
|
"Changer la difficulté",
|
||||||
|
JOptionPane.DEFAULT_OPTION,
|
||||||
|
JOptionPane.QUESTION_MESSAGE,
|
||||||
|
null,
|
||||||
|
options,
|
||||||
|
options[0]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Si l'utilisateur a choisi une option
|
||||||
|
if (choice >= 0) {
|
||||||
|
String newDifficulty;
|
||||||
|
switch (choice) {
|
||||||
|
case 0:
|
||||||
|
newDifficulty = "facile";
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
newDifficulty = "moyen";
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
newDifficulty = "difficile";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
newDifficulty = "moyen";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ferme la fenêtre actuelle
|
||||||
|
gameFrame.dispose();
|
||||||
|
|
||||||
|
// Crée un JFrame temporaire pour utiliser PlayButtonListener
|
||||||
|
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
@@ -5,7 +5,7 @@ import java.awt.*;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Classe Display représentant le menu principal du jeu "Hanging Man".
|
* Classe Display représentant le menu principal du jeu "Hanging Man".
|
||||||
* Elle affiche le titre et le bouton Play.
|
* Elle affiche le titre et trois boutons de difficulté : Facile, Moyen et Difficile.
|
||||||
*/
|
*/
|
||||||
public class Display {
|
public class Display {
|
||||||
|
|
||||||
@@ -25,24 +25,37 @@ public class Display {
|
|||||||
text.setAlignmentX(Component.CENTER_ALIGNMENT);
|
text.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
text.setFont(new Font("Arial", Font.BOLD, 70));
|
text.setFont(new Font("Arial", Font.BOLD, 70));
|
||||||
|
|
||||||
// Bouton Play
|
// Création des boutons de difficulté
|
||||||
JButton play = new JButton("Play");
|
JButton easyButton = new JButton("Facile");
|
||||||
play.setAlignmentX(Component.CENTER_ALIGNMENT);
|
JButton mediumButton = new JButton("Moyen");
|
||||||
play.setPreferredSize(new Dimension(300, 100));
|
JButton hardButton = new JButton("Difficile");
|
||||||
play.setMaximumSize(new Dimension(300, 150));
|
|
||||||
play.setFont(new Font("Arial", Font.PLAIN, 36));
|
|
||||||
|
|
||||||
// Listener séparé pour gérer le clic sur Play
|
// Mise en forme des boutons
|
||||||
play.addActionListener(new PlayButtonListener(frame));
|
JButton[] buttons = { easyButton, mediumButton, hardButton };
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
// Ajouter les composants avec de l'espace
|
// Ajout d'écouteurs (listeners)
|
||||||
|
easyButton.addActionListener(new PlayButtonListener(frame, "facile"));
|
||||||
|
mediumButton.addActionListener(new PlayButtonListener(frame, "moyen"));
|
||||||
|
hardButton.addActionListener(new PlayButtonListener(frame, "difficile"));
|
||||||
|
|
||||||
|
// Ajout des composants à la fenêtre
|
||||||
frame.add(Box.createVerticalGlue());
|
frame.add(Box.createVerticalGlue());
|
||||||
frame.add(text);
|
frame.add(text);
|
||||||
frame.add(Box.createVerticalStrut(30));
|
frame.add(Box.createVerticalStrut(50));
|
||||||
frame.add(play);
|
frame.add(easyButton);
|
||||||
|
frame.add(Box.createVerticalStrut(20));
|
||||||
|
frame.add(mediumButton);
|
||||||
|
frame.add(Box.createVerticalStrut(20));
|
||||||
|
frame.add(hardButton);
|
||||||
frame.add(Box.createVerticalGlue());
|
frame.add(Box.createVerticalGlue());
|
||||||
|
|
||||||
// Affichage de la fenêtre
|
// Affichage de la fenêtre
|
||||||
frame.setVisible(true);
|
frame.setVisible(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import java.awt.event.ActionEvent;
|
|||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Classe PlayButtonListener qui gère le clic sur le bouton "Play" du menu.
|
* Classe PlayButtonListener qui gère le clic sur les boutons de difficulté.
|
||||||
* Lorsqu'on clique sur le bouton, cette classe ouvre la fenêtre du jeu
|
* Lorsqu'on clique sur un bouton, elle ouvre la fenêtre du jeu avec le niveau choisi
|
||||||
* et ferme la fenêtre principale.
|
* et ferme la fenêtre principale.
|
||||||
*/
|
*/
|
||||||
public class PlayButtonListener implements ActionListener {
|
public class PlayButtonListener implements ActionListener {
|
||||||
@@ -14,22 +14,29 @@ public class PlayButtonListener implements ActionListener {
|
|||||||
/** Référence à la fenêtre principale du menu */
|
/** Référence à la fenêtre principale du menu */
|
||||||
private JFrame parentFrame;
|
private JFrame parentFrame;
|
||||||
|
|
||||||
|
/** Niveau de difficulté choisi */
|
||||||
|
private String difficulty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructeur de PlayButtonListener.
|
* Constructeur de PlayButtonListener.
|
||||||
* @param frame La fenêtre principale à fermer après ouverture du jeu
|
* @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) {
|
public PlayButtonListener(JFrame frame, String difficulty) {
|
||||||
this.parentFrame = frame;
|
this.parentFrame = frame;
|
||||||
|
this.difficulty = difficulty;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Méthode appelée lorsque le bouton est cliqué.
|
* Méthode appelée lorsque le bouton est cliqué.
|
||||||
* Ouvre la fenêtre du jeu et ferme la fenêtre principale.
|
* 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
|
* @param e L'événement déclenché par le clic
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
new Action(); // Ouvre la fenêtre du jeu
|
// Passe la difficulté à la classe Action
|
||||||
parentFrame.dispose(); // Ferme la fenêtre principale
|
new Action(difficulty);
|
||||||
|
parentFrame.dispose(); // Ferme le menu principal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,38 +3,86 @@ package fr.iut.Projet;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe gérant la logique du jeu du pendu.
|
||||||
|
*
|
||||||
|
* Cette classe choisit un mot aléatoire selon la difficulté,
|
||||||
|
* garde en mémoire le mot secret, les lettres devinées,
|
||||||
|
* les lettres incorrectes, et le nombre de vies restantes.
|
||||||
|
*/
|
||||||
public class Random_word {
|
public class Random_word {
|
||||||
|
|
||||||
|
/** Mot secret que le joueur doit deviner */
|
||||||
private String secretWord;
|
private String secretWord;
|
||||||
|
|
||||||
|
/** Tableau représentant le mot caché avec les lettres découvertes */
|
||||||
private char[] hiddenWord;
|
private char[] hiddenWord;
|
||||||
|
|
||||||
|
/** Lettres correctement devinées par le joueur */
|
||||||
private Set<Character> lettersGuessed;
|
private Set<Character> lettersGuessed;
|
||||||
|
|
||||||
|
/** Lettres incorrectes devinées par le joueur */
|
||||||
private Set<Character> incorrectLetters;
|
private Set<Character> incorrectLetters;
|
||||||
|
|
||||||
|
/** Nombre de vies restantes */
|
||||||
private int lives;
|
private int lives;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructeur : sélectionne un mot aléatoire et initialise le jeu.
|
* Constructeur : sélectionne un mot aléatoire selon la difficulté
|
||||||
|
* et initialise les structures du jeu.
|
||||||
|
*
|
||||||
|
* @param difficulty Niveau de difficulté ("facile", "moyen", "difficile")
|
||||||
|
* @throws RuntimeException si aucun mot n'a pu être choisi
|
||||||
*/
|
*/
|
||||||
public Random_word() {
|
public Random_word(String difficulty) {
|
||||||
this.secretWord = getRandomWord();
|
this.secretWord = getRandomWord(difficulty);
|
||||||
|
|
||||||
if (this.secretWord == null) {
|
if (this.secretWord == null) {
|
||||||
throw new RuntimeException("Impossible de choisir un mot aléatoire !");
|
throw new RuntimeException("Impossible de choisir un mot aléatoire !");
|
||||||
}
|
}
|
||||||
this.lives = 8;
|
|
||||||
|
this.lives = 8; // nombre de vies par défaut
|
||||||
this.hiddenWord = new char[secretWord.length()];
|
this.hiddenWord = new char[secretWord.length()];
|
||||||
Arrays.fill(this.hiddenWord, '_');
|
Arrays.fill(this.hiddenWord, '_'); // initialise le mot caché avec des underscores
|
||||||
this.lettersGuessed = new HashSet<>();
|
this.lettersGuessed = new HashSet<>();
|
||||||
this.incorrectLetters = new HashSet<>();
|
this.incorrectLetters = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lit les mots dans "Word.txt" et retourne un mot aléatoire.
|
* 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() {
|
private String getRandomWord(String difficulty) {
|
||||||
InputStream is = Random_word.class.getResourceAsStream("Word.txt");
|
String fileName;
|
||||||
if (is == null) return null;
|
|
||||||
|
// 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;
|
String randomWord = null;
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
|
|
||||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
|
||||||
String line;
|
String line;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@@ -42,6 +90,7 @@ public class Random_word {
|
|||||||
line = line.trim();
|
line = line.trim();
|
||||||
if (!line.isEmpty()) {
|
if (!line.isEmpty()) {
|
||||||
count++;
|
count++;
|
||||||
|
// Choix aléatoire d'une ligne avec la méthode de Reservoir Sampling
|
||||||
if (random.nextInt(count) == 0) {
|
if (random.nextInt(count) == 0) {
|
||||||
randomWord = line;
|
randomWord = line;
|
||||||
}
|
}
|
||||||
@@ -50,61 +99,87 @@ public class Random_word {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return randomWord != null ? randomWord.toLowerCase() : null;
|
return randomWord != null ? randomWord.toLowerCase() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tente une lettre, met à jour l'état du jeu et retourne un message.
|
* Tente une lettre proposée par le joueur.
|
||||||
|
* Met à jour l'état du jeu (mot caché, lettres incorrectes, vies).
|
||||||
|
*
|
||||||
|
* @param letter lettre proposée
|
||||||
|
* @return message d'information pour le joueur
|
||||||
*/
|
*/
|
||||||
public String guessLetter(char letter) {
|
public String guessLetter(char letter) {
|
||||||
letter = Character.toLowerCase(letter);
|
letter = Character.toLowerCase(letter);
|
||||||
|
|
||||||
|
// Vérifie si la lettre a déjà été essayée
|
||||||
if (lettersGuessed.contains(letter) || incorrectLetters.contains(letter)) {
|
if (lettersGuessed.contains(letter) || incorrectLetters.contains(letter)) {
|
||||||
return "Vous avez déjà essayé cette lettre !";
|
return "Vous avez déjà essayé cette lettre !";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Si la lettre est dans le mot secret
|
||||||
if (secretWord.indexOf(letter) >= 0) {
|
if (secretWord.indexOf(letter) >= 0) {
|
||||||
lettersGuessed.add(letter);
|
lettersGuessed.add(letter);
|
||||||
|
|
||||||
|
// Remplace les underscores correspondants par la lettre devinée
|
||||||
for (int i = 0; i < secretWord.length(); i++) {
|
for (int i = 0; i < secretWord.length(); i++) {
|
||||||
if (secretWord.charAt(i) == letter) {
|
if (secretWord.charAt(i) == letter) {
|
||||||
hiddenWord[i] = letter;
|
hiddenWord[i] = letter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isWon()) {
|
if (isWon()) {
|
||||||
return "Félicitations ! Vous avez trouvé le mot : " + secretWord;
|
return "Félicitations ! Vous avez trouvé le mot : " + secretWord;
|
||||||
}
|
}
|
||||||
return "Bien joué !";
|
return "Bien joué !";
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
// Lettre incorrecte
|
||||||
incorrectLetters.add(letter);
|
incorrectLetters.add(letter);
|
||||||
lives--;
|
lives--;
|
||||||
|
|
||||||
if (lives <= 0) {
|
if (lives <= 0) {
|
||||||
return "Vous avez perdu ! Le mot était : " + secretWord;
|
return "Vous avez perdu ! Le mot était : " + secretWord;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Mauvaise lettre ! Il vous reste " + lives + " vies.";
|
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() {
|
public String getHiddenWord() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (char c : hiddenWord) sb.append(c).append(' ');
|
for (char c : hiddenWord) sb.append(c).append(' ');
|
||||||
return sb.toString().trim();
|
return sb.toString().trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return la liste des lettres incorrectes devinées */
|
||||||
public String getIncorrectLetters() {
|
public String getIncorrectLetters() {
|
||||||
return incorrectLetters.toString();
|
return incorrectLetters.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return nombre de vies restantes */
|
||||||
public int getLives() {
|
public int getLives() {
|
||||||
return lives;
|
return lives;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return true si le jeu est terminé (victoire ou défaite) */
|
||||||
public boolean isGameOver() {
|
public boolean isGameOver() {
|
||||||
return lives <= 0 || isWon();
|
return lives <= 0 || isWon();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return true si le mot est entièrement deviné */
|
||||||
public boolean isWon() {
|
public boolean isWon() {
|
||||||
return secretWord.equals(String.valueOf(hiddenWord));
|
for (char c : hiddenWord) {
|
||||||
|
if (c == '_') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return le mot secret */
|
||||||
public String getSecretWord() {
|
public String getSecretWord() {
|
||||||
return secretWord;
|
return secretWord;
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user