diff --git a/Letter.java b/Letter.java new file mode 100644 index 0000000..cdd8e28 --- /dev/null +++ b/Letter.java @@ -0,0 +1,50 @@ +import java.util.*; + +public class Letter { + public String wordToFind; + public char selectedLetter; + public StringBuilder wordBuilding; + public StringBuilder incorrectLetters = new StringBuilder(); + + // Constructeur + public Letter(String mot) { + this.wordToFind = mot; + this.wordBuilding = new StringBuilder(); + // Initialise le mot caché avec des underscores + for (int i = 0; i < mot.length(); i++) { + if (mot.charAt(i) == ' ') { + this.wordBuilding.append(' '); // garde les espaces + } else { + this.wordBuilding.append('_'); + } + } + } + + // Setter pour la lettre sélectionnée + public void setLetter(char currentLetter) { + this.selectedLetter = currentLetter; + } + + // Vérifie si la lettre est dans le mot + public boolean letterInWord() { + boolean found = false; + + for (int i = 0; i < this.wordToFind.length(); i++) { + if (this.selectedLetter == this.wordToFind.charAt(i)) { + // Remplace l'underscore par la lettre correcte + this.wordBuilding.setCharAt(i, this.selectedLetter); + found = true; + } + } + + // Si la lettre n'est pas trouvée, on l'ajoute aux incorrectes + if (!found) { + if (this.incorrectLetters.length() > 0) { + this.incorrectLetters.append(" "); + } + this.incorrectLetters.append(this.selectedLetter); + } + + return found; + } +} diff --git a/enter_word.java b/enter_word.java new file mode 100644 index 0000000..56f9ea2 --- /dev/null +++ b/enter_word.java @@ -0,0 +1,22 @@ +import java.util.Scanner; + +public class enter_word { + + public static char getLetter() { + Scanner scanner = new Scanner(System.in); + char letter = ' '; + + while (true) { + System.out.print("Mets UNE (genre 1) lettre frero: "); + String input = scanner.nextLine().toLowerCase(); + if (input.length() == 1 && Character.isLetter(input.charAt(0))) { + letter = input.charAt(0); + break; + } else { + System.out.println("Frero ? 1 lettre, comment tu as pu rater ça , Quel singe"); + } + } + + return letter; + } +} diff --git a/main.java b/main.java new file mode 100644 index 0000000..de345c1 --- /dev/null +++ b/main.java @@ -0,0 +1,123 @@ +import java.util.Scanner; + +public class main { + public String word = word_search.getRandomWord(); + public Letter curentLetter = new Letter(word); + public int nbError=0; + public boolean winCondition=false; + + //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 d’erreurs + System.out.println(hangman[mistakes]); + } + + public void showGame(){ + System.out.println("lettre fausse : " + this.curentLetter.incorrectLetters); + displayHangman(nbError); + System.out.println("mot a trouver " + this.curentLetter.wordBuilding); + } + + public void game(){ + while(nbError<6 && !winCondition()){ + char letter = enter_word.getLetter(); + this.curentLetter.setLetter(letter); + if (!this.curentLetter.letterInWord()){ + nbError++; + } + showGame(); + } + if (winCondition()){ + System.out.println("victoir (celui qui a écris a 5 de QI)"); + } + else{ + System.out.println("ta perdu sale étron"); + } + } + + public static void main(String[] args) { + main test =new main(); + test.game(); + + } +} diff --git a/word_search.java b/word_search.java new file mode 100644 index 0000000..c988930 --- /dev/null +++ b/word_search.java @@ -0,0 +1,18 @@ +import java.util.Random; + +public class word_search { + + // Liste des mots + private static String[] WORDS = { + "singe", "google", "frapper", "haine", "dio", "java", + "felix vimalaratnam", "lebreton", "argent", "pauvre", "grocaillou" + }; + + /** + * Retourne un mot aléatoire du tableau WORDS + */ + public static String getRandomWord() { + Random random = new Random(); + return WORDS[random.nextInt(WORDS.length)]; + } +}