ulrail.gif

HW J4: Loops

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Lectures | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif

Purpose

In this homework, you will write a word guessing game, similar to the hangman game that most people played when they were young.  Because we don't want to get fired, we won't be calling it hangman, even though that is what everybody else calls it.  Instead will use a more "politically correct" name, such as The Word Guessing Game (or TWoGG, for short).  It sounds (intentionally) silly, but it allows us to keep our jobs....

The idea behind the game is simple: a word is chosen by the computer, and is initially shown to the player with all blanks.  The player gets a certain number of guesses, and on each guess, they can select a letter.  If that letter appears in the word, then the word is shown with that letter filled in in the correct spot(s), along with all previously guessed letters filled in.  If the letter does not appear in the word, then a list of "misses", meaning letters guessed that are not in the word, is displayed as well.  While the player can guess many times, the game ends when either (1) the player completely fills in the word, or (2) the number of misses hits a pre-determined number (we'll use 8 for this program).

We are assuming that most people in the class are familiar with the game.  If not, then more details on TWoGG (a.k.a. hangman) can be found here.

There are two files to download: TWoGG.java, the skeleton code, and TWoGGHelper.class, which we describe below.

Algorithm

This game has a number of steps.  We define a "turn" as a single guess, whether the guessed letter is in the word, not in the word, or a letter already guessed.

  1. Determine the word to be guessed
  2. While the player still has misses, and the word is not yet completely filled in
    1. Print out the guessed word so far, along with the missed letters, and the number of remaining guesses
    2. Get a letter from the keyboard
    3. If that letter has already been guessed, skip to the next turn
    4. Otherwise, find if the letter is in the word
    5. If so, then print out that it was a hit
    6. Otherwise, print out that it was a miss
    7. Either way, call guessLetter() with that letter
  3. Once the loop terminates, print out if the player won or lost, and display the word

Note: If you don't understand the above steps, then you are just going to be confused with what is listed below.  Don't worry if you don't know how to implement each step in Java yet -- that is explained below.  But you should understand the basic idea of what each step does, and the entire algorithm should make sense.

Class TWoGGHelper

We have provided you with a TWoGGHelper class file, which has a number of method that you should call to get your game working.

  • boolean alreadyGuessedLetter (char c): This method will return true or false, depending on if the passed letter has already been guessed.
  • void guessLetter (char c): This method is called whenever the player guesses a new letter.
  • String getWord(): This method will return a (relatively easy) word for the player to guess.  Your final code can call either this method or getHardWord().
  • String getHardWord(): This method will return a very difficult word for the player to guess.  Your final code can call either this method or getWord().
  • boolean fullyGuessedWord(String word): This method will return true or false, depending on whether the user has fully guessed the passed word.

The skeleton code defines a TWoGGHelper object named helper.  You can use that to call the various methods (helper.getWord(), etc.).

Let c be a char variable.  When a letter c is guessed by the player, you should call helper.guessLetter(c).  This will tell the TWoGGHelper to add that letter to its list of letters that have already been guessed.  Then, to find out if a given letter c has already been guessed, you should call helper.alreadyGuessedLetter(c).  This will return true or false, depending on whether the passed letter has already been guessed.  Lastly, to find out if all the letters in a word have been guessed, you should call helper.fullyGuessedWord(word), where word is a String.  This will also return true or false.

Note that three of the methods (alreadyGuessedLetter(), guessLetter(), and fullyGuessedWord()) are provided to help you remember which characters have been guessed.  You can choose to use them or not (some may want to implement the same functionality differently, such as with Strings or Vectors).  However, you MUST use getWord() or getHardWord() (we don't care which one) to choose your word!  This is what we will use to grade your program.

Revisiting the Algorithm

Having given the algorithm above, we give a bit more detail to the steps here.

  1. Determine the word to be guessed

    This is just a simple call to the methods we have provided you.  You can use either helper.getWord() or helper.getHardWord() when you submit your assignment.
     
  2. While the player still has misses, and the word is not yet completely filled in

    Note that both have to be true!
     
    1. Print out the guessed word so far, along with the missed letters, and the number of remaining guesses

      This is going to require a loop for each of the first two.  Note that if you have a word such as "nineteen", and 'e' is guessed, then all the 'e's should be printed.  We can loop through each letter in the word, and for each letter, see if it has been guessed already.  For the missed letters, what do we have to loop through?
       
    2. Get a letter from the keyboard

      Scanners can only take in Strings (via the next() method in Scanner), so we will read in a String and just take the first character in that String.
       
    3. If that letter has already been guessed, skip to the next turn

      Via alreadyGuessedLetter(), we can easily tell if the letter has been guessed or not.
       
    4. Otherwise, find if the letter is in the word

      This step should be easy -- the String class defines methods to do this.
       
    5. If so, then print out that it was a hit

      Not much to say....
       
    6. Otherwise, print out that it was a miss

      Not much to say...
       
    7. Either way, call guessLetter() with that letter

      This allows the alreadyGuessedLetter() method to know that the particular letter was already guessed
       
  3. Once the loop terminates, print out if the player won or lost, and display the word

    This step should be pretty easy

Incremental Development

  • Test your program incrementally.
     
  • You should strive to test your program incrementally.
     
  • Seriously, incremental testing.
     
  • By incremental testing, we mean that you should implement some tiny subset of the necessary features and convince yourself that they work before you move on.  You should first write a program that just manages to obtain a random word (via the getWord() method), and prints it out to the screen.  To get started, you can just use while(true) for your main loop (you will have to fix this later, of course).  Test it!  The work on the part where you print out the word with the currently guessed letters (and dashes or underscores for non-guessed letters).  Test this!  The add the letters that have been missed.  Test that!  And so on through the steps within the loop.  Etc.
  • If you fail to do this, you will have a big pile of broken code that will be impossible for you, me, or your TA to debug.
  • Test your program incrementally.
  • We're serious about this.

Sample Execution

As before, the text in red is what was input by the user.

Welcome to our game of TWoGG!

Word: _ _ _ _ _ _ _ _
Misses:
You have used 0 of 8 guesses
Please enter your letter guess: a
That guess is a miss!

Word: _ _ _ _ _ _ _ _
Misses: a
You have used 1 of 8 guesses
Please enter your letter guess: b
That guess is a miss!

Word: _ _ _ _ _ _ _ _
Misses: a b
You have used 2 of 8 guesses
Please enter your letter guess: c
That guess is a hit!

Word: c _ _ _ _ _ _ _
Misses: a b
You have used 2 of 8 guesses
Please enter your letter guess: o
That guess is a hit!

Word: c o _ _ _ _ _ _
Misses: a b
You have used 2 of 8 guesses
Please enter your letter guess: r
That guess is a hit!

Word: c o _ _ _ _ _ r
Misses: a b
You have used 2 of 8 guesses
Please enter your letter guess: e
That guess is a hit!

Word: c o _ _ _ _ e r
Misses: a b
You have used 2 of 8 guesses
Please enter your letter guess: a
You already guessed that letter!

Word: c o _ _ _ _ e r
Misses: a b
You have used 2 of 8 guesses
Please enter your letter guess: m
That guess is a hit!

Word: c o m _ _ _ e r
Misses: a b
You have used 2 of 8 guesses
Please enter your letter guess: e
You already guessed that letter!

Word: c o m _ _ _ e r
Misses: a b
You have used 2 of 8 guesses
Please enter your letter guess: t
That guess is a hit!

Word: c o m _ _ t e r
Misses: a b
You have used 2 of 8 guesses
Please enter your letter guess: q
That guess is a miss!

Word: c o m _ _ t e r
Misses: a b q
You have used 3 of 8 guesses
Please enter your letter guess: z
That guess is a miss!

Word: c o m _ _ t e r
Misses: a b q z
You have used 4 of 8 guesses
Please enter your letter guess: x
That guess is a miss!

Word: c o m _ _ t e r
Misses: a b q x z
You have used 5 of 8 guesses
Please enter your letter guess: u
That guess is a hit!

Word: c o m _ u t e r
Misses: a b q x z
You have used 5 of 8 guesses
Please enter your letter guess: v
That guess is a miss!

Word: c o m _ u t e r
Misses: a b q v x z
You have used 6 of 8 guesses
Please enter your letter guess: w
That guess is a miss!

Word: c o m _ u t e r
Misses: a b q v w x z
You have used 7 of 8 guesses
Please enter your letter guess: n
That guess is a miss!

Sorry, you lost.  Better luck next time!
The word was computer

Miscellaneous Notes

The good programming practices from HW J1 need to be in this homework as well.

As the homeworks in the course progress, less detailed information will be provided, and it is up to you to fill in the detail that is left out (in this case, the legend, Scanner creation, test runs as comments, etc.).

The output, above, is going to be a bit long.  Rather than having to put a // in front of each line for your test case, there is an easier way to comment out a block of text.  Put a /* at the beginning of the block, and a */ at the end.  This will cause everything between them to be a comment (even if it spans multiple lines).

Lastly, the maximum number of guesses (set at 8) should be set to a constant somewhere in your code.

Submission

When you are finished, submit the TWoGG.java file.

spacer.gif
spacer.gif footer-middle.gif spacer.gif