feat: jeu mvp fonctionnel avec difficultés et compteur erreurs
This commit is contained in:
51
src/GestionMotsPendu.java
Normal file
51
src/GestionMotsPendu.java
Normal file
@@ -0,0 +1,51 @@
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class GestionMotsPendu {
|
||||
|
||||
private static final String CHEMIN_FICHIER = "../res/mots_pendu.txt";
|
||||
|
||||
private final Map<String, List<String>> motsParDifficulte = new HashMap<>();
|
||||
|
||||
public GestionMotsPendu() throws IOException {
|
||||
motsParDifficulte.put("FACILE", new ArrayList<>());
|
||||
motsParDifficulte.put("MOYEN", new ArrayList<>());
|
||||
motsParDifficulte.put("DIFFICILE", new ArrayList<>());
|
||||
chargerMots();
|
||||
}
|
||||
|
||||
private void chargerMots() throws IOException {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(CHEMIN_FICHIER))) {
|
||||
String ligne;
|
||||
String section = "";
|
||||
while ((ligne = reader.readLine()) != null) {
|
||||
ligne = ligne.trim();
|
||||
if (ligne.startsWith("#")) {
|
||||
section = ligne.substring(1).toUpperCase();
|
||||
} else if (!ligne.isEmpty() && motsParDifficulte.containsKey(section)) {
|
||||
motsParDifficulte.get(section).add(ligne);
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new IOException("Fichier introuvable : " + CHEMIN_FICHIER);
|
||||
}
|
||||
}
|
||||
|
||||
public String getMotAleatoire(int difficulte) {
|
||||
String cle = convertirDifficulte(difficulte);
|
||||
List<String> liste = motsParDifficulte.get(cle);
|
||||
if (liste == null || liste.isEmpty()) return null;
|
||||
|
||||
Random rand = new Random();
|
||||
return liste.get(rand.nextInt(liste.size()));
|
||||
}
|
||||
|
||||
private String convertirDifficulte(int difficulte) {
|
||||
return switch (difficulte) {
|
||||
case 1 -> "FACILE";
|
||||
case 2 -> "MOYEN";
|
||||
case 3 -> "DIFFICILE";
|
||||
default -> "FACILE"; // Valeur par défaut
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user