Today We'll be:
- Working with an instantiable class containing a driver program and methods for testing
- Manipulating strings, characters, and Character objects
- Using ArrayLists
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:
- Class Variables (done)
- Instance Variables (done)
- Constructors (needs modification)
- Public Instance Methods (done)
- Private Instance Methods (NOT done)
- Methods for Testing (done)
- main Method (needs modification)
You will need to complete the code for the following private instance
methods:
- private boolean gameWon()
- private boolean gameLost()
- private boolean gameOver()
- private void handleGuess(char ch)
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:
- A String that contains the chosen secret word.
Look at the constructor's code in section 3 to see how this variable is
initialized.
- An ArrayList of
Character objects whose length is the same as
the length of the secret word. This array list keeps track of correct
guesses of letters made by the user. Note in the constructor it is
initialized to contain all underscores ('_').
- An ArrayList of
Character objects that contains all the
incorrect letters that the user has chosen.
- A Scanner object to read the user's guesses in
from the console.
Methods for Testing
In section 6, we've implemented several methods that are used for testing
purposes only. These include:
- public String toString():
This method returns a string representation of the internal state of
the Hangman object suitable for printing. The
string that is returned contains all of the instance data members.
- Mutator methods:
These provide a way to change the value of the instance variables.
This technique allows a more direct means for testing methods. You can
change the value of a data member, test the method again, change the
value again, test again, etc.
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:
- the user has correctly guessed all the letters in the secret word
(a win), or
- the user has made seven wrong guesses (a loss).
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:
- Calls the printHangman method (which we've
written) to display the current Hangman.
- Prints the secret word so far (with the letters that
the user has guessed and the other letters represented using
underscores).
- Prints the incorrect letters so far.
- Asks the user to guess a letter.
- 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:
- Modify the program to handle if the user guesses the same letter
more than once.
- Modify the program to handle if the user enters an uppercase
letter.
- Modify the program to handle if the user types in a character
that is not a letter.
- 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.
Let your Lab TA know which of these challenge tasks
you complete.