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
Your programs are graded on both correctness and style. Please review
the comments regarding programming style on
the
main page. In particular,
each function needs a comment.
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.
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.
This assignment will span two weeks. In the first week, you will be required to design your solution and implement minimalistic function stubs for your design. In the second week, you will finish implementing and testing your design.
For the design of a program in the first week, you should have:
- main() completely written
- functions called from main() all stubbed out with parameters,
comment, and a return statement
- clear use of data structures and variables in main() and the stubbed-out functions
- a design that runs without errors (although it doesn't play the game yet)
Here is a simple
example of a top-down design.
Note: Once you have your top-level design
done you need to run
handin21 and
send me an email that you
have completed your top-level design. I will look at your design
and email you with feedback on your design within 24-48 hours. The
earlier you send me your design, the earlier you can continue working
on your lab. I would suggest you wait to hear back from me before
continuing on your program because if I suggest you do something
differently, you may have to redo some of your code.
Examples
Here are two examples of how your hangman program should work.
Example 1
Example 2
Requirements
- The program should get its randomly-chosen word from a file of words (e.g., words.txt)
- Players should be able to enter either upper or lowercase letters
- Tell players if they have already entered a given letter (and don't count it against them)
- Update the display after each letter guessed and tell the players how many incorrect guesses they have left
- Print out a message indicating if the guessed letter was correct or not
- Print out a message at the end of the game indicating if the player won or
not, and if not, print out the word the player was trying to guess
- Write at least one function to get the user's next valid letter guess
(it has to be a single alphabetic character and not a previously guessed letter).
- Start by using Top-Down design to break this problem into smaller steps, and
write several functions (one for each high-level step and perhaps one
for each medium-level step).
Helpful tips
-
In python you can test if a character is in a given string with something like this:
if 'a' in mystring:
...
Use not in to test if a character is not in a string.
-
At some point in your program it might be easier to work with a list of
characters, instead of a string of characters. Here's how you can convert
a string to a list, and then use join to get a string again (I'm running
the python interpreter in interactive mode here):
>>> word = "apple"
>>> charList = list(word)
>>> print charList
['a', 'p', 'p', 'l', 'e']
>>> rejoined = ''.join(charList)
>>> print rejoined
'apple'
- in and not in work on lists too, e.g.,
- Before using the full words.txt, use a simpler file with
with only one word in it. You program will be easier to debug if you
know what the word you are trying to guess is.
word = "apple"
charList = list(word)
print ('a' in charList) #prints True
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:
- give the user a choice or word topics (harry potter, rock bands, sports teams, dog breeds)
- allow answers with more than one word (e.g., PERRY THE PLATYPUS)
- let the user choose the difficulty of the game (easy, medium, hard)
- add some ascii art to the program
________
|/ |
| (_)
| \|/
| |
| _/ \_
|
____|____
- use the graphics library to draw the hangman or tree of apples
- allow user to quit or solve the puzzle at any point in the game
Submit
Once you are satisfied with your programs, hand them in by typing
handin21 at the unix prompt.
You may run handin21 as many times as you like, and only the
most recent submission will be recorded. This is useful if you
realize, after handing in some programs, that you'd like to make a few
more changes to them.