import java.io.*; import java.util.*; public class ChooseWord { /*Fonction pour choisir le mot selon la difficulté*/ public static String chooseTheWord(String difficulty) { String filename = getFilenameByDifficulty(difficulty); List words = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { String line; while ((line = reader.readLine()) != null) { if (!line.trim().isEmpty()) { String word = line.trim().toLowerCase(); // Filtre selon la difficulté if (isValidForDifficulty(word, difficulty)) { words.add(word); } } } } catch (IOException e) { e.printStackTrace(); } if (words.isEmpty()) { return "default"; } Random rand = new Random(); return words.get(rand.nextInt(words.size())); } private static String getFilenameByDifficulty(String difficulty) { switch (difficulty.toLowerCase()) { case "facile": return "motsFacile.txt"; case "moyen": return "motsMoyen.txt"; case "difficile": return "motsDifficiles.txt"; default: return "motsFacile.txt"; } } private static boolean isValidForDifficulty(String word, String difficulty) { switch (difficulty.toLowerCase()) { case "facile": return word.length() < 8 && !word.contains(" "); case "moyen": return word.length() >= 8 && !word.contains(" "); case "difficile": return word.contains(" "); // Phrases avec espaces default: return true; } } }