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 { public class ChooseWord {
/* Fonction pour choisir un mot selon la difficulté */ /*Fonction pour choisir le mot aléatoirement*/
public static String chooseTheWordByDifficulty(String difficulty) { public static String chooseTheWord() {
List<String> words = new ArrayList<>(); List<String> words = new ArrayList<>();
String difficultyDictio = "motsFacile.txt"; // par défaut
if (difficulty.equals("easy")) { try (BufferedReader reader = new BufferedReader(new FileReader("motsFacile.txt"))) {
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))) {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
String word = line.trim().toLowerCase(); if (!line.trim().isEmpty()) {
if (word.isEmpty()) continue; words.add(line.trim().toLowerCase());
words.add(word); }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@@ -32,14 +23,6 @@ public class ChooseWord {
} }
Random rand = new Random(); Random rand = new Random();
return words.get(rand.nextInt(words.size()));
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,26 +8,17 @@ public class GameState {
private Set<Character> triedLetters; private Set<Character> triedLetters;
private static final int MAX_ERRORS = 9; private static final int MAX_ERRORS = 9;
/* Constructeur : initialise le mot à deviner */
public GameState(String wordToGuess) { public GameState(String wordToGuess) {
this.word = wordToGuess.toLowerCase(); this.word = wordToGuess.toLowerCase();
this.hiddenWord = new char[word.length()]; this.hiddenWord = new char[word.length()];
Arrays.fill(hiddenWord, '_');
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == ' ') {
hiddenWord[i] = ' '; // espaces visibles dans les mots difficiles
} else {
hiddenWord[i] = '_';
}
}
this.triedLetters = new HashSet<>(); this.triedLetters = new HashSet<>();
this.errors = 0; this.errors = 0;
} }
/* Tente une lettre dans le mot */ /*Fonction pour essayer une lettre*/
public void tryLetter(char letter) { public void tryLetter(char letter) {
letter = Character.toLowerCase(letter); letter = Character.toLowerCase(letter);
triedLetters.add(letter); triedLetters.add(letter);
boolean found = false; boolean found = false;
@@ -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) { public boolean hasTriedLetter(char letter) {
letter = Character.toLowerCase(letter); letter = Character.toLowerCase(letter);
return triedLetters.contains(letter); return triedLetters.contains(letter);
} }
/* Vérifie si le joueur a gagné */ /*Fonction pour vérifier si on a gagné*/
public boolean isWon() { public boolean isWon() {
for (char c : hiddenWord) { for (char c : hiddenWord) {
if (c == '_') { if (c == '_') {
@@ -59,17 +50,17 @@ public class GameState {
return true; return true;
} }
/* Vérifie si le joueur a perdu */ /*Fonction pour vérifier si on a perdu*/
public boolean isLost() { public boolean isLost() {
return errors >= MAX_ERRORS; return errors >= MAX_ERRORS;
} }
/* Retourne le nombre d'erreurs */ /*Fonction pour voir le nombre d'erreur*/
public int getErrors() { public int getErrors() {
return errors; return errors;
} }
/* Retourne le mot caché avec espaces entre lettres */ /*Fonction pour voir le mot caché*/
public String getHiddenWord() { public String getHiddenWord() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < hiddenWord.length; i++) { for (int i = 0; i < hiddenWord.length; i++) {
@@ -81,7 +72,7 @@ public class GameState {
return sb.toString(); return sb.toString();
} }
/* Retourne le mot original */ /*Fonction pour voir le mot*/
public String getWord() { public String getWord() {
return word; return word;
} }

Binary file not shown.

View File

@@ -5,30 +5,29 @@ public class HangmanPanel extends JPanel {
private int errors = 0; private int errors = 0;
/* Met à jour le nombre d'erreurs et redessine */ /*mettre à jour les erreurs*/
public void setErrors(int errors) { public void setErrors(int errors) {
this.errors = errors; this.errors = errors;
repaint(); repaint();
} }
/* Dessine le pendu en fonction des erreurs */ /*Dessiner le pendu*/
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
// Structure du pendu
g.drawLine(50, 300, 200, 300); g.drawLine(50, 300, 200, 300);
g.drawLine(125, 300, 125, 50); g.drawLine(125, 300, 125, 50);
g.drawLine(125, 50, 250, 50); g.drawLine(125, 50, 250, 50);
g.drawLine(250, 50, 250, 80); g.drawLine(250, 50, 250, 80);
if (errors > 0) g.drawOval(230, 80, 40, 40); // tête if (errors > 0) g.drawOval(230, 80, 40, 40);
if (errors > 1) g.drawLine(250, 120, 250, 200); // corps if (errors > 1) g.drawLine(250, 120, 250, 200);
if (errors > 2) g.drawLine(250, 140, 220, 170); // bras gauche if (errors > 2) g.drawLine(250, 140, 220, 170);
if (errors > 3) g.drawLine(250, 140, 280, 170); // bras droit if (errors > 3) g.drawLine(250, 140, 280, 170);
if (errors > 4) g.drawLine(250, 200, 220, 250); // jambe gauche if (errors > 4) g.drawLine(250, 200, 220, 250);
if (errors > 5) g.drawLine(250, 200, 280, 250); // jambe droite if (errors > 5) g.drawLine(250, 200, 280, 250);
if (errors > 6) g.drawLine(230, 90, 270, 90); // yeux barres if (errors > 6) g.drawLine(230, 90, 270, 90);
if (errors > 7) g.drawString("X", 240, 100); // oeil gauche if (errors > 7) g.drawString("X", 240, 100);
if (errors > 8) g.drawString("X", 255, 100); // oeil droit 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.*;
import java.awt.event.*; import java.awt.event.*;
public class main { public class main {
public static GameState gameState; public static GameState gameState;
@@ -10,36 +11,17 @@ public class main {
public static JTextField inputField; public static JTextField inputField;
public static JLabel messageLabel; public static JLabel messageLabel;
public static long startTime; /*Fonction main*/
public static long endTime;
public static int score;
/* Fonction main */
public static void main(String[] args) { public static void main(String[] args) {
String difficulty = chooseDifficulty(); String selectedWord = ChooseWord.chooseTheWord();
String selectedWord = ChooseWord.chooseTheWordByDifficulty(difficulty);
gameState = new GameState(selectedWord); gameState = new GameState(selectedWord);
startTime = System.currentTimeMillis();
createInterface(); createInterface();
} }
/* Fonction pour choisir la difficulté */ /*Fonction pour créer l'interface*/
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 */
public static void createInterface() { public static void createInterface() {
JFrame window = new JFrame("Hangman Game"); JFrame window = new JFrame("HangmanGame");
window.setSize(800, 600); window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout()); window.setLayout(new BorderLayout());
@@ -62,6 +44,7 @@ public class main {
window.add(inputPanel, BorderLayout.SOUTH); window.add(inputPanel, BorderLayout.SOUTH);
/*evenement du bouton*/
submitButton.addActionListener(new ActionListener() { submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
handleLetterInput(); handleLetterInput();
@@ -71,8 +54,12 @@ public class main {
window.setVisible(true); window.setVisible(true);
} }
/* Fonction pour gérer la saisie d'une lettre */ /*Fonction pour mettre à jour le pendu*/
public static void handleLetterInput() { public static void handleLetterInput() {
System.out.println(gameState.getWord());
System.out.println(gameState.getWord().length());
String input = inputField.getText().toLowerCase(); String input = inputField.getText().toLowerCase();
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) { if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
messageLabel.setText("Enter a single valid letter."); messageLabel.setText("Enter a single valid letter.");
@@ -92,12 +79,7 @@ public class main {
hangmanPanel.setErrors(gameState.getErrors()); hangmanPanel.setErrors(gameState.getErrors());
if (gameState.isWon()) { if (gameState.isWon()) {
endTime = System.currentTimeMillis(); messageLabel.setText("Congratulations! You've won!");
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");
inputField.setEditable(false); inputField.setEditable(false);
} else if (gameState.isLost()) { } else if (gameState.isLost()) {
messageLabel.setText("You lost! Word was: " + gameState.getWord()); messageLabel.setText("You lost! Word was: " + gameState.getWord());