1.1. simplegame.cpp ¶
// 기. all rights reserved. //simplegame.cpp simple huh? #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <io.h> #define MAX_STRING 100 #define MAX_TOWN 5 #define MAX_ENEMY 5 #define MAX_ITEMS 40 #define FILE_NAME "csave.dat" typedef struct{ int base_hp; int hp; int max_hp; int att; int base_att; int def; int base_def; char name[MAX_STRING]; int townNum; int level; unsigned int xp; unsigned int next_xp; int money; }entity; typedef struct{ char name[MAX_STRING]; int att; int def; int hp_plus; char tooltip[MAX_STRING]; }inventory; typedef struct{ char name[MAX_STRING]; }town; town T[MAX_TOWN]; inventory inven[MAX_ITEMS]; inventory tmpInven; int i_top=-1; inventory storage[3]; int cost[3]; entity Main; entity Enemy[MAX_ENEMY]; FILE *state; bool gameover=false; void init(bool first); void itemPush(inventory item); inventory itemFind(int index); void discardItem(int index); void store(); void travel(town dest,int distance,int townNum); void setTravelDest(); void battle(); void rest(); void viewItems(); void setStat(); void start(bool first); void buyItem(inventory item,int index); bool loadGame(FILE *state){ int count=0; i_top=-1; inventory tmpItem; state=fopen(FILE_NAME,"r"); if(state==NULL){ return false; } printf("Loading...\n"); char buff[MAX_STRING]; while(strcmp(buff,"EOF")!=0){ fscanf(state,"%s",buff); if(strcmp(buff,"level:")==0) fscanf(state,"%d",&Main.level); else if(strcmp(buff,"base_hp:")==0) fscanf(state,"%d",&Main.base_hp); else if(strcmp(buff,"hp:")==0) fscanf(state,"%d",&Main.hp); else if(strcmp(buff,"base_att:")==0) fscanf(state,"%d",&Main.base_att); else if(strcmp(buff,"base_def:")==0) fscanf(state,"%d",&Main.base_def); else if(strcmp(buff,"name:")==0){ char tmp; char tmpName[MAX_STRING]; int i=0; fscanf(state,"%c",&tmp); do{ fscanf(state,"%c",&tmp); tmpName[i++]=tmp; }while(tmp!='\n'); tmpName[i-1]='\0'; strcpy(Main.name,tmpName); } else if(strcmp(buff,"townNum:")==0) fscanf(state,"%d",&Main.townNum); else if(strcmp(buff,"xp:")==0) fscanf(state,"%u",&Main.xp); else if(strcmp(buff,"next_xp:")==0) fscanf(state,"%u",&Main.next_xp); else if(strcmp(buff,"money:")==0) fscanf(state,"%d",&Main.money); else if(strcmp(buff,"inven:")==0){ int num; fscanf(state,"%d",&num); for(int i=0;i<num;i++){ do{ fscanf(state,"%s",buff); if(strcmp(buff,"name:")==0){ char tmp; char tmpName[MAX_STRING]; int i=0; fscanf(state,"%c",&tmp); do{ fscanf(state,"%c",&tmp); tmpName[i++]=tmp; }while(tmp!='\n'); tmpName[i-1]='\0'; strcpy(tmpInven.name,tmpName); } else if(strcmp(buff,"att:")==0) fscanf(state,"%d",&tmpInven.att); else if(strcmp(buff,"def:")==0) fscanf(state,"%d",&tmpInven.def); else if(strcmp(buff,"hp_plus:")==0) fscanf(state,"%d",&tmpInven.hp_plus); else if(strcmp(buff,"tooltip:")==0){ char tmp; char tmpName[MAX_STRING]; int i=0; fscanf(state,"%c",&tmp); do{ fscanf(state,"%c",&tmp); tmpName[i++]=tmp; }while(tmp!='\n'); tmpName[i-1]='\0'; strcpy(tmpInven.tooltip,tmpName); } }while(strcmp(buff,"END")!=0); itemPush(tmpInven); } } } fclose(state); setStat(); printf("Load complete!\n"); fflush(stdin); getchar(); return true; } void saveState(FILE *state){ state=fopen(FILE_NAME,"w+"); printf("Saving...\n"); fprintf(state,"level: %d\n",Main.level); fprintf(state,"base_hp: %d\n",Main.base_hp); fprintf(state,"hp: %d\n",Main.hp); fprintf(state,"base_att: %d\n",Main.base_att); fprintf(state,"base_def: %d\n",Main.base_def); fprintf(state,"name: %s\n",Main.name); fprintf(state,"townNum: %d\n",Main.townNum); fprintf(state,"xp: %u\n",Main.xp); fprintf(state,"next_xp: %u\n",Main.next_xp); fprintf(state,"money: %d\n",Main.money); fprintf(state,"inven: %d\n",i_top+1); for(int i=0;i<=i_top;i++){ fprintf(state,"name: %s\n",inven[i].name); fprintf(state,"att: %d\n",inven[i].att); fprintf(state,"def: %d\n",inven[i].def); fprintf(state,"hp_plus: %d\n",inven[i].hp_plus); fprintf(state,"tooltip: %s\n",inven[i].tooltip); fprintf(state,"END\n"); } fprintf(state,"EOF"); fclose(state); printf("Save complete!\n"); fflush(stdin); getchar(); } int main(void){ char buff; while(true){ fflush(stdin); printf("<< rpg ver1.0 made by 기>>\n<<그 .>>\n"); printf("1.기\n2.기\n3.\n"); scanf("%c",&buff); switch(buff){ case '1': system("cls"); init(true); start(true); break; case '2': system("cls"); if(loadGame(state)){ init(false); start(false); } else{ init(true); start(true); } break; case '3': return 0; } system("cls"); } return 0; } void init(bool first){ gameover=false; strcpy(T[0].name,"208"); strcpy(T[1].name,""); strcpy(T[2].name,"R&D"); strcpy(T[3].name,""); strcpy(T[4].name,""); if(first){ i_top=-1; inventory item; item.att=1; item.def=0; item.hp_plus=0; strcpy(item.name," "); strcpy(item.tooltip,"구 ."); itemPush(item); Main.base_hp=30; Main.hp=Main.base_hp; Main.base_att=1; Main.base_def=1; Main.townNum=0; Main.level=1; Main.xp=0; Main.next_xp=30; strcpy(Main.name,""); } strcpy(Enemy[0].name,"과"); strcpy(Enemy[1].name,""); strcpy(Enemy[2].name,"권"); strcpy(Enemy[3].name,""); strcpy(Enemy[4].name,""); Enemy[0].att=3; Enemy[0].def=0; Enemy[0].hp=15; Enemy[0].max_hp=15; Enemy[0].money=15; Enemy[0].xp=10; Enemy[0].townNum=0; Enemy[1].att=10; Enemy[1].def=8; Enemy[1].hp=50; Enemy[1].max_hp=50; Enemy[1].money=35; Enemy[1].xp=20; Enemy[1].townNum=1; Enemy[2].att=40; Enemy[2].def=18; Enemy[2].hp=130; Enemy[2].max_hp=130; Enemy[2].money=46; Enemy[2].xp=30; Enemy[2].townNum=2; Enemy[3].att=68; Enemy[3].def=37; Enemy[3].hp=350; Enemy[3].max_hp=350; Enemy[3].money=58; Enemy[3].xp=64; Enemy[3].townNum=3; Enemy[4].att=726; Enemy[4].def=427; Enemy[4].hp=5430; Enemy[4].max_hp=5430; Enemy[4].money=70; Enemy[4].xp=120; Enemy[4].townNum=4; } void start(bool first){ char buff; if(first){ char name[MAX_STRING]; printf(" : "); scanf("%s",name); strcpy(Main.name,name); } setStat(); while(true){ if(gameover) break; system("cls"); printf(" %s .\n 겠까?\n",T[Main.townNum].name); printf("1. \n2.\n3. \n4. \n5.\n6.기\n7.\n"); scanf("%c",&buff); switch(buff){ case '1': system("cls"); store(); break; case '2': system("cls"); rest(); break; case '3': system("cls"); viewItems(); break; case '4': system("cls"); setTravelDest(); break; case '5': system("cls"); saveState(state); break; case '6': system("cls"); loadGame(state); break; case '7': system("cls"); gameover=true; break; } } return; } void rest(){ fflush(stdin); printf(" .\n"); Main.hp=Main.max_hp; getchar(); } void store(){ float num; bool exit=false; srand((unsigned)time(NULL)); num=(rand()%10)/10+rand()%2; if(num<=0.4){ strcpy(storage[0].name," 구()"); storage[0].att=6; storage[0].def=2; storage[0].hp_plus=4; cost[0]=1300; strcpy(storage[0].tooltip,"기 ."); } else if(num<=0.7){ strcpy(storage[0].name,"교 ()"); storage[0].att=10; storage[0].def=4; storage[0].hp_plus=20; cost[0]=2400; strcpy(storage[0].tooltip," 깊.\n"); } else if(num<=0.9){ strcpy(storage[0].name," ()"); storage[0].att=100; storage[0].def=100; storage[0].hp_plus=200; cost[0]=13500; strcpy(storage[0].tooltip," .\n"); } else{ strcpy(storage[0].name," 기(구)"); storage[0].att=2; storage[0].def=1; storage[0].hp_plus=0; cost[0]=200; strcpy(storage[0].tooltip,"기 간 ."); } strcpy(storage[1].name,"공 과(구)"); storage[1].att=0; storage[1].def=2; storage[1].hp_plus=5; cost[1]=320; strcpy(storage[1].tooltip,"40 과."); strcpy(storage[2].name,"교 각()"); storage[2].att=3; storage[2].def=0; storage[2].hp_plus=0; cost[2]=350; strcpy(storage[2].tooltip,"교 ."); while(true){ if(exit) break; printf("고 : %d\n",Main.money); for(int i=0;i<3;i++){ printf("%d: %s\t--격: %d \n",i+1,storage[i].name,cost[i]); } printf(" 겠까?\n1.\n2. 간\n"); scanf("%f",&num); switch((int)num){ case 1: int num2; printf(" 겠까?: "); scanf("%d",&num2); buyItem(storage[num2],num2-1); system("cls"); break; case 2: exit=true; break; } } } void buyItem(inventory item,int index){ if(Main.money<cost[index]){ printf(" .\n"); fflush(stdin); getchar(); return; } if(i_top<=MAX_ITEMS-1){ printf(": 감!\n"); Main.money-=cost[index]; printf(" %s 구.\n",storage[index].name); itemPush(storage[index]); setStat(); fflush(stdin); getchar(); return; } else{ printf(" ."); fflush(stdin); getchar(); return; } } void viewItems(){ inventory tmp; char buff; bool flag=false; while(true){ if(flag) break; system("cls"); fflush(stdin); int att_plus=0,def_plus=0; for(int i=0;i<=i_top;i++){ att_plus+=inven[i].att; def_plus+=inven[i].def; } printf("--%s --\n:%d\n경:%d\n 까:%d\n공격:%d + %d\n:%d + %d\n:%d / %d\n",Main.name, Main.level,Main.xp,Main.next_xp,Main.base_att,att_plus,Main.base_def,def_plus,Main.hp,Main.max_hp); printf(":\n"); for(int i=0;i<=i_top;i++){ tmp=itemFind(i); printf("%d: %s\n",i+1,tmp.name); } printf("------------------\n"); printf(" 겠까?\n1. \n2.간\n"); scanf("%c",&buff); switch(buff){ case '1': int num; printf(": "); scanf("%d",&num); if(num>=1&&num<=i_top+1){ bool flag2=false; int num2; while(true){ if(flag2) break; fflush(stdin); tmp=itemFind(num-1); printf("<%s>\n:\n1.기\n2.기\n3.\n",tmp.name); scanf("%d",&num2); switch(num2){ case 1: printf("%s\n",inven[num-1].tooltip); printf("공격 : %d\n : %d\n : %d\n",tmp.att,tmp.def,tmp.hp_plus); flag2=true; fflush(stdin); getchar(); system("cls"); break; case 2: printf(" .\n"); discardItem(num-1); flag2=true; system("cls"); break; case 3: flag2=true; system("cls"); break; } } } break; case '2': flag=true; break; } } } void setTravelDest(){ char buff; bool exit=false; int distance; printf(" 겠까?(거 .)\n"); for(int i=0;i<MAX_TOWN;i++){ if(i!=Main.townNum){ distance=i-Main.townNum; if(distance<0) printf("%s: 거 %d\n",T[i].name,-distance); else printf("%s: 거 %d\n",T[i].name,distance); } } printf("----------------------------\n"); int count=0; int T_ptr[MAX_TOWN-1]; for(int i=0;i<MAX_TOWN;i++){ if(i!=Main.townNum){ printf("%d, %s\n",count+1,T[i].name); T_ptr[count++]=i; } } while(true){ if(exit) break; fflush(stdin); printf(": "); scanf("%c",&buff); switch(buff){ case '1': travel(T[T_ptr[0]],distance,T_ptr[0]); exit=true; break; case '2': travel(T[T_ptr[1]],distance,T_ptr[1]); exit=true; break; case '3': travel(T[T_ptr[2]],distance,T_ptr[2]); exit=true; break; case '4': travel(T[T_ptr[3]],distance,T_ptr[3]); exit=true; break; case '5': travel(T[T_ptr[4]],distance,T_ptr[4]); exit=true; break; } } } void travel(town dest,int distance,int townNum){ if(distance<0){ for(int i=0;i<-distance;i++){ srand((unsigned)time(NULL)); int num=rand()%10+1; if(num<=6) battle(); if(gameover) return; } Main.townNum=townNum; } else{ for(int i=0;i<distance;i++){ srand((unsigned)time(NULL)); int num=rand()%10+1; if(num<4) battle(); if(gameover) return; } Main.townNum=townNum; } } void battle(){ char buff; int en; bool end=false; system("cls"); entity tmpEn=Enemy[Main.townNum]; printf("%s() 고 !!\n",tmpEn.name); fflush(stdin); getchar(); while(true){ if(end) break; fflush(stdin); system("cls"); printf(":%s---\n: %d / %d\n:%s---\n: %d / %d\n",Main.name,Main.hp,Main.max_hp,tmpEn.name,tmpEn.hp,tmpEn.max_hp); if(tmpEn.hp<=0){ printf("!\n"); printf(":\n"); srand((unsigned)time(NULL)); int money,xp; money=tmpEn.money; xp=tmpEn.xp; printf("1.: %d \n",money); printf("2.경: %d\n",xp); Main.money+=money; Main.xp+=xp; if(Main.xp>=Main.next_xp){ Main.level++; Main.next_xp=Main.xp*2; Main.base_hp=Main.base_hp+Main.level*10; Main.base_att=Main.base_att+Main.level*2; Main.base_def=Main.base_def+Main.level; setStat(); printf("!\n : %d\t 까 경: %d\n: %d\t공격: %d\t: %d",Main.level,Main.next_xp,Main.max_hp,Main.att,Main.def); } setStat(); fflush(stdin); getchar(); end=true; continue; } else if(Main.hp<=0){ printf(".\n"); fflush(stdin); getchar(); gameover=true; end=true; continue; } else{ printf("1.공격\n2.\n3.\n"); scanf("%c",&buff); switch(buff){ case '1': srand((unsigned)time(NULL)); en=rand()%10; printf("%s() 공격!\n",tmpEn.name); fflush(stdin); getchar(); if(en<=4){ if(tmpEn.def-Main.att>0) tmpEn.hp--; else tmpEn.hp-=Main.att; printf(" %s() 공격!\n",tmpEn.name); fflush(stdin); getchar(); if(Main.def-tmpEn.att>0) Main.hp--; else Main.hp-=tmpEn.att; } else if(en>4&&en<=8){ if(tmpEn.def-Main.att>0) tmpEn.hp--; else tmpEn.hp-=Main.att-tmpEn.def; printf(" %s() .\n",tmpEn.name); fflush(stdin); getchar(); } else{ printf(" %s() 갔.\n",tmpEn.name); fflush(stdin); getchar(); end=true; continue; } break; case '2': srand((unsigned)time(NULL)); en=rand()%10; printf(".\n"); fflush(stdin); getchar(); if(en<=4){ printf(" %s() 공격!\n",tmpEn.name); fflush(stdin); getchar(); if(Main.def-tmpEn.att>0) Main.hp--; else Main.hp-=tmpEn.att-Main.def; } else if(en>4&&en<=8){ printf(" %s() .\n",tmpEn.name); fflush(stdin); getchar(); } else{ printf(" %s() 갔.\n",tmpEn.name); fflush(stdin); getchar(); end=true; continue; } break; case '3': srand((unsigned)time(NULL)); int flee=rand()%10; if(flee>=6){ printf(" .\n"); fflush(stdin); getchar(); end=true; continue; } else{ printf(" .\n"); fflush(stdin); getchar(); break; } } } } } void setStat(){ Main.max_hp=Main.base_hp; Main.att=Main.base_att; Main.def=Main.base_def; for(int i=0;i<=i_top;i++){ Main.max_hp=Main.max_hp+inven[i].hp_plus; Main.att=Main.att+inven[i].att; Main.def=Main.def+inven[i].def; } } void itemPush(inventory item){ if(i_top>=MAX_ITEMS-1) return; inven[++i_top]=item; } inventory itemFind(int index){ return inven[index]; } void discardItem(int index){ inventory tmpItem; tmpItem=inven[index]; for(int i=index+1;i<=i_top;i++) inven[i-1]=inven[i]; i_top--; Main.max_hp-=tmpItem.hp_plus; Main.att-=tmpItem.att; Main.def-=tmpItem.def; }
2.1. game_comp.h ¶
#pragma once #ifndef _GAME_COMP_H_ #define _GAME_COMP_H #define MAX_STRING 100 #define MAX_ITEMS 40 class item{ private: char name[MAX_STRING]; int att; int def; int hp_plus; char tooltip[MAX_STRING]; int cost; public: item(){} void setInfo(char *_name,int _att,int _def,int _hp_plus,char *_tooltip,int _cost); char* getName(); void setName(char *_name); void setatt(int _att); void setdef(int _def); void sethp(int _hp); void settool(char *_tool); void setcost(int _cost); int getatt(); int getdef(); int gethp_plus(); char* gettooltip(); int getcost(); }; class inventory{ private: item inven[MAX_ITEMS]; int i_top; public: inventory(){} void init(); void add(item _item); void discard(int index); item find(int index); int gettop(); }; class town{ private: char name[MAX_STRING]; int num; public: town(){} void setInfo(char *_name,int _num); char* getName(); int getNum(); }; class entity{ private: char name[MAX_STRING]; int base_hp; int hp; int max_hp; unsigned long xp; unsigned long next_xp; int base_att; int att; int base_def; int def; int townNum; int level; int money; public: entity(){}; void setInfo(char *_name,int _base_hp,int _hp,int _max_hp,unsigned int _xp,unsigned int _next_xp,int _base_att,int _base_def,int _townNum,int _level,int _money); void setStat(inventory _inventory); void sethp(int amount); void refresh(); char* getName(); int getbase_hp(); int gethp(); int getmax_hp(); unsigned long getxp(); unsigned long getnext_xp(); int getbase_att(); int getatt(); int getbase_def(); int getdef(); int gettownNum(); int getlevel(); int getmoney(); void travel(town dest); void setmoney(int _cost); void addxp(unsigned int _xp); void checkLevel(inventory &inven); }; #endif _GAME_COMP_H_
2.2. game_comp_entity.cpp ¶
#include "game_comp.h" #include <string.h> #include <stdio.h> void entity::setInfo(char *_name,int _base_hp,int _hp,int _max_hp,unsigned int _xp,unsigned int _next_xp,int _base_att,int _base_def,int _townNum,int _level,int _money){ strcpy(name,_name); base_hp=_base_hp; hp=_hp; max_hp=_max_hp; xp=_xp; next_xp=_next_xp; base_att=_base_att; base_def=_base_def; townNum=_townNum; level=_level; money=_money; } void entity::setStat(inventory _inventory){ int top=_inventory.gettop(); item tmp; max_hp=base_hp; att=base_att; def=base_def; for(int i=0;i<=top;i++){ tmp=_inventory.find(i); max_hp+=tmp.gethp_plus(); att+=tmp.getatt(); def+=tmp.getdef(); } } int entity::getatt(){ return att; } int entity::getdef(){ return def; } char* entity::getName(){ return name; } int entity::getbase_hp(){ return base_hp; } int entity::gethp(){ return hp; } int entity::getmax_hp(){ return max_hp; } int entity::getbase_att(){ return base_att; } int entity::getbase_def(){ return base_def; } int entity::gettownNum(){ return townNum; } int entity::getlevel(){ return level; } int entity::getmoney(){ return money; } unsigned long entity::getxp(){ return xp; } unsigned long entity::getnext_xp(){ return next_xp; } void entity::travel(town dest){ townNum=dest.getNum(); } void entity::refresh(){ hp=max_hp; } void entity::sethp(int amount){ if(hp+amount<=0) hp=0; else if(hp+amount<=max_hp) hp+=amount; else hp=max_hp; } void entity::setmoney(int _cost){ money-=_cost; } void entity::addxp(unsigned int _xp){ xp+=_xp; } void entity::checkLevel(inventory &inven){ if(xp>=next_xp){ level++; next_xp=xp*2; base_hp+=level*10; base_att+=level*2; base_def+=level; setStat(inven); printf("!\n : %d\t 까 경: %d\n: %d\t공격: %d\t: %d",level,next_xp,max_hp,att,def); } setStat(inven); }
2.3. game_comp_inven.cpp ¶
#include "game_comp.h" void inventory::init(){ i_top=-1; } void inventory::add(item _item){ if(i_top<MAX_ITEMS-1) inven[++i_top]=_item; } void inventory::discard(int index){ for(int i=index;i<i_top;i++) inven[i]=inven[i+1]; i_top--; } item inventory::find(int index){ return inven[index]; } int inventory::gettop(){ return i_top; }
2.4. game_comp_item.cpp ¶
#include "game_comp.h" #include <string.h> void item::setInfo(char *_name,int _att,int _def,int _hp_plus,char* _tooltip,int _cost){ strcpy(name,_name); strcpy(tooltip,_tooltip); att=_att; def=_def; hp_plus=_hp_plus; cost=_cost; } char* item::getName(){ return name; } int item::getatt(){ return att; } int item::getdef(){ return def; } int item::gethp_plus(){ return hp_plus; } char* item::gettooltip(){ return tooltip; } int item::getcost(){ return cost; } void item::setName(char *_name){ strcpy(name,_name); } void item::setatt(int _att){ att=_att; } void item::setdef(int _def){ def=_def; } void item::sethp(int _hp){ hp_plus=_hp; } void item::settool(char *_tool){ strcpy(tooltip,_tool); } void item::setcost(int _cost){ cost=_cost; }
2.5. game_comp_town.h ¶
#include "game_comp.h" #include <string.h> void town::setInfo(char *_name,int _num){ strcpy(name,_name); num=_num; } char* town::getName(){ return name; } int town::getNum(){ return num; }
2.6. game_file.h ¶
#ifndef _GAME_FILE_H_ #define _GAME_FILE_H_ #include <stdio.h> #include "game_comp.h" class fileManager{ private: FILE *file; public: fileManager(){}; bool loadGame(char *FILENAME,entity &PLAYER,inventory &inven); bool saveGame(char *FILENAME,entity &PLAYER,inventory &inven); }; #endif _GAME_FILE_H_
2.7. game_file.cpp ¶
#include "game_file.h" #include <string.h> bool fileManager::loadGame(char *FILENAME,entity &PLAYER,inventory &inven){ inven.init(); file=fopen(FILENAME,"r"); if(file==NULL) return false; printf("...\n"); char buff[MAX_STRING]; int basehp,hp,baseatt,basedef,townnum,inum,level,money,att,def,hpplus; unsigned int xp,nextxp; char name[MAX_STRING]; item itmp; while(strcmp(buff,"EOF")!=0){ fscanf(file,"%s",buff); if(strcmp(buff,"name:")==0){ char tmp; int i=0; fscanf(file,"%c",&tmp); do{ fscanf(file,"%c",&tmp); name[i++]=tmp; }while(tmp!='\n'); name[i-1]='\0'; } else if(strcmp(buff,"level:")==0) fscanf(file,"%d",&level); else if(strcmp(buff,"base_hp:")==0) fscanf(file,"%d",&basehp); else if(strcmp(buff,"hp:")==0) fscanf(file,"%d",&hp); else if(strcmp(buff,"base_att:")==0) fscanf(file,"%d",&baseatt); else if(strcmp(buff,"base_def:")==0) fscanf(file,"%d",&basedef); else if(strcmp(buff,"townNum:")==0) fscanf(file,"%d",&townnum); else if(strcmp(buff,"xp:")==0) fscanf(file,"%lu",&xp); else if(strcmp(buff,"next_xp:")==0) fscanf(file,"%lu",&nextxp); else if(strcmp(buff,"money:")==0) fscanf(file,"%d",&money); else if(strcmp(buff,"inven:")==0){ fscanf(file,"%d",&inum); for(int j=0;j<=inum;j++){ do{ fscanf(file,"%s",buff); if(strcmp(buff,"iname:")==0){ char tmp; int i=0; char tmpName[MAX_STRING]; fscanf(file,"%c",&tmp); do{ fscanf(file,"%c",&tmp); tmpName[i++]=tmp; }while(tmp!='\n'); tmpName[i-1]='\0'; itmp.setName(tmpName); } else if(strcmp(buff,"att:")==0){ fscanf(file,"%d",&att); itmp.setatt(att); } else if(strcmp(buff,"def:")==0){ fscanf(file,"%d",&def); itmp.setdef(def); } else if(strcmp(buff,"hp_plus:")==0){ fscanf(file,"%d",&hpplus); itmp.sethp(hpplus); } else if(strcmp(buff,"tooltip:")==0){ char tmp; int i=0; char tmpName[MAX_STRING]; fscanf(file,"%c",&tmp); do{ fscanf(file,"%c",&tmp); tmpName[i++]=tmp; }while(tmp!='\n'); tmpName[i-1]='\0'; itmp.settool(tmpName); } }while(strcmp(buff,"END")!=0); itmp.setcost(0); inven.add(itmp); } } } PLAYER.setInfo(name,basehp,hp,0,xp,nextxp,baseatt,basedef,townnum,level,money); PLAYER.setStat(inven); printf("기 !\n"); fflush(stdin); getchar(); return true; } bool fileManager::saveGame(char *FILENAME,entity &PLAYER,inventory &inven){ file=fopen(FILENAME,"w+"); printf("...\n"); fprintf(file,"name: %s\n",PLAYER.getName()); fprintf(file,"level: %d\n",PLAYER.getlevel()); fprintf(file,"base_hp: %d\n",PLAYER.getbase_hp()); fprintf(file,"hp: %d\n",PLAYER.gethp()); fprintf(file,"base_att: %d\n",PLAYER.getbase_att()); fprintf(file,"base_def: %d\n",PLAYER.getbase_def()); fprintf(file,"townNum: %d\n",PLAYER.gettownNum()); fprintf(file,"xp: %lu\n",PLAYER.getxp()); fprintf(file,"next_xp: %lu\n",PLAYER.getnext_xp()); fprintf(file,"money: %d\n",PLAYER.getmoney()); fprintf(file,"inven: %d\n",inven.gettop()); for(int i=0;i<=inven.gettop();i++){ item tmp=inven.find(i); fprintf(file,"iname: %s\n",tmp.getName()); fprintf(file,"att: %d\n",tmp.getatt()); fprintf(file,"def: %d\n",tmp.getdef()); fprintf(file,"hp_plus: %d\n",tmp.gethp_plus()); fprintf(file,"tooltip: %s\n",tmp.gettooltip()); fprintf(file,"END\n"); } fprintf(file,"EOF"); fclose(file); printf(" !\n"); fflush(stdin); getchar(); return true; }
2.8. game.cpp ¶
#include "game_comp.h" #include "game_file.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <io.h> #include <math.h> #define MAX_TOWN 8 #define MAX_ENEMY 8 #define MAX_STORAGE 3 #define MAX_COLLEN 4 #define MAX_LEVEL 30 #define FILE_NAME "csave.dat" entity PLAYER; inventory INVENTORY; town TOWN[MAX_TOWN]; entity ENEMY[MAX_ENEMY]; entity COLLOSEUM[MAX_TOWN][MAX_COLLEN]; item storage[MAX_STORAGE]; fileManager FILEMGR; bool gameover=false; void init(bool first); void store(); void travel(town dest,int distance); void setTravelDest(); void battle(entity &enemy); void rest(); void viewItems(); void setStat(); void start(bool first); void buyItem(item _item,int index); void colloseum(entity *colloseum); int main(void){ char buff; while(true){ printf("<< rpg ver1.1.0 made by 기>>\n"); printf("1.기\n2.기\n3.\n4.\n"); scanf("%c",&buff); switch(buff){ case '1': system("cls"); init(true); start(true); break; case '2': system("cls"); if(FILEMGR.loadGame(FILE_NAME,PLAYER,INVENTORY)){ init(false); start(false); } else{ init(true); start(true); } break; case '3': system("cls"); printf("CAU RPG ver 1.1.0 Patch notes(What's new)\n"); printf("------------------------------------------\n"); printf("1. 게 10%% 게 .\n"); printf("2.각 겼! 게 .\n"); printf("3. 겼. 개 8개 .\n"); fflush(stdin); getchar(); break; case '4': return 0; } system("cls"); } return 0; } void init(bool first){ gameover=false; TOWN[0].setInfo("208",0); TOWN[1].setInfo("",1); TOWN[2].setInfo("R&D",2); TOWN[3].setInfo("",3); TOWN[4].setInfo("",4); TOWN[5].setInfo("",5); TOWN[6].setInfo("",6); TOWN[7].setInfo(" 구",7); if(first){ INVENTORY.init(); item tmp=item(); tmp.setInfo(" ",1,0,0,"구 .",0); INVENTORY.add(tmp); PLAYER.setInfo("",30,30,30,0,30,1,1,0,1,0); PLAYER.setStat(INVENTORY); } ENEMY[0].setInfo("과",15,15,15,10,0,3,0,0,1,15); ENEMY[1].setInfo("",50,50,50,20,0,10,8,1,3,35); ENEMY[2].setInfo("권",130,130,130,30,0,40,18,2,5,46); ENEMY[3].setInfo("",350,350,350,64,0,68,37,3,10,58); ENEMY[4].setInfo("",3200,3200,3200,120,0,430,357,4,15,70); ENEMY[5].setInfo("공",430,430,430,80,0,124,43,5,13,102); ENEMY[6].setInfo("과 ",530,530,530,134,0,245,150,6,17,130); ENEMY[7].setInfo(" ",340,340,340,165,0,563,230,7,20,160); COLLOSEUM[0][0].setInfo("과",200,200,200,60,0,50,34,0,7,70); COLLOSEUM[0][1].setInfo("고NXT",450,450,450,80,0,87,56,0,10,95); COLLOSEUM[0][2].setInfo("간",640,640,640,110,0,95,104,0,12,134); COLLOSEUM[0][3].setInfo(" NXT",1000,1000,1000,106,0,130,56,0,15,185); COLLOSEUM[1][0].setInfo("",430,430,430,75,0,84,96,0,8,96); COLLOSEUM[1][1].setInfo("",360,360,360,96,0,120,87,0,10,143); COLLOSEUM[1][2].setInfo("Olleh 3G",140,140,140,132,0,204,108,0,14,235); COLLOSEUM[1][3].setInfo(" ",1030,1030,1030,165,0,342,105,0,20,260); COLLOSEUM[2][0].setInfo("",530,530,530,85,0,103,178,0,16,113); COLLOSEUM[2][1].setInfo("공",2054,2054,2054,150,0,260,234,0,21,176); COLLOSEUM[2][2].setInfo("과 ",1043,1043,1043,160,0,203,549,0,24,200); COLLOSEUM[2][3].setInfo(" 구",3400,3400,3400,241,0,540,426,0,28,243); COLLOSEUM[3][0].setInfo(" 과",400,400,400,120,0,100,68,0,15,140); COLLOSEUM[3][1].setInfo(" 고NXT",900,900,900,160,0,174,112,0,16,190); COLLOSEUM[3][2].setInfo(" 간",1280,1280,1280,220,0,190,208,0,23,268); COLLOSEUM[3][3].setInfo(" NXT",2000,2000,2000,212,0,260,112,0,26,370); COLLOSEUM[4][0].setInfo(" ",860,860,860,150,0,168,192,0,17,192); COLLOSEUM[4][1].setInfo(" ",720,720,720,192,0,240,174,0,20,286); COLLOSEUM[4][2].setInfo(" Olleh 3G",280,280,280,264,0,408,216,0,18,470); COLLOSEUM[4][3].setInfo("갑 ",2060,2060,2060,330,0,684,210,0,30,520); COLLOSEUM[5][0].setInfo(" ",1060,1060,1060,170,0,206,256,0,30,226); COLLOSEUM[5][1].setInfo(" 공",4108,4108,4108,300,0,520,468,0,30,352); COLLOSEUM[5][2].setInfo(" 과 ",2086,2086,2086,320,0,406,1098,0,30,400); COLLOSEUM[5][3].setInfo("기 ",5400,5400,5400,406,0,860,652,0,30,526); COLLOSEUM[6][0].setInfo(" 과",2120,2120,2120,340,0,500,430,0,30,512); COLLOSEUM[6][1].setInfo(" NXT",4300,4300,4300,300,0,480,880,0,30,540); COLLOSEUM[6][2].setInfo(" 게",6400,6400,6400,430,0,1800,598,0,30,612); COLLOSEUM[6][3].setInfo(" 계 계 ",4600,4600,4600,506,0,430,640,0,30,622); COLLOSEUM[7][0].setInfo(" ",7400,7400,7400,600,0,1430,860,0,30,0); COLLOSEUM[7][1].setInfo(" ",9700,9700,9700,600,0,1650,1300,0,30,0); COLLOSEUM[7][2].setInfo(" 구",13000,13000,13000,600,0,2400,1650,0,30,0); COLLOSEUM[7][3].setInfo(" ",53200,53200,53200,600,0,6450,4300,0,30,0); } void start(bool first){ char buff; if(first){ char name[MAX_STRING]; printf(" : "); scanf("%s",name); PLAYER.setInfo(name,30,30,30,0,30,1,1,0,1,0); } PLAYER.setStat(INVENTORY); while(true){ if(gameover) break; system("cls"); printf(" %s .\n 겠까?\n",TOWN[PLAYER.gettownNum()].getName()); printf("1. \n2.\n3. \n4. \n5. \n6.\n7.기\n8.\n"); scanf("%c",&buff); switch(buff){ case '1': system("cls"); store(); break; case '2': system("cls"); rest(); break; case '3': system("cls"); viewItems(); break; case '4': system("cls"); setTravelDest(); break; case '5': system("cls"); colloseum(COLLOSEUM[PLAYER.gettownNum()]); break; case '6': system("cls"); FILEMGR.saveGame(FILE_NAME,PLAYER,INVENTORY); break; case '7': system("cls"); FILEMGR.loadGame(FILE_NAME,PLAYER,INVENTORY); break; case '8': system("cls"); gameover=true; break; } } return; } void rest(){ printf(" .\n"); PLAYER.refresh(); fflush(stdin); getchar(); } void store(){ float num; bool exit=false; srand((unsigned)time(NULL)); num=(rand()%10+1)/10+rand()%2; if(num<=1.2&&num>0.6) storage[0].setInfo(" 구()",6,2,4,"기 .",1300); else if(num<=1.6&&num>1.2) storage[0].setInfo("교 ([])",10,4,20," 깊.",2400); else if(num<=1.8&&num>1.6) storage[0].setInfo(" ()",100,100,200," .",13500); else storage[0].setInfo(" 기(구)",2,1,0,"기 간 .",200); storage[1].setInfo("공 과(구)",0,2,5,"40 기 과.",320); storage[2].setInfo("교 각([])",3,0,0,"교 .",350); while(true){ if(exit) break; printf("고 : %d\n",PLAYER.getmoney()); for(int i=0;i<3;i++){ printf("%d: %s\t--격: %d \n",i+1,storage[i].getName(),storage[i].getcost()); } printf(" 겠까?\n1.\n2. 간\n"); scanf("%f",&num); switch((int)num){ case 1: int num2; printf(" 겠까?: "); scanf("%d",&num2); buyItem(storage[num2-1],num2-1); system("cls"); break; case 2: exit=true; break; } } } void buyItem(item _item,int index){ if(PLAYER.getmoney()<_item.getcost()){ printf(" .\n"); fflush(stdin); getchar(); return; } if(INVENTORY.gettop()<=MAX_ITEMS-1){ printf(": 감!\n"); PLAYER.setmoney(_item.getcost()); printf(" %s() 구.\n",_item.getName()); INVENTORY.add(_item); PLAYER.setStat(INVENTORY); fflush(stdin); getchar(); return; } else{ printf(" ."); fflush(stdin); getchar(); return; } } void viewItems(){ char buff; bool flag=false; while(true){ if(flag) break; system("cls"); fflush(stdin); int att_plus=0,def_plus=0; for(int i=0;i<=INVENTORY.gettop();i++){ att_plus+=INVENTORY.find(i).getatt(); def_plus+=INVENTORY.find(i).getdef(); } printf("--%s --\n:%d\n경:%lu\n 까:%lu\n공격:%d + %d\n:%d + %d\n:%d / %d\n",PLAYER.getName(), PLAYER.getlevel(),PLAYER.getxp(),PLAYER.getnext_xp(),PLAYER.getbase_att(),att_plus,PLAYER.getbase_def(),def_plus, PLAYER.gethp(),PLAYER.getmax_hp()); printf(":\n"); for(int i=0;i<=INVENTORY.gettop();i++){ item tmp=item(); tmp=INVENTORY.find(i); printf("%d: %s\n",i+1,tmp.getName()); if(i>=5){ printf("...more...\n"); break; } } printf("------------------\n"); printf(" 겠까?\n1. \n2.간\n"); scanf("%c",&buff); switch(buff){ case '1': system("cls"); for(int i=0;i<=INVENTORY.gettop();i++){ item tmp=item(); tmp=INVENTORY.find(i); printf("%d: %s\n",i+1,tmp.getName()); } int num; printf(": "); scanf("%d",&num); if(num>=1&&num<=INVENTORY.gettop()+1){ bool flag2=false; while(true){ if(flag2) break; int num2; item tmp=item(); tmp=INVENTORY.find(num-1); printf("<%s>\n:\n1.기\n2.기\n3.\n",tmp.getName()); scanf("%d",&num2); switch(num2){ case 1: printf("%s\n",tmp.gettooltip()); printf("공격 : %d\n : %d\n : %d\n",tmp.getatt(),tmp.getdef(),tmp.gethp_plus()); flag2=true; fflush(stdin); getchar(); system("cls"); break; case 2: printf(" .\n"); INVENTORY.discard(num-1); flag2=true; system("cls"); break; case 3: flag2=true; system("cls"); break; } } } break; case '2': flag=true; break; } } } void setTravelDest(){ bool exit=false; int distance; printf(" 겠까?(거 .)\n"); for(int i=0;i<MAX_TOWN;i++){ if(i!=PLAYER.gettownNum()){ distance=i-PLAYER.gettownNum(); printf("%s: 거 %d\n",TOWN[i].getName(),abs(distance)); } } printf("----------------------------\n"); int count=0; int T_ptr[MAX_TOWN-1]; for(int i=0;i<MAX_TOWN;i++){ if(i!=PLAYER.gettownNum()){ printf("%d, %s\n",count+1,TOWN[i].getName()); T_ptr[count++]=i; } } while(true){ int dest; if(exit) break; fflush(stdin); printf(": "); scanf("%d",&dest); if(dest>=1&&dest<=MAX_TOWN){ travel(TOWN[T_ptr[dest-1]],abs(PLAYER.gettownNum()-T_ptr[dest-1])); exit=true; } } } void travel(town dest,int distance){ if(distance<0){ for(int i=0;i<-distance;i++){ srand((unsigned)time(NULL)); int num=rand()%10+1; if(num<=6){ entity tmpEn=ENEMY[PLAYER.gettownNum()]; battle(tmpEn); } if(gameover) return; } PLAYER.travel(dest); } else{ for(int i=0;i<distance;i++){ srand((unsigned)time(NULL)); int num=rand()%10+1; if(num<=6){ entity tmpEn=ENEMY[PLAYER.gettownNum()]; battle(tmpEn); } if(gameover) return; } PLAYER.travel(dest); } } void colloseum(entity *colloseum){ bool exit=false; printf(" !\n"); fflush(stdin); getchar(); while(!exit&&!gameover){ system("cls"); char buff; printf("---- ----\n"); for(int i=0;i<MAX_COLLEN;i++){ printf("%d: %s --- Lv.%d\n",i+1,colloseum[i].getName(),colloseum[i].getlevel()); } printf("1. 결\n2.기\n"); scanf("%c",&buff); switch(buff){ case '1': int num; printf(" 결: "); scanf("%d",&num); if(num>=1&&num<=MAX_COLLEN){ fflush(stdin); printf("결 !\n"); getchar(); entity tmpEn=colloseum[num-1]; battle(tmpEn); } break; case '2': exit=true; break; } } } void battle(entity &enemy){ char buff; int en; bool end=false; system("cls"); printf("%s() 고 !!\n",enemy.getName()); fflush(stdin); getchar(); while(true){ if(end) break; fflush(stdin); system("cls"); printf(":%s Lv.%d ---\n: %d / %d\n:%s Lv.%d ---\n: %d / %d\n",PLAYER.getName(),PLAYER.getlevel(), PLAYER.gethp(),PLAYER.getmax_hp(),enemy.getName(),enemy.getlevel(),enemy.gethp(),enemy.getmax_hp()); if(enemy.gethp()<=0){ printf("!\n"); printf(":\n"); srand((unsigned)time(NULL)); int money; unsigned long xp; money=enemy.getmoney(); xp=enemy.getxp(); printf("1.: %d \n",money); printf("2.경: %lu\n",xp); PLAYER.setmoney(-money); if(PLAYER.getlevel()<MAX_LEVEL) PLAYER.addxp(xp); PLAYER.checkLevel(INVENTORY); fflush(stdin); getchar(); end=true; continue; } else if(PLAYER.gethp()<=0){ printf(".\n"); fflush(stdin); getchar(); gameover=true; end=true; continue; } else{ printf("1.공격\n2.\n3.\n"); scanf("%c",&buff); switch(buff){ case '1': srand((unsigned)time(NULL)); en=rand()%10; printf("%s() 공격!\n",enemy.getName()); fflush(stdin); getchar(); if(en<=5){ enemy.sethp(-PLAYER.getatt()); printf(" %s() 공격!\n",enemy.getName()); fflush(stdin); getchar(); PLAYER.sethp(-enemy.getbase_att()); } else{ if(enemy.getbase_def()-PLAYER.getatt()>0) enemy.sethp(-1); else enemy.sethp(-(PLAYER.getatt()-enemy.getbase_def())+(int)(enemy.getmax_hp()*0.1)); printf(" %s() .\n",enemy.getName()); fflush(stdin); getchar(); } break; case '2': srand((unsigned)time(NULL)); en=rand()%10; printf(".\n"); fflush(stdin); getchar(); if(en<=5){ printf(" %s() 공격!\n",enemy.getName()); fflush(stdin); getchar(); if(PLAYER.getdef()-enemy.getbase_att()>0) PLAYER.sethp(-1); else PLAYER.sethp(-(enemy.getbase_att()-PLAYER.getdef())+(int)(PLAYER.getmax_hp()*0.1)); } else{ printf(" %s() .\n",enemy.getName()); fflush(stdin); getchar(); } break; case '3': srand((unsigned)time(NULL)); int flee=rand()%10; if(flee>=6){ printf(" .\n"); fflush(stdin); getchar(); end=true; continue; } else{ printf(" .\n"); fflush(stdin); getchar(); break; } } } } }