forked from menault/TD3_DEV51_Qualite_Algo
Exo 3 appliqué
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user