This commit is contained in:
2025-10-08 12:28:34 +02:00
parent 86537ff528
commit c14e481572
9 changed files with 40 additions and 85 deletions

Binary file not shown.

View File

@@ -3,25 +3,16 @@ import java.util.*;
public class ChooseWord {
/* Fonction pour choisir un mot selon la difficulté */
public static String chooseTheWordByDifficulty(String difficulty) {
/*Fonction pour choisir le mot aléatoirement*/
public static String chooseTheWord() {
List<String> words = new ArrayList<>();
String difficultyDictio = "motsFacile.txt"; // par défaut
if (difficulty.equals("easy")) {
difficultyDictio = "motsFacile.txt";
} else if (difficulty.equals("medium")) {
difficultyDictio = "motsMoyen.txt";
} else if (difficulty.equals("hard")) {
difficultyDictio = "motsDifficiles.txt";
}
try (BufferedReader reader = new BufferedReader(new FileReader(difficultyDictio))) {
try (BufferedReader reader = new BufferedReader(new FileReader("motsFacile.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String word = line.trim().toLowerCase();
if (word.isEmpty()) continue;
words.add(word);
if (!line.trim().isEmpty()) {
words.add(line.trim().toLowerCase());
}
}
} catch (IOException e) {
e.printStackTrace();
@@ -32,14 +23,6 @@ public class ChooseWord {
}
Random rand = new Random();
if (difficulty.equals("hard")) {
// Pour "hard", on peut choisir deux mots concaténés
String first = words.get(rand.nextInt(words.size()));
String second = words.get(rand.nextInt(words.size()));
return first + " " + second;
} else {
return words.get(rand.nextInt(words.size()));
}
}
}

Binary file not shown.

View File

@@ -8,24 +8,15 @@ public class GameState {
private Set<Character> triedLetters;
private static final int MAX_ERRORS = 9;
/* Constructeur : initialise le mot à deviner */
public GameState(String wordToGuess) {
this.word = wordToGuess.toLowerCase();
this.hiddenWord = new char[word.length()];
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == ' ') {
hiddenWord[i] = ' '; // espaces visibles dans les mots difficiles
} else {
hiddenWord[i] = '_';
}
}
Arrays.fill(hiddenWord, '_');
this.triedLetters = new HashSet<>();
this.errors = 0;
}
/* Tente une lettre dans le mot */
/*Fonction pour essayer une lettre*/
public void tryLetter(char letter) {
letter = Character.toLowerCase(letter);
triedLetters.add(letter);
@@ -43,13 +34,13 @@ public class GameState {
}
}
/* Vérifie si une lettre a déjà été essayée */
/*Fonction pour vérifier si une lettre à déjà été essayé*/
public boolean hasTriedLetter(char letter) {
letter = Character.toLowerCase(letter);
return triedLetters.contains(letter);
}
/* Vérifie si le joueur a gagné */
/*Fonction pour vérifier si on a gagné*/
public boolean isWon() {
for (char c : hiddenWord) {
if (c == '_') {
@@ -59,17 +50,17 @@ public class GameState {
return true;
}
/* Vérifie si le joueur a perdu */
/*Fonction pour vérifier si on a perdu*/
public boolean isLost() {
return errors >= MAX_ERRORS;
}
/* Retourne le nombre d'erreurs */
/*Fonction pour voir le nombre d'erreur*/
public int getErrors() {
return errors;
}
/* Retourne le mot caché avec espaces entre lettres */
/*Fonction pour voir le mot caché*/
public String getHiddenWord() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hiddenWord.length; i++) {
@@ -81,7 +72,7 @@ public class GameState {
return sb.toString();
}
/* Retourne le mot original */
/*Fonction pour voir le mot*/
public String getWord() {
return word;
}

Binary file not shown.

View File

@@ -5,30 +5,29 @@ public class HangmanPanel extends JPanel {
private int errors = 0;
/* Met à jour le nombre d'erreurs et redessine */
/*mettre à jour les erreurs*/
public void setErrors(int errors) {
this.errors = errors;
repaint();
}
/* Dessine le pendu en fonction des erreurs */
/*Dessiner le pendu*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Structure du pendu
g.drawLine(50, 300, 200, 300);
g.drawLine(125, 300, 125, 50);
g.drawLine(125, 50, 250, 50);
g.drawLine(250, 50, 250, 80);
if (errors > 0) g.drawOval(230, 80, 40, 40); // tête
if (errors > 1) g.drawLine(250, 120, 250, 200); // corps
if (errors > 2) g.drawLine(250, 140, 220, 170); // bras gauche
if (errors > 3) g.drawLine(250, 140, 280, 170); // bras droit
if (errors > 4) g.drawLine(250, 200, 220, 250); // jambe gauche
if (errors > 5) g.drawLine(250, 200, 280, 250); // jambe droite
if (errors > 6) g.drawLine(230, 90, 270, 90); // yeux barres
if (errors > 7) g.drawString("X", 240, 100); // oeil gauche
if (errors > 8) g.drawString("X", 255, 100); // oeil droit
if (errors > 0) g.drawOval(230, 80, 40, 40);
if (errors > 1) g.drawLine(250, 120, 250, 200);
if (errors > 2) g.drawLine(250, 140, 220, 170);
if (errors > 3) g.drawLine(250, 140, 280, 170);
if (errors > 4) g.drawLine(250, 200, 220, 250);
if (errors > 5) g.drawLine(250, 200, 280, 250);
if (errors > 6) g.drawLine(230, 90, 270, 90);
if (errors > 7) g.drawString("X", 240, 100);
if (errors > 8) g.drawString("X", 255, 100);
}
}

Binary file not shown.

Binary file not shown.

View File

@@ -2,6 +2,7 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class main {
public static GameState gameState;
@@ -10,36 +11,17 @@ public class main {
public static JTextField inputField;
public static JLabel messageLabel;
public static long startTime;
public static long endTime;
public static int score;
/* Fonction main */
/*Fonction main*/
public static void main(String[] args) {
String difficulty = chooseDifficulty();
String selectedWord = ChooseWord.chooseTheWordByDifficulty(difficulty);
String selectedWord = ChooseWord.chooseTheWord();
gameState = new GameState(selectedWord);
startTime = System.currentTimeMillis();
createInterface();
}
/* Fonction pour choisir la difficulté */
public static String chooseDifficulty() {
String[] options = {"Easy", "Medium", "Hard"};
int choice = JOptionPane.showOptionDialog(null, "Choose difficulty level:",
"Difficulty Selection", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);
if (choice == 0) return "easy";
else if (choice == 1) return "medium";
else if (choice == 2) return "hard";
else return "easy"; // défaut
}
/* Fonction pour créer l'interface */
/*Fonction pour créer l'interface*/
public static void createInterface() {
JFrame window = new JFrame("Hangman Game");
JFrame window = new JFrame("HangmanGame");
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
@@ -62,6 +44,7 @@ public class main {
window.add(inputPanel, BorderLayout.SOUTH);
/*evenement du bouton*/
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleLetterInput();
@@ -71,8 +54,12 @@ public class main {
window.setVisible(true);
}
/* Fonction pour gérer la saisie d'une lettre */
/*Fonction pour mettre à jour le pendu*/
public static void handleLetterInput() {
System.out.println(gameState.getWord());
System.out.println(gameState.getWord().length());
String input = inputField.getText().toLowerCase();
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
messageLabel.setText("Enter a single valid letter.");
@@ -92,12 +79,7 @@ public class main {
hangmanPanel.setErrors(gameState.getErrors());
if (gameState.isWon()) {
endTime = System.currentTimeMillis();
long elapsedSeconds = (endTime - startTime) / 1000;
score = 1000 - (gameState.getErrors() * 50) - ((int) elapsedSeconds * 10);
if (score < 0) score = 0;
messageLabel.setText("Congrats! You won! Score: " + score + " Time: " + elapsedSeconds + "s");
messageLabel.setText("Congratulations! You've won!");
inputField.setEditable(false);
} else if (gameState.isLost()) {
messageLabel.setText("You lost! Word was: " + gameState.getWord());