Files
TD3_DEV51_brigitte_aissi/PenduJudeChrist/WordSelector.java
2025-10-08 15:33:06 +02:00

41 lines
1.4 KiB
Java

import java.io.*;
import java.util.*;
public class WordSelector {
private static final Map<String, List<String>> wordsByDifficulty = new HashMap<>();
/**
* Charge la liste de mots pour une difficulté donnée depuis un fichier.
*/
public static void loadWordsForDifficulty(String difficulty, String filename) {
List<String> words = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String word = line.trim().toLowerCase();
if (!word.isEmpty()) {
words.add(word);
}
}
} catch (IOException e) {
System.err.println("Error loading words from " + filename);
e.printStackTrace();
}
wordsByDifficulty.put(difficulty.toLowerCase(), words);
}
/**
* Retourne un mot aléatoire selon la difficulté ("easy", "medium", "hard").
* Retourne "default" si aucun mot trouvé.
*/
public static String pickWord(String difficulty) {
List<String> words = wordsByDifficulty.get(difficulty.toLowerCase());
if (words == null || words.isEmpty()) {
return "default";
}
Random rand = new Random();
return words.get(rand.nextInt(words.size()));
}
}