Python Programming: An Introduction to Computer Science

(Nora) #1

Chapter 9


Simulation and Design


Youmaynotrealizeit,butyouhave reacheda significantmilestoneinthejourney tobecominga computer
scientist.Younow have allthetoolsto writeprogramsthatsolve interestingproblems.Byinteresting,I mean
problemsthatwouldbedifficultorimpossibletosolve withouttheabilitytowriteandimplementcomputer
algorithms.Youareprobablynotyetreadytowritethenextgreatkillerapplication,butyoucandosome
nontrivialcomputing.


Oneparticularlypowerfultechniqueforsolvingreal-worldproblemsissimulation. Computerscanmodel
real-worldprocessestoprovideotherwiseunobtainableinformation. Computersimulationisusedevery
daytoperformmyriadtaskssuchaspredictingtheweather, designingaircraft,creatingspecialeffectsfor
movies,andentertainingvideogameplayers,to namejusta few. Mostoftheseapplicationsrequireextremely
complex programs,butevenrelativelymodestsimulationscansometimesshedlightonknottyproblems.


Inthischapterwearegoingtodevelopa simplesimulationofthegameofracquetball.Alongtheway,
youwilllearnsomeimportantdesignandimplementationstrategiesthatwillhelpyouintacklingyourown
problems.


9.1 SimulatingRacquetball.


9.1.1 A SimulationProblem


SuzieProgrammer’s friend,Denny Dibblebit,playsracquetball. Overyearsofplaying,hehasnoticeda
strangequirkinthegame.Heoftencompeteswithplayerswhoarejusta littlebitbetterthanheis.Inthe
process,healwaysseemstogetthumped,losingthevastmajorityofmatches.Thishasledhimtoquestion
whatis goingon. Onthesurface,onewouldthinkthatplayerswhoareslightlybettershouldwinslightly
moreoften,butagainstDenny, they seemtowinthelion’s share.


Oneobviouspossibilityis thatDenny Dibblebit’s problemis inhishead.Maybehismentalgameisn’t
uptoparwithhisphysicalskills.Orperhapstheotherplayersarereallymuchbetterthanheis,andhejust
refusestoseeit.


Oneday, Denny wasdiscussingracquetballwithSuzie,whenshesuggestedanotherpossibility. Maybeit
is thenatureofthegameitselfthatsmalldifferencesinabilityleadtolopsidedmatchesonthecourt.Denny
wasintriguedbytheidea;hedidn’t wanttotowastemoney onanexpensive sportspsychologistif it wasn’t
goingtohelp.Buthow couldhefigureoutif theproblemwasmentalorjustpartofthegame?


Suziesuggestedshecouldwritea computerprogramto simulatecertainaspectsofracquetball.Usingthe
simulation,they couldletthecomputermodelthousandsofgamesbetweenplayersofdifferingskilllevels.
Sincetherewouldnotbeany mentalaspectsinvolved,thesimulationwouldshowwhetherDenny is losing
morethanhisshareofmatches.


Let’s writeourownracquetballsimulationandseewhatSuzieandDenny discovered.

137
Free download pdf