Vous êtes sur la page 1sur 14

EECS 280 Project 3: Euchre

Due Wednesday, 6 November 2013, 11:55 pm Mid-project checkpoint due Wednesday, 16 October 2013, 11:55pm
EuchreisatricktakingcardgamepopularinMichigan.Itismostcommonlyplayedbyfour peopleintwopartnershipswithadeckof24cards.Partnershipsaccumulatepointsforwinning tricks,andthegamecontinuesuntilonesidereachesthemaximumnumberofpoints. Inthisproject,youwillwriteasimulatorforagameofEuchre.Youwillgainexperiencewith composabledatastructures,objectorientedprogrammingtechniquesanddecomposingatask usingfunctions.Whilebuildingthesimulator,youwillusepointers,arrays,structures,strings andfileIO.

Euchre Rules
TherearemanyvariantsofEuchre.TherulesforthisprojectarederivedfromGorensHoyle EncyclopediaofGames[Goren1961],withtheadditionofseveralvariationscommonin Michiganplay.

Number of players
Four,intwopartnerships.

The pack
Apackof24cards,madebydiscardingfromafullpackallcardsbelowthenines.Inother words,ranksninethroughaceofallfoursuits.

Order of cards
Cardsareorderedaccordingtowhichsuitistrump,asuitwhosecardsareelevatedabovetheir normalrankduringplay.Thehighestcardisjackofthetrumpsuit,andiscalledtherightbower. Thesecondhighestcardisthejackoftheothersuitofthesamecolorasthetrump,andis calledtheleftbower.Theleftbowerisalsoconsideredatrumpcard.Forexample,ifDiamonds istrump,theJackofHeartsisalsoconsideredaDiamond,notaHeart.Thesuitoftheleft boweriscallednext,whilethetwosuitsoftheoppositecolorarecalledcrosssuits. Tosimplifydebuggingandensurethateverycorrectsolutionwillgetthesameoutput,weve addedanadditionalrulehere.Inadditiontobeingorderedbyrank,cardsarealsoorderedby suit:diamondsfirst,thenclubs,hearts,spades. Thecompleteorderingofallthecardsis:J(rightbower,high),J(leftbower),A,K,Q,10,9ofthe trumpsuit.Theacesoftheremainingthreesuitsfollowinsuitorder,theremainingkingsinsuit order,,andfinallytheremainingninesinsuitorder.

Drawing for partnerships


Wevesimplifiedtherulesabithere.Partnershipswillbefixed,wherefourplayers(03)are Revision20131002 1

partnered0&2,and1&3.

Shuffle and cut


Again,wevesimplifiedtheruleshere.Thedealershufflesandcutsthedeckbeforeeachhand. Thealgorithmweuseforshufflingisavariantoftheonehandedshuffle.Cutthedeckat17 cards(thatis,cards016and1723)andswapthetwoparts.Dothis3times.Later,well discussanoptionthatdisablesshufflingforeasiertestinganddebugging.Ifshufflingisdisabled, thenresetthepackbacktothebeginning.

The deal
Inourgame,thefirstdealeristhefirstplayer.Eachhand,thedealmovesoneplayertotheleft. Eachplayerreceivesfivecards,dealtinbatchesof32.Thefirstpersontoreceiveacardisleft ofthedealer.Thatis,deal3232cardsthen2323cards,foratotalof5555cards.Afterall handsaredealt,thenextcardofthepackisturneduptoproposethetrumpsuit.

Making trump
Theturnedupcardinfrontofthedealerproposesthetrumpsuit,butitsacceptancedependson uptotworoundsofmaking.Eldesthand(playeratleftofthedealer,e.g.,player3isleftofplayer 2,andplayer0isleftofplayer3)speaksfirst.Hemayeitherpassororderup.Orderup acceptstheturnupsuit.Ifthetrumpsuitisorderedupduringroundone,thedealeraddstheup cardtohishandanddiscardsonecardtobringhishandbacktofivecards,total.Hint:be carefulnottogoofftheendofanarrayhere!Ifaplayerpasses,thenextplayertotheleft speaks. Ifallplayerspassduringthefirstround,thereisasecondroundofdeclaring,againbeginning withtheeldesthand.Thistime,eachplayerinturnmayeitherpassororderupbynamingasuit otherthantherejectedsuit. Ifmakingreachesthedealerduringthesecondround,avariantcalledscrewthedealeris invoked.Thedealermustorderupasuitotherthantherejectedsuit. SidenoteforproEuchreplayers:wehaveomittedgoingaloneinthisproject,asimplification.

