Title | 2048 Brief |
---|---|
Course | Software engineering - introduction, algorithms and data structures |
Institution | Imperial College London |
Pages | 6 |
File Size | 247.1 KB |
File Type | |
Total Downloads | 102 |
Total Views | 156 |
2048 Brief...
14/07/2019
introcomp:a2sp19 [EEE computing]
EEEcomputing
Assignment2--Spring2019 WriteaC++programforthe2048gamehttp://gabrielecirulli.github.io/2048/[http://gabrielecirulli.github.io/2048/](the submissionareaislinkedfromthe“EE1-07SoftwareEngineering1:IntroductiontoComputing(2018-2019)”pageon Blackboard). Thefollowingscreenshotsshowthegameinterfaceandexpandonmorespecificrequirements. Letfileinputconf.txtcontainedinthesamedirectoryoftheexecutable(this,asusual,assumingtheprogramisrunfrom thecommandline,conditionsmayvaryifanIDEisused)havethefollowingcontent: 0 0 16 4 32 0 4 8 128 2 2 2 0 0 0 2
Theprograminitiallyasksforthenameofafilecontainingtheinitialconfiguration.Ifthefileisfound,itisloadedandthe gamebeginsfromsaidconfiguration.Commandsaregivenininputusingaforleft,sfordown,dforrightandwforup. Thelettersaregivenininputintheusualway(i.e.followedbyenter).Intheoriginalgameeithera2ora4,withacertain probability,isplacedrandomlyinoneofthefreetilesafteramove.Forthisassignmentitshouldalwaysbea2. https://intranet.ee.ic.ac.uk/m.cattafi/scripts/dw/doku.php?id=introcomp:a2sp19
1/6
14/07/2019
introcomp:a2sp19 [EEE computing]
Iftheinputfileisnotfoundthenamessageisprintedandthegamestartsfromadefaultconfiguration(a4×4gridwitha2 inthebottomrightcorner).
Whatshouldhappenwhentheuserentersamoveinadirectionthatdoesn'tinvolvechangestothecontentofthegrid. Notethatthisalsomeansthat,asintheoriginalgame,whenthishappensnoadditional2isgenerated.
Nopossiblemoves:gameover.Thisistheonlyterminationcondition:thegameshouldgoonevenwhenthe2048valueis reached. Theexamplesaboveareabouta4×4gridasintheoriginalgame,buttheprogramshouldworkonanynxn(let'ssaywithn > 1)gridreadfromthefile(thedefaultconfigurationisalways4×4). Moreoverthefilemaywellcontainnumbersthatarenotpowersof2(butwhatisgeneratedinarandompositionaftera moveisstillalwaysa2)andtheseshouldbehandlednormally(e.g.theyshouldbesummedtogetheriftheyarethesame numberandamoveismadesuchthattheyareshiftedtomatchtogether).Weassumethatthefilealwayscontainstheright numberofelements(i.e.suchthatitcanbeasquare)andthatitalwayscontainsatleastonenon-zeroelement. Also,notethatinputconf.txtisjustanexample,thereisn'tafixednameforthefilethatcontainstheinput(that'swhyitis readfromtheuserandnothardcodedintheprogram). Donotalterthewaythingsareprinted,anddonotaddanythingelse.Followascloselyaspossiblethescreenshots.
Design
https://intranet.ee.ic.ac.uk/m.cattafi/scripts/dw/doku.php?id=introcomp:a2sp19
2/6
14/07/2019
introcomp:a2sp19 [EEE computing]
Thedatastructureforthegridneedstobeaonedimensionalvectorcontainingthedatarowbyrowaswehavedonefor thesudokucheckerprogram. And,asinthesudokucheckerprogram,oneofthebuildingblocksofyourprogramwillbeafunctionliketwod_to_onedin ordertomapthebidimensionalindiceswehaveinmindwhenwedealwiththegrid,tothecorrespondingone-dimensional indexontheone-dimensionalvector. Thefollowingisalistoffunctionsyouhavetodefineanduseinyourprogram. Thesewillbeautomaticallytested,soit'simportantthatyoukeepthenameandparameterlistasspecifiedandfollowallthe instructionsverycarefully. Itisalsoimportantthatyoumakegooduseoftheminyourdesignoftheprogram:perfunctoryinclusioninthecodejustto sayyouhavefollowedtherequirementsisnotenough. Youcandefineanduseotherfunctionsinadditiontotheseandyoucandefineanduseotherfunctionstobuildthe implementationofthesefunctions. proc_num bool proc_num(std::vector& v, int bi, int ei)
Thefirstparameterisavectorofint(v)whichthefunctionusesbothforinputandoutput.Parametersbiandeiareint andtheyareusedforinput.Thefunctionreturnsabool. Theexamplesbelowspecifytherequiredbehaviourofthefunction: Example1: v (before): [0 0 4 0 0 0 3 0 0 3 0 3 2 0 0 5 0 6 4] bi: 4 ei: 14 --v (after): [0 0 4 0 6 3 2 0 0 0 0 0 0 0 0 5 0 6 4] return: true
Example2: v (before): [0 0 0 5 0 2 0 0 2 0 0 0 2 0 0 2 6 0 0 5 0 6 4] bi: 0 ei: 23 [or v.size()] --v (after): [5 4 4 6 5 6 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] return: true
Example3: v (before): [0 0 0 5 1 2 0 2 2 0 0 0 2 0 0 2 6 0 0 5 0 6 4] https://intranet.ee.ic.ac.uk/m.cattafi/scripts/dw/doku.php?id=introcomp:a2sp19
3/6
14/07/2019
introcomp:a2sp19 [EEE computing]
bi: 3 ei: 7 --v (after): [0 0 0 5 1 2 0 2 2 0 0 0 2 0 0 2 6 0 0 5 0 6 4] return: false
Example4: v (before): [0 0 0 5 1 2 0 2 2 0 0 0 2 0 0 2 6 0 0 5 0 6 4] bi: 1 ei: 4 --v (after): [0 5 0 0 1 2 0 2 2 0 0 0 2 0 0 2 6 0 0 5 0 6 4] return: true
Notethateiisnottheindexofthelastelementinvolvedinthetransfor mation,ratherit's1+theindexofthelastelement involvedinthetransformation.(Itisquitecommon,whenrangesareexpressedinC++,thattheleftextremeisincludedin theintervalwhiletherightoneisn't.) rotate_anti_clock void rotate_anti_clock(std::vector& v)
Thisfunctionperformsan“anti-clockwiserotation”ofthegridcontainedinvectorv(againthisisbothaninputandoutput parameter). Forexampleifvinitiallycontains1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16,thefunctionshouldchangethecontentto4 8 12 16 3 7 11 15 2 6 10 14 1 5 9 13. Notethatthisisatypicalexampleinwhichtwod_to_oned,asmentionedabove,mustbeusedforthesakeofgooddesignand readability. game_over bool game_over(const std::vector& v)
Thisfunctionshouldreturntrueifit'struethatthecontentofvrepresentsagameoversituation,andfalseotherwise. (Aninputfilemaywellcontainagridalreadyinagameoversituation,inwhichcasetheprogramshouldjustprintgameover asinthescreenshotabove.) print_grid void print_grid(const std::vector& v)
Thisfunctionshouldprintthecontentofvasannxngrid(asshowninthescreenshots). Functionsotherthanthemainandprint_gridshouldnotcontainuserorfileinputoroutput(cin,cout,ifstreametc)in theirimplementation.
https://intranet.ee.ic.ac.uk/m.cattafi/scripts/dw/doku.php?id=introcomp:a2sp19
4/6
14/07/2019
introcomp:a2sp19 [EEE computing]
Guidelines Allthevariablesshouldbedeclaredinthescopeofafunction(eitherthemainorsomeotherone).Inotherwords,global variablesarenotallowed. Alltheloopsshouldbecontrolled/terminatedeitherbytheloopconditionorbyreturn.Statementssuchasbreak,continue, gotoarenotallowedanywhereintheprogram. Donotusetheswitchstatement. Functionsshouldnotreturnvectorsprovidedasoutput.Inotherwords,vectorsmustbeprovidedinoutputusingoutput parameters(i.e.passingbyreference). Onlyheadersfromthestandardlibraryareallowed(ifyouarenotsurewhetheraheaderispartofthestandardlibraryor not,trycompilingontheLinuxinstallonthelabcomputers,ifitworksit'spartofthestandardlibrary),howevertheuseof theheaderisnotallowed. Donotusetheusing namespacedirective. OurreferencestandardintermsofcompilerandruntimeenvironmentistheLinuxinstallonthelabcomputers.Inother words,ifyourprogramdoesn'tcompileorworkcorrectlyonLinuxonthelabcomputers,forourpurposesitmeansitdoesn't compileorworkcorrectlyanditwillbeassessedassuch(nomatterthebehaviourshownonyourowncomputerorother computers).Itisyourresponsibilitytoensurecompliancewiththisstandardandyouarestronglyadvisedtocompileandtest yourprogramonLinuxonthelabcomputersbeforethesubmission. It'soktouseC++11features;ifyoudo,youwillneedtocompileyourprogramonthelabcomputersincludingthestd=c++11flag.
Q&A: Howarethesubmissionsgoingtobeassessed?Isitenoughiftheprogramworks? Correctnessisofcourseimportant.Automatedtestswillberunforeachofthefunctionstocheckcorrectness(includingthe testsprovidedasexampleinthecommentsofthesourcefile).Eachfunctionwillneedtopassallthetestsinordertobe consideredcorrect. Thesubmissionswillalsobecheckedtomakesurethatalltheinstructionsandguidelinesarefollowed. Forinstanceifaprogramusesglobalvariables,oriftherearechangesintheprovidedfunctioninterfaces,orifthefunctions don'tactuallydowhatisrequestedinthedescriptionsprovided,etc.thisisgoingtoresultina(potentiallymuch)lowermark (eveniftheprogram“works”). Itisimportantingeneralthatthecodeisclearandreadable.Indentationneedstobefollowedverycarefully.Variablesshould haveusefulnames. TosomeextentIbelievethatyouhavealreadydonetheexperienceofwritingapieceofcode,gettingtoaversionwhich worksandthendoingsomerefinementortidyingup.Asyoucanimagineyouareexpectedtoperformthisprocessalsofor yoursubmission.Ifyouarenotsureabouthowtodothis,askmetogothroughtheprocesswithyouforoneofyourexercises (nottheassignment).
Whataboutefficiency? Thealgorithmsthatshouldbeimplementedinthisprogramarequitestraightforwardanddon'treallyoffermuchforreal efficiencydifferences.Pleasenotethatdeclaringonelessvariableandotherthingslikethatarenotmoreefficientforour purposesandfallinthecategoryofthe“clevertricks”(seequestionbelow). Ofcourseit'sagoodtouchtoavoidwastefulnessasintheexamplesinthenotes.Thinkoftheprimenumberexample,in whichtheloopterminatesassoonasthefirstfactorisfound(insteadofgoingontotryotherpotentialfactors)orassoonas thesquarerootofthenumberisreached(insteadoftryingallthenumbersuptotheinput). https://intranet.ee.ic.ac.uk/m.cattafi/scripts/dw/doku.php?id=introcomp:a2sp19
5/6
14/07/2019
introcomp:a2sp19 [EEE computing]
(ForEIE1students:)InIntroductiontoComputerArchitectureweoftenneedtominimize thenumberoflinesofcode.Doesthesameapplyhere? Noandinfactaggressivelyshortercode(orcodewith“clevertricks”)mightbepenalisedasnotveryreadableanderror prone.
Whataboutcomments? Wearenotreallygoingtoconsidercommentsinthissubmission.Feelfreetoaddsomeifyouthinktheywouldbeusefulfor yourselftounderstandyourowncodeortoexplainsomeaspectsofyourprogram.Butitismoreimportantthatyourcodeis clearandreadable(minimisingtheneedforcomments). Donotaddtoomanycomments:thatwillonlymakeuswastetimebyhavingtoremovethembeforewecheckyour submission.
Whenwillthegradesbeavailable? ByMonday4thMarchgradesandfeedbackforthesubmissionsnotpresentingparticularissueswillbeavailableon Blackboard(Iwillsendanotificationemail).
CanyouoraTAhelpmewiththeassignment? Nobecauseit'sassessed.Wecanhoweverhelpyouwithotherexercisesonthetopicscoveredbytheassignment.
Ihaveanotherquestion. Sure,youcansendmeanemailbut: 1)Firstpleaserereadthispageanddoublecheckthattheanswerisn'talreadyhereorelsewhereonthiswebsiteor Blackboard. 2)MakesureyousendyouremailbyThursday7thFebruarytoallowtimeforareply.
introcomp/a2sp19.txt·Lastmodified:2019bymc
https://intranet.ee.ic.ac.uk/m.cattafi/scripts/dw/doku.php?id=introcomp:a2sp19
6/6...