Files
TD3_DEV51_Qualite_Algo/src/GestionMotsPendu.java

52 lines
1.8 KiB
Java
Raw Normal View History

import java.io.*;
import java.util.*;
public class GestionMotsPendu {
private static final String CHEMIN_FICHIER = "../res/mots_pendu.txt";
private final Map<String, List<String>> 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<String> 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
};
}
}