WEEK07: File I/O, top-down design
---------------------------------------------------------------
F: more top-down design
LINGO/LAB 7:
- design.py due first (email it to me)
- show lingo game (rules, how to play, etc)
TOP-DOWN DESIGN:
- let's try writing another program, using top-down design
- see if we can come up with main and stubbed-out functions
for a wheel-of-fortune game (NOTE: I haven't written this in
a long time, so the design process might be messy!)
WOF GAME:
$ python wof.py
Welcome to WHEEL -- OF -- FORTUNE v1.0
======================================================================
the puzzle so far: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
======================================================================
Your winnings so far: $0
1. Spin the Wheel
2. Buy a Vowel
3. Guess the Puzzle
4. Quit, I can't stand this game anymore!
your choice --> 1
spin amount: $310
letters guessed so far:
enter a consonant guess: c
Yes! There are 3 c's in the puzzle!! :)
======================================================================
the puzzle so far: c _ _ _ _ _ _ _ _ c _ _ _ c _ _ _ _ _ _ _ _ _ _
======================================================================
Your winnings so far: $930
1. Spin the Wheel
2. Buy a Vowel
3. Guess the Puzzle
4. Quit, I can't stand this game anymore!
your choice --> 2
enter a vowel to buy: o
Yes! There is 1 o in the puzzle!! :)
(and so on...)
DESIGN:
print rules of game
pick a phrase/puzzle (read in possibilities from a file??)
set up wheel
initialize winnings = 0 to start game
loop:
print puzzle (dashes for letters that haven't been guessed)
display menu, get user choice
if choice==1:
call a spin() function
elif choice==2:
call a buyvowel() function
and so on...
Q: what does the spin function need?
what all should it do?
what should it return (if anything)?
example:
- spin() needs the phrase, so we can check how many of each letter
guessed are in the answer
- spin() needs the puzzle, so we can update it after a letter is guessed
- spin() needs the wheel, so we can spin it and help determine the winnings
- spin() might need the winnings, so if they spin BANKRUPT, we can return 0
- spin() might need a list of letters guessed so far, so we can tell the
user when they make a bad guess
winnings = spin(phrase, puzzle, wheel, winnings, lettersguessed)
- and with those parameters, spin() could do all of this:
* spin wheel, display spun amount
* ask for letter (make sure it's valid: consonant, not already guessed)
* figure out how many of letter are in puzzle
* calculate winnings
* update the puzzle being displayed
YOUR TURN:
- below is the start of a wof design
- finish the main function and stubbed-out other functions
- don't write the details of each function UNTIL you have main and
the stubbed-out functions running
- NOTE: this runs, even though it doesn't do much yet!
Now I can start working on each function, TESTING each
as I write them...
"""
wof design
J. Knerr
Fall 2012
"""
from random import choice
##############################################
def main():
"""main game loop for wof game"""
printIntro()
phrases = readFromFile("wofphrases.txt")
answer = choice(phrases)
puzzlesofar = convertToDashes(answer)
wheel = [200,500,0,100,1000,50]
winnings = 0
lettersguessed = []
while True:
# print "puzzle: ", puzzlesofar
displayGameStatus(puzzlesofar, winnings)
choice = displayMenuGetChoice()
if choice == 1:
amt = spin(answer, puzzlesofar, wheel, winnings, lettersguessed)
winnings = winnings + amt
elif choice == 2:
if winnings >= 250:
buyVowel(answer, puzzlesofar, lettersguessed)
winnings = winnings - 250
else:
print "You don't have enough to buy a vowel!!"
elif choice == 3:
guessPuzzle(answer, winnings)
break
else:
print "LOSER"
break
##############################################
def printIntro():
"""print out rules and user info"""
print "wheel of fortune..."
##############################################
def spin(answer, puzzlesofar, wheel, winnings, lettersguessed):
"""
takes care of spinning wheel and geting guess,
should return amount of money
"""
return 100
##############################################
def displayGameStatus(puzzlesofar, winnings):
"""display the puzzle and winnings"""
return
##############################################
main()