Exo 3 appliqué

This commit is contained in:
Aissi Jude Christ
2025-10-08 14:57:57 +02:00
parent 9869c9b289
commit 7270ba1a20
20 changed files with 216 additions and 36 deletions

View File

@@ -3,15 +3,20 @@ import java.util.*;
public class ChooseWord {
/*Fonction pour choisir le mot aléatoirement*/
public static String chooseTheWord() {
/*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("motsFacile.txt"))) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.trim().isEmpty()) {
words.add(line.trim().toLowerCase());
String word = line.trim().toLowerCase();
// Filtre selon la difficulté
if (isValidForDifficulty(word, difficulty)) {
words.add(word);
}
}
}
} catch (IOException e) {
@@ -25,4 +30,26 @@ public class ChooseWord {
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;
}
}
}