Programacion Mit
Transcript of Programacion Mit
-
7/29/2019 Programacion Mit
1/4
MassachusettsInstituteofTechnologyDepartmentofElectricalEngineeringandComputerScience
6.087:PracticalProgramming inCIAP2010
Lab1: GameofLifeIn-Lab:Wednesday,January13,2010 Due:Tuesday,January19,2010OverviewTheGameofLife,inventedbyJohnConwayin1970,isanexampleofazero-playergameknownasacellularautomaton. Thegameconsistsofatwo-dimensionalworldextending infinitely inalldirections,divided intocells. Eachcell iseitherdeadoraliveatagivengeneration. Thegame consists of a set of rules thatdescribehow the cells evolve from generation to generation.These rules calculate the state of a cell in thenext generation as a function of the states of itsneighboring cells in the current generation. In a 2-Dworld, a cells neighbors are those 8 cellsvertically,horizontally,ordiagonallyadjacent to thatcell. Conways setof rulesare summarizedas:1. A livecellwithfewerthantwo liveneighborsdies.2. A livecellwithmorethanthree liveneighborsalsodies.3. A livecellwithexactlytwoorthreeliveneighbors lives.4. Adeadcellwithexactlythree liveneighborsbecomesalive.
In this lab, we will be implementing Conways Game of Life, with theminor restriction thatour 2-Dworld isfinite. Theneighbors of a cell on the edge of theworld thatwouldbebeyondthe edge are assumed dead. You can readmore aboutConwaysGame ofLife onWikipedia athttp://en.wikipedia.org/wiki/Conways_Game_of_Life .Example(theso-calledglider):
| | | | | | || | | |*| | || |*| |*| | || | |*|*| | || | | | | | |
| | | | | | || | |*| | | || | | |*|*| || | |*|*| | || | | | | | |
Legend: *=alive, =deadPart A: Implementing Evolution (In-Lab)Inthispart,wewillfocusonimplementingtherulesneededtoevolvetheworldfromonegenerationtothenext. Toassistyou,weprovide several functionsyoushoulduse toaccessandmodifythecurrentandnextstateoftheworld. These functionsaredescribed in theheaderfilelifegame.hand implemented in thefile lifegame.c. Also,wehaveprovideda skeletondescribingwhatyouneedtodoforthispart inlab1a.c.
1
http://en.wikipedia.org/wiki/Conway's_Game_of_Lifehttp://en.wikipedia.org/wiki/Conway's_Game_of_Life -
7/29/2019 Programacion Mit
2/4
Before getting started, you should copy all these files from the locker (/mit/6.087/Lab1/) intoyourworkingdirectory. Whencompiling,youneedtocompilelab1a.candlifegame.ctogethertogenerateasingleexecutablefile(letscall itlab1a.o)withallthecode in it(otherwise,youllgetundefinedreferenceerrors).Heresanexamplecommand lineforcompilingthiscode:
athena% gcc -g -O0 -Wall lab1a.c lifegame.c -o lab1a.oathena%
Startby examining the contents of lifegame.h and lab1a.c. Youneed tofill in a few lines inmain()andcompletethefunctionsnext generation(),get next state(x,y),andnum neighbors(x,y).Thereisnoneedtomodifythefileslifegame.horlifegame.cinthispart.(a) Howwillyou initialize theworld for theGameofLife? Write theappropriate code in the
main()function.(b) Howwillyououtputthefinalstateoftheworldoncealltheevolutionsaredone?Writethe
appropriatefunctioncall inmain().(c) Themain() function calls next generation() for each generation tohandle the evolution
process. Yourcodeshouldseteachcellsstate inthenextgenerationaccordingtotherulesspecified in theOverview of this handout. Once the states of all the cells have been setfor the next generation, calling finalize evolution()will set the currentworld state tothenextgenerationand reset thenextgeneration state. Yourcode shouldmakeuseof theget next state(x,y)functiontocomputethenextstateofeachcell.
(d)Writethecode forget next state(x,y),sothe functionreturnsthenextstate(ALIVEorDEAD)ofthecellatthespecifiedcoordinatesusingthenumberof liveneighbors(returnedbythenum neighbors(x,y)function)andtheGameofLiferules.
(e) Fill in the function num neighbors(x,y), so it returns the number of live neighbors (cellsvertically,horizontally,ordiagonallyadjacent)forthespecifiedcell. Sinceourworldisfinite,adjacentcellswhicharebeyondtheedgeoftheworldarepresumedDEAD.
Nowthatyouredone,compileandruntheprogram.FeelfreetochangethedefinitionofNUM GENERATIONS,butwhenyourereadytobecheckedoff,makesureNUM GENERATIONS=50andshowyourprogramsoutputtotheLabAssistant.
Checkoff:Part B: The World in a FileInthefirstpartofthis lab,the initialstateoftheworldwashard-codedintolifegame.candthefinalstateoftheworldwasoutputtotheconsole. Inthispart,wewillmodifythecodesothattheinitialstateoftheworldcanbereadfromafile,andthefinalstate isoutputtoafile.First,letsexaminelifegame.c.Noticethefunctionsyouneedto implement:initialize world from file(filename)andsave world to file(filename).
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
21
1
-
7/29/2019 Programacion Mit
3/4
(a) Thefirstofthese,initialize world from file(filename),readsthefilespecifiedbyfilenameandinitializestheworldfromthepatternspecifiedinthefile.Basically,thefileisamatrixofcharacters,*tospecifyalivecell,and (space)tospecifyadeadcell.Theithcharacterofthejthline(zero-indexed)representstheinitialstateofthecelllocatedat(i,j). Ifthelinedoesnt contain enough characters or the filedoesnt contain enough lines, the unspecifiedcells
are
presumed
dead.
Fill
in
the
function
to
read
the
file
and
initialize
the
world.
Dont
forgettoresetallthenextgenerationstatestoDEAD.Useappropriateerrorhandling.
(b) Theotherfunction,save world to file(filename),savesthefinalstateoftheworldtothefilefilename inthesame formatasused inthe initialization function: the ithcharacterofthejth line(zero-indexed)representsthestateofthecelllocatedat(i,j) intheworld.
(c) Fill in the contents of lab1b.c using the code from PartA (lab1a.c) andmodifying tocallthese functions. Thenameofthefileto loadwillbespecified inthefirstcommand lineargumentpassedtotheexecutable. Ifnofileisspecified,youshoulddefaulttoinitializingtheworldtothehard-codeddefaultgliderpattern. Savethefinaloutputtothefileworld.txt.
Tohelp you test your code,weve provided a couple testfiles: glider.txt (shouldmatchyouroutputfromPartA)andsship.txt(output insshipout.txt).Tofinish,writeabrief (1pagemax.) lab reportdescribingyourexperience completing this lab,whatchallengesyoufacedandhowyouaddressedthem,andwhatyoulearnedfromthislab.Turninazipfilecontainingallyourcode(lab1a.c,lab1b.c,lifegame.h,andlifegame.c),andyourlabreport,usingtheonlinesubmissionsystemonStellar.Congratulations,youredone!
3
-
7/29/2019 Programacion Mit
4/4
MIT OpenCourseWarehttp://ocw.mit.edu
6.087 Practical Programming in C
January (IAP) 2010
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
http://ocw.mit.edu/http://ocw.mit.edu/termshttp://ocw.mit.edu/termshttp://ocw.mit.edu/