[C++]_S4-04_Text_RPG

신치우·2024년 7월 28일

CPP

목록 보기
27/62
#include <iostream>
using namespace std;

enum PlyaerType
{
	PT_Knight = 1,
	PT_Archer = 2,
	PT_Mage = 3,
};

enum MonsterType
{
	MT_Slime = 1,
	MT_Orc = 2,
	MT_Skeleton = 3,
};

struct ObjectInfo
{
	int type;
	int hp;
	int attack;
	int defence;
};

ObjectInfo playerInfo;
//int playerType;
//int hp;
//int attack;
//int defence;

ObjectInfo monsterInfo;
//int monsterType;
//int monsterHp;
//int monsterAttack;
//int monsterDefence;

void EnterLobby();
void SelectPlayer();
void EnterField();
void CreateRandomMonster();
void EnterBattle();

int main()
{
	// 랜덤 시드 설정
	srand(time(0));

	EnterLobby();
	return 0;
}

void EnterLobby()
{
	while (true)
	{
		cout << "---------------------" << endl;
		cout << "로비에 입장하하였습니다." << endl;
		cout << "---------------------" << endl;

		// 플레이어 직업 선택
		SelectPlayer();
		cout << "---------------------" << endl;
		cout << "(1) 필드입장 (2) 게임종료" << endl;
		cout << "---------------------" << endl;

		int input;
		cin >> input;
		if (input == 1)
		{
			EnterField();
		}
		else
		{
			return;
		}
	}

}

void SelectPlayer()
{
	while (true)
	{
		cout << "---------------------" << endl;
		cout << "직업을 골라주세요!" << endl;
		cout << "(1) 기사 (2) 궁수 (3) 법사" << endl;
		cout << ">";

		cin >> playerInfo.type;

		if (playerInfo.type == PT_Knight)
		{
			cout << "기사 생성중...!" << endl;
			playerInfo.hp = 150;
			playerInfo.attack = 10;
			playerInfo.defence = 5;
			break;
		}
		else if (playerInfo.type == PT_Archer)
		{
			cout << "궁수 생성중...!" << endl;
			playerInfo.hp = 100;
			playerInfo.attack = 15;
			playerInfo.defence = 3;
			break;
		}
		else if (playerInfo.type == PT_Mage)
		{
			cout << "법사 생성중...!" << endl;
			playerInfo.hp = 80;
			playerInfo.attack = 25;
			playerInfo.defence = 0;
			break;
		}
	}
}

void EnterField()
{
	while (true)
	{
		cout << "---------------------" << endl;
		cout << "필드에 입장했습니다!" << endl;
		cout << "---------------------" << endl;

		cout << "[PLAYER] HP : " << playerInfo.hp << " / ATT : " << playerInfo.attack << " / DEF : " << playerInfo.defence << endl;

		CreateRandomMonster();
		
		cout << "---------------------" << endl;
		cout << "(1) 전투 (2) 도주" << endl;
		cout << ">";

		int input;
		cin >> input;

		if (input == 1)
		{
			EnterBattle();
			if (playerInfo.hp == 0)
				return;
		}
		else
		{
			return;
		}

	}
}

void CreateRandomMonster()
{
	// 1 ~ 3
	monsterInfo.type = 1+ (rand() % 3);

	switch (monsterInfo.type)
	{
	case MT_Slime:
		cout << "슬라임 생성중...! (HP:15 / ATT:5 / DEF:0)" << endl;
		monsterInfo.hp = 15;
		monsterInfo.attack = 5;
		monsterInfo.defence = 0;
		break;
	case MT_Orc:
		cout << "오크 생성중...! (HP:40 / ATT:10 / DEF:3)" << endl;
		monsterInfo.hp = 40;
		monsterInfo.attack = 10;
		monsterInfo.defence = 3;
		break;
	case MT_Skeleton:
		cout << "스켈레톤 생성중...! (HP:80 / ATT:15 / DEF:5)" << endl;
		monsterInfo.hp = 80;
		monsterInfo.attack = 15;
		monsterInfo.defence = 5;
		break;
	}
}

void EnterBattle()
{
	while (true)
	{
		int damage = playerInfo.attack - monsterInfo.defence;
		if (damage < 0)
			damage = 0;

		// 선빵
		monsterInfo.hp -= damage;
		if (monsterInfo.hp < 0)
			monsterInfo.hp = 0;

		cout << "몬스터 남은 체력 : " << monsterInfo.hp << endl;
		if (monsterInfo.hp == 0)
		{
			cout << "몬스터를 처치했습니다!" << endl;
			return;
		}

		damage = monsterInfo.attack - playerInfo.defence;
		if (damage < 0)
			damage = 0;

		// 반격
		playerInfo.hp -= damage;
		if (playerInfo.hp < 0)
			playerInfo.hp = 0;
		
		cout << "플레이어 남은 체력 : " << playerInfo.hp << endl;
		if (playerInfo.hp == 0)
		{
			cout << "당신은 사망했습니다... GAME OVER" << endl;
			return;
		}
	}
}

이번 시간에는 간단한 text PRG를 짜는 방법에 대해 공부하였다.
함수와 함수간의 호출 --> 지난 시간에 배운 호출스택과 관련.
이를 통해 함수 수업에서 배운 부분을 복습하는 과정이었다.

추가적으로 배운 부분은
구조체 부분이다.
처음에는 아래 주석처리 된 것처럼 사용하였다가, 이를 보다 편리하게 관리하기 위하여 구조체를 도입하여 작성하였다.
정해진 변수를 여러번 만들어야하는 경우 구조체를 이용하면 보다 편리하게 사용할 수 있게 된다는 걸 기억하자.

struct ObjectInfo
{
	int type;
	int hp;
	int attack;
	int defence;
};

ObjectInfo playerInfo;
//int playerType;
//int hp;
//int attack;
//int defence;

ObjectInfo monsterInfo;
//int monsterType;
//int monsterHp;
//int monsterAttack;
//int monsterDefence;
profile
https://shin8037.tistory.com/

0개의 댓글