4강 과제 묵찌빠 게임 만들기의 코드입니다.
코드 자체는 정상적으로 구동하는것을 확인했습니다만,
공격시, 혹은 방어시의 상태를 구분하기 위해 넣은
반복문
While (bAttack)//공격상태 과 While (bDeffense)//방어상태 반복문에서
방어상태 -> 공격상태로 넘어가는 부문을 만들기가 어려워 'goto Reattack;' 코드로 되돌렸는데요.
다른 방식으로 조건문을 번복시킬수 있는 방법이 있을까 싶어 질문 드립니다.
#include
#include
using namespace std;
int main()
{
srand(time(NULL));
rand();
int nPlayer;
bool bCheck=true;
bool bAttack = false, bDeffense = false;
cout << "가위 바위 보!" << endl << "1.가위 2.바위 3.보" << endl;
// 1 가위 2 바위 3 보
while (bCheck)
{
cin >> nPlayer;
cout << "당신의 선택은 :";
int nCom = rand() % 3;
if (nPlayer == 1)
{
cout << "가위! vs 컴퓨터는 ";
if (nCom == 0)
{
cout << "가위!" << "무승부!" << endl;
}
else if (nCom == 1)
{
cout << "바위!" << "방어!" << endl;
bDeffense = true;
bCheck = false;
}
else
{
cout << "보!" << "공격!" << endl;
bAttack = true;
bCheck = false;
}
}
else if (nPlayer == 2)
{
cout << "바위! vs 컴퓨터는 ";
if (nCom == 0)
{
cout << "가위!" << "공격!" << endl;
bAttack = true;
bCheck = false;
}
else if (nCom == 1)
{
cout << "바위!" << "무승부!" << endl;
}
else
{
cout << "보!" << "방어!" << endl;
bDeffense = true;
bCheck = false;
}
}
else
{
cout << "보! vs 컴퓨터는 ";
if (nCom == 0)
{
cout << "가위!" << "방어!" << endl;
bDeffense = true;
bCheck = false;
}
else if (nCom == 1)
{
cout << "바위!" << "공격!" << endl;
bAttack = true;
bCheck = false;
}
else
{
cout << "보!" << "무승부!" << endl;
}
}
}
Reattack: <<<< 이부분
while (bAttack)
{
cout << "공격할 차례 입니다!" << endl << "1.가위 2.바위 3.보" << endl;
cout << "당신의 선택은 :";
cin >> nPlayer;
int nCom = rand() % 3;
if (nPlayer == 1)
{
cout << "가위! vs 컴퓨터는 ";
if (nCom == 0)
{
cout << "가위!" << endl << "승리!" << endl;
break;
}
else if (nCom == 1)
{
cout << "바위!" << endl << "방어!" << endl;
bAttack = false;
bDeffense = true;
}
else
{
cout << "보!" << endl << "다시 공격!" << endl;
}
}
else if (nPlayer == 2)
{
cout << "바위!";
if (nCom == 0)
{
cout << "가위!" << endl << "다시 공격!" << endl;
}
else if (nCom == 1)
{
cout << "바위!" << endl << "승리!" << endl;
break;
}
else
{
cout << "보!" << endl << "방어!" << endl;
bAttack = false;
bDeffense = true;
}
}
else
{
cout << "보!";
if (nCom == 0)
{
cout << "가위!" << endl << "방어!" << endl;
bAttack = false;
bDeffense = true;
}
else if (nCom == 1)
{
cout << "바위!" << endl << "다시 공격!" << endl;
}
else
{
cout << "보!" << endl << "승리!" << endl;
break;
}
}
}
while (bDeffense)
{
cout << "방어할 차례 입니다!" << endl << "1.가위 2.바위 3.보" << endl;
cout << "당신의 선택은 :";
cin >> nPlayer;
int nCom = rand() % 3;
if (nPlayer == 1)
{
cout << "가위! vs 컴퓨터는 ";
if (nCom == 0)
{
cout << "가위!" << endl << "패배!" << endl;
break;
}
else if (nCom == 1)
{
cout << "바위!" << endl << "다시 방어!" << endl;
}
else
{
cout << "보!" << endl << "공격!" << endl;
bAttack = true;
bDeffense = false;
goto Reattack; <<< 이부분!
}
}
else if (nPlayer == 2)
{
cout << "바위!";
if (nCom == 0)
{
cout << "가위!" << endl << "공격!" << endl;
bAttack = true;
bDeffense = false;
goto Reattack; <<< 이부분!
}
else if (nCom == 1)
{
cout << "바위!" << endl << "패배!" << endl;
break;
}
else
{
cout << "보!" << endl << "다시 방어!" << endl;
}
}
else
{
cout << "보!";
if (nCom == 0)
{
cout << "가위!" << endl << "다시 방어!" << endl;
}
else if (nCom == 1)
{
cout << "바위!" << endl << "공격!" << endl;
bAttack = true;
bDeffense = false;
goto Reattack; <<< 이부분!
}
else
{
cout << "보!" << endl << "패배!" << endl;
break;
}
}
}
system("pause");
}
안녕하세요 게임클래스 입니다.
아무래도 goto문은 실시간으로 CPU의 제어 부분을 변경해 주는 만큼
잘 사용하지는 않고 있다 정도만 알아두셔도 무방합니다.
가장 간단한 방법으로는 반복과 탈출이 있습니다.
while(true) <--무한반복
{
while(조건) <-- 공격에 대한 while
{
}
while(조건) <-- 방어에 대한 while
{
}
}
와 같은 구조로 작성하게 되면 방어가 끝난후 이 2가지를 묶는 while이 있기 때문에 다시 공격while로 반복됩니다.
이후 특정 조건 만족 시 break를 통하여 무한반복하는 while를 탈출하여 게임을 종료시키는 방법이 있습니다.
감사합니다.