Python Programming: An Introduction to Computer Science

(Nora) #1
210 CHAPTER12. OBJECT-ORIENTEDDESIGN

theGame.play() # play it
stats.update(theGame) # get info about completedgame

# Print the results
stats.printReport()

main()
raw_input("\nPress to quit")


12.3 CaseStudy:DicePoker.


Backin Chapter10,I suggestedthatobjectsareparticularlyusefulforthedesignofgraphicaluserinterfaces.
I wanttofinishupthischapterbylookingata graphicalapplicationusingsomeofthewidgetsthatwe
developedinpreviouschapters.


12.3.1 ProgramSpecification


Ourgoalis towritea gameprogramthatallowsa usertoplayvideopokerusingdice.Theprogramwill
displaya handconsistingoffive dice.Thebasicsetofrulesis asfollows:
Theplayerstartswith$100.


 Eachroundcosts$10to play. Thisamountis subtractedfromtheuser’s money at thestartoftheround.

 Theplayerinitiallyrollsa completelyrandomhand(i.e.,allfive dicearerolled).

 Theplayergetstwo chancestoenhancethehandbyrerollingsomeorallofthedice.

 Attheendofthehand,theplayer’s money is updatedaccordingtothefollowingpayoutschedule:

Hand Pay
Two Pairs 5
Threeofa Kind 8
FullHouse(APairanda Threeofa Kind) 12
Fourofa Kind 15
Straight(1–5or2–6) 20
Five ofa Kind 30
Ultimately, wewantthisprogramtopresenta nicegraphicalinterface.Ourinteractionwillbethrough
mouseclicks.Theinterfaceshouldhave thefollowingcharacteristics:
Thecurrentscore(amountofmoney)is constantlydisplayed.


 Theprogramautomaticallyterminatesif theplayergoesbroke.

 Theplayermaychoosetoquitat appropriatepointsduringplay.

 Theinterfacewillpresentvisualcuestoindicatewhatis goingonat any givenmomentandwhatthe
validuserresponsesare.

12.3.2 IdentifyingCandidateObjects


Ourfirststepis toanalyzetheprogramdescriptionandidentifysomeobjectsthatwillbeusefulinattacking
thisproblem.Thisis a gameinvolvingdiceandmoney. Areeitherofthesegoodcandidatesforobjects?Both
themoney andanindividualdiecanbesimplyrepresentedasnumbers.Bythemselves,they donotseemto
begoodobjectcandidates.However, thegameusesfive dice,andthissoundslike a collection.We willneed
tobeabletorollallthediceora selectionofdiceaswellasanalyzethecollectiontoseewhatit scores.
We canencapsulatetheinformationaboutthediceinaDiceclass.Herearea fewobviousoperations
thatthisclasswillhave toimplement.

Free download pdf