CS 302 Lab 11

Lab 11: ArrayLists and Wrappers: Hangman 2

CS302, UW-Madison

Today We'll be:

The Game of Hangman

Recall the Hangman program you wrote in lab 4 that used arrays of characters to implement the word guessing game Hangman? Today you'll be implementing the same game but now using Java's ArrayList so that you can compare the two approaches. This implementation also demonstrates combining a main method and testing (i.e., driver) methods into an instantiable class that is a complete application program. If you need to, review the game described in lab 4, and then here's what you need to do:

Getting started

Begin by starting a new project in Eclipse.  Name your project Hangman2Lab. Next, download the following file to your Hangman2Lab folder. (An easy way to do this is to right-click on a link and then choose the Save Link As... option.)

Task 1: Reviewing the Hangman Class

The Hangman class implements the game and includes a main method that is initially used as a driver program. The code for this class is organized into seven sections:

  1. Class Variables (done)
  2. Instance Variables (done)
  3. Constructors (needs modification)
  4. Public Instance Methods (done)
  5. Private Instance Methods (NOT done)
  6. Methods for Testing (done)
  7. main Method (needs modification)

You will need to complete the code for the following private instance methods:

Data Members and the Constructor

Look at data member in the Hangman class, section 1. We're using a class (static) variable that is a static array of words (Strings). One of these words will be randomly chosen as the secret word each time you create an instance of a Hangman. Look at the constructor in section 3, and you'll see the code (intially commented out) that does the random selection of the secret word.

In section 2, the Hangman class has the following instance variables:

Methods for Testing

In section 6, we've implemented several methods that are used for testing purposes only. These include:

main Method

In section 7, we've implemented the main method that is used as a driver program. As you develop your Hangman class, uncomment parts of the main method to test your Hangman methods. Note that we've set up the code so that it is easy to uncomment a section at a time. For example, to test the constructor add a '/' character in the code in the location corresponding to what's shown below.


        /* 
         * To test the constructor, use the toString method
         * to see if the data members are what you expect.
         */

This will uncomment that section. Removing the '/' will again comment out that testing section. The other sections of the main method are set up similarly.

Task 2: Methods to Code

There are four methods you need to implement to complete the program. They are all located in section 5 of the program. Additionally there is a testing section in the main method for each of these methods for you to use as you develop your code. Show your work to your Lab TA after you've coded and tested EACH method and make sure to switch control of the keyboard.

private boolean gameWon()

This method should return true iff (i.e., if and only if) the user has won the game (i.e., has guessed all of the letters in the secret word). One way to tell whether the user has won is to check whether the array list of correct guesses contains any underscores (if it doesn't, the user has guessed all of the letters).

private boolean gameLost()

This method should return true iff the user has lost the game (i.e., has made 7 wrong guesses).

private boolean gameOver()

This method should return true iff the user has either won or lost the game:

private void handleGuess(char ch)

This method will be called each time the user guesses a letter. The job of this method is to determine whether the guessed letter (parameter ch) is in the secret word. If it is, the array list of correct guesses should be updated so that the underscores are replaced with the guessed letter at each position where the letter occurs in the secret word. If the guessed letter is not in the secret word, and the array of incorrect letters should be updated to include the guessed letter. This method should also tell the user whether the guess was correct or wrong.

Task 3: Playing the Game

Once you've completed the methods above, then you're ready to test the game by playing it. In section 4 of the code, we've implemented a public method:

public void playGame()

Take a look at this code. This method will play one game of Hangman with the user. It repeatedly executes until the user has either won or lost; and then it tells the user the result.

Within the loop, the code performs the following steps:

  1. Calls the printHangman method (which we've written) to display the current Hangman.
  2. Prints the secret word so far (with the letters that the user has guessed and the other letters represented using underscores).
  3. Prints the incorrect letters so far.
  4. Asks the user to guess a letter.
  5. Uses the Scanner object to read in the letter and calls your handleGuess method to check whether it is in the secret word.

The output your code prints each time around the loop look like this (with more and more of the Hangman filled in for each wrong guess):

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

Comment out the prior testing sections in the main method and uncomment the last part in main. Also uncomment the code in the constructor that randomly chooses a word. Now run your program and play the game! When your game is working, show it to your Lab TA. Also let your Lab TA know which approach (arrays vs. ArrayLists) was easier and why.

Challenge Tasks:

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

Let your Lab TA know which of these challenge tasks you complete.