比較提交

...

2 次程式碼提交

共有 5 個檔案被更改,包括 670 行新增0 行删除

554
res/mots_pendu.txt 一般檔案

檔案差異因為檔案過大而無法顯示 載入差異

二進制
src/GestionMotsPendu.class 一般檔案

未顯示二進位檔案。

51
src/GestionMotsPendu.java 一般檔案
查看文件

@@ -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
};
}
}

二進制
src/TestPendu.class 一般檔案

未顯示二進位檔案。

65
src/TestPendu.java 一般檔案
查看文件

@@ -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());
}
}
}