diff --git a/src/GestionMotsPendu.class b/src/GestionMotsPendu.class new file mode 100644 index 0000000..008f719 Binary files /dev/null and b/src/GestionMotsPendu.class differ diff --git a/src/GestionMotsPendu.java b/src/GestionMotsPendu.java new file mode 100644 index 0000000..d519d74 --- /dev/null +++ b/src/GestionMotsPendu.java @@ -0,0 +1,51 @@ +import java.io.*; +import java.util.*; + +public class GestionMotsPendu { + + private static final String CHEMIN_FICHIER = "../res/mots_pendu.txt"; + + private final Map> motsParDifficulte = new HashMap<>(); + + public GestionMotsPendu() throws IOException { + motsParDifficulte.put("FACILE", new ArrayList<>()); + motsParDifficulte.put("MOYEN", new ArrayList<>()); + motsParDifficulte.put("DIFFICILE", new ArrayList<>()); + chargerMots(); + } + + private void chargerMots() throws IOException { + try (BufferedReader reader = new BufferedReader(new FileReader(CHEMIN_FICHIER))) { + String ligne; + String section = ""; + while ((ligne = reader.readLine()) != null) { + ligne = ligne.trim(); + if (ligne.startsWith("#")) { + section = ligne.substring(1).toUpperCase(); + } else if (!ligne.isEmpty() && motsParDifficulte.containsKey(section)) { + motsParDifficulte.get(section).add(ligne); + } + } + } catch (FileNotFoundException e) { + throw new IOException("Fichier introuvable : " + CHEMIN_FICHIER); + } + } + + public String getMotAleatoire(int difficulte) { + String cle = convertirDifficulte(difficulte); + List liste = motsParDifficulte.get(cle); + if (liste == null || liste.isEmpty()) return null; + + Random rand = new Random(); + return liste.get(rand.nextInt(liste.size())); + } + + private String convertirDifficulte(int difficulte) { + return switch (difficulte) { + case 1 -> "FACILE"; + case 2 -> "MOYEN"; + case 3 -> "DIFFICILE"; + default -> "FACILE"; // Valeur par défaut + }; + } +} diff --git a/src/TestPendu.class b/src/TestPendu.class new file mode 100644 index 0000000..321ce8e Binary files /dev/null and b/src/TestPendu.class differ diff --git a/src/TestPendu.java b/src/TestPendu.java new file mode 100644 index 0000000..a6d86a6 --- /dev/null +++ b/src/TestPendu.java @@ -0,0 +1,65 @@ +import java.io.IOException; +import java.util.*; + +public class TestPendu { + public static void main(String[] args) { + try { + Scanner scanner = new Scanner(System.in); + GestionMotsPendu gestion = new GestionMotsPendu(); + + System.out.println("Bienvenue dans le jeu du pendu !"); + System.out.println("Choisissez la difficulté : 1 = Facile, 2 = Moyen, 3 = Difficile"); + int difficulte = scanner.nextInt(); + scanner.nextLine(); // consommer la fin de ligne + + String mot = gestion.getMotAleatoire(difficulte); + if (mot == null) { + System.out.println("Erreur : aucun mot disponible pour cette difficulté."); + return; + } + + char[] motCache = new char[mot.length()]; + Arrays.fill(motCache, '_'); + + Set lettresDevinees = new HashSet<>(); + int erreurs = 0; + int maxErreurs = 6; + + while (erreurs < maxErreurs && new String(motCache).contains("_")) { + System.out.println("\nMot : " + new String(motCache)); + System.out.println("Erreurs : " + erreurs + "/" + maxErreurs); + System.out.print("Devinez une lettre : "); + String input = scanner.nextLine().toLowerCase(); + if (input.isEmpty()) continue; + + char lettre = input.charAt(0); + if (lettresDevinees.contains(lettre)) { + System.out.println("Vous avez déjà essayé cette lettre !"); + continue; + } + + lettresDevinees.add(lettre); + if (mot.indexOf(lettre) >= 0) { + for (int i = 0; i < mot.length(); i++) { + if (mot.charAt(i) == lettre) motCache[i] = lettre; + } + System.out.println("Bien joué !"); + } else { + erreurs++; + System.out.println("Raté !"); + } + } + + if (new String(motCache).equals(mot)) { + System.out.println("\nFélicitations ! Vous avez trouvé le mot : " + mot); + } else { + System.out.println("\nVous avez perdu ! Le mot était : " + mot); + } + + scanner.close(); + + } catch (IOException e) { + System.err.println("Erreur lors du chargement des mots : " + e.getMessage()); + } + } +}