묵찌빠 게임의 구조는 4강에서 만들었던 코드를 그대로 사용했습니다.
전체 게임을 반복시키기 위한 bool 값 bSetgame 값을 추가하였고
플레이어와 컴퓨터의 승리를 체크하기위한 int nPW, nCW를 추가하여
논리연산자 ||을 사용하여 게임을 끝냈습니다.
피드백 부탁드립니다.
#include
#include
using namespace std;
int main()
{
srand(time(NULL));
rand();
int nPlayer, nPW=0, nCW=0;
bool bCheck = true, bSetgame = true;
bool bAttack = false, bDeffense = false;
cout << "가위 바위 보!" << endl << "1.가위 2.바위 3.보" << endl;
// 1 가위 2 바위 3 보
while (bSetgame)
{
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;
nPW++;
bAttack = false;
}
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;
nPW++;
bAttack = false;
}
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;
nPW++;
bAttack = false;
}
}
}
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;
nCW++;
bDeffense = false;
}
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;
nCW++;
bDeffense = false;
}
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;
nCW++;
bDeffense = false;
}
}
}
cout << "플레이어:" << nPW << " vs 컴퓨터:" << nCW;
if (nPW == 2 || nCW == 2)
{
bSetgame = false;
if (nPW == 2)
{
cout << endl << "승리했습니다!" << endl;
}
else
{
cout << endl << "패배했습니다!" << endl;
}
}
else
{
bCheck = true;
cout << endl<< "다시 시작하겠습니다." << endl << "가위 바위 보!" << endl << "1.가위 2.바위 3.보" << endl;
}
}
system("pause");
}
안녕하세요 게임클래스 입니다.
기본적인 답변은 전에 질문에 답변을 드렸습니다.
goto같은 구문을 사용하지 않고 코드를 간결하게 완성할 수 있는 방법에 대해 말씀 드렸는데
이미 해결을 하신듯 하네요^^
정상적으로 진행이 된다면 문제는 없는 코드이며, 차후 함수부분에 대해 공부를 하시고 나면
좀더 가독성있고 깔끔한 코딩이 될 것입니다.
순서대로 공부를 하다가 함수를 공부하고 다시 코딩해 보시길 추천드립니다.
감사합니다.