The Hangman class

advertisement
Program: Strings, Files, Objects
Hangman is a game in which a person tries to guess a word. The player is given the length
of the word (represented by one blank per letter) and each letter is initially represented by
some substitute character such as underscores, hyphens, asterisks, etc. Every time a
correct letter is guessed, it is displayed in its correct location(s) in the word. The game ends
when one of two things happens: (1) either the player guesses all of the letters in the word,
or (2) he reaches his maximum number of incorrect guesses. The TV show Wheel of Fortune
is basically a glorified version of Hangman. Write a program to play the game of Hangman.
Your program will read text from a file and manipulate character strings. NOTE: You can use
the String class for all of the text data in this assignment. It is not necessary to use the
StringBuilder or StringBuffer class.
The Hangman class
Create a Hangman class that encapsulates all of the data required to play a single game of
Hangman. The class should have the following data, all of which is private:
 An array of 1000 words (Strings) from which to choose a random word.
 An integer (int) representing the number of words in the array. This number will be
determined by the number of words that are read from the data file. It will
necessarily be no more than 1000.
 A word (String) that is to be guessed by the player. This will be referred to as the
hidden word. The word is randomly chosen from the list of words in the array
(above).
 A word (String) that represents a clue that will be given to the player.
 A character (char) that represents the player's most recent guess.
 An integer (int) representing the number of incorrect guesses that the player has
made so far.
 A Boolean array of 26 elements (0 for 'a', 1 for 'b', 2 for 'c', ... 25 for 'z'). The
Boolean array is used to keep track of which letters have been guessed already. Set
all elements to false at the beginning (you may want to add another private method
for this), and every time a letter is guessed, set the corresponding value to true.
Note that the ASCII code for 'a' is 97, 'b' is 98, etc. Note also that if you subtract 97
from the ASCII code, you can convert each code into a number in the range 0-25 for
use with your array.
The Hangman class should implement the following methods:
 Hangman(). Default (and only) constructor. Calls the following private methods:
 readWords(). Reads the words from a file of words (one word per line) into the
array and counts the number of words read. The number of words will be no
more than 1000. Trims each word and shifts it to upper case.
 printWords(). Prints out the words from the array. For debugging purposes only.
Comment this out after you are sure that your program is correctly reading in the
words.










setHiddenWord(). Generates a random number from 0 to the number of words in
the array minus 1. Uses this number as an index into the array to pick the hidden
word for the game.
 initializeClue(). Initializes the clue to a string of asterisks. The number of
asterisks must be the same as the number of letters in the hidden word.
getClue(). Returns the clue.
setGuess(char). Sets the guess to the character that is passed in.
hiddenWordContains(char). Returns true if the hiddenWord contains the character,
false otherwise. This is a private method.
alreadyGuessed(char). Returns true if the character has already been guessed, false
otherwise. This is a public method.
updateClue(). Looks at the hidden word and the guess and updates the clue to
reflect the guess. If the guessed letter occurs in the hidden word, make each
occurrence of the guessed letter visible in the clue. If the guessed letter does not
occur in the hidden word, the clue will not be changed.
gameLost(). A boolean that returns true if the number of incorrect guesses is 7.
Returns false otherwise.
gameWon(). A boolean that returns true if the game has been won. The game will
have been won if there are no asterisks left in the clue (or, another way of saying
this is if the clue is equal to the hidden word). Returns false if there are still asterisks
in the clue.
getPic(). This String method is optional. It will return a multiline string that
represents the state of the hangman so far. Pics to return are shown below, along
with the number of incorrect letters associated with each picture.
1
2
3
4
5
6
7
|
O
O
O
O
O
O
O
|
/|
/|\ /|\ /|\ /|\
/
/ \ / \
I suggest that you write the following methods. They are, however, for debugging
purposes only.
 getWordCount(). Returns the number of words that were read into the array. For
debugging purposes only.
 getHiddenWord(). Returns the word that is to be guessed. For debugging
purposes only.
Write a main program that will create a Hangman object and then repeatedly do the
following until the game ends:
 Display the current clue.
 Get a guess from the user. If it is more than a single character, trim it, shift it to
lowercase, and extract the first character of the input string.
 See if the guess is a lowercase letter. You can do this without using the Hangman
class. Use the Character class. If it is not, print an error message reminding the
user to enter a letter.
 See if the guess has already been guessed. If it has already been guessed, print
an error message.
 Update the clue using the latest guess.



Check to see if the game is won. If so, print an appropriate message and end the
program.
Check to see if the game is lost. If so, print an appropriate message and end the
program.
Optional: Print out the stick figure.
Files
Use the file of common words. Put it inside of your project folder at the same level as the
build, nbproject, and src folders and you will be able to read it without having to provide
any path information.
Debugging
In addition to using the built-in debugger, here is a suggestion for debugging. Add the
following to the top of your Hangman class:
public boolean debug = false;
Then, put all of your debugging code inside of an if statement, like this:
If (debug)
System.out.println (whatever you want to print);
When you want to see debugging code, add the following to your client (main) program (I'm
assuming that h is your Hangman variable):
h.debug = true;
Comment the above instruction out when you want to turn debugging off.
Download