WEEK07: top-down design --------------------------------------------------------------- M: start top-down design ANNOUNCEMENTS: - Lab 6 (frog race) due this Saturday - Quiz 3 on FRIDAY TOP-DOWN DESIGN: Top Down Design is a problem solving technique where you: 1. Start with General Description of Problem 2. Break it into several high-level steps 3. Iteratively break the steps into smaller steps until you have steps that are easy to solve - just like writing a paper...start with an outline, then fill in the second-level details, and so on until you can start writing each section/function - for example, writing a wheel-of-fortune game might start with something like this: main: read phrases from file pick one phrase for the game play the game, with the chosen phrase output the results Each one of those could be a single function, or may need to be broken down into multiple functions. If you think one of the above should be a single function, sketch it out (what arguments would it need, what would it do and how, and what should it return). If you think one of the above (ex: play the game) will be more than one function, do another level of top-down design on that. - GOALS: get main() written, decide on data structures, how data will go to/from each function, write function stubs so we can write and TEST EACH FUNCTION ONE AT-A-TIME! YOUR TURN: Let's demonstrate this idea with the dice game Craps. - A player rolls a pair of six-sided dice. - If the initial roll is 2, 3, or 12, the player loses. - If the initial roll is 7 or 11, the player wins. - Any other initial roll causes the player to "roll for point". This means that the player keeps rolling either until she re-rolls the initial value (a win) or rolls a 7 (a loss). Let's write a program to simulate multiple games of craps and estimate the probability that the player wins. """ Craps simulator using top down design Input: Number of games to simulate Output: Probability of winning A. Danner Fall 2013 """ from random import randrange #################################################### def main(): """ brief outline/overview of program """ ngames = getNumberOfGames() nwins = 0 for game in range(ngames): result = playOneGame() if(result == True): nwins = nwins+1 print "Winning percentage: %.2f%% " % (100.0*nwins/ngames) # ----------------------------------------------- # def getNumberOfGames(): """prompt user to enter a number of games to simulate check if input is valid return positive integer """ val = 10 return val # ----------------------------------------------- # def rollTwoDice(): """ roll two dice, return sum """ roll1 = 5 roll2 = 8 return roll1 + roll2 # ----------------------------------------------- # def playOneGame(): """ Plays a single of craps Returns True if player wins, False otherwise """ return False #################################################### main() NOTE: it doesn't do everything yet, but the structure and design are all specified. The program runs, and I can start working on adding each function. I can also TEST each function as I write them. Once I know they are working, I can move on to the next function. TEST AS YOU GO!!! - Let's add more to the playOneGame() function: def playOneGame(): """ Plays a single of craps Returns True if player wins, False otherwise """ print "Playing one game" roll = rollTwoDice() if roll == 2 or roll == 3 or roll == 12: return False elif roll == 7 or roll == 11: return True else: #play for point print "not implemented" return False - should still be able to run it! - maybe we need another function for "play for point"???