CS21 HW5: Hangman

Due 11:59pm Tuesday, 9 October 2007
You should save your program for this assignment in your cs21/homework/05 directory. A skeleton version of the program will appear in this directory when you run update21 in a terminal window. The program handin21 will only submit files in this directory.

You may work with one partner on this assignment. If you work with a partner, put your name and the name of your partner in the list of authors in your program. Only one partner needs to run handin21 to submit the files for the group. Both partners will receive the same grade.

Introduction
For this assignment you will write a program that plays the game of Hangman. This program will be larger than any of the programs that you have written for previous assignments. Large programs require more up-front organization than small programs. You should design an outline of your program on paper first. Break the problem into smaller pieces that can be handled by functions. Then implement one function at a time, testing each function you write before you move on to the next function. If you carefully plan your solution, you should be able write a full solution in less than 100 lines of code.
Sample runs
In the sample run below, the user was able to successfully guess a secret word selected by the computer. Notice that in each turn the program displays the status of the secret word and the letters guessed so far, and then prompts the user to guess the next letter. Letters can be entered in upper or lower case. Notice that the letters guessed so far are displayed in alphabetic order in upper case. User input is shown in blue text.
Let's play hangman!  I will pick a secret word.
On each turn, you guess a letter.  If the letter is
in the secret word, I will show you where it appears.
You only get 6 incorrect guesses.  Good luck.

Secret word:  - - - - - - -
Letters guessed:
You have 6 wrong guesses left
Your guess: e

Secret word:  - - - - - - -
Letters guessed: E
You have 5 wrong guesses left
Your guess: I

Secret word:  - - - - - I -
Letters guessed: E I
You have 5 wrong guesses left
Your guess: n

Secret word:  - - - - - I N
Letters guessed: E I N
You have 5 wrong guesses left
Your guess: o

Secret word:  - - - - - I N
Letters guessed: E I N O
You have 4 wrong guesses left
Your guess: u

Secret word:  - U - - - I N
Letters guessed: E I N O U
You have 4 wrong guesses left
Your guess: m

Secret word:  - U M - - I N
Letters guessed: E I M N O U
You have 4 wrong guesses left
Your guess: s

Secret word:  - U M - - I N
Letters guessed: E I M N O S U
You have 3 wrong guesses left
Your guess: P

Secret word:  P U M P - I N
Letters guessed: E I M N O P S U
You have 3 wrong guesses left
Your guess: k

Secret word:  P U M P K I N

Congratulations, you guessed the word!
And you only had 3 wrong guesses

In the next sample run of the game the user is not able to guess the secret word. Notice that the program checks if the letter guessed has been guessed before, or if the user has entered an invalid letter. In either of these cases, the input is consider a wrong guess.
Let's play hangman!  I will pick a secret word.
On each turn, you guess a letter.  If the letter is
in the secret word, I will show you where it appears.
You only get 6 incorrect guesses.  Good luck.

Secret word:  - - - - - -
Letters guessed:
You have 6 wrong guesses left
Your guess: sagjkf
Please enter only a single letter

Secret word:  - - - - - -
Letters guessed:
You have 5 wrong guesses left
Your guess: 8
Please enter only a single letter

Secret word:  - - - - - -
Letters guessed:
You have 4 wrong guesses left
Your guess: R

Secret word:  - - R - - -
Letters guessed: R
You have 4 wrong guesses left
Your guess: S

Secret word:  - - R - - -
Letters guessed: R S
You have 3 wrong guesses left
Your guess: T

Secret word:  - - R - T -
Letters guessed: R S T
You have 3 wrong guesses left
Your guess: E

Secret word:  - - R - T E
Letters guessed: E R S T
You have 3 wrong guesses left
Your guess: L

Secret word:  - - R - T E
Letters guessed: E L R S T
You have 2 wrong guesses left
Your guess: E
You already guessed this letter

Secret word:  - - R - T E
Letters guessed: E L R S T
You have 1 wrong guesses left
Your guess: O

Sorry you lost!

Secret word: P I R A T E

Requirements

Submit

Once you are satisfied with your programs, hand them in by typing handin21 in a terminal window.