포인터 실습 #2

이승덱·2021년 7월 21일

CPP

목록 보기
26/70
// 포인터를 사용하여 간단한 TextRpg를 만들어보자!
#include <iostream>

using namespace std;

//main

//-EnterLobby

//--CreatePlayer

//--EnterGame

//---CreateMonster

//---EnterBattle

enum PlayerType {

 PT_Knight = 1,

 PT_Archer=2,

 PT_Mage=3,

};

enum MonsterType {

 MT_Slime = 1,

 MT_Orc = 2,

 MT_Skeleton = 3,

};

struct StatInfo {

 int hp;

 int attack;

 int defence;

};

void EnterLobby();

void PrintMessage(const char* msg);

void CreatePlayer(StatInfo *pi);

void PrintStatInfo(const char* name, const StatInfo& info);

void EnterGame(StatInfo* pi);

void CreateMonster(StatInfo monsterInfo[], int count);

bool EnterBattle(StatInfo* pi,StatInfo* mi);

int main()

{

 srand((unsigned)time(nullptr));

 EnterLobby();

}

void EnterLobby() {

 while (true) {

 PrintMessage("로비에 입장했습니다!.");

 StatInfo playerInfo;

 CreatePlayer(&playerInfo);

 PrintStatInfo("Player", playerInfo);

 EnterGame(&playerInfo);

 }

}

void PrintMessage(const char* msg) {

 cout << "--------------------" << endl;

 cout << msg<< endl;

 cout << "--------------------" << endl;

}

void CreatePlayer(StatInfo* pi) {

 while (true) {

 PrintMessage("캐릭터 생성창");

 PrintMessage("[1] 기사 [2] 궁수 [3] 법사 ");

 int input;

 cin >> input;

 switch (input)

 {

 case PT_Knight:

 pi->hp = 150;

 pi->attack = 11;

 pi->defence = 5;

 break;

 case PT_Archer:

 pi->hp = 100;

 pi->attack = 15;

 pi->defence = 3;

 break;

 case PT_Mage:

 pi->hp = 80;

 pi->attack = 25;

 pi->defence = 0;

 break;

 default:

 continue;

 break;

 }

 break;

 }

}

void PrintStatInfo(const char* name, const StatInfo& info) {

 cout << "--------------------" << endl;

 cout << name << ": HP = " << info.hp << " Attack = " << info.attack << " Defece = " << info.defence << endl;

 cout << "--------------------" << endl;

}

void EnterGame(StatInfo* pi) {

 PrintMessage("게임에 입장했습니다");

 const int MONSTER_COUNT = 2;

 while (true) {

 StatInfo monsterInfo[MONSTER_COUNT];

 CreateMonster(monsterInfo, MONSTER_COUNT);

 for (int i = 0;i < MONSTER_COUNT; i++)

 PrintStatInfo("Monster", monsterInfo[i]);

 PrintStatInfo("Player", *pi);

 PrintMessage("[1] 전투 [2] 전투 [3] 도망");

 int input;

 cin >> input;

 if (input == 1 || input == 2) {

 int index = input - 1;

 bool victory=EnterBattle(pi,&monsterInfo[index]);

 if (victory == false)

 return;

 }

 else {

 return;

 }

 }

}

void CreateMonster(StatInfo monsterInfo[], int count) {

 for (int i = 0;i < count;i++) {

 int monsterType=rand() % 3+1;

 switch (monsterType)

 {

 case MT_Slime:

 monsterInfo[i].hp = 30;

 monsterInfo[i].attack = 5;

 monsterInfo[i].defence = 1;

 break;

 case MT_Orc:

 monsterInfo[i].hp = 40;

 monsterInfo[i].attack = 8;

 monsterInfo[i].defence = 2;

 break;

 case MT_Skeleton:

 monsterInfo[i].hp = 50;

 monsterInfo[i].attack =15;

 monsterInfo[i].defence = 3;

 break;

 }

 }

}

bool EnterBattle(StatInfo* pi, StatInfo* mi) {

 while (true) {

 int damage = pi->attack - mi->defence;

 if (damage < 0)

 damage = 0;

 mi->hp -= damage;

 if (mi->hp < 0)

 mi->hp = 0;

 PrintStatInfo("Monster", *mi);

 if (mi->hp == 0) {

 PrintMessage("몬스터를 처치했습니다.");

 return true;

 }

 damage = mi->attack - pi->defence;

 if (damage < 0)

 damage = 0;

 pi->hp -= damage;

 if (pi->hp < 0)

 pi->hp = 0;

 PrintStatInfo("Player", *pi);

 if (pi->hp == 0) {

 PrintMessage("플레이어가 사망했습니다");

 return false;

 }

 }

}
profile
공부 기록용 블로그입니다

0개의 댓글