
오늘의 학습
- C++ 강의
- C++ TextRPG
C++ TextRPG
필수 구현 기능의 STEP 2를 구현하였다.

- HP/MP 입력 블록을 while(true)로 감싸기
- HP와 MP가 모두 50보다 클 때만 break로 입력 루프 탈출하기
- 공격력/방어력도 동일하게 반복 입력 구현하기
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 4;
void printStatus(string name, int stat[])
{
cout << "\n====================================" << endl;
cout << " " << name << "\'s Stats" << endl;
cout << "====================================" << endl;
cout << "HP: " << stat[0] << "\t\t" << "MP: " << stat[1] << endl;
cout << "Attack: " << stat[2] << "\t" << "Defense: " << stat[3] << endl;
cout << "====================================" << endl;
}
int main(void)
{
string heroName;
int stat[SIZE] = { 0 };
cout << "===========================================" << endl;
cout << " [ Dungeon Escape Text RPG ]" << endl;
cout << "===========================================" << endl;
cout << "Enter your hero's name: ";
cin >> heroName;
while (true)
{
cout << "\nEnter HP and MP: ";
cin >> stat[0] >> stat[1]; //stat[0] : HP , stat[1] : MP
if (stat[0] > 50 && stat[1] > 50)
{
break;
}
else
{
cout << "HP or MP is too low. Try again.";
}
}
while(true)
{
cout << "Enter Attack and Defense: ";
cin >> stat[2] >> stat[3]; //stat[2] : Attack , stat[3] : Defense
if (stat[2] > 50 && stat[3] > 50)
{
break;
}
else
{
cout << "Attack or Defense is too low. Try again." << endl;
}
}
printStatus(heroName, stat);
return 0;
}
필수 구현의 STEP3을 구현하였다.

- HP/MP 포션 각 5개를 변수로 선언
- bool isGameStart = false같은 종료 플래그 만들기
- while(!isGameStart) + switch(choice)로 메뉴 루프 구현
- 1번 : HP+20, HP 포션 차감
- 2번 : MP+20, MP 포션 차감
- HP/MP 포션이 0개면 "포션 부족" 출력
- 3번 : 공격력 2배
- 4번 : 방버력 2배
- 5번 : 현재 능력치 출력
- 0번 : "게임을 시작합니다." 출력 후 isGameStart = true
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 4;
void printStatus(string name, int stat[])
{
cout << "\n====================================" << endl;
cout << " " << name << "\'s Stats" << endl;
cout << "====================================" << endl;
cout << "HP: " << stat[0] << "\t\t" << "MP: " << stat[1] << endl;
cout << "Attack: " << stat[2] << "\t" << "Defense: " << stat[3] << endl;
cout << "====================================" << endl;
}
int main(void)
{
string heroName;
int stat[SIZE] = { 0 };
int HP_Potion = 5, MP_Potion = 5;
int Attack_UP = 1, Defense_UP = 1;
int choice;
bool isGameStart = false;
cout << "===========================================" << endl;
cout << " [ Dungeon Escape Text RPG ]" << endl;
cout << "===========================================" << endl;
cout << "Enter your hero's name: ";
cin >> heroName;
while (true)
{
cout << "\nEnter HP and MP: ";
cin >> stat[0] >> stat[1]; //stat[0] : HP , stat[1] : MP
if (stat[0] > 50 && stat[1] > 50)
{
break;
}
else
{
cout << "HP or MP is too low. Try again.";
}
}
while(true)
{
cout << "Enter Attack and Defense: ";
cin >> stat[2] >> stat[3]; //stat[2] : Attack , stat[3] : Defense
if (stat[2] > 50 && stat[3] > 50)
{
break;
}
else
{
cout << "Attack or Defense is too low. Try again." << endl;
}
}
printStatus(heroName, stat);
cout << "* You received 5 HP Potions and 5 MP Potions." << endl;
cout << "============================================" << endl;
cout << " < Character Upgrade >" << '\n'
<< "1. HP UP" << '\t'
<< "2. MP UP" << '\t'
<< "3. Attack x2" << '\n'
<< "4. Defense x2" << '\t'
<< "5. Show stats" << '\t'
<< "0. Start Game" << endl;
cout << "============================================" << endl;
while (!isGameStart)
{
cout << "Choose: ";
cin >> choice;
switch (choice)
{
case 0:
cout << "Starting the game!" << endl;
isGameStart = true;
break;
case 1:
if (HP_Potion <= 0)
{
cout << "There are no potions left." << endl;
break;
}
else
{
HP_Potion -= 1;
cout << "* HP increased by 20. (HP Potion used: " << HP_Potion << " Left)" << endl;
stat[0] += 20;
break;
}
case 2:
if (MP_Potion <= 0)
{
cout << "There are no potions left." << endl;
break;
}
else
{
MP_Potion -= 1;
cout << "* MP increased by 20. (MP Potion used: " << MP_Potion << " Left)" << endl;
stat[1] += 20;
break;
}
case 3:
if (Attack_UP <= 0)
{
cout << "You already used the chance." << endl;
break;
}
else
{
Attack_UP -= 1;
cout << "* Attack increased by x2. (Attack_UP used: " << Attack_UP << " Left)" << endl;
stat[2] *= 2;
break;
}
case 4:
if (Defense_UP <= 0)
{
cout << "You already used the chance." << endl;
break;
}
else
{
Defense_UP -= 1;
cout << "* Defense increased by x2. (Defense_UP used: " << Defense_UP << " Left)" << endl;
stat[3] *= 2;
break;
}
case 5:
printStatus(heroName, stat);
break;
}
}
return 0;
}'내일배움캠프' 카테고리의 다른 글
| 내일배움캠프 언리얼트랙 11일차 (0) | 2026.05.06 |
|---|---|
| 내일배움캠프 언리얼트랙 10일차 (0) | 2026.05.04 |
| 내일배움캠프 언리얼트랙 8일차 (0) | 2026.04.29 |
| 내일배움캠프 언리얼트랙 7일차 (0) | 2026.04.28 |
| 내일배움캠프 언리얼트랙 6일차 (0) | 2026.04.27 |