Project 4 - Practice with File I/O
and Strings: Hangman
Write a program to play the word guessing game called hangman. Your
program will randomly select a word from a text file of 30 words,
and then the human user will try to guess the word. A sample run will
look like this:
Let's play Hangman!
Word to guess: _ _ _ _
Please enter a letter to guess: a
Word to guess: _ a _ _
Please enter a letter to guess: t
Sorry, there is no letter 't' in the word.
Hangman: O
Word to guess: _ a _ _
Please enter a letter to guess: x
Sorry, there is not letter 'x' in the word.
Hangman: O
/
...
The game will end in one of two ways:
The player will fail to guess the word before the hangman is drawn
completely, and your program will print the message "Sorry, you lost.
The word is rant." for example.
Or the player will guess the word before the hangman is completed, and
your program will print "That's right! You won!". In this case, you
should print the complete guessed word, with no remaining embedded
blanks.
While you are working on the program, create a word file of 30 words
called "hangmanTestWords.txt". When we run your program, we will use
our own word file to test your work. In this file, each word will
appear on a separate line.
If a character other than a letter is entered, your program should
print an error message and reprompt the user for another character. If
a previously entered letter is entered, the program should print an
error message and reprompt the user for another letter.
Convert all letters to lower case so that case does not affect the
outcome.
The hangman will be drawn in the following steps:
1. O
2. O
/
3. O
/ |
4. O
/ | \
5. O
/ | \
|
6. O
/ | \
|
/
7. O
/ | \
|
/ \
So the user gets 7 guesses.
Your Hangman.java file will provide the methods and data needed to play
a hangman game. You will need to keep track of the number of moves and
which
letters have been guessed, for example. You will need methods that draw
the hangman, search for the guess letter in the word (you need to find
all occurrences), print the current guessed version of the word (with
blanks embedded among guessed letters), etc.
Your HangmanInterface.java file will contain code that selects the
random word from the "hangmanTestWords.txt" file, prompts the users for
the guess letter, etc.
Part of your grade will be based on your design of the two classes,
Hangman and HangmanInterface. Think carefully about what methods you
need, what variables you need, and which of these should be private and
public. You will also be graded on readability (white space, comments,
indentation), and how closely you match the sample output and follow
the project specification.
Your two files must be submitted via the turnin program by 5 pm on
Monday, October 4, 2004.