The play
Thefirstcard,calledthelead,isplayedbytheeldesthand,regardlessofwhoisthemaker.The leader(personplayingtheleadcard)toanytrickmayleadanycard.Playmovestotheleft aroundthetable,witheachhandplayingonecard.Eachotherhandmustfollowtheledsuit,if able,andotherwisemayplayanycard.Atrickiswonbythehighesttrump,or,ifitcontainsno trump,bythehighestcardofthesuitled.Attheendofatrick,playedcardsarediscarded.The winnerofthetrick(orround)leadsthenext.

Object of play
Theobjectiveistowinamajorityoftricks.

Scoring
Revision20131002 2

Onlythesidethatwinsthemajorityoftricksscores.Thesidethatmadetrumpwins1pointfor3 or4tricks,and2pointsfortakingall5,whichiscalledmarch.Theothersidewins2pointsfor taking3,4or5tricks,whichiscalledeuchered. Thefirstsidetoreach10pointswins.Inthisproject,thenumberofpointstowinwillbe configurable.

Euchre strategy
Making
Inmakingtrump,aplayerconsiderstheupcard,theplayerwhodealt,andwhetheritisthefirst orsecondround.Amorecomprehensivestrategywouldconsidertheotherplayersresponses, butwewillkeepitsimple. Duringroundone,aplayerwillorderupifhehastwoormoretrumpfacecardsinhishand. Trumpfacecardsaretherightandleftbowers,andQ,K,Aofthetrumpsuit,whichisthesuit proposedbytheupcardinthiscase.Ifthetrumpsuitisorderedupduringroundone,thedealer receivesit.Hethendiscardsthelowestcardamonghishandandtheturnup,forafinaltotalof fivecards. Duringroundtwo,aplayerwillorderupthesuitwiththesamecolorastheupcardifhehasone ormorefacecardsofthatsuitinhishand.Forexample,iftheupcardwasaheartandhehad thekingofdiamondsinhishand,hewouldorderupdiamonds.Ifmakingreachesthedealer duringthesecondround,weinvokescrewthedealer,wherethedealerisforcedtoorderup.In thecaseofscrewthedealer,thedealerwillalwaysorderupthesuitwiththesamecolorasthe upcard.

Leading
Whenaplayerleadsatrick,heplaysthehighestnontrumpcardinhishand.Ifhehasonly trump,heplaysthehighesttrumpcardinhishand.

Playing
Whenplayingacard,playersuseasimplestrategythatconsidersonlythesuitthatwasled.A morecomplexstrategywouldalsoconsiderthecardsonthetable,butagain,wewillkeepit simple. Ifaplayercanfollowsuit,heplaysthehighestcardthatfollowssuit.Otherwise,heplaysthe lowestcardinhishand.

Example
Theoutputfor. / e u c h r e p a c k . i n n o s h u f f l e 1 A l i c e B o b C a t h y D r e w issavedin e u c h r e _ t e s t 0 0 . o u t . c o r r e c t .Thissectionexplainstheoutput,linebyline.Makesure thatyoursimulatorproducesonlyoutputcalledforbythisdocument.

Revision20131002

First,printtheexecutableandallargumentsonthefirstline.Printasinglespaceattheend, whichmakesiteasiertoprintanarray. . / e u c h r e p a c k . i n n o s h u f f l e 1 A l i c e B o b C a t h y D r e w Atthebeginningofeachhand,announcethehand,startingatzero,followedbythedealerand theturnup. H a n d 0 A l i c e d e a l s J a c k o f D i a m o n d s t u r n e d u p Printthedecisionofeachplayerduringthemakingprocedure.Printanextranewlinewhen makingiscomplete. B o b p a s s e s C a t h y p a s s e s D r e w p a s s e s A l i c e p a s s e s B o b o r d e r s u p H e a r t s Eachofthefivetricksisannounced,includingthelead,cardsplayedandtheplayerthattookthe trick.Printanextranewlineattheendofeachtrick. J a c k o f S p a d e s l e d b y B o b K i n g o f S p a d e s p l a y e d b y C a t h y A c e o f S p a d e s p l a y e d b y D r e w N i n e o f D i a m o n d s p l a y e d b y A l i c e D r e w t a k e s t h e t r i c k Attheendofthehand,printthewinnersofthehand.Whenprintingthenamesofapartnership, printtheplayerwiththelowerindexfirst.Forexample,Alicewasspecifiedonthecommandline beforeCathy,soshegoesfirst. A l i c e a n d C a t h y w i n t h e h a n d Ifmarchoccurs,printm a r c h ! followedbyanewline.Ifeuchreoccurs,printe u c h r e d ! followed byanewline.Ifneitheroccurs,printnothing. e u c h r e d ! Printthescore,followedbyanextranewline. A l i c e a n d C a t h y h a v e 2 p o i n t s B o b a n d D r e w h a v e 0 p o i n t s Whenthegameisover,printthewinnersofthegame. A l i c e a n d C a t h y w i n !

