Homework 4
The game of Hangman

Due: Tuesday, November 23, 2010 at 5pm

Outcomes

After successfully completing this assignment, you will be able to...

Before Starting

Read Chapters 7 and 8.

The Assignment

In this modified game of Hangman, the computer chooses a secret word and the player must guess letters in the word. The secret word is displayed as a series of *'s (the number of *'s displayed indicates the number of letters in the word). Each time the player guesses a letter in the word, the corresponding *'s are replaced with the letters correctly guessed so far. The game ends when the player correctly guesses the entire word (player wins) or when the player uses up all of his turns (player loses). The player will be allowed a maximum of 7 incorrect guesses.

Include files

Sample Execution


                      Welcome to HANGMAN

You will be asked to guess the computer's secret word.
The word will be displayed as a number of *'s.  Every time
you guess a letter correctly, that letter will be shown in its
correct position in the word.  If you guess incorrectly, the
number of tries you have left will be decremented.  You will be
given a maximum of 7 incorrect guesses.


******
7 turns left
Enter a letter:  t

**tt**
7 turns left
Enter a letter:  a

**tt**
6 turns left
Enter a letter:  s

**tt**
5 turns left
Enter a letter:  e

**tt*e
5 turns left
Enter a letter:  l

**ttle
5 turns left
Enter a letter:  o

*ottle
5 turns left
Enter a letter:  b

Congratulations!
You guessed the secret word:  bottle.

Another Sample Execution


                      Welcome to HANGMAN

You will be asked to guess the computer's secret word.
The word will be displayed as a number of *'s.  Every time
you guess a letter correctly, that letter will be shown in its
correct position in the word.  If you guess incorrectly, the
number of tries you have left will be decremented.  You will be
given a maximum of 7 incorrect guesses.


**********
7 turns left
Enter a letter:  e

****e*****
7 turns left
Enter a letter:  r

****e*****
6 turns left
Enter a letter:  v

****e*****
5 turns left
Enter a letter:  s

**s*e*****
5 turns left
Enter a letter:  t

**s*et****
5 turns left
Enter a letter:  d

**s*et****
4 turns left
Enter a letter:  i

**s*et****
3 turns left
Enter a letter:  m

**s*et****
2 turns left
Enter a letter:  o

**s*et****
1 turn left
Enter a letter:  p

Sorry, no more turns left.  The secret word was basketball.


Design Assumptions and Restrictions

You must use functions from the C string library wherever possible (if you write your own code where a string function could accomplish the same thing, you will lose points). See sections 8.6 - 8.8 in the textbook for a description of the functions in the string library.

You must initialize an array of strings (see section 7.10) to hold the computer's dictionary of secret words. The dictionary must contain at least 20 words of varying lengths. Use lower-case letters only for the words in the dictionary. The secret word will be chosen randomly from the dictionary every time a game of hangman is played.

Your program should contain at least one function that you have defined and designed yourself. (For example, the part of the program that reads in the user's next guess could be made into a function. Or the part of the program that updates the asterisks in the user's word so that the word contains all occurrences of the most recently-guessed letter could be a function.) Remember that one of the purposes of breaking a program up into functions is to make the main program more understandable. Whichever parts of your program you choose to implement as functions, you should provide pre- and post-conditions for your functions, and choose parameters, return types, and local variables appropriately.

Your program should behave as shown in the Sample Executions.

Deliverables

Write a short text file called README that summarizes your program, how to run it, and detailing any assumptions and any problems you had. From your Linux account, submit your C source code file and your README file using the following turnin command:

/cs/bin/turnin submit cs2301 PROJECT4 hw4.c README

Programs submitted after 5pm on November 23 will be tagged as late, and will be subject to the late homework policy.

Grading

This assignment will be graded on the following areas: documentation (including pre- and post-conditions for any user-defined functions), appropriate use of functions from the string library, use of pointers, and correctness. Programs must compile successfully in order to receive points for correctness.

Here is the grade sheet that the graders will use for this assignment.