9.4.BOTTOM-UPIMPLEMENTATION 149
rball1.gameOver(3,15)
1
I have selectedtestdatathatexercisealltheimportantcasesforthefunction.Thefirsttimeit is called,the
scorewillbe0 to0. Thefunctioncorrectlyrespondswith0 (false);thegameisnotover. Asthegame
progresses,thefunctionwillbecalledwithintermediatescores.Thesecondexampleshowsthatthefunction
againrespondedthatthegameis stillinprogress. Thelasttwo examplesshowthatthefunctioncorrectly
identifiesthatthegameis overwheneitherplayerreaches15.
HavingconfidencethatgameOverisfunctioningcorrectly, nowwecangobackandimplementthe
simOneGamefunction. Thisfunctionhassomeprobabilisticbehavior, soI’mnotsureexactlywhatthe
outputwillbe.Thebestwecandointestingit is toseethatit behavesreasonably. Hereis a samplesession.
import rball
rball1.simOneGame(.5,.5)
(13, 15)
rball1.simOneGame(.5,.5)
(15, 11)
rball1.simOneGame(.3,.3)
(15, 11)
rball1.simOneGame(.3,.3)
(11, 15)
rball1.simOneGame(.4,.9)
(4, 15)
rball1.simOneGame(.4,.9)
(1, 15)
rball1.simOneGame(.9,.4)
(15, 3)
rball1.simOneGame(.9,.4)
(15, 0)
rball1.simOneGame(.4,.6)
(9, 15)
rball1.simOneGame(.4,.6)
(6, 15)
Noticethatwhentheprobabilitiesareequal,thescoresareclose.Whentheprobabilitiesarefartherapart,the
gameis a rout.Thatsquareswithhow wethinkthisfunctionshouldbehave.
We cancontinuethispiecewiseimplementation,testingouteachcomponentasweaddit intothecode.
Softwareengineerscallthisprocessunittesting. Testingeachfunctionindependentlymakesit easierto
spoterrors.Bythetimeyougetaroundtotestingtheentireprogram,chancesarethateverythingwillwork
smoothly.
Separatingconcernsthrougha modulardesignmakesit possibletodesignsophisticatedprograms.Sep-
aratingconcernsthroughunittestingmakesit possibletoimplementanddebugsophisticatedprograms.Try
thesetechniquesforyourself,andyou’ll seethatyouaregettingyourprogramsworkingwithlessoverall
effortandfarlessfrustration.
9.4.2 SimulationResults
Finally, wecantake a lookat Denny Dibblebit’s question.Is it thenatureofracquetballthatsmalldifferences
inabilityleadtolargedifferencesintheoutcome?SupposeDenny winsabout60%ofhisservesandhis
opponentis5%better. HowoftenshouldDenny winthegame? Here’s anexamplerunwhereDenny’s
opponentalwaysservesfirst.
This program simulatesa game of racquetball between two
players called "A" and"B". The abilities of each player is
indicated by a probability(a number between 0 and 1) that
the player wins the pointwhen serving. Player A always