You are on page 1of 1

Hints

1. Keep two copies of the word as instance variables: one with the
word itself (named word), and one with the copy of the word the user
sees (with *’s in place of letters they haven’t guessed yet) (named
blankWord).

2. If you do the above, you can use this code to store a correct copy of
the blank word:
if(isInWord(word,guess)) {
correctGuesses += guess;
blankWord =
word.replaceAll("[^"+correctGuesses+"]", "*");
}

In this code, correctGuesses stores a String containing all letters that


the user has guessed correctly so far. This code updates blankWord to
store something like: *a**ma* if, for example, the user had correctly
guessed a and m in the word “hangman”.

3. Following this pattern, I need 4 instance variables:

String word; // stores the chosen word


String blankWord; // stores the word the user sees
String correctGuesses; // stores all the correct guesses so far
int inc; // stores number of incorrect guesses so far

correctGuesses might contain something like: “api” if a, p, and i are


all letters in the word that the user has guessed so far.

4. Detecting when the user has won can then be done by checking if
blankWord equals word.

You might also like