Code structure
Revision20131002 4

Thecodeisstructuredasanobjectorientedprogram.TheC++structmechanismisusedto represententitiesintheEuchreworld,forexampleC a r d ,P a c k , andP l a y e r .These structuresaredefinedinseveralheaderfiles,andtheyinteractwitheachotherthroughfunctions thatformaproceduralabstraction.Yourtaskistoimplementthesefunctionsin. c p p files, addinganyadditionalhelperfunctionstothe. c p p files.Finally,youwillwriteam a i n ( ) function withacommandlineinterfacetothegame.

Running the program


TheEuchresimulatortakesseveralcommandlineargumentstodeterminewhatkindof simulationtorun.ThefollowingcommandwillrunatraditionalgameofEuchre: . / e u c h r e p a c k . i n s h u f f l e 1 0 A l i c e B o b C a t h y D r e w Eachoftheargumentsare: . / e u c h r e Nameoftheexecutable p a c k . i n Filenameofthepack s h u f f l e Shufflethedeck,orusenoshuffletoturnoffshuffling 1 0 Pointstowinthegame A l i c e Nameofplayer0 B o b Nameofplayer1 C a t h y Nameofplayer2 D r e w Nameofplayer3 Thesimulatorchecksforthefollowingerrors: Thereareexactly7arguments,inadditiontotheexecutablenameitself Pointstowinthegameisbetween1and100,inclusive Theshuffleargumentiseithers h u f f l e orn o s h u f f l e Ifthesimulatorfindsanyofthepreviouserrors,itshouldprintthefollowingmessageandquitby callinge x i t ( E X I T _ F A I L U R E ) ,whichisfrom< c s t d l i b > .Thee x i t ( ) functionterminates theprogram. c o u t < < " U s a g e : e u c h r e P A C K _ F I L E N A M E [ s h u f f l e | n o s h u f f l e ] P O I N T S _ T O _ W I N N A M E 1 N A M E 2 N A M E 3 N A M E 4 " < < e n d l

Reading the Pack


TheEuchresimulatorreadsapackfromafile(inP a c k . c p p ).Wehaveprovidedonepack,with thecardsinnewpackorder.Forexample: N i n e o f S p a d e s T e n o f S p a d e s J a c k o f S p a d e s . . . Q u e e n o f D i a m o n d s K i n g o f D i a m o n d s A c e o f D i a m o n d s First,openthefileandcheckforsuccess.Ifthefileopenoperationfails,usethefollowingcode Revision20131002 5

toprintanerrormessage,andthencalle x i t ( E X I T _ F A I L U R E ) . c o u t < < " E r r o r o p e n i n g " < < p a c k _ f i l e n a m e < < e n d l AfterthePackfileisopen,youmayassumethatthereareexactly24uniqueandcorrectly formattedcards.Inotherwords,youdonthavetoworryaboutcheckingthecontentsofthefile forerrors.

C-strings and C++ strings


ThevastmajorityofthisprojectusesCstrings.UseC++stringsonlyinconjunctionwithC++ IO,forexample,whenreadingfromafile. WhenusingCstrings,youcanassumeamaximumarraysizeof1024.Hint:theonlyplaceyou willneedthisisinP l a y e r . c p p ,whichalreadyhasc o n s t i n t M A X _ S T R _ L E N = 1 0 2 4 in P l a y e r . h .Also,dontforgettouses t r c p y ( ) whencopyingCstrings!

