Files
Aissi Jude Christ 7270ba1a20 Exo 3 appliqué
2025-10-08 14:57:57 +02:00

55 lines
1.8 KiB
Java

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<String> 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;
}
}
}