UMBC CMSC201, Computer Science I, Spring '97
Project 4: Hangman
Due: Thursday, April 24, 1997
Your assignment is to write a program that allows the user to play the game
Hangman. This will give you practice using graphics, strings, random numbers,
and separate compilation.
Description of Hangman
The player plays against the computer.
The computer randomly selects a word, and the player tries to guess
what the word is. As the player makes mistakes, a picture is drawn.
The game is called hangman because of this drawing.
The drawing starts out as an empty gallows.
Each time the player makes a mistaken guess,
a body part is added to the drawing:
On the first miss, a head is drawn, on the second miss a torso is drawn,
and so on.
If the player guesses the word before he is hanged (has all of his body parts),
he wins.
At each turn, the player guesses either a letter or the whole word.
Suppose it's a letter:
If the chosen letter is in the word,
all occurrences of that letter are displayed and
the player is given another chance to guess a letter.
If the letter does not appear in the word,
then the guess misses and a body part is drawn as described above.
The player can choose to guess the whole word instead of guessing a
letter. If the player guesses the word correctly, he wins. If not the game
can continue, but a new body part will be drawn, because the word was missed.
There are eight body parts: head, torso, leftArm, rightArm, leftLeg, rightLeg,
leftFoot and RightFoot.
With each miss,
these body parts should be added to the drawing in the order shown.
A Sample Run with a Description of the Drawing
Welcome to Hangman
(The drawing of the gallows is displayed in the graphics window)
_ _ _ _ _ _
Letters guessed:
Please enter a letter: (Player types an e)
_ _ _ _ _ e
Letters guessed: e
Please enter a letter: (Player types a t)
_ _ _ _ _ e
Letters guessed: e t
(The head is drawn, because the letter t wasn't in the word)
Please enter a letter: (Player types an a)
_ _ a _ _ e
Letters guessed: e t a
Please enter a letter: (Player types an r)
_ r a _ _ e
Letters guessed: e t a r
Please enter a letter: (Player types a c)
_ r a n _ e
Letters guessed: e t a r c
(The torso is drawn, because the letter c wasn't in the word)
Please enter a letter: (Player types a g)
_ r a n g e
Letters guessed: e t a r c g
Please enter a letter: (Player enters an o)
o r a n g e
Correct. The word was orange.
Congratulations ! You have lived to play another day.
More details
The word to be used for a new game must be chosen randomly from the 10 words
shown below.
strong quaint garage apples budget
answer matter dollar pursue please
For each turn, you should do the following:
- Display the known parts of the word.
Initially, none of the letters are known, so you should
display one underscore for each letter of the word, with a
space between each of the underscores.
As the game progresses, if letters are correctly guessed,
then they should be displayed instead of their underscores.
- Display the letters guessed so far.
Leave a blank line, and on the next line
display a list of letters (correct or incorrect)
that the player has already chosen. This list of letters should be in the
order that the player entered them.
- Prompt the user for a guess and read it in.
You may assume that the guess does not have spaces before it,
and you may assume that the guess is a legal letter or word.
You do not have to complain if the guess repeats an earlier guess.
- If the guess was for the whole word and the guess is correct,
or if the guess was for a letter, the guess is correct, and the
guess completes the word,
then print the message:
"Congratulations! You have lived to play another day."
- If the guess is a letter and the letter is correct,
then on the next turn all occurrences of the letter in the
word should be displayed in their correct positions.
(Underscores should continue to show the positions of the unknown letters.)
The newly guessed letter should be added to
the list of guessed letters.
- If the guess misses,
draw the next body part of the hanged man drawing.
If this completes the drawing,
print out the word to let the player know what it was and print the message:
"Sorry, game over."
What to Turn In
You must use separate compilation
for this project and should have one file, called drawing.c, that
contains all of the drawing functions that you write. The file
drawing.c will have it's own header file that contains the prototypes
for the functions defined in drawing.c. That header file will be
named drawing.h.
Your program will consist of at least three files, including
drawing.c, drawing.h, and your main program, hangman.c.
To submit you will use the submit command, but you must list all
of the files, like this:
submit cs201 proj4 hangman.c graphics.c graphics.h
The order in which the files are listed doesn't matter. However, you
must make sure that all files necessary to compile your
project are listed.
Honors Project or Extra Credit
Allow the user to increase the difficulty of the game by choosing to
guess words up to ten letters long. You will need to add 10
seven-letter words, 10 eight-letter words, ... , 10 ten-letter words
to your program. The user will get to choose the length of the word,
but the current word will still be chosen at random from the
appropriate list of ten words.