Restrictions
Inthisproject,itisourgoalforyoutogainpracticewithgoodCstylecode,structs,andpointers. Toencouragethisstyle,wehaveafewrestrictions,describedinthefollowingtable. Dos Modify. c p p files Putanyextrahelperfunctionsinthe. c p p filesanddeclarethems t a t i c Usetheselibraries:< i o s t r e a m > , < f s t r e a m > ,< c s t d l i b > ,< c a s s e r t > , < c s t d i o > , < c s t r i n g > , < s t r i n g > # i n c l u d e alibrarytouseitsfunctions UseC++stringstoreadfromfilesand Cstringseverywhereelse Sendalloutputtostandardout(AKAstdout) byusingc o u t c o n s t globalvariables Donts Modify. h files Modify. h files Useotherlibraries

Assumethatthecompilerwillfindthelibrary foryou(somedo,somedont) UseC++stringseverywhere Sendanyoutputtostandarderror(AKA stderr)byusingc e r r Globalorstaticvariables

Cstylefunctionsthatpassastructbypointer Classesormemberfunctions Passlargestructsbypointer Passbyconstpointerwhenappropriate Passlargestructsbyvalue IdontthinkIllmodifyit

Revision20131002

Variablesonthestack

Dynamicmemory(n e w ,m a l l o c ( ) ,etc.)

How to complete this project


Play a (real) game of Euchre
Beforegettingstartedonthisproject,considerplayingagameofEuchrewiththreefriendsor online.ItmakethespeceasiertounderstandanditsaMichigantradition!

Download the starter code


Downloadthestartercodefrom/ a f s / u m i c h . e d u / c l a s s / e e c s 2 8 0 / p r o j 3 / Thefollowing tabledescribeseachfile. C a r d . h C a r d . c p p . s t a r t e r Proceduralabstractionrepresenting operationsonaplayingcard StartercodeforC a r d . c p p ,whichwill containfunctionimplementationsforthe prototypesinC a r d . h .Copythisfileto C a r d . c p p togetstarted. UnittestsforfunctionsinC a r d . h . Copy thisfiletoC a r d _ t e s t . c p p togetstarted. Proceduralabstractionrepresenting operationsonapackofplayingcards UnittestsforfunctionsinP a c k . h .Youwill needtoaddmore.Copythisfileto P a c k _ t e s t . c p p togetstarted. Proceduralabstractionrepresenting operationsonaeuchreplayer UnittestsforfunctionsinP l a y e r . h .Youwill needtoaddmore.Copythisfileto P l a y e r _ t e s t . c p p togetstarted. Proceduralabstractionrepresenting operationsonagameofeuchre Inputfilecontainingaeuchredeck Usedbythemakecommandtocompilethe executable.Typem a k e atthecommandline. 7

C a r d _ t e s t . c p p . s t a r t e r

P a c k . h P a c k _ t e s t . c p p . s t a r t e r

P l a y e r . h P l a y e r _ t e s t . c p p . s t a r t e r

G a m e . h p a c k . i n M a k e f i l e . s t a r t e r

Revision20131002

CopythisfiletoM a k e f i l e togetstarted. e u c h r e _ t e s t 0 0 . o u t . c o r r e c t e u c h r e _ t e s t 0 1 . o u t . c o r r e c t Correctoutputforsystemtestsofmain executable.Thefirstlineofeachfilecontains thecommandusedtogenerateit.

Code and test Card


ImplementthefunctionsinC a r d . c p p whoseprototypesaredeclaredinC a r d . h .Manyofthe functionimplementationsaregiveninC a r d . c p p . s t a r t e r .Remember,onlymodify C a r d . c p p ,notC a r d . h !Afterwritingeachfunction,testitusingunittestsinC a r d _ t e s t . c p p . ManytestsareprovidedinC a r d _ t e s t . c p p . s t a r t e r . CompiletheCardunittestbytypingm a k e C a r d _ t e s t ,whichrunsthecommand g + + p e d a n t i c W a l l W e r r o r O 1 C a r d _ t e s t . c p p C a r d . c p p o C a r d _ t e s t Runtheunittestbytyping. / C a r d _ t e s t atthecommandline.Thetestwillreturn0andprinta messageifitpasses.Itwillexitnonzerowithanerrormessageifatestfails.

Code and test Pack


