forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
2 Commits
d074170b0f
...
baab5c7ce2
| Author | SHA1 | Date | |
|---|---|---|---|
| baab5c7ce2 | |||
| 90645b7673 |
554
res/mots_pendu.txt
Normal file
554
res/mots_pendu.txt
Normal file
File diff suppressed because it is too large
Load Diff
BIN
src/GestionMotsPendu.class
Normal file
BIN
src/GestionMotsPendu.class
Normal file
Binary file not shown.
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
src/TestPendu.class
Normal file
BIN
src/TestPendu.class
Normal file
Binary file not shown.
65
src/TestPendu.java
Normal file
65
src/TestPendu.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
import java.io.IOException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class TestPendu {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
GestionMotsPendu gestion = new GestionMotsPendu();
|
||||||
|
|
||||||
|
System.out.println("Bienvenue dans le jeu du pendu !");
|
||||||
|
System.out.println("Choisissez la difficulté : 1 = Facile, 2 = Moyen, 3 = Difficile");
|
||||||
|
int difficulte = scanner.nextInt();
|
||||||
|
scanner.nextLine(); // consommer la fin de ligne
|
||||||
|
|
||||||
|
String mot = gestion.getMotAleatoire(difficulte);
|
||||||
|
if (mot == null) {
|
||||||
|
System.out.println("Erreur : aucun mot disponible pour cette difficulté.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char[] motCache = new char[mot.length()];
|
||||||
|
Arrays.fill(motCache, '_');
|
||||||
|
|
||||||
|
Set<Character> lettresDevinees = new HashSet<>();
|
||||||
|
int erreurs = 0;
|
||||||
|
int maxErreurs = 6;
|
||||||
|
|
||||||
|
while (erreurs < maxErreurs && new String(motCache).contains("_")) {
|
||||||
|
System.out.println("\nMot : " + new String(motCache));
|
||||||
|
System.out.println("Erreurs : " + erreurs + "/" + maxErreurs);
|
||||||
|
System.out.print("Devinez une lettre : ");
|
||||||
|
String input = scanner.nextLine().toLowerCase();
|
||||||
|
if (input.isEmpty()) continue;
|
||||||
|
|
||||||
|
char lettre = input.charAt(0);
|
||||||
|
if (lettresDevinees.contains(lettre)) {
|
||||||
|
System.out.println("Vous avez déjà essayé cette lettre !");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
lettresDevinees.add(lettre);
|
||||||
|
if (mot.indexOf(lettre) >= 0) {
|
||||||
|
for (int i = 0; i < mot.length(); i++) {
|
||||||
|
if (mot.charAt(i) == lettre) motCache[i] = lettre;
|
||||||
|
}
|
||||||
|
System.out.println("Bien joué !");
|
||||||
|
} else {
|
||||||
|
erreurs++;
|
||||||
|
System.out.println("Raté !");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (new String(motCache).equals(mot)) {
|
||||||
|
System.out.println("\nFélicitations ! Vous avez trouvé le mot : " + mot);
|
||||||
|
} else {
|
||||||
|
System.out.println("\nVous avez perdu ! Le mot était : " + mot);
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Erreur lors du chargement des mots : " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user