CS21 Lab 7: Top-Down Design

Design is due Saturday, November 3, before midnight

Full implementation due Saturday, November 10, before midnight


Please read through the entire lab before starting! Also, as always, run update21 to initialize your cs21/labs/07 directory. You should create your programs for lab 7 in this directory. Note that this is a two-part lab, split over two weeks.

You have two weeks to complete the full program. The first week is for the design, and the second for the full game. Your initial top-down design is due this Saturday (Nov 3) and the full implementation the following week (Nov 10). It is highly recommended that you submit your top-down design a few days before the due date so that we have time to give you feedback, before you start your full game implementation. If you submit your design on Sat, Nov 3, at midnight, it might take us a few days get to it and send you comments on your design.

See below for the design requirements.

Word Warp

Write a game called Word Warp! This is a word game where the user is given 6 letters and has to make as many 4, 5, and 6-letter words as possible from the given letters. For example, given the letters BTALTE, you can make ‘tablet’, ‘belt’, ‘able’, and so on…

Examples, Requirements, and Tips

Here are a few examples of the running program, to help you see how things should work. Pay attention to how the game proceeds, and how input from the user is handled.

Example 1 is reproduced below. Please look at all the examples before starting.

$ python3 ww.py

Welcome to Word Warp v0.1!

Find as many words as you can from the given letters.
Only words of 4 or more letters count!
Longer words are worth more points:

    4 letters.....4 points
    5 letters.....5 points
    6 letters.....6 points

Hit the ENTER key if you want to quit.
Enter WW to shuffle the given letters (and lose 5 points).

 Current Score: 0
Possible Words: ['-----', '----', '------', '----', '----']
       Letters: bcbato

Your Word: boat

 Current Score: 4
Possible Words: ['-----', 'boat', '------', '----', '----']
       Letters: bcbato

Your Word: TACO

 Current Score: 8
Possible Words: ['-----', 'boat', '------', '----', 'taco']
       Letters: bcbato

Your Word: coat

 Current Score: 12
Possible Words: ['-----', 'boat', '------', 'coat', 'taco']
       Letters: bcbato

Your Word: ww

 Current Score: 7
Possible Words: ['-----', 'boat', '------', 'coat', 'taco']
       Letters: tcabob

Your Word: bobcat

 Current Score: 13
Possible Words: ['-----', 'boat', 'bobcat', 'coat', 'taco']
       Letters: tcabob

Your Word: abbot
Good job!! You got all of the words!

Game OVER.
Your final score was: 18

You have some freedom in how you want your game to look. Here are our requirements for the game:

Here are a few tips you may find useful…

A summary of methods and functions we used in our solutions. See the sections below for more explanations.

Name Module Example Description
count string num = word.count(“a”) Returns the number of times a appears in word
lower string word = word.lower() Returns a new string with all letters in lower-case
list built-in letters = list(word) Creates a list containing the letters in word
join string word = "".join([‘a’,‘b’,‘s’]) Returns a string from the letters in the passed list
isalpha string flag = word.isalpha() Returns True if a word contains all alphabetic characters. False otherwise
strip string cleanWord = word.strip() Returns a string with preceding and trailing whitespace removed
shuffle random random.shuffle(letters) Shuffles the contents of a list (in place)


To shuffle a python list, use the shuffle() function in the random library. To convert between strings and lists, you can use list() and join():

>>> S = "hello!"
>>> L = list(S)
>>> print(L)
['h', 'e', 'l', 'l', 'o', '!']
>>> "".join(L)

Testing whether a word can be made with random letters

The system word list (/usr/share/dict/words) is a simple text file you can open and read. You can use this file to find all possible words you can make: search through the system word list, one word at a time, and decide if that word can be made with your random letters. Here’s an example testing whether we can make the word “boot” from the letters “bcbato”:

In “boot”, we see the letter “b” first. Because “bcbato” has 2 “b”’s in it, “boot” might work. We now check the next letter: “o”. However, because “o” appears twice in “boot” but only once in “bcbato”, we see that “boot” is impossible. We can stop looking at the remaining letters in “boot” and start testing another word!

Let’s look at another example: “abbot”

In “abbot”, we see the letter “a” first. “bcbato” contains an “a”, so we check the next letter: “b”. “abbot” has 2 “b”’s and so does “bcbato”, so we keep going and check the next letter: “b” again! Comparing again shows that “abbot” and “bcbato” have the same number of “b”’s and so we continue and check “o”. We see “abbot” has 1 “o” and so does “bcbato”, so we keep going and check “t”. Both “abbot” and “bcbato” have 1 “t”. We can see that “abbot” can be made from “bcbato”!

Useful string methods

Make use of the python str methods: strip(), lower(), isalpha(), islower(), count(), etc. Some of the words in /usr/share/dict/words have non-alphabetic characters in them, such as accents and apostrophes. It is fine for this game if you just ignore all non-alphabetic words:

>>> word = "pony"
>>> word.isalpha()
>>> word = "doesn't"
>>> word.isalpha()

Using the method count(), you can get the number of times a letter appears in a word. For example,

>>> word = "lettuce"
>>> word.count("t")
>>> word.count("u")
>>> word.count("x")

Using a small input file for testing

The system word file has more than 100K words! A lot of these will be filtered by isalpha(); however, that will still leave a lot left for testing. What if you want to verify that you’ve tested all the 4-letter words? A good trick is to use a smaller version of the word list to get your initial program running.

To help you, we’ve provided a tiny word file words-small that you can use for testing. It has two potential examples in it, including “bobcat”, the example we used above.

Top-Down Design Requirements

You should complete your top-down design (TDD), submit it, and obtain feedback on it before beginning the full game implementation. Special procedures for this two-week lab:

Here is a simple example of a top-down design.

And here are examples of running different stubbed designs

Your design doesn’t have to match this exactly, but should allow the user to input some choices, and should show the (fake) game progress. In other words, your core game loop should be sketched out. Note that it’s ok for your game loop to be in its own function, rather than main. However, if it is in its own function, you will need to sketch that function in addition to main.

Optional Challenges

Here are a few optional challenges for this game. If you do these (just for fun, no extra points!), please copy your working game to a new file: cp ww.py ww-extra.py

Answer the Questionnaire

After you have turned in the full program, please edit the QUESTIONS-07.txt file in your cs21/labs/07 directory and answer the questions in that file.

Turning in your labs….

For this lab, run handin21 to turn in your design and then send your lecture professor a quick email letting them know you turned in your design. We will take a look at each design and send you comments (usually within a day or two). Once you have our comments, copy the file (cp design-ww.py ww.py), finish implementing the game (in ww.py) and then run handin21 again.