CS21 Lab 7: Top-Down Design: hangman program

Due 11:59pm Tuesday night, Oct. 28

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 at the top of your program. Only one partner needs to run handin21 to submit the files for the group. Both partners will receive the same grade.

Run update21, if you haven't already, to create the cs21/labs/07 directory. Then cd into your cs21/labs/07 directory and create the python programs for lab 7 in this directory (handin21 looks for your lab 7 assignments in your cs21/labs/07 directory):

$ update21
$ cd cs21/labs/07
In the 07 directory is a very large word file (wordfile.txt). You may want to start testing with a much smaller word file, and then try this one later.

If you want to start with your Lab 6 solution, copy it from your lab 06 subdirectory to a file named hangman.py in your lab 07 subdirectory (I'd recommend doing this rather than starting with an empty hangman.py file):

$ cp ~/cs21/labs/06/guess.py hangman.py
$ vi hangman.py

To get my lab 6 solution: After the due date for lab 6, re-run update21 and in your labs/07 directory will be a file lab6soln.py that contains my solution to lab 6. If you didn't solve lab 6, or even if you did, you can use my solution as a starting point for your hangman program, but make sure to read through my code and understand what it is doing.

Introduction

For this assignment, you will write a program that implements the hangman game (you can use the "apples falling from the tree" alternative if you want). The focus of this assignment is for you to practice using top-down design to construct your solution incrementally. You should make use of your programs from Lab 6 in this assignment.

Hangman is a simple letter guessing game. The computer picks a word (from a file) and displays it with underscores replacing the letters. The user tries to figure out the word by guessing one letter at a time. The user loses if they guess too many incorrect letters.


Example

Here are two examples of how your hangman program should work.


$ python hangman.py

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
This program plays a game of hangman.
Guess letters in the mystery word.
You can only make 8 incorrect guesses before you lose.
See if you can guess the word before you run out of guesses...
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

word:  -  -  -  -  -  -  -  - 
incorrect guesses left: 8

Enter a letter: a
 good guess, 'a' is in the word.

word:  -  a  -  a  -  -  -  - 
incorrect guesses left: 8

Enter a letter: A
You already guessed 'a', try again...
Enter a letter: b
 sorry there is no 'b' in the word.

word:  -  a  -  a  -  -  -  - 
incorrect guesses left: 7

Enter a letter: c
 sorry there is no 'c' in the word.

word:  -  a  -  a  -  -  -  - 
incorrect guesses left: 6

Enter a letter: d
 sorry there is no 'd' in the word.

word:  -  a  -  a  -  -  -  - 
incorrect guesses left: 5

Enter a letter: e
 good guess, 'e' is in the word.

word:  -  a  -  a  -  -  e  - 
incorrect guesses left: 5

Enter a letter: f
 sorry there is no 'f' in the word.

word:  -  a  -  a  -  -  e  - 
incorrect guesses left: 4

Enter a letter: g
 sorry there is no 'g' in the word.

word:  -  a  -  a  -  -  e  - 
incorrect guesses left: 3

Enter a letter: sdlfjsdlfj
Hey, 'sdlfjsdlfj' isn't an alphabetic character, try again...
Enter a letter: a
You already guessed 'a', try again...
Enter a letter: A
You already guessed 'a', try again...
Enter a letter: b
You already guessed 'b', try again...
Enter a letter: l
 good guess, 'l' is in the word.

word:  -  a  -  a  l  l  e  l 
incorrect guesses left: 3

Enter a letter: p
 good guess, 'p' is in the word.

word:  p  a  -  a  l  l  e  l 
incorrect guesses left: 3

Enter a letter: r
 good guess, 'r' is in the word.


You won!!!  Next time I'll win though
Here is a version with ascii graphics added (adding ascii graphics is not required, but is one extra feature you can add if you have time). Also, in this example, the player doesn't win, so note what the program prints out at the end:
$ python hangman.py

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
This program plays a game of hangman.
Guess letters in the mystery word.
You can only make 8 incorrect guesses before you lose.
See if you can guess the word before you run out of guesses...
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

word:  -  -  -  -  -  -  -  -  -  -  -  - 
incorrect guesses left: 8

Enter a letter: a
 good guess, 'a' is in the word.

      -------
    |/      |
    |
    |
    |
    | 
    | 
____|____

word:  -  -  -  a  -  -  -  -  -  -  -  - 
incorrect guesses left: 8

Enter a letter: e
 good guess, 'e' is in the word.

      -------
    |/      |
    |
    |
    |
    | 
    | 
____|____

word:  -  -  -  a  -  -  e  -  -  e  -  - 
incorrect guesses left: 8

Enter a letter: i
 good guess, 'i' is in the word.

      -------
    |/      |
    |
    |
    |
    | 
    | 
____|____

word:  i  -  -  a  -  -  e  -  -  e  -  - 
incorrect guesses left: 8

Enter a letter: o
 sorry there is no 'o' in the word.

      -------
    |/      |
    |      (_)
    |
    |
    | 
    | 
____|____

word:  i  -  -  a  -  -  e  -  -  e  -  - 
incorrect guesses left: 7

Enter a letter: m
 sorry there is no 'm' in the word.

      -------
    |/      |
    |      (_)
    |       |
    |       |
    | 
    | 
____|____

word:  i  -  -  a  -  -  e  -  -  e  -  - 
incorrect guesses left: 6

Enter a letter: u
 sorry there is no 'u' in the word.

      -------
    |/      |
    |      (_)
    |      \|
    |       |
    | 
    | 
____|____

word:  i  -  -  a  -  -  e  -  -  e  -  - 
incorrect guesses left: 5

Enter a letter: n
 good guess, 'n' is in the word.

      -------
    |/      |
    |      (_)
    |      \|
    |       |
    | 
    | 
____|____

word:  i  n  -  a  n  -  e  -  -  e  n  - 
incorrect guesses left: 5

Enter a letter: g
 sorry there is no 'g' in the word.

      -------
    |/      |
    |      (_)
    |      \|/
    |       |
    | 
    | 
____|____

word:  i  n  -  a  n  -  e  -  -  e  n  - 
incorrect guesses left: 4

Enter a letter: t
 good guess, 't' is in the word.

      -------
    |/      |
    |      (_)
    |      \|/
    |       |
    | 
    | 
____|____

word:  i  n  -  a  n  -  e  -  -  e  n  t 
incorrect guesses left: 4

Enter a letter: m
You already guessed 'm', try again...
Enter a letter: l
 sorry there is no 'l' in the word.

      -------
    |/      |
    |      (_)
    |      \|/
    |       |
    |      / 
    | 
____|____

word:  i  n  -  a  n  -  e  -  -  e  n  t 
incorrect guesses left: 3

Enter a letter: p
 sorry there is no 'p' in the word.

      -------
    |/      |
    |      (_)
    |      \|/
    |       |
    |      / \ 
    | 
____|____

word:  i  n  -  a  n  -  e  -  -  e  n  t 
incorrect guesses left: 2

Enter a letter: r
 sorry there is no 'r' in the word.

      -------
    |/      |
    |      (_)
    |      \|/
    |       |
    |     _/ \ 
    | 
____|____

word:  i  n  -  a  n  -  e  -  -  e  n  t 
incorrect guesses left: 1

Enter a letter: f
 sorry there is no 'f' in the word.

      -------
    |/      |
    |      (_)
    |      \|/
    |       |
    |     _/ \_
    | 
____|____


Sorry, you didn't win this time.  The word was:  incandescent
Requirements
Helpful tips
Extensions

Here are some fun extensions that you might want to add to your program:


Submit

Once you are satisfied with your program, hand it in by typing handin21 at the unix prompt. If you work with a partner on this assignment, only one of you needs to run handin21.