CS 302 Lab 4

Lab 4: Hangman

CS302, UW-Madison

Today we'll cover:

The Game of Hangman

Today you will be completing a program that enables the program's user to play the game of Hangman. Let's first review the game of Hangman:

The goal of Hangman is for the player to determine a secret word by guessing its letters. Initially, the player only knows how many letters are in a word. For example, the four letter word is displayed as "_ _ _ _", where each underscore ('_') represents a letter in the word. The player guesses one letter at a time, and if that letter is in the secret word, all occurrences of that letter are revealed. For example, if the player guesses 'a', then assume the following is revealed: "_ a _ a". If the guessed letter is not in the secret word, that letter is added to a list of incorrect guesses, and a part of the Hangman picture is drawn. If a player can determine the word before making seven incorrect guesses, s/he wins.

Now that we've reviewed the game, here is what you need to do:

Getting started

Begin by starting a new project in Eclipse.  Name your project HangmanLab. Next, download the following file to your HangmanLab folder as you've done in prior labs.

Task 1: Reviewing the Hangman Class

The Hangman class implements the game in the main method, which uses two additional static methods. The code for this class is organized into sections. You'll be completing the code in the sections indicated below:

  1. Secret Words List (done)
  2. Constant (done)
  3. Variables (3B to be completed in Task 2)
  4. Main Loop for Game (4B, part of 4E, and 4C to be completed in Task 3)
  5. End of Game Message (done)
  6. Additional Static Methods (6A to be completed in Task 4)

Review the code provided, but do not change the code at this point - you'll be doing that in the tasks below. Familiarize yourself with the code sections, the flow of execution, and the constants and variables used. The following is a summary of the program's main loop that repeats so long as the game hasn't been won and the player hasn't used all their guesses:

  1. Call the printHangman method (which we've written) to display the current hangman picture.
  2. Print the secret word so far showing the letters that the user has guessed and the other letters as underscores.
  3. Print the incorrect letters so far.
  4. Ask the user to guess a letter.
  5. Use the Scanner object to read in the letter and then handle the guessed letter to check whether or not it is in the secret word.

Note we're using a separate method, named printHangman, to output the hangman picture using characters. We've put this code in its own method to make the main method more readable. Now, in the main method we only have to ask that the printHangman be done with the method call: printHangman(badGuessesCount);. Below shows what this output looks like for a point during the execution of a Hangman game (with parts of the hangman filled in for each wrong guess):

 
  ___
  |  |
  |  0
  | \|/
  |
  |
  |
  |
__|__
g e o g _ _ p _ _
Wrong guesses: t l s b

TIP: Note the blue boxes just to the right of the scroll bar in Eclipse's editor window. These boxes indicate sections that have been tagged as TODO items. Click on one of the blue boxes and it will take you to the corresponding "TODO" item. You can use this feature in your own programs by putting comments in your code that begin with TODO like the comments you see in the Hangman program.

Task 2: Declaring and Constructing the Arrays

Go to section 3B. of the program and complete the code as indicated for each of the TODO items in that section. Check with your Lab TA that your code is correct before moving on to the next task.

Task 3: Playing the Game

Next go to section 4B of the program and complete the code as indicated for this TODO item. You should be able to run your program to see if the new code you added works as expected (but the game isn't finished).

Next go to section 4E of the program and review the code there. Once you understand what this code is doing then complete the code as indicated for this TODO item. Run your program and make sure that the code you've added works correctly.

Next go to section 4C of the program and complete the code as indicated for this TODO item. Run your program and make sure that the code you've added works correctly.

When this task is completed, show your Lab TA.

Task 4: Winning the Game

Switch so that your partner is in control of the keyboard

At this point our game is nearly complete, but it doesn't detect when a game has been won. We'll use a separate method to detect this. You'll be learning more about methods in lecture soon, but we'll give them a try now. They're a very useful way to reduce redundancy and make our programs more organized. (You just need to use a main method in programming assignment 1. In program 2 you'll be using methods.)

In addition to main, we've already been using another method to display the hangman picture. We use the method by coding the method call statement: printHangman(badGuessesCount);. A method call has the name of the method possibly followed by additional information in parentheses that we call arguments (aka parameter values). Our hangman picture displaying method needs one argument, the number of bad guesses, so that it can determine how much of the hangman picture to draw.

The return type of this method is void indicating it doesn't return any answer. Often methods return some answer to the calling program using a return statement, which we'll describe further below. Methods that return something would need to say the type of the answer they return instead of void. Take a look at this print method to see the layout used for coding methods.

For this task, you'll complete the gameWon method that is passed the array of correct guesses and returns a boolean value (i.e., true/false). If the array of correct guesses doesn't have any blanks (i.e., '_') then all of the characters in the word have been guessed and the game is won. Go to section 6A of the program and complete the gameWon method as indicated for this TODO item. We've already coded the call of method gameWon in the condition of the program's main game loop (while loop at top of section 4). We've also provided the shell for the method in which you'll fill the method's body.

For our main game loop to work, we need gameWon to return either true or false depending on whether or not the game has been won. We'll use a return statement as in this example: return false;. The method ends as soon as a return statement is executed and the method's call is replaced with its return value. In our program main calls gameWon, which determines if the game was won, and then returns either true or false accordingly.

Challenge Tasks:

In the lab time remaining, make the following improvements to the program:

  1. Modify the program to accept uppercase letters and convert them to lowercase (hint: checkout the toLowerCase method of the Character class).
  2. Modify the program to display an error if the user enters in a character that is not a letter.
  3. Modify the program to display an error if the user fails to enter any character for their input.
  4. Modify the program to not allow the user to guess the same letter more than once.
  5. Modify the program so that the user is able to play the game until they desire to quit. Display a count of the wins and losses.

You are welcome to use additional static methods if you feel they would be helpful on these challenge tasks. Remember the first line of a method, which is called the heading, is in this form:

public static <return type> <methodName> (<comma separated list of parameters>)

Where the return type is the Java type of the return value (if no value is returned, use void for the return type), methodName is an identifier you choose, and the parameters are pairs of type name followed by parameter name (separated by commas if you use more than one parameter). Refer to the two methods in our program for examples.

Let your Lab TA know which of these challenge tasks you've completed.