WriteyourfunctionsinP a c k . c p p andtesttheminP a c k _ t e s t . c p p .Sometestsareprovided inP a c k _ t e s t . c p p . s t a r t e r ,butyouwillneedtoaddmore. CompilethePackunittestbytypingm a k e P a c k _ t e s t ,whichwillrunthecommand g + + p e d a n t i c W a l l W e r r o r O 1 P a c k _ t e s t . c p p P a c k . c p p C a r d . c p p o P a c k _ t e s t Runtheunittestbytyping. / P a c k _ t e s t atthecommandline.Thetestwillreturn0andprinta messageifitpasses.Itwillexitnonzerowithanerrormessageifatestfails.In P a c k _ t e s t . c p p ,Usea s s e r t ( ) tocheckforfailures,oralternatively,usei f ( ) statements andcalle x i t ( E X I T _ F A I L U R E ) orr e t u r n E X I T _ F A I L U R E ifatestfails.

Code and test Player


WriteyourfunctionsinP l a y e r . c p p andtesttheminP l a y e r _ t e s t . c p p .Likeprevioustests, P l a y e r _ t e s t . c p p shouldcontainunitstestsforthefunctionsinP l a y e r . h . CompilethePlayerunittestbytypingm a k e P l a y e r _ t e s t ,whichwillrunthecommand g + + p e d a n t i c W a l l W e r r o r O 1 P l a y e r _ t e s t . c p p P l a y e r . c p p P a c k . c p p C a r d . c p p o P l a y e r _ t e s t Runtheunittestbytyping. / P l a y e r _ t e s t atthecommandline.Likepreviousunittests,the testwillreturn0andprintamessageifitpasses.

Code main
Revision20131002 8

Themainfunctioncodedine u c h r e . c p p willbeverysimple.Itshouldreadthecommandline arguments,checkthemforerrors,andthenprintthem.Next,callG a m e _ i n i t ( ) followedby G a m e _ p l a y ( ) .Finishwithr e t u r n E X I T _ S U C C E S S attheendofmain. Inorderformaintocompile,itwillneedG a m e . c p p tocompile.ForG a m e . c p p tocompile,all thefunctionsfromG a m e . h needtobeinG a m e . c p p .AddfunctionstubstoG a m e . c p p ,which donothingbutreturnaconstantvalue.Thiswillallowyoutocompilethewholeproject,evenif youarentfinishedyet.Hereisanexampleofafunctionstub,fromG a m e . c p p : v o i d G a m e _ p l a y ( G a m e * g a m e _ p t r ) { r e t u r n } Compilethemaineuchreexecutablebytypingm a k e e u c h r e ,whichwillrunthecommand g + + p e d a n t i c W a l l W e r r o r O 1 e u c h r e . c p p G a m e . c p p P l a y e r . c p p P a c k . c p p C a r d . c p p o e u c h r e Runtheprogrambyprovidingcommandlinearguments,forexample: . / e u c h r e p a c k . i n s h u f f l e 1 0 A l i c e B o b C a t h y D r e w

Code Game
Thispartwillrequirethemostplanning.Beforeyoubegin,thinkaboutwhichhelperfunctionsyou wouldliketoaddandwhattheyshoulddo.Forexample,functionsthatshuffle,dealandmake trumpareagoodstartingpoint.Remember,onlyaddfunctionstoG a m e . c p p anddeclarethem static.HereisanexampleofapossiblehelperfunctionfromG a m e . c p p : / / R E Q U I R E S : g a m e _ p t r i s i n i t i a l i z e d / / M O D I F I E S : g a m e _ p t r / / E F F E C T S : I f t h e s h u f f l e o p t i o n i s s e l e c t e d s h u f f l e t h e p a c k . / / O t h e r w i s e , r e s e t t h e p a c k . s t a t i c v o i d G a m e _ s h u f f l e ( G a m e * g a m e _ p t r )

Test Game using main


Usee u c h r e . c p p toperformsystemtestsonyourGame.Runagamefromthecommandline andcheckitsoutputusingd i f f .Wehaveprovidedseveralexampletestsandyouwillneedto addmore.Usearegressiontesttorerunandchecktheoutputofalltestswhenyoufixabugor modifyyourcode.WehaveprovidedthebeginningofaregressiontestintheMakefile,which youcanrunbytypingm a k e t e s t . Torunasimplesystemtestmanually,compile,runtheprogramandredirecttheoutputtoafile. Then,usedifftocomparetheoutputtothecorrectoutput: g + + p e d a n t i c W a l l W e r r o r O 1 e u c h r e . c p p G a m e . c p p P l a y e r . c p p P a c k . c p p C a r d . c p p o e u c h r e . / e u c h r e p a c k . i n n o s h u f f l e 1 A l i c e B o b C a t h y D r e w > e u c h r e _ t e s t 0 0 . o u t d i f f q e u c h r e _ t e s t 0 0 . o u t e u c h r e _ t e s t 0 0 . o u t . c o r r e c t

