Week 8: More Top Down Design (TDD)
Week 8 Goals
-
Continue to practice Top Down Design
-
Learn how to write functions stubs
-
Learn how to implement a design bottom-up
-
Learn to design and test functions incrementally
Week 8 Code
-
design_wheel.py
: designing a wheel of fortune program -
/data/cs21/wheel/puzles.csv
: csv file with many wheel of fortune puzzles
Top Down Design
We will continue the TDD discussion from last week.
Let’s look at a new example: designing a (simplified) Wheel of Fortune game.
The game begins by randomly choosing a puzzle and a category from a list of puzzles.
The player starts with a score of 0
At each turn, the program shows the status of the puzzle with underscores for unrevealed letters, capital letters for revealed letters, and any punctuation that is part of the puzzle. The program also shows the category of the puzzle and the player’s current score.
The player can take one of three actions:
-
Spin: spin the wheel and guess a letter
-
If the wheel lands on 0, the player loses all points.
-
If the letter is in the puzzle, the player gains points equal to the value of the wheel times the number of times the letter appears in the puzzle.
-
If the letter is not in the puzzle, play continues.
-
The player can only guess a letter that has not been guessed before.
-
-
Solve: guess the puzzle
-
If the guess is correct, the player wins the game with the current point total.
-
If the guess is incorrect, the game continues.
-
-
Quit: quit the game
-
Show the player the puzzle and category and exit the game with 0 points.
-
The game ends when the player solves the puzzle, or quits the game.
Data File
The file /data/cs21/wheel/puzzles.csv
contains a list of puzzles in the following format: PUZZLE,category. We can use the head
command to see the first few lines of the file:
$ head /data/cs21/wheel/puzzles.csv
CLASSROOM RULES,Things
TABLECLOTH & NAPKINS,Around the House
A LEGEND IN THE MAKING,Phrase
RAISE YOUR HAND PUPPET,Before & After
LAGOONS BAYS AND REEFS,Places
CHARLOTTE'S WEB,Title
VALIDATION,Thing
LET'S BRAINSTORM,Phrase
A SENSE OF HUMOR,Thing
ABSENCE MAKES THE HEART GROW FONDER,Quotation
To see how the program might work, let’s use the example puzzle of "CHARLOTTE’S WEB,Title"
When the game starts, the player sees a blank board with a score of 0:
Wheel of Fortune!
_________'_ ___
Category: Title Points: 0
Let’s spin and guess some letters.
Enter your choice (spin, solve, quit): spin
You spun 900
Enter a letter: T
There are 2 T's in the puzzle
______TT_'_ ___
Category: Title Points: 1800
Enter your choice (spin, solve, quit): spin
You spun 900
Enter a letter: S
There are 1 S's in the puzzle
______TT_'S ___
Category: Title Points: 2700
Enter your choice (spin, solve, quit): spin
You spun 100
Enter a letter: E
There are 2 E's in the puzzle
______TTE'S _E_
Category: Title Points: 2900
Enter your choice (spin, solve, quit): spin
You spun 700
Enter a letter: E
Letter has already been guessed
Invalid guess. Please try again.
Enter a letter: T
Letter has already been guessed
Invalid guess. Please try again.
Enter a letter: !
Letter must be a letter
Invalid guess. Please try again.
Enter a letter: Z
Sorry, Z is not in the puzzle.
______TTE'S _E_
Category: Title Points: 2900
Enter your choice (spin, solve, quit): spin
You spun 400
Enter a letter: C
There are 1 C's in the puzzle
C_____TTE'S _E_
Category: Title Points: 3300
Enter your choice (spin, solve, quit): solve
Enter your guess: charlotte's web
Congratulations! You solved the puzzle!
Your final score is 3300
Design
Think about how you would design the game:
-
What variables/state do you need to maintain in main?
-
What are the high-level steps of the game?
-
What helper functions can aid in implementing the high-level steps?
-
For each helper function:
-
What are the input parameters/types?
-
What are the return values/types (if any)?
-
What are the side effects?
-