forked from menault/TD3_DEV51_Qualite_Algo
mot trouvé dans le terminal
This commit is contained in:
105
src/fr/iut/Projet/Random_word.java
Normal file
105
src/fr/iut/Projet/Random_word.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package fr.iut.Projet;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Random_word {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String motAleatoire = getRandomWord();
|
||||
|
||||
if (motAleatoire == null) {
|
||||
System.err.println("Impossible de choisir un mot aléatoire !");
|
||||
return;
|
||||
}
|
||||
|
||||
jouer(motAleatoire);
|
||||
}
|
||||
|
||||
public static String getRandomWord() {
|
||||
InputStream is = Random_word.class.getResourceAsStream("Word.txt");
|
||||
|
||||
if (is == null) {
|
||||
System.err.println("File 'Word.txt' not found in the package !");
|
||||
return null;
|
||||
}
|
||||
|
||||
String randomWord = null;
|
||||
Random random = new Random();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
|
||||
String line;
|
||||
int count = 0;
|
||||
while ((line = br.readLine()) != null) {
|
||||
line = line.trim();
|
||||
if (!line.isEmpty()) {
|
||||
count++;
|
||||
if (random.nextInt(count) == 0) {
|
||||
randomWord = line;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("Erreur lors de la lecture du fichier : " + e.getMessage());
|
||||
}
|
||||
|
||||
return randomWord != null ? randomWord.toLowerCase() : null;
|
||||
}
|
||||
|
||||
public static void jouer(String Secretword) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
Set<Character> letterGuessed = new HashSet<>();
|
||||
Set<Character> incorrectletters = new HashSet<>();
|
||||
int lives = 8; // nombre d'essais
|
||||
|
||||
char[] Hiddenword = new char[Secretword.length()];
|
||||
Arrays.fill(Hiddenword, '_');
|
||||
|
||||
System.out.println("Bienvenue dans le jeu du mot mystère !");
|
||||
System.out.println("Le mot contient " + Secretword.length() + " lettre.");
|
||||
|
||||
while (lives > 0) {
|
||||
System.out.print("\nMot actuel : ");
|
||||
for (char c : Hiddenword) {
|
||||
System.out.print(c + " ");
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println("lettre incorrectes : " + incorrectletters);
|
||||
System.out.print("Entrez une lettre : ");
|
||||
String input = scanner.nextLine().toLowerCase();
|
||||
|
||||
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
|
||||
System.out.println("Entrez une seule lettre !");
|
||||
continue;
|
||||
}
|
||||
|
||||
char letter = input.charAt(0);
|
||||
|
||||
if (letterGuessed.contains(letter) || incorrectletters.contains(letter)) {
|
||||
System.out.println("Vous avez déjà essayé cette letter !");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Secretword.indexOf(letter) >= 0) {
|
||||
letterGuessed.add(letter);
|
||||
for (int i = 0; i < Secretword.length(); i++) {
|
||||
if (Secretword.charAt(i) == letter) {
|
||||
Hiddenword[i] = letter;
|
||||
}
|
||||
}
|
||||
System.out.println("Bien joué !");
|
||||
} else {
|
||||
incorrectletters.add(letter);
|
||||
lives--;
|
||||
System.out.println("Mauvaise lettre ! Il vous reste " + lives + " vies.");
|
||||
}
|
||||
|
||||
if (String.valueOf(Hiddenword).equals(Secretword)) {
|
||||
System.out.println("\nFélicitations ! Vous avez trouvé le mot : " + Secretword);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\nVous avez perdu ! Le mot était : " + Secretword);
|
||||
}
|
||||
}
|
||||
835
src/fr/iut/Projet/Word.txt
Normal file
835
src/fr/iut/Projet/Word.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user