Revision20131002

Submit
Thisprojecthastwosubmissions.Thefirstisamidprojectcheckpoint,whereseveral componentsaredue.Thesecondisthefinaldeadlineforthecompletedproject. Forthemidprojectcheckpoint,submitthefollowingfilesusings u b m i t 2 8 0 3 a .TheC a r d , P a c k andP l a y e r componentsshouldpassthepublishedunittests. C a r d . c p p P a c k . c p p P l a y e r . c p p Forthefinalsubmission,submitthefollowingfilesusings u b m i t 2 8 0 3 .Forthispartofyour grade,wewillnotuseanycodeturnedinatthecheckpoint. C a r d . c p p P a c k . c p p P l a y e r . c p p G a m e . c p p e u c h r e . c p p

Acknowledgements
OriginalprojectwrittenbyAndrewDeOrio,fall2013.ThetextforthebasicrulesofEuchrewas adaptedfromGorensHoyleEncyclopediaofGames[Goren,1961].

Revision20131002

10

Appendix A: Euchre Glossary


Trump:Asuitwhosecardsareelevatedabovetheirnormalrankduringplay. RightBower:TheJackcardoftheTrumpsuit,whichisconsideredthehighestcardinEuchre. LeftBower:TheJackfromtheothersuitofthesamecolorastheTrumpcard,consideredas thesecondhighestcardinEuchre.TheLeftBowerisalsoconsideredaTrumpcard. Next:Thesuitofthesamecolorastrump. Crosssuits:Thetwosuitsoftheoppositecoloroftrump. Making:Theprocessinwhichatrumpcardischosen,consistsoftworounds. Eldest:Playertotheleftofthedealer. Turnup:Theturnedupcardinfrontofthedealerthatproposesthetrumpsuit. Orderup:AcceptstheTurnupsuit. Pass:Playerrejectsthesuitandpassesonthedecisiontothenextplayer. ScrewtheDealer:Whenmakingreachesthedealeronroundtwo,thedealermustorderupa suitotherthantherejectedone. Lead:Thefirstcardplayedbytheeldesthand,regardlessofwhoisthemaker. Leader:Personplayingtheleadcardinatrick,allowedtoleadanycard. March:Whenthesidethatmadetrumpwinsall5tricks. Euchred:Whenthesidethatdidntmaketrumpwins3,4,or5tricks.

Revision20131002

11

Appendix B: Makefile tutorial


Makeisatoolthathelpsyoucompileaprogramfromtheprogram'ssourcefiles.Usingmake, youcanavoidtypinglongandtediouscompilercommands.Forexample,onewaytocompile thisprojectistotypethefollowingcommandattheterminal: g + + p e d a n t i c W a l l W e r r o r O 1 e u c h r e . c p p G a m e . c p p P l a y e r . c p p P a c k . c p p C a r d . c p p o e u c h r e Anotherwaytocompilethisprojectistousem a k e , whichreadstheprovidedM a k e f i l e and findstherightcommand.Simpletypem a k e atthecommandline. Letslookintotheproject3Makefile.Makefilescontainrules,whichtellMakehowtoexecutea seriesofcommandsinordertocreateatargetfilefromsourcefiles.Italsospecifiesalistof dependenciesofthetargetfile.Thislistshouldincludeallfileswhichareusedasinputstothe commandsintherule.Acommandisprecededbyatab.
e u c h r e : e u c h r e . c p p G a m e . c p p G a m e . h P l a y e r . c p p P l a y e r . h P a c k . c p p P a c k . h C a r d . c p p C a r d . h g + + p e d a n t i c W a l l W e r r o r O 1 e u c h r e . c p p G a m e . c p p P l a y e r . c p p P a c k . c p p C a r d . c p p o e u c h r e C a r d _ t e s t : C a r d _ t e s t . c p p C a r d . c p p C a r d . h g + + p e d a n t i c W a l l W e r r o r O 1 C a r d _ t e s t . c p p C a r d . c p p o C a r d _ t e s t t e s t : C a r d _ t e s t e u c h r e . / C a r d _ t e s t . / e u c h r e p a c k . i n n o s h u f f l e 1 A l i c e B o b C a t h y D r e w > e u c h r e _ t e s t 0 0 . o u t d i f f q e u c h r e _ t e s t 0 0 . o u t e u c h r e _ t e s t 0 0 . o u t . c o r r e c t . / e u c h r e p a c k . i n s h u f f l e 1 0 A l i c e B o b C a t h y D r e w > e u c h r e _ t e s t 0 1 . o u t d i f f q e u c h r e _ t e s t 0 1 . o u t e u c h r e _ t e s t 0 1 . o u t . c o r r e c t c l e a n : r m v f * . o u t e u c h r e C a r d _ t e s t

