CS21 Spring 2005
Homework 11
Due Sunday, April 17 before 11:30pm


Overview of the Assignment

For this assignment, you are to write a program that plays the game of Hangman. The assignment serves two purposes. First, it is designed to give you practice with strings, pointers and file I/O. Second, it gives you a chance to write a program that is larger than those you have written for most previous assignments. Large programs require more up-front organization than small programs, and it is important that you begin to get experience with larger programs at this point in the course.

Because of the additional organizational work and the fact that there are more details to manage, you are strongly encourage to work in teams of two for this assignment.

Look on pages 519-521 of the book (Chapter 14 Programming Exercise #12) for a more complete description of the Hangman game. For your version, you will use phrases rather than single words for the user to guess.

There are two important requirements for this assignment:

Grading

Part of your grade on this program will be style. To receive an A, your program must have good modular design and good use of data structures. Also, your code should be efficient and well commented.

For extra credit:

Use the graphics library to draw an evolving picture of the game situation, as shown on page 521. Or implement a version that uses ascii graphics (example output from the ascii graphics version can be seen on this page).

If you add graphics, you should design and test your program in two parts. First, get the interactive part working without any graphics at all. Once that part is done, make a copy of it, and then add code to draw the Hangman picture using the graphics library functions from Chapter 7.

The Basic Hangman Game

Write a program that implements the hangman game without graphics. To solve the problem, your program must be able to: Your Hangman program should implement the following features:

The Graphics Version (Extra Credit)

For extra credit, your task is to extend the program you have already written so that it now keeps track of the Hangman graphical display. Everything you need to draw a simple picture is available in the graphics library, or you can use ascii graphics to implement this. Your task is simply to determine the best way to integrate the new part of the program into the existing structure. The complete picture used in the game appears on page 521 in your textbook. The scaffold is drawn initially before the game begins, and then the parts are added in the following order: head, body, left arm, right arm, left leg, right leg, left foot, right foot.

Sample Run of the Program (notice error handling)

Here are three example runs of the program (1) the user wins, (2) the user loses, (3) the user quits the game early.
==============================
run 1: the user wins the game
==============================
% ./a.out
Let's play hangman!  I will pick a secret phrase
On each turn, you guess a letter.  If the letter you 
guess is in the secret phrase, I will show you where it appears.
You only get 8 incorrect guesses.  Good luck.

Please enter the name of the words file: blah
Could not open file blah, try again
Please enter the name of the words file: dict.txt

Secret phrase: -   - - - - - - -   - - - - -   - - - - - - -   - -   - - - - 
You have 8 guesses left
Letters guessed: 
Your guess (enter ! to quit): a
That guess is correct.

Secret phrase: A   - - - - - - -   - - - - -   - A - - - - -   - -   - - - - 
You have 8 guesses left
Letters guessed: A, 
Your guess (enter ! to quit): b
There are no B's in the phrase

Secret phrase: A   - - - - - - -   - - - - -   - A - - - - -   - -   - - - - 
You have 7 guesses left
Letters guessed: A, B, 
Your guess (enter ! to quit): c
There are no C's in the phrase

Secret phrase: A   - - - - - - -   - - - - -   - A - - - - -   - -   - - - - 
You have 6 guesses left
Letters guessed: A, B, C, 
Your guess (enter ! to quit): d
There are no D's in the phrase

Secret phrase: A   - - - - - - -   - - - - -   - A - - - - -   - -   - - - - 
You have 5 guesses left
Letters guessed: A, B, C, D, 
Your guess (enter ! to quit): e
That guess is correct.

Secret phrase: A   - - - - - - -   - - - - E   - A - - E - -   - -   - - - - 
You have 5 guesses left
Letters guessed: A, B, C, D, E, 
Your guess (enter ! to quit): f
There are no F's in the phrase

Secret phrase: A   - - - - - - -   - - - - E   - A - - E - -   - -   - - - - 
You have 4 guesses left
Letters guessed: A, B, C, D, E, F, 
Your guess (enter ! to quit): .
.'s not an alphabetic character, try again
Your guess (enter ! to quit): a
You already guessed A, try again.
Your guess (enter ! to quit): F
You already guessed F, try again.
Your guess (enter ! to quit): r
That guess is correct.

Secret phrase: A   R - - - - - -   - - - - E   - A - - E R -   - -   - - - - 
You have 4 guesses left
Letters guessed: A, B, C, D, E, F, R, 
Your guess (enter ! to quit): o
That guess is correct.

Secret phrase: A   R O - - - - -   - - O - E   - A - - E R -   - O   - O - - 
You have 4 guesses left
Letters guessed: A, B, C, D, E, F, O, R, 
Your guess (enter ! to quit): l
That guess is correct.

Secret phrase: A   R O L L - - -   - - O - E   - A - - E R -   - O   - O - - 
You have 4 guesses left
Letters guessed: A, B, C, D, E, F, L, O, R, 
Your guess (enter ! to quit): i
That guess is correct.

Secret phrase: A   R O L L I - -   - - O - E   - A - - E R -   - O   - O - - 
You have 4 guesses left
Letters guessed: A, B, C, D, E, F, I, L, O, R, 
Your guess (enter ! to quit): n
That guess is correct.

Secret phrase: A   R O L L I N -   - - O N E   - A - - E R -   N O   - O - - 
You have 4 guesses left
Letters guessed: A, B, C, D, E, F, I, L, N, O, R, 
Your guess (enter ! to quit): g
That guess is correct.

Secret phrase: A   R O L L I N G   - - O N E   G A - - E R -   N O   - O - - 
You have 4 guesses left
Letters guessed: A, B, C, D, E, F, G, I, L, N, O, R, 
Your guess (enter ! to quit): s
That guess is correct.

Secret phrase: A   R O L L I N G   S - O N E   G A - - E R S   N O   - O S S 
You have 4 guesses left
Letters guessed: A, B, C, D, E, F, G, I, L, N, O, R, S, 
Your guess (enter ! to quit): t
That guess is correct.

Secret phrase: A   R O L L I N G   S T O N E   G A T - E R S   N O   - O S S 
You have 4 guesses left
Letters guessed: A, B, C, D, E, F, G, I, L, N, O, R, S, T, 
Your guess (enter ! to quit): h
That guess is correct.

Secret phrase: A   R O L L I N G   S T O N E   G A T H E R S   N O   - O S S 
You have 4 guesses left
Letters guessed: A, B, C, D, E, F, G, H, I, L, N, O, R, S, T, 
Your guess (enter ! to quit): m
That guess is correct.

You guessed the phrase A ROLLING STONE GATHERS NO MOSS.
You Win!!!
You only had 4 wrong guesses

==============================
run 2: the user loses the game
==============================
% ./a.out

Let's play hangman!  I will pick a secret phrase
On each turn, you guess a letter.  If the letter you 
guess is in the secret phrase, I will show you where it appears.
You only get 8 incorrect guesses.  Good luck.

Please enter the name of the words file: dict.txt

Secret phrase: -   - - - - - - -   - - - - -   - - - - - - -   - -   - - - - 
You have 8 guesses left
Letters guessed: 
Your guess (enter ! to quit): a 
That guess is correct.

Secret phrase: A   - - - - - - -   - - - - -   - A - - - - -   - -   - - - - 
You have 8 guesses left
Letters guessed: A, 
Your guess (enter ! to quit): r
That guess is correct.

Secret phrase: A   R - - - - - -   - - - - -   - A - - - R -   - -   - - - - 
You have 8 guesses left
Letters guessed: A, R, 
Your guess (enter ! to quit): o
That guess is correct.

Secret phrase: A   R O - - - - -   - - O - -   - A - - - R -   - O   - O - - 
You have 8 guesses left
Letters guessed: A, O, R, 
Your guess (enter ! to quit): l
That guess is correct.

Secret phrase: A   R O L L - - -   - - O - -   - A - - - R -   - O   - O - - 
You have 8 guesses left
Letters guessed: A, L, O, R, 
Your guess (enter ! to quit): x
There are no X's in the phrase

Secret phrase: A   R O L L - - -   - - O - -   - A - - - R -   - O   - O - - 
You have 7 guesses left
Letters guessed: A, L, O, R, X, 
Your guess (enter ! to quit): z
There are no Z's in the phrase

Secret phrase: A   R O L L - - -   - - O - -   - A - - - R -   - O   - O - - 
You have 6 guesses left
Letters guessed: A, L, O, R, X, Z
Your guess (enter ! to quit): y
There are no Y's in the phrase

Secret phrase: A   R O L L - - -   - - O - -   - A - - - R -   - O   - O - - 
You have 5 guesses left
Letters guessed: A, L, O, R, X, Y, Z
Your guess (enter ! to quit): p
There are no P's in the phrase

Secret phrase: A   R O L L - - -   - - O - -   - A - - - R -   - O   - O - - 
You have 4 guesses left
Letters guessed: A, L, O, P, R, X, Y, Z
Your guess (enter ! to quit): q
There are no Q's in the phrase

Secret phrase: A   R O L L - - -   - - O - -   - A - - - R -   - O   - O - - 
You have 3 guesses left
Letters guessed: A, L, O, P, Q, R, X, Y, Z
Your guess (enter ! to quit): r
You already guessed R, try again.
Your guess (enter ! to quit): s
That guess is correct.

Secret phrase: A   R O L L - - -   S - O - -   - A - - - R S   - O   - O S S 
You have 3 guesses left
Letters guessed: A, L, O, P, Q, R, S, X, Y, Z
Your guess (enter ! to quit): m
That guess is correct.

Secret phrase: A   R O L L - - -   S - O - -   - A - - - R S   - O   M O S S 
You have 3 guesses left
Letters guessed: A, L, M, O, P, Q, R, S, X, Y, Z
Your guess (enter ! to quit): n
That guess is correct.

Secret phrase: A   R O L L - N -   S - O N -   - A - - - R S   N O   M O S S 
You have 3 guesses left
Letters guessed: A, L, M, N, O, P, Q, R, S, X, Y, Z
Your guess (enter ! to quit): w
There are no W's in the phrase

Secret phrase: A   R O L L - N -   S - O N -   - A - - - R S   N O   M O S S 
You have 2 guesses left
Letters guessed: A, L, M, N, O, P, Q, R, S, W, X, Y, Z
Your guess (enter ! to quit): u
There are no U's in the phrase

Secret phrase: A   R O L L - N -   S - O N -   - A - - - R S   N O   M O S S 
You have 1 guesses left
Letters guessed: A, L, M, N, O, P, Q, R, S, U, W, X, Y, Z
Your guess (enter ! to quit): i
That guess is correct.

Secret phrase: A   R O L L I N -   S - O N -   - A - - - R S   N O   M O S S 
You have 1 guesses left
Letters guessed: A, I, L, M, N, O, P, Q, R, S, U, W, X, Y, Z
Your guess (enter ! to quit): b
There are no B's in the phrase

Sorry, you lose.  The phrase was A ROLLING STONE GATHERS NO MOSS.


==============================
run 3: the user quits the game
==============================
% ./a.out


Let's play hangman!  I will pick a secret phrase
On each turn, you guess a letter.  If the letter you 
guess is in the secret phrase, I will show you where it appears.
You only get 8 incorrect guesses.  Good luck.

Please enter the name of the words file: dict.txt

Secret phrase: - - -   - - - -   - - - -   - - - - - - - -   - - - - - - - 
You have 8 guesses left
Letters guessed: 
Your guess (enter ! to quit): o
That guess is correct.

Secret phrase: O - -   - O O -   - - - -   - - - - - - - -   - - O - - - - 
You have 8 guesses left
Letters guessed: O, 
Your guess (enter ! to quit): g
That guess is correct.

Secret phrase: O - -   G O O -   - - - -   - - - - - - - -   - - O - - - - 
You have 8 guesses left
Letters guessed: G, O, 
Your guess (enter ! to quit): !
You quit

Sorry, you lose.  The phrase was ONE GOOD TURN DESERVES ANOTHER.