I have to complete this programming homework which consists of building a hangman game. However, the problem is: I do not know how to make it so that, when you guess a correct letter, it reveals that letter in the hidden word while still keeping the other parts of the word hidden.

For example :
The hidden word is REVEAL.
You guess "E".
It should now show ".E.E.." in the user's screen.

If you guys could help me out with this, I'd be extremely grateful, especially since I'm in a big hurry!

By the way, since I live in Quebec, Canada, French is our primary language, so my code contains some French on it (like the name of the variables and the text printed on the screen, which I all translate in comments).

Secondly, since we haven't learned how to use "import", we have to use a class named "Keyboard" in order to ask the user to type something. For a String, we use "variable = Keyboard.readString();.

public class Pendu {
    public static void main(String[] args) {

        String difficulte, motMystere, lettreEntree, nouvellePartie, lettresDejaEntrees = ""; // difficulte = difficulty ; motMystere = hidden word ; lettreEntree = letter guessed ; nouvellePartie = new game ; lettresDejaEntrees = letters already guessed
        int nombreErreurs, nombreLettres, numeroEssai = 1; // nombreErreurs = number of mistakes ; nombreLettres = number of letters in the word ; numeroEssai = which attempt it is (attempt #1, ect.)
        boolean invalideOuNon = true; //invalideOuNon = a boolean which checks if the letter is invalid or not (it has to be from A to Z or a question mark(a question marks makes you give up) to be valid)

        System.out.println("JEU DU BONHOMME PENDU");
        System.out.println("---------------------");
        System.out.println("");

        do { 
            System.out.print("Entrez le niveau de difficulte (F=Facile, I=Intermediaire, D=Difficile) : "); // Facile = Easy ; Intermediaire = Intermediate ; Difficile = Difficult
            difficulte = Keyboard.readString().toUpperCase();
        }
        while (!difficulte.equals("F") && !difficulte.equals("I") && !difficulte.equals("D"));

        if (difficulte.equals("F"))
            nombreErreurs = 8;
        else if (difficulte.equals("I"))
            nombreErreurs = 6;
        else
            nombreErreurs = 4;

        motMystere = Dictionnaire.genereMot();
        motMystere = motMystere.toUpperCase();
        nombreLettres = motMystere.length();

        System.out.println("Vous devez trouvez le mot mystere en faisant moins de " +nombreErreurs +" erreurs."); // "You have to find the hidden word without exceeding said number of mistakes."
        System.out.println("Generation d'un mot mystere..."); // "Generating a hidden word..."
        System.out.println("Le mot mystere contient " +nombreLettres +" lettres."); // "The hidden word contains said number of letters."

        do {
            // This for loop prints on the screen a number of dots corresponding to the number of letters in the hidden word.
            for (i = 0; i < nombreLettres; i++) {
                System.out.print(".");
            }
            System.out.print("Essai #" +numeroEssai +", entrez une lettre (A-Z) ou ? pour abandonner : "); // "Guess a letter (A-Z) or type ? to give up."
            lettreEntree = Keyboard.readString().toUpperCase();
            lettreInvalideOuNon = lettreEntree.matches("[^A-Z?]"); // This checks if the character typed by the user is valid (am I doing it right, by the way?).

            if (lettreInvalideOuNon == false) {
                System.out.println("Veuillez entrez une lettre ou symbole valide."); // "Please enter a valid letter."
                while
            }
            else if (lettreEntree.equals("?")) {
                System.out.println("Vous avez perdu!"); // "You lost!"
                System.out.println("La solution du mot mystere etait " +motMystere +"."); // "The solution was ..."

                do {
                    System.out.println("Voulez-vous jouer une autre partie du Bonhomme Pendu (O/N)? "); // "Would you like to play another game (Y/N)?"
                    nouvellePartie = Keyboard.readString().toUpperCase();

                    // Will come back here later.
                    if (nouvellePartie == "O") {
                    }
                    else (nouvellePartie == "N") {
                    }

                    while (!nouvellePartie.equals("O") && !nouvellePartie.equals("N"))
            else (lettreInvalideOuNon == true) {
                if (lettresDejaEntrees.contains(lettreEntree)) {
                    System.out.println("Vous avez deja essaye la lettre " +lettreEntree +", essayez encore."); // "You already guessed this letter, please try again."
                    while
                else // THIS is the part I DON'T understand.
                    if (motMystere.contains(lettreEntree)) { // This checks if the hidden word contains the guessed letter.
                    // What do I do here?

Edited 3 Years Ago by xTurK

nothing urgent about it.
Just create a new String each time, showing just the already guessed letter.

not to mention you should start with the basics before going to logical issues..

if (nouvellePartie == "O")

see why that might give you problems?

I do not know how to make it so that, when you guess a correct letter, it reveals that letter in the hidden word while still keeping the other parts of the word hidden.

You can use a StringBuilder - it's like a String but you can update individual characters in it. So start with a StringBuilder that just contains the right nunmber of '.' characters. As the user guesses correctly you can replace the corresponding '.' with the correct character. You can print a StringBuilder just like printing a String, so no problems there! À Bientôt J

http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

You can use a StringBuilder - it's like a String but you can update individual characters in it. So start with a StringBuilder that just contains the right nunmber of '.' characters. As the user guesses correctly you can replace the corresponding '.' with the correct character. You can print a StringBuilder just like printing a String, so no problems there! À Bientôt J

http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

Thank you for the reply, but how do you make it so that the StringBuilder has the same amount of '.' as there are letters in the hidden word? So far, I've only learned to do this by printing dots to the same line using a for loop, but I don't know how that would work with a StringBuilder (let's say the StringBuilder contains "." and you want to add dots in it as many times as there are letters in the hidden word, how do you do that?).

I could try reading the whole doc, but since I only started Java a month ago, I'm still a beginner and the API can be a bit confusing at times. :(

Also, what if the guessed letter appears 2 or more times in the hidden word? How do I show that?

If you could show a code example, I'd be very grateful, as I learn better through those. :)

Well, fortunately, all you really need to do is change your System.out.print() calls to .append() calls on your StringBuilder:

            StringBuilder motCache = new StringBuilder(nombreLettres);

            for (i = 0; i < nombreLettres; i++) {
                motCache.append('.');
            }

(I hope that 'mot cache' is the correct phrasing for 'hidden word'...)

You can then print out the StringBuilder object whenever you need it, and manipulate the characters using the .setCharAt() method.

Edited 3 Years Ago by Schol-R-LEA

Thank you so much, everyone (especially you, Schol-R-LEA)! Yes, mot cache is the correct phrasing for 'hidden word'. :)

This question has already been answered. Start a new discussion instead.