forked from menault/TD3_DEV51_Qualite_Algo
46 lines
1.5 KiB
Java
46 lines
1.5 KiB
Java
import java.io.*;
|
|
import java.util.*;
|
|
|
|
public class ChooseWord {
|
|
|
|
/* Fonction pour choisir un mot selon la difficulté */
|
|
public static String chooseTheWordByDifficulty(String difficulty) {
|
|
List<String> words = new ArrayList<>();
|
|
String difficultyDictio = "motsFacile.txt"; // par défaut
|
|
|
|
if (difficulty.equals("easy")) {
|
|
difficultyDictio = "motsFacile.txt";
|
|
} else if (difficulty.equals("medium")) {
|
|
difficultyDictio = "motsMoyen.txt";
|
|
} else if (difficulty.equals("hard")) {
|
|
difficultyDictio = "motsDifficiles.txt";
|
|
}
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(difficultyDictio))) {
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
String word = line.trim().toLowerCase();
|
|
if (word.isEmpty()) continue;
|
|
words.add(word);
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
if (words.isEmpty()) {
|
|
return "default";
|
|
}
|
|
|
|
Random rand = new Random();
|
|
|
|
if (difficulty.equals("hard")) {
|
|
// Pour "hard", on peut choisir deux mots concaténés
|
|
String first = words.get(rand.nextInt(words.size()));
|
|
String second = words.get(rand.nextInt(words.size()));
|
|
return first + " " + second;
|
|
} else {
|
|
return words.get(rand.nextInt(words.size()));
|
|
}
|
|
}
|
|
}
|