还剩12页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
/*A simplegame*//*CopyRight:Guanlin*/#includestdio.h#includestdlib.h#includestring.h#includetime.h#includeconio.h#include process.hstruct object_fix{char name
[20];char id
[5];char desc
[500];char action
[30];char im
[5];;}struct object_move{char name
[20];char id
[5];char desc
[500];int loc;int pwr;int strg;char im
[5];;}struct rover{char name
[20];char id
[5];char desc
[500];int pwr;int strg;int location
[2];char im
[5];;}struct map/*this isthe mapstructure*/{char data
[20];char add_data
[20];int amount;int x;/*this werethe successorkeeps itsxy values*/int y;;}struct location/*this structureis for the successorlister*/float height;char obj;break;case3:add_objnumber,number_2,rsny,rsld,mnt,dst,vshr,vgnt,mtre,wshl,wdp,aagr,anon,rvr,pstn,p_rover;break;default:printfchoose anumber fromthe list\nH;}}void directionint input,struct map number,struct map*number_2,struct object_fix*rsny,struct object_fix*rsld,struct object_fix*mnt,struct object_fix*dst,struct object_fix*vshr,struct object_fix*vgnt,struct object_fix*mtre,struct object_fix*wshl,struct object_fix*wdp,struct object_move*aagr,struct object_move*anon,struct object_move*rvr,structobject_move*pstn struct rover*p_roverswitchinput{zcase119:/*w=up*/ifp_rover-location
[0]0{p_rover-location
[0]—;p_rover-pwr;}else p_rover-location
[0]=7;break;case115:/*s=down*/ifp_rover-location
[0]7{p_rover-location
[0]++;p_rover-pwr-;}else p_rover-location
[0]=0;break;case97:/*a=left*/ifp_rover-location
[1]0{p_rover-location[l];p_rover-pwr—;}else p_rover-location
[1]=7;break;case100:/*d=right*/ifp_rover-location
[1]7{p_rover-location[l]++;p_rover-pwr--;}else p_rover-location
[1]=0;break;default:printfInvalid operator!\n\nn;break;}}int controlint inputinput=_getch;return input;}void mainvoid{int menu;int quit=0;int input=0;struct object_fix rsny,rsld,mnt,dst,vshr,vgnt,mtre,wshl,wdp;struct object_move aagr,anon,rvr,pstn;struct roverp_rover;struct mapnumber,number_2;number.amount=0;/*setting theinitial amountof aliensas0*/number
2.amount=0;strcpyrsny.name,Sandy Rock;strcpyrsny.id,RSNY;strcpyrsny.desc A rock made of softermaterial thatcan be destroyed orcollected;zstrcpyrsny.im/*H;strcpyrsld.name,HSolid Rock;strcpyrsld.id,RSLD;strcpyrsld.desc,Arockmadeofhard materialthat cannot bedestroyed,but can be collectedoravoided;strcpyrsld.im O;zstrcpymnt.name Mountain;z;strcpymnt.id,”MNT“strcpymnt.desc,A largeobstacle madeof differentmaterials thatcan onlybe avoided;strcpymnt.im,AA/KA;strcpydst.name/Dust;strcpydst.id,DST;strcpydst.desc,Small particlesof sand,rock,etc.that mayreduce visibilityor slowdown thevehicle,can be driven throughbut hasextra power consumption;strcpydst.im.zstrcpyvshr.name,HSheer Valley;strcpyvshr.id/,VSHRH;strcpyvshr.desc,A depressionin theland withsheer sides,it cannotbe driven through andmustbe avoided;strcpyvshr.im,HVVV\/;strcpyvgnt.name,Gentle Valley;strcpy vgnt.id VGNT1;zstrcpyvgnt.desc,A depressionin theland withgentle sides,it can bedriven throughwith extra powerconsumptionor can be avoided;・strcpyvgnt imJvvvv;strcpymtre.name,Martian Tree;strcpymtre.id,MTREH;strcpymtre.desc/A woodyplant nativeto Mars,it mayhave thesame characteristicsas anEarth*tree;strcpymtre.im,\330;strcpywshl.name,Shallow Water;strcpywshl.id,WSHL;strcpywshl.desc,A shallowbody ofwater thatcan bedriven through,can bein theform ofasmall puddleor a large lake;strcpywshl.im,n\260\260\260\260H;strcpywdp.name,Deeper Water;strcpyCwdp.id/WDP;strcpywdp.desc,A deeperbody ofwater thatcannotbedriventhrough,commonly inthe formofalargelake orriver;strcpywdp.im,,,\262\262\262\262;strcpyaagr.name/Aggressive Alien;strcpyaagr.id,,,AAGRH;strcpyaagr.desc,HAn aliencreature that will moveupon itsown accord,may becomeaggressiveon contact*;strcpyaagr.im*_*;zstrcpyanon.name,Non-Aggressive Alien;strcpyanon.id/,ANONH;strcpyanon.desc,HAn aliencreature thatwill moveupon itsown accord,will notbecomeaggressive oncontact1;strcpyanon.im,strcpyrvr.name,Rover;strcpyCrvr.id/RVR;strcpyrvr.desc,A MarsRover vehicleto transportaround themap,it candestroy,collect oravoid objects.It hasapowerand objectstorage1;strcpyrvr.im,*#;strcpypstn.name,Power Station*;strcpyCpstn.id/^STN;strcpypstn.desc/A movablepower stationthatwillrestore powerto theMars Rovers;strcpypstn.im,\025;strcpyp_rover.name,User MarsRover;;strcpyp_rover.id,”URVR”strcpyp_rover.desc,A Marsvehicle thatis controlledby the user.Explores themap collecting,destroying andavoiding objects;strcpy p_ro ver.i m,A A;p_rover.pwr=100;p_rover.strg=0;p_rover.location
[0]=7;p_rover.location[l]=0;t1K K1f IlJl***\n\nH;do{/*Object willbe gainedfrom mapper*/printf”+++++++++++++++++++++++++++++++MENU+++++++++++++++++++++++++++++++++\rT;printfPlease chooseone ofthe followingoption:\n;printfl-Start\n;printf2-Controls\n;printfu3-Show Map\nH;printfu4-Help Menu\n;printf5-Quit\n\n;printf---------------------n;scanf%d,menu;switch menu{case1:show_mapnumber number_2,rsny,rsld,mnt,dst,vshr,vgnt,mtre,wshl,zwdp,aagr,anon,rvr,pstn,p_rover;while quit!=1{input=controlinput;if input==27quit=1;elseifp_rover.pwr0{directioninput,number,number_2,rsny,rsld,mnt,dst,vshr,vgnt,mtre,wshl,wdp,aagr,anon,rvr,pstn,p_rover;show_mapnumber,number_2,rsny,rsld,mnt,dst,vshr,vgnt,mtre,wshl,wdp,aagr,anon,rvr,pstn,p_rover;printfRover stats:\nPower:%d%%\nStorage:%d%%\n p_rover.pwr,p_rover.strg;z}else{・printf”*******************END GAME********************\n\n”//ends gamewhencharge=
0.break;}}}break;case2:quit=0;printf\n;printfn***\n;printf Controls\n;//menu forthe keysused onthe keyboardto movethe roverin differentdirections.printfII Down:S\n;printfII*Left:A*\n;printfII*Right:D*\nn;printfII choose:Enter\nn;printfprintf Up:W\n;break;case3:quit=0;show_mapnumber,number_2,rsny,rsld,mnt,dst,vshr,vgnt,mtre,wshl,wdp,aagr,anon,rvr,pstn,p_rover;break;case4:quit=0;helpnumber,number_2,rsny,rsld,mnt,dst,vshr,vgnt,mtre,wshl,wdp,aagr,anon,rvr,pstn,p_rover;break;case5:break;}}whilemenu!=5;printf\n--------Please pressany keyto exitgame---------\n;getch;}};void stats_updateint selected,struct rover*p_rover{switch selectedcase1:ifp_rover-pwr7printf\n\nYou donot haveenough powerto performthis action!\n\n;else{p_rover-pwr-=7;printfHYou havedestroyed the object!\n\n;}break;case2:ifp_over-pwr3「printf\n\nYou donot haveenough powerto performthis action!\n\n;else ifp_rover-strg90printf\n\nYou donot haveenough storagespace forthis object!\n\n;else{p_rover-pwr-=3;p_rover-strg+=10;printfnYou havecollected asample oftheobject!\n\nn;}break;case3:p_rover-pwr-=10;/Distance aroundobject-value gainedfrom mappermodule.1square=-1power*/printfYou haveavoided theobject!\n\n;break;case4:p_rover-pwr-=2;printfYou havedriventhroughthe obstacle!\n\nH;break;case5:ifp_rover-pwr==100printf\n\nYou donot needto chargeup!\n\n;else{p_rover-pwr=100;printfYou havecharged upyour rover!\n\n;}break;default:printf,,\n\n*****ERROR*****\nInvalid Selection\n\n;break;}}void actioncharobject,struct rover*p_rover{int selection;switchobject{case1:printf\nYou haveencountered:A SandyRock\n\nH;printfThis objectcan be:\nl.\tDestroyed\n
2.\tCollected\nPlease chooseaction1or2\t;scanf%d,selection;stats_updateselection,p_rover;break;case2:printfH\nYou haveencountered:A SolidRock\n\nH;printfThis objectcan be:\nl.\tAvoided\n
2.\tCollected\nPlease chooseaction1or2:\t;scanf%d,selection;if selection==1selection=3;stats_updateselection,p_rover;break;case3:printf\nYou haveencountered:A Mountain\n\n;printfThis objectcan be:\n
1.\tAvoided\nPlease enterl:\f;scanf%cT,selection;selection=3;stats_updateselection,p_rover;break;case4:printf\nYou haveencountered:Dust\n\n;printfThis objectcan be:\nl.\tDriven through\n
2.\tCollected\nPlease chooseaction1or2:\tH;scanf%d,selection;if selection==1selection=4;stats_updateselection,p_rover;break;case5:printf\nYou haveencountered:A SheerValley\n\n;printfThis objectcan be:\n
1.\tAvoided\nPlease enterscanf%d,selection;selection=3;stats_updateselection,p_rover;break;case6:printf\nYou haveencountered:A GentleValley\n\n;printfThis objectcan be:\nl.\tDriven through\n
2.\tAvoided\nPlease chooseaction1or2\t;scanf%d selection;zif selection==1selection=4;if selection==2selection=3;stats_updateselection,p_rover;break;case7:printf\nYou haveencountered:A MartianTree\n\n;printfThis objectcan be:\nl.\tDestroyed\n
2.\tCollected\n
3.\tAvoided\nPlease chooseaction1,2or3:\tn;%,scanf d”selection;stats_updateselection,p_rover;break;case8:printf\nYou haveencountered:Shallow Water\n\n;printfThis objectcan be:\nl.\tDriven through\n
2.\tCollected\n
3.\tAvoided\nPleasechoose action1,2or3:\t;scanf%d,selection;if selection==1selection=4;stats_updateselection,p_rover;break;case9:printf\nYou haveencountered:Deep Water\n\n;printfThis objectcan be:\nl.\tAvoided\n
2.\tCollected\nPlease chooseaction1or2:\tn;scanf%cT,selection;if selection==1selection=3;stats_updateselection,p_rover;break;case10:printf\nYou haveencountered:An Aggressive Alien\n\nH;printfThis objectcanbe:\n
1.\tDestroyed\nPlease enterl:\t;scanf%d,selection;selection=1;stats_updateselection,p_rover;break;case11:printfH\nYou haveencountered:A Non-AggressiveAlien\n\nn;printfThis objectcanbe:\n
1.\tAvoided\nPlease enterl:\f;scanf%d,selection;selection=3;stats_updateselection,p_rover;break;case12:printf\nYou haveencountered:Another Rover\n\n;printfThis objectcanbe:\nl.\tAvoided\nPlease enter%scanf cT,selection;selection=3;stats_updateselection,p_rover;break;case13:printf\nYou haveencountered:A PowerStation\n\n;printfHYou can:\nl.\tCharge up\n
2.\tAvoid\nPlease chooseaction1or2:\tn;scanf%d,selection;if selection==1selection=5;if selection==2selection=3;stats_updateselection,p_rover;break;default:;printf“\n\n*****ERROR*****\n\n“break;}}void show_mapstruct mapnumber,struct map*number_2,struct object_fix*rsny,structobject_fix*rsld,struct object_fix*mnt struct object_fix*dst,struct object_fix*vshr,structzobject_fix*vgnt,struct object_fix*mtre,struct object_fix*wshl,struct object_fix*wdp,structobject_move*aagr,struct object_move*anon,struct object_move*rvr,struct object_move*pstn,struct rover*p_rover/*the show map functioncalling numberand number_2from themapstructure tosee ifmoveable objectsare needed*/{struct map f_map
[8]
[8];/*8by8map*/inti,j,rx,ry,object;/*this isyour x and y value in your mapf_map*/systemCcls1;/*thisis yourxand yvalueinyourmapf_map*///srandtimeNULL;/calling thetime fromincludeto gatherrandom variables*/fori=0;i8;i++/*for loopto copyall yourfixed objectlistsinto f_map sothey canbe displayed*/{forj=0;j8;j++strcpyf_map[i][j].data;z}if number-amount0/*this islooking atthe addfunction tosee weatheror notthere arealiens in f_map*/{fori=0;inumber-amount;i++/*allocating the number of aliensinf_map*/strcpyf_map[rand%8][rand%8].data,anon-im;/*randomizing theirposition*/}ifnumber_2-amount0/repeat ofabove justfor roversinstead*/{fori=0;inumber_2-amount;i++strcpyf_map[rand%8][rand%8].data,rvr-im;}rx=p_rover-location
[0];ry=p_rover-location
[1];strcpyf_map[l][O].data,wdp-im;strcpyf_map
[4]
[0].data,mnt-im;strcpyf_map
[5]
[0].data,mnt-im;strcpyf_map
[3][l].data,dst-im;strcpyf_map
[4][l].data dst-im;zstrcpyf_map
[3]
[2].data,dst-im;strcpyf_map
[4]
[2].data,dst-im;strcpyf_map[O]
[3].data,mnt-im;strcpyf_map[l]
[3].data,mnt-im;strcpyf_map
[3]
[3].data,vshr-im;strcpyf_map
[4]
[3].data,dst-im;strcpyf_map
[5]
[3].data,vgnt-im;strcpyf_map
[3]
[4].data,vshr-im;strcpyf_map
[4]
[4].data,dst-im;strcpyf_map
[5]
[4].data,vgnt-im;strcpyf_map
[2]
[5].data,wshl-im;strcpyf_map
[3]
[5].data,wshl-im;strcpyf_map
[4]
[5].data,wshl-im;strcpyf_map[l]
[6].data,pstn-im;strcpyf_map
[2]
[6].data,wdp-im;strcpyf_map
[3]
[6].data,wdp-im;strcpyf_map
[4]
[6].data,wshl-im;strcpyf_map
[7]
[6].data,mnt-im;strcpyf_map[O]
[7].data,mnt-im;strcpyf_map[l]
[7].data,wdp-im;strcpyf_map
[2]
[7].data,wshl-im;strcpyf_map
[3]
[7].data,wshl-im;strcpyf_map
[6]
[7].data,mnt-im;strcpyf_map[rx][ry].data,p_rover-im;ifrx==1ry==011rx==2ry==6||rx==3ry==6||rx==1ry==7{object=9;actionobject,p_rover;}else ifrx==4ry==011rx==5ry==011rx==0ry==311rx==1ry==311rx==7ry==611rx==0ry==711rx==6ry==7{object=3;actionobject p_rover;z}else ifrx==3ry==111rx==4ry==111rx==3ry==211rx==4ry==211rx==4ry==311rx==4ry==4object=4;actionobject p_rover;z}else ifrx==3ry==311rx==3ry==4{object=5;actionobject p_rover;z}else ifrx==5ry==311rx==5ry==4{object=6;actionobject p_rover;z}else ifrx==2ry==511rx==3ry==511rx==4ry==511rx==4ry==6||rx==2ry==711rx==3ry==7{object=8;actionobject,p_rover;}else ifrx==1ry==6{object=13;actionobject p_rover;z}i=0;/*re-allocate ito0so mapis printedfrom start*/fori=0;i8;i++/*8by8map*/{printfH------+—+—+-----------F------H------+-------F—+\n;forj=0;j8;j++/*8by8map*/{ifstrlenf_map[i][j].data!=O/*if functionto printnothing but4spaces ifthere innothingallocated in[i][j]*/printf|%4s,f_map[i][j].data;elseprintf|H;/*end ofcoloumn visible map*/}printf|\n;/*end oflast coloumnvisiblemap*/}printf+——+——+——+——+——+——+——+——+\n;/bottom ofmap*/printf\n;;printf270”、printfGroup B\n;}void add_objstruct map*number,struct map*number_2,struct object_fix*rsny,structobject_fix*rsld,struct object_fix*mnt,struct object_fix*dst,struct object_fix*vshr,structobject_fix*vgnt,struct object_fix*mtre,struct object_fix*wshl,struct object_fix*wdp,structobject_move*aagr,struct object_move*anon,struct object_move*rvr,struct object_move*pstn,struct rover*p_rover/*add movable object function,*number=aliens,*number_2=rovers*/{int object,t,f;char alien;char rover;printfThis isthe add movableobjectfunction\nn;printfhow manyaliens wouldyou like\nH;scanf%d,t;/*user inputof amountof aliens*/number-amount=t;/*saving thenumber instructure map-amount*/printfuhow manyrovers wouldyou like\n;%scanf d”,f;/*user inputof amountof rovers*/number_2-amount=f;/saving thenumber instructure map-amount*/show_mapnumber,number_2,rsny,rsld,mnt,dst,vshr,vgnt,mtre,wshl,wdp,aagr,anon,rvr,pstn,p_rover;/*go to showmapfunction withthenumberofaliensand numberofrovers*/}void successor/*this functionsaskes theuser forthe locationand thenwere theywant to gofrom there,printing outthat location*/struct maplocation;int menu;int ncolnrow;/*we canchange then valuedepends howlarge themap youneed.*/zncol=8;nrow=8;printfThis isthe successor function\n;printfEnter the\nx=\ty=\n;scanf%d%d location.x,location.y;/saving locationin maplocation xandy*/zprintfEnter operatorchoice\nH;printfl=left\n;printf2=right\n;printf3=upwards\n;printf,,4=down\n;printf,,5=quit\n,,;scanf%d,menu;switchmenu/*menu switchtoshowthe positionabove,underneith,and nextto therover*/{case1:location.x=location.x-1;printfThe positionis[%d,%d]\n,location.xjocation.y;break;case2:location.x=location.x+1;printfThe positionis[%d,%d]\n,location.x,location.y;break;case3:location.y=location.y-1;printfThe positionis[%d,%d]\n“,location.x,location.y;break;case4:location.y=location.y+l;printfThe positionis[%d,%d]\n,location.xjocation.y;break;case5:printf*****back toprogram*****\nn;/*Quits theprogram andprints outthe message*/break;default:printf*****Please enteran integerfrom l-5*****\n;/*A defaultoption iftheuserenters anincorrectvalue*/break;}printf\n;}void helpstruct mapnumber,structmap*number_2,struct object_fix*rsny,struct object_fix*rsld,struct object_fix*mnt,struct object_fix*dst,struct object_fix*vshr,struct object_fix*vgnt struct object_fix*mtre,struct object_fix*wshl,struct object_fix*wdp,structzobject_move*aagr,struct object_move*anon,structobject_move*rvr,structobject_move*pstn,structrover*p_rover/*help function*/{inti;char mov_obj;printf++This isthe helpfunction++\nn;printfu|1-successor|\n;printf|2-addmovableobj|\nH;printf|3-remove moveablaobj|\n\n;printfn\t***\n“;printf\t Index:\n\n;printf\t SandyRock:*\n;printf\t SolidRock:o\n;printfn\t Water:\262\262\262\262\nn;printfn\t Shallowwater:\260\260\260\260\n;;printf\t Mountain:\n“八八人人printf\t Dust:….\n;printf\t*sher valley:VVVV*\n;printf\t shallowvaley:vvvvrT;printf\t powerstation:\025\n;printf\t alien:*_*\n;printf\t otherrovers:#\n;printf\t***\n;%scanf”d”,i;switchi/switch menutogoto thesuccessorfunction,ortheadd/remove function*/{case1:successor;break;case2:add_objnumber,number_2,rsny,rsld,mnt,dst,vshr,vgnt,mtre,wshl,wdp,aagr,anon,rvr,pstn,p_rover;。
个人认证
优秀文档
获得点赞 0