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 Java Objects Creating the MVP Remaining Tries

Kabir Gandhiok
Kabir Gandhiok
12,553 Points

The game doesnt quit once the word is guessed

I noticed the while loop doesnt stop once the word has been guessed. Here's what I have come up with to end the game if the user guessed the word right.

public boolean endGame(){ boolean currentState = false; if (hits.length() == answer.length()){ currentState = true; } return currentState; }

public class Hangman{ public static void main(String[] args){ Game game = new Game("treehouse"); Prompter prompter = new Prompter(game);

    while (game.getTriesLeft() > 0 && !game.endGame()){
        prompter.displayProgress();
        prompter.promptForGuess();
    }
}

}

This works but there's a bug. Repeat characters are added to the length of hits only once because they are entered only once. So to end this game the character 'e' would have to be entered once again for it to get added to hits length. Any ideas how to workaround this? Thanks!

Kabir Gandhiok
Kabir Gandhiok
12,553 Points

Edit: Im not sure why the first block of code, endGame(), is not getting formatted properly.