Petro Ko Petro Ko - 7 months ago 21
Java Question

Hangman using boolean and arrays

This is an assignment for school. I have to make a Hangman game in java. My secret word is "REFRIGERATOR". It's an intro to programming class, so cut me some slack please. The minimum specifications of this project are:


A String to hold the mystery word. (Done)

A boolean array. Each letter in the String is `paired' with a boolean value
in the array. These values can be used to determine whether or not the
letter has been correctly guessed.(Having trouble with this)

An integer to store the maximum number of incorrect guesses allowed. (Done)

An integer to store the number of incorrect guesses made so far. (Done)

A String to store the incorrect guesses. (Done)


I'm stuck at the boolean array part. I don't really understand what the teacher means by "each letter in the String is paired to a boolean value". The only code I wrote that I think could be useful for this part is:

boolean [] foundLetters;
foundLetters = new boolean[mysteryWord.length()];


and

String[] myWord = {"r","e","f","r","i","g","e","r","a","t","o","r"};


but I don't know what to do exactly.

So that's one problem. On another note, one thing I do know is that I will have to use
boolean b = x.contains(myWord)
because I can use this command to check if the letter (that the user inputs) is inside the mystery word string.

I know bits and pieces of what I need to do, I am simply looking for someone to help me stitch these pieces together. I'm not asking the community to do my homework for me, just to help me out with this specific part. This is my entire code so far

String mysteryWord = "refrigerator";
String[] myWord = {"r","e","f","r","i","g","e","r","a","t","o","r"};
int maxGuess = 20;
int errorGuess = 0;
String wrongGuess;

boolean [] foundLetters;
foundLetters = new boolean[mysteryWord.length()];

System.out.println("Welcome to the game of Hangman.");
System.out.println("Start by guessing one letter at a time.");

String x = JOptionPane.showInputDialog("Guess a letter!");

Answer

This should work:

public static void main(String[] args) {
    String mysteryWord = "refrigerator";

    String[] myWord = {"r", "e", "f", "r", "i", "g", "e", "r", "a", "t", "o", "r"};

    //Initialize boolean array to hold as many values as mysteryWord
    //The reason you want this is to keep track of what letters have been guessed.
    boolean[] foundLetters = new boolean[mysteryWord.length()];

    int maxGuess = 20;
    int errorGuess = 0;
    //int correctGuess will keep track of how many letters we have correctly guessed
    int correctGuess = 0;

    System.out.println("Welcome to the game of Hangman.");
    System.out.println("Start by guessing one letter at a time.");

    //Create a loop that will terminate once the user has guessed incorrectly
    //20 times or once the user has gussed all the letters.
    //We know how many letters the user has guessed with int correctGuess
    while ((errorGuess < maxGuess) && (correctGuess != foundLetters.length)) {
        //For each letter in mysteryword, check if the corresponding value in the 
        //boolean array array has been set to true. If it has then print it.
        //Otherwise print out an underscore.
        for (int i = 0; i < foundLetters.length; i++) {
            System.out.print(foundLetters[i] == true ? mysteryWord.charAt(i) + " " : " _ ");
        }
        System.out.println();
        //Ask the user to guess a letter.
        String x = JOptionPane.showInputDialog("Guess a letter!");
        //If the letter that the user entered contains the word 
        if (mysteryWord.contains(x)) {
            //For each letter in mysteryWord
            for (int i = 0; i < mysteryWord.length(); i++) {
                //If it happens to be the letter that the user provided,
                //And the corresponding value in the boolean array has not been set to true
                if ((mysteryWord.charAt(i) == x.charAt(0)) && (foundLetters[i] != true)) {
                    //Set the corresponding value in the array to true
                    foundLetters[i] = true;
                    //Inicrease the correct count by 1.
                    correctGuess++;
                }
            }
        } else {
            //If the user guessed incorrectly then increment the number of errors.
            errorGuess++;
        }
    }
    //Check if the number of correct guesses matches the number letters in mystery word
    if (correctGuess == foundLetters.length) {
        //If it does then let the user know they guessed correctly.
        System.out.println("Congrats, you guessed it!");
    } else {
        System.out.println("You lose!");
    }
}

Here's a sample run:

run:
Welcome to the game of Hangman.
Start by guessing one letter at a time.
_ _ _ _ _ _ _ _ _ _ _ _
r _ _ r _ _ _ r _ _ _ r
r e _ r _ _ e r _ _ _ r
r e f r _ _ e r _ _ _ r
r e f r _ g e r _ _ _ r
r e f r i g e r _ _ _ r
r e f r i g e r _ _ _ r
r e f r i g e r _ t _ r
r e f r i g e r _ t o r
r e f r i g e r _ t o r
Congrats, you guessed it!
BUILD SUCCESSFUL (total time: 15 seconds)

Let me know if you need me to explain anything. Cheers!

Comments