Files
TD3_DEV51_Maxime_Marvin/main.java
2025-10-08 16:34:25 +02:00

177 lines
5.5 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import java.util.*;
public class main {
public String word; // mot courant
public Letter curentLetter; // gestion lettres
public int nbError = 0; // erreurs
public boolean winCondition = false;
// Stockage des mots par difficulté
public String easyWord;
public String mediumWord;
public String hardWord1;
public String hardWord2;
public main() {
// ---------------------------------------------------
// 1⃣ Récupération des mots aléatoires pour chaque difficulté
// ---------------------------------------------------
ArrayList<String> allWords = new ArrayList<>();
allWords.add(word_search.getRandomWord()); // exemple simple
allWords.add(word_search.getRandomWord());
allWords.add(word_search.getRandomWord());
allWords.add(word_search.getRandomWord());
// Trier par longueur pour facile/moyen
for (String w : allWords) {
if (w.length() < 8 && easyWord == null) easyWord = w;
else if (w.length() >= 8 && mediumWord == null) mediumWord = w;
else if (hardWord1 == null) hardWord1 = w;
else if (hardWord2 == null) hardWord2 = w;
}
// ---------------------------------------------------
// 2⃣ Choix de la difficulté
// ---------------------------------------------------
Scanner sc = new Scanner(System.in);
System.out.println("Choisis la difficulté : facile / moyen / difficile");
String choice = sc.nextLine().toLowerCase();
switch (choice) {
case "facile":
word = easyWord;
break;
case "moyen":
word = mediumWord;
break;
case "difficile":
// Ici on choisira les 2 mots pour faire deviner
word = hardWord1 + " " + hardWord2;
break;
default:
System.out.println("Difficulté invalide, on prend facile par défaut");
word = easyWord;
}
curentLetter = new Letter(word);
}
//creer wincondtion (je lis le mot en construction (wordbuilding), compare avec word, si égal c'est gagné)
public boolean winCondition(){
if(curentLetter.wordBuilding.toString().equals(word)){
return true;
}else{
return false;
}
}
public static void displayHangman(int mistakes) {
String[] hangman = new String[7]; // 7 étapes du pendu
// Étape 0 : début du jeu
hangman[0] =
"------\n" +
"| |\n" +
" |\n" +
" |\n" +
" |\n" +
" |\n" +
"=========\n";
// Étape 1 : tête
hangman[1] =
"------\n" +
"| |\n" +
"O |\n" +
" |\n" +
" |\n" +
" |\n" +
"=========\n";
// Étape 2 : tronc
hangman[2] =
"------\n" +
"| |\n" +
"O |\n" +
"| |\n" +
" |\n" +
" |\n" +
"=========\n";
// Étape 3 : un bras
hangman[3] =
"------\n" +
"| |\n" +
"O |\n" +
"/| |\n" +
" |\n" +
" |\n" +
"=========\n";
// Étape 4 : deux bras
hangman[4] =
"------\n" +
"| |\n" +
"O |\n" +
"/|\\ |\n" +
" |\n" +
" |\n" +
"=========\n";
// Étape 5 : une jambe
hangman[5] =
"------\n" +
"| |\n" +
"O |\n" +
"/|\\ |\n" +
"/ |\n" +
" |\n" +
"=========\n";
// Étape 6 : pendu complet (défaite)
hangman[6] =
"------\n" +
"| |\n" +
"O |\n" +
"/|\\ |\n" +
"/ \\ |\n" +
" |\n" +
"=========\n";
// Affiche le pendu correspondant au nombre derreurs
System.out.println(hangman[mistakes]);
}
//Element toujours présent
public void showGame(){
System.out.println("lettre fausse : " + this.curentLetter.incorrectLetters);
displayHangman(nbError);
System.out.println("mot a trouver " + this.curentLetter.wordBuilding);
}
//Ligne 100
//Permet au jeu de fonctionner et de se terminer
public void game(){
showGame();
while(nbError<6 && !winCondition()){
char letter = enter_word.getLetter();
this.curentLetter.setLetter(letter);
if (!this.curentLetter.letterInWord()){
nbError++;
}
showGame();
}
if (winCondition()){
// tu gagnes
System.out.println("victoir (celui qui a écris a 5 de QI)");
}
else{ //tu perds
System.out.println("ta perdu sale étron");
System.out.println(" le bon mot était : " + this.word);
}
}
public static void main(String[] args) {
main test =new main();
test.game();
}
}