CS21 Lab 7: Top-Down Design: hangman program

Due before 11:59pm Tuesday night, March 23

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.

Using your lab 6 solution as a starting point:
If you successfully completed lab 6, you should use it as a starting point for lab 7. 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

Using our lab 6 solution as a starting point:
If you did not successfully complete lab 6, you can use our solution as a starting point. However, we strongly recommend that you use your own lab06 solution if it works.

To use our solution as a starting point:

  1. Import it. To the top of your program before main:
    from lab06 import *
    
  2. Use the getNewLetter function in your hangman code. This function takes a list of characters (the list contains the characters already guessed), and keeps prompting the user to enter a alphabetic character until s/he enters a valid one. A valid alphabetic character is one that has not already been guessed (i.e. is not in the passed list). The function will add the newly guessed letter to the guessed list and return the guessed letter to the caller. A call to this function might look like:
    guessed_letters = []
    ...
    ch = getNewLetter(guessed_letters)
    

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

Extentions are not a required part of lab 7, but you may want to try adding some once you get the required part implemented and tested.

Note: if you implement one or more extra features, include in the comment at the top of your file a description of the extra feature(s) you added, and tell us how to test your feature if it is like one of the first three. If we need any special input files to test your extra feature, you should include those in your lab07 directory when you submit your solution.

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

  1. give the user a choice or word topics (harry potter, tv cartoon characters, rock bands)
  2. allow answers with more than one word (e.g., PERRY THE PLATYPUS)
  3. let the user choose the difficulty of the game (easy, medium, hard)
  4. add some ascii art to the program
         ________
         |/      |
         |      (_)
         |      \|/
         |       |
         |     _/ \_
         |
     ____|____
    
  5. use the graphics library to draw the hangman or tree of apples
  6. allow user to quit or solve the puzzle at any point in the game

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.