Lab 8: The Hanging Man
Due by the end of class
The classic game of Hangman presents a player with an unknown word consisting of a known number of letters. The player must then make guesses about which letters of the alphabet make up the word. As each guess is made, the spaces that represent the letters of the word are filled in if a guess correctly identifies letters in the word. In general, the player has a limited number of incorrect guesses. As guesses run out, the countdown is often represented by drawing additional body parts suspended from a gallows. When the entire body has been drawn, the player has run out of guesses and loses. If the player is able to guess all of the letters in the word before running out of guesses, he or she wins.
Our goal is to implement a version of the game that randomly picks a word from a dictionary file. Our game will always give the player six incorrect guesses, but it will not draw a man being hanged to represent the remaining chances.
Specification
Create a project called Lab8
. Add a class called Hangman
. This lab is long, but each part of the lab is relatively straightforward.
The program should work with any text file that lists unique words, each on separate lines, but we are providing the following dictionary file. Right-click on the link below and save it into your project folder Lab8
(but not in the src
or bin
sub-folders).
Opening the file
Initialize a Scanner
for reading from the keyboard. Declare another Scanner
called file
for reading from the file, but set it to null
rather than creating a Scanner
object.
Similar to examples from class, create a while
loop that runs as long as file
is null
. Inside this loop and within a try
block, ask the user to enter a word file name and read it in. Set file
equal to a new Scanner
object created from a new File
object created from this name.
Below the try
block but still within the loop, make a catch
clause to catch exceptions of type FileNotFoundException
. Within this clause, print out File not found!
In this way, the user will be given an error message if he or she tries to open a file that doesn't exist. The loop will keep running until file
points at a real (non-null
) Scanner
object.
Reading the words
Now that the file is open, ask the user to enter the length of the word and store it into an integer variable named length
.
We haven't discussed them in class, but an ArrayList
is a Java class that allows us to store an arbitrarily large number of items. Declare and instantiate a new ArrayList
to hold words as follows.
ArrayList<String> words = new ArrayList<String>();
Then, loop as long as there's another line in the file (using the hasNextLine()
method in your Scanner
object). Read the line (using the nextLine()
method) and convert this input to upper case. If the length of this line (a word) is length
(the length the user chose), add it to the ArrayList
as follows.
words.add(word);
Close the file.
Setting up the game
Now, we have all the words of the appropriate length stored in the list called words
. We want to select a random word from this list. Create a Random
object. Call its nextInt()
method with words.size()
as its parameter. Doing so will generate a random integer between 0 and words.size() - 1
. Call this number choice
. To get the word from the list at that location, use the following code.
String target = words.get(choice);
There are a few other variables we need to set up to make the game work. Refer to the following table.
Type | Variable | Initial Value | Meaning |
---|---|---|---|
int |
chances |
6 |
Remaining incorrect guesses allowed |
boolean |
found |
false |
Remaining incorrect guesses allowed |
char[] |
letters |
An array of length length (the length of target ). Use a for loop to fill it initially with underscore ('_' ) characters. |
A display for the letters in the word that are currently known |
boolean[] |
used |
An array of length 26. Simply creating an array of 26 boolean values automatically initializes them all to false , which we want. |
Which letters have been guessed |
Playing the game
With all the variables set up properly, the code to play the game should follow the pattern below. Be sure to refer to the Sample Output below, and try to match the output as closely as possible.
- While
chances
is greater than zero andfound
is not true: - Print out
Letters:
- Loop through all the characters in
letters
and print them out with a space afterward - Print a newline
- Print the number of chances that remain
- Ask the user to enter a guess
- Read in a
String
, convert it to upper case, and store the first character in it into a variable calledletter
- Subtract
'A'
from this letter to get an index - If the index in
used
is true:- Print
Letter already used!
- Print
- Otherwise:
- Mark the index in
used
true - If
target.indexOf(letter)
is not -1 (the letter must be intarget
): - Print
Correct guess!
- Set an integer variable
underscores
to 0 - Loop over all the indexes in
letters
:- If the character at the corresponding index in
target
matchesletter
: - Set
letters
at that index toletter
- Otherwise, if the character at the corresponding index in
letters
is still an underscore ('_'
): - Increment
underscores
by one
- If the character at the corresponding index in
- If after the loop,
undercores
is still zero: - There must have been no underscores left, so all letters were found, so set
found
to true - Otherwise,
letter
is not intarget
: - Print
Incorrect guess!
- Decrement
chances
by one
Winning and losing
There are two possible ways to leave the loop (other than bad input crashing the program, which you're not required to deal with in this lab): Either the user ran out of chances or found the word.
If found
is true, the user found the word, so print out You win!
Otherwise, print You lose!
Either way, print out what target
was.
Sample Output
Below is sample output from the program. The user selects a dictionary file called dict2.txt
and picks a word length of 8. The user makes a number of guesses and is barely able to identify the word "BREAKAGE."
Enter word file name: dict2.txt Enter length of word: 8 Letters: _ _ _ _ _ _ _ _ 6 chances remain. Enter guess: E Correct guess! Letters: _ _ E _ _ _ _ E 6 chances remain. Enter guess: T Incorrect guess! Letters: _ _ E _ _ _ _ E 5 chances remain. Enter guess: A Correct guess! Letters: _ _ E A _ A _ E 5 chances remain. Enter guess: N Incorrect guess! Letters: _ _ E A _ A _ E 4 chances remain. Enter guess: S Incorrect guess! Letters: _ _ E A _ A _ E 3 chances remain. Enter guess: H Incorrect guess! Letters: _ _ E A _ A _ E 2 chances remain. Enter guess: R Correct guess! Letters: _ R E A _ A _ E 2 chances remain. Enter guess: C Incorrect guess! Letters: _ R E A _ A _ E 1 chances remain. Enter guess: B Correct guess! Letters: B R E A _ A _ E 1 chances remain. Enter guess: K Correct guess! Letters: B R E A K A _ E 1 chances remain. Enter guess: G Correct guess! You win! The word was "BREAKAGE."
In this second sample output, the user selects the same dictionary file dict2.txt
but picks a word length of 4. The user makes a number of guesses but fails to identify the word "BOAR" before using up six incorrect guesses.
Enter word file name: dict2.txt Enter length of word: 4 Letters: _ _ _ _ 6 chances remain. Enter guess: E Incorrect guess! Letters: _ _ _ _ 5 chances remain. Enter guess: T Incorrect guess! Letters: _ _ _ _ 4 chances remain. Enter guess: A Correct guess! Letters: _ _ A _ 4 chances remain. Enter guess: O Correct guess! Letters: _ O A _ 4 chances remain. Enter guess: N Incorrect guess! Letters: _ O A _ 3 chances remain. Enter guess: S Incorrect guess! Letters: _ O A _ 2 chances remain. Enter guess: H Incorrect guess! Letters: _ O A _ 1 chances remain. Enter guess: R Correct guess! Letters: _ O A R 1 chances remain. Enter guess: D Incorrect guess! You lose! The word was "BOAR."
Turn In
Turn in your code by uploading Hangman.java
from the Lab8\src
folder inside your workspace folder to Blackboard. Do not upload the entire project. I only want the Hangman.java
file.
All work must be done individually. Never look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.