승쨩개발공부
[C++] Text RPG 본문
텍스트 RPG
#include <iostream>
using namespace std;
typedef struct tagInfo
{
char szName[16];
int iAtt;
int iMaxHp;
int iHp;
}INFO; // 플레이어 정보 구조체 생성
INFO* Create_Object(char* _pName, int _iAtt, int _iMaxHp); // 오브젝트를 만드는 함수
INFO* Select_Player(); // 플레이어를 선택하는 함수
void Progress(INFO* _pPlayer); // 전체 흐름 함수
void Render(INFO* _pPlayer); // 플레이어 정보 함수
int Combat(INFO* _pPlayer, INFO* _pMonster); // 전투 함수
// 메인 함수
void main()
{
INFO* pPlayer = Select_Player();
// if (nullptr == pPlayer)
if (!pPlayer)
return;
Progress(pPlayer);
if (pPlayer)
{
delete pPlayer;
pPlayer = nullptr;
}
// Render 함수
void Render(INFO* _pPlayer)
{
cout << "이름: " << _pPlayer->szName << endl;
cout << "공격력: " << _pPlayer->iAtt << endl;
cout << "체력: " << _pPlayer->iHp << " / " << _pPlayer->iMaxHp << endl;
cout << "============================================" << endl;
}
// Progress 함수
void Progress(INFO* _pPlayer)
{
int iSelect = 0;
INFO* pMonster = nullptr;
while (true)
{
system("cls");
Render(_pPlayer);
cout << "1.초급 2.중급 3.고급 4.종료" << endl;
cout << "============================================" << endl;
cout << "입력: ";
cin >> iSelect;
switch (iSelect)
{
case 1:
pMonster = Create_Object("슬라임", 5, 30);
break;
case 2:
pMonster = Create_Object("오우거", 30, 60);
break;
case 3:
pMonster = Create_Object("골렘", 60, 90);
break;
case 4:
return;
default:
continue;
}
int iRes = Combat(_pPlayer, pMonster);
switch (iRes)
{
case 0: // 승리
break;
case 1: // 패배
_pPlayer->iHp = _pPlayer->iMaxHp;
break;
case 2: // 도망
break;
}
if (pMonster)
{
delete pMonster;
pMonster = nullptr;
}
}
}
// Combat 함수
int Combat(INFO* _pPlayer, INFO* _pMonster)
{
int iSelect = 0;
while (true)
{
system("cls");
Render(_pPlayer);
Render(_pMonster);
cout << "1.전투 2.도망" << endl;
cout << "============================================" << endl;
cout << "입력: ";
cin >> iSelect;
switch (iSelect)
{
case 1:
_pPlayer->iHp -= _pMonster->iAtt;
_pMonster->iHp -= _pPlayer->iAtt;
break;
case 2:
return 2;
default:
continue;
}
if (0 >= _pPlayer->iHp)
{
cout << "플레이어 사망!" << endl;
system("pause");
return 1;
}
if (0 >= _pMonster->iHp)
{
cout << "전투 승리!" << endl;
system("pause");
return 0;
}
}
}
// Create_Object 함수
INFO* Create_Object(char* _pName, int _iAtt, int _iMaxHp)
{
INFO* pObj = new INFO;
strcpy_s(pObj->szName, 16, _pName);
pObj->iAtt = _iAtt;
pObj->iMaxHp = _iMaxHp;
pObj->iHp = _iMaxHp;
return pObj;
}
// Select_Player 함수
INFO* Select_Player()
{
int iSelect = 0;
INFO* pPlayer = nullptr;
while (true)
{
system("cls");
cout << "1.전사 2.마법사 3.도적 4.종료" << endl;
cout << "============================================" << endl;
cout << "입력: ";
cin >> iSelect;
switch (iSelect)
{
case 1:
pPlayer = Create_Object("전사", 10, 200);
//pPlayer = new INFO;
//strcpy_s(pPlayer->szName, 16, "전사");
//pPlayer->iAtt = 10;
//pPlayer->iMaxHp = 200;
//pPlayer->iHp = 200;
return pPlayer;
case 2:
pPlayer = Create_Object("마법사", 30, 100);
return pPlayer;
case 3:
return Create_Object("도적", 20, 150);
case 4:
return nullptr;
default:
continue;
}
}
return nullptr;
}
'C++' 카테고리의 다른 글
[C++] 메모리 구조 (0) | 2021.11.30 |
---|---|
[C++] 동적할당 (0) | 2021.11.29 |
[C++] 구조체(struct) (0) | 2021.11.26 |
[C++] 문자열 함수 / 메모리 함수 (0) | 2021.11.25 |
[C++] 배열 (0) | 2021.11.24 |