forked from menault/TD3_DEV51_Qualite_Algo
mot trouvé dans le terminal
This commit is contained in:
27
Makefile
Normal file
27
Makefile
Normal file
@@ -0,0 +1,27 @@
|
||||
# === Configuration ===
|
||||
SRC_DIR = src
|
||||
OUT_DIR = out
|
||||
PACKAGE = fr/iut/Projet
|
||||
MAIN_CLASS = fr.iut.Projet.Random_word
|
||||
|
||||
# === Règle principale ===
|
||||
all: compile run
|
||||
|
||||
# === Compilation ===
|
||||
compile:
|
||||
@echo "Compilation du projet..."
|
||||
@mkdir -p $(OUT_DIR)
|
||||
@javac -d $(OUT_DIR) $(SRC_DIR)/$(PACKAGE)/Random_word.java
|
||||
@cp $(SRC_DIR)/$(PACKAGE)/Word.txt $(OUT_DIR)/$(PACKAGE)/
|
||||
@echo "Compilation terminée."
|
||||
|
||||
# === Exécution ===
|
||||
run:
|
||||
@echo "Exécution du programme..."
|
||||
@java -cp $(OUT_DIR) $(MAIN_CLASS)
|
||||
|
||||
# === Nettoyage ===
|
||||
clean:
|
||||
@echo "Suppression des fichiers compilés..."
|
||||
@rm -rf $(OUT_DIR)
|
||||
@echo "Nettoyage terminé."
|
||||
BIN
out/fr/iut/Projet/Random_word.class
Normal file
BIN
out/fr/iut/Projet/Random_word.class
Normal file
Binary file not shown.
835
out/fr/iut/Projet/Word.txt
Normal file
835
out/fr/iut/Projet/Word.txt
Normal file
File diff suppressed because it is too large
Load Diff
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