Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

I feel logic to be wrong. Is it so??

Craig in 6:55 minutes writes a code where he does mHits.indexOf(letter) for throwing exception. But what if there existed the same the same character in a given word more than one time. Wouldn't that be wrong where you are not allowed to enter the same charcater two times. For example "treehouse" has e 3 three times it. If you enter 'e' for the second time it throws an error which makes your programming logic wrong.

Can you post a link to the video; I'll have a look.

4 Answers

I see.

I don't think that you have to guess the letter 'e' three times to make the word 'Treehouse'. If you choose 'e' once, it fills in all three. Thus, you shouldn't be able to choose the same selection a second time.

I hope that helps.

Steve.

Hi Durgaprasad,

You are allowed to enter the same character many times, but you will be warned and asked to enter an other character.

I will try to give you a step by step explanation for your question without commenting the whole code of the Hangman game.

If I run the programm and enter "e" into a console I can see this:

You have 6 tries to solve :  ---------
Enter a letter: 
e
You have 6 tries to solve :  --ee----e
Enter a letter: 
e
e is already been guessed. Please try again. 
Enter a letter: 

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

So first of all you are creating a Game object that expects a String answer as argument. This String is "Treehouse" and the User will try to guess it. This game object that contains the answer is given to the Promter class that holds the play() method. play() shows the progress and initializing the promptForGuess() that makes the Users input possible.

public class Hangman {
    public static void main(String[] args) {
        Game game = new Game("Treehouse");
// "Treehouse" is our mAnswer that is stored in the Game object
// we will compare our input with it
        Prompter prompter = new Prompter(game); 
// new Prompter object that takes the game object with the answer "Treehouse"
        prompter.play();
// so from here the compiler is going to the Prompter class to compare the Users input with "Treehouse"
    }
}

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

So inside the Prompter class the play() method kicks of the displayProgress() method to print the remaining tries and shows the progress of the game (-----x--y) and the promtForGuess() method is activated to interact with the User.

You have 6 tries to solve :  ---------
    public void play() {
        while (mGame.getRemainingTries() > 0 && !mGame.isSolved()) {
            displayProgress();
// shows how many tries are left and the letters that have been already guessed 
            promptForGuess();
// >>>>>>>>INPUT GUESS-METHOD<<<<<<<<<
        }
        if (mGame.isSolved()) {
            System.out.printf("Congratulations you won the game with %s remaining tries", mGame.getRemainingTries());
        } else {
            System.out.printf("Bummer the answer was %s", mGame.getAnswer());
        }
    }

After promptForGuess() is running you see the prompt for Users input:

Enter a letter: 
  public boolean promptForGuess() {
        Scanner s = new Scanner(System.in);
        char guess;
        boolean isHit = false;
        boolean isValidGuess = false;

        while (!isValidGuess) {
            System.out.println("Enter a letter: ");
            String guessAsString = s.nextLine();
// >>>>>>> guessAsString is the Users input

            try {
                isHit = mGame.applyGuess(guessAsString);
// to validate the Input and compare it with the mAnswer ("Treehouse")
// the applyGuess method of the Game class is activated
// this method takes the input of the User
                isValidGuess = true;
            } catch (IllegalArgumentException iae) {
                System.out.printf("%s. Please try again. \n", iae.getMessage());
            }
        }
        return isHit;// returns isHit true if guess is true
    }

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

From the promptForGuess() method you take the Users-input "guessAsString" and give it to the applyGuess(char letter) method of the Game class. This method takes the first char from the String "guessAsString" and uses it as target to search from left-to-right inside of the mAnswer ("Treehouse").

Here is the applyGuess method:

 public boolean applyGuess(char letter) { 
        letter = validateGuess(letter);
// this method returns only a letter if it is really a letter and the letter haven“t been guessed already
// if the letter has been already guessed you get a WARNING and you need to guess again (loop)
        boolean isHit = mAnswer.indexOf(letter) >= 0;
       //  str.indexOf(char target) -- searches left-to-right of the String mAnswer for target
       //  Returns index where found, or -1 if not found
       //  so if the return is > 1 it is a hit

        if (isHit) {
            mHits += letter;
        } else {
            mMisses += letter;
        }
        return isHit;
    }

So if the User inputs a letter, that have been already guessed, we get the warning from the validateGuess() method.

e is already been guessed. Please try again. 

I hope I could help a little bit :)

Grigorij

I got in when i compiled the code. Anyway thanks a lot for helping me out.