Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.