TheprovidedMakefilecontainstargetstobuildthee u c h r e executableandaC a r d _ t e s t executable.Bydefaultmakewillbuildthefirsttarget.Foradifferenttarget,specifyitafterthe makecommand.Forexample,typingm a k e C a r d _ t e s t willbuildtheC a r d _ t e s t executable.m a k e t e s t willrunseveraltestsandchecktheoutputusingd i f f .Finally,m a k e c l e a n willremovetheexecutablesallfilesendingin. o u t .

Revision20131002

12

Appendix C: Debugging pro-tips


First,dontforgettochangeyourcompilerflagsbeforedebugging.Remove O 1 andadd g .

Use your debugger like a pro


Oureuchresimulatorhascommandlineargumentsandinputfiles.Firstmakesureyourinput fileisinthesamedirectoryastheexecutable.Next,tellthedebuggertoprovidecommandline argumentstotheexecutable.Youonlyneedtogivetheargumentsthefirsttimeyoucallrun. Afterthat,itwillrememberthem.Forexample,ingdb: $ g d b e u c h r e ( g d b ) r u n p a c k . i n n o s h u f f l e 1 A l i c e B o b C a t h y D r e w . . . ( g d b ) r u n . . . Dereferencepointersinsidegdbtoinspectthem: $ g d b e u c h r e ( g d b ) b r e a k P l a y e r _ p l a y _ C a r d ( g d b ) r u n p a c k . i n n o s h u f f l e 1 A l i c e B o b C a t h y D r e w ( g d b ) p r i n t p l a y e r _ p t r $ 1 = ( P l a y e r * ) 0 x 7 f f f f f f f d 3 5 0 ( g d b ) p r i n t * p l a y e r _ p t r $ 3 = { n a m e = . . . h a n d = . . . h a n d _ s i z e = 5 } Callfunctionsinsidegdb: $ g d b e u c h r e ( g d b ) b r e a k P l a y e r _ p l a y _ C a r d ( g d b ) r u n p a c k . i n n o s h u f f l e 1 A l i c e B o b C a t h y D r e w ( g d b ) c a l l P l a y e r _ p r i n t ( p l a y e r _ p t r ) C a t h y Q u e e n o f S p a d e s K i n g o f S p a d e s N i n e o f C l u b s T e n o f C l u b s J a c k o f C l u b s

Assert like a pro


Usea s s e r t ( ) todetectproblemsbeforetheyhappen.a s s e r t ( S T A T E M E N T ) willcrashthe programwithadebuggingmessageifS T A T E M E N T isfalse.Forexamplea s s e r t ( 0 ) willfail Revision20131002 13

everytime.

Checking REQUIRES clauses


Forexampleyoucoulduseanassertiontoavoidaccidentallytryingtoaskaplayerwithanempty handtoplayacard: C a r d P l a y e r _ p l a y _ C a r d ( P l a y e r * p l a y e r _ p t r , S u i t t r u m p , S u i t l e d _ s u i t ) { a s s e r t ( p l a y e r _ p t r > h a n d _ s i z e > 0 ) / / . . . }

Checking for NULL pointers


Anassert()isagreatwaytocheckforaNULLpointer.Thistechniqueismostusefulifyou makethehabitofalwaysinitializingpointerstoNULL. v o i d P l a y e r _ p r i n t ( c o n s t P l a y e r * p l a y e r _ p t r ) { a s s e r t ( p l a y e r _ p t r ) / / w i l l c r a s h i f p l a y e r _ p t r i s N U L L c o u t < < p l a y e r _ p t r > n a m e < < " \ n " / / . . . }

Disabling assert
Todisabletheassert()statementsinafile,simplyadd# d e f i n e N D E B U G toeach.cppthat usesassert().

Revision20131002

14

Vous aimerez peut-être aussi