8강 과제, 배열 셔플을 이용한 야구게임 입니다.
nRand 데이터값은 10~19가 나오도록 조정했고
nCount변수값으로 10회 이상 틀릴경우 함수를 빠져나가 실패했다는 텍스트를 출력하게했습니다.
#include
#include
using namespace std;
int main()
{
int nUser[3];
int nRand[10];
int nStrike = 0, nBall, nOut;
int nCount;
bool bEnding = true;
srand(time(NULL));
rand();
while (bEnding) // 게임 지속 or 끝내기
{
for (int i = 0; i < 10; i++)
nRand[i] = i + 10;
for (int i = 0; i < 100; i++)
{
int nScr = rand() % 9;
int nDest = rand() % 9;
int nTemp = nRand[nScr];
nRand[nScr] = nRand[nDest];
nRand[nDest] = nTemp;
}//랜덤셔플
nCount = 0;
nStrike = nBall = nOut = 0; // 변수 초기화
while (nStrike < 3 && nCount < 10) // 게임 시작
{
nStrike = nBall = nOut = 0;
nCount++;
cout << nCount << "회 시도" << endl;
cout << "첫번째 숫자를 입력하세요. (10~19) : ";
cin >> nUser[0];
cout << endl << "두번째 숫자를 입력하세요. (10~19) :";
cin >> nUser[1];
cout << endl << "세번재 숫자를 입력하세요. (10~19) :";
cin >> nUser[2];
cout << nRand[0]<< "." << nRand[1]<<"." << nRand[2] << endl;
for (int i = 0 ; i < 3; i++)
{
if (nUser[i] == nRand[i])
nStrike++;
else if (nUser[i] == nRand[(i + 1) % 3] || nUser[i] == nRand[(i + 2) % 3])
nBall++;
else
nOut++;
}
cout << nStrike << ":스트라이크" << endl;
cout << nBall << ":볼" << endl;
cout << nOut << ":아웃" << endl;
system("pause");
system("cls");
}
if (nStrike == 3)
{
cout << nCount << "회 만에 성공했습니다." << endl;
}
else
{
cout << nCount << "회를 넘어 실패했습니다." << endl;
}
cout << "새 게임을 시작할까요? (y/n)" << endl;
char chContinue;
cin >> chContinue;
if (chContinue == 'n')
{
bEnding = false;
cout << "게임 끝!";
}
}
}
안녕하세요 게임클래스 입니다.
언제나 열심히 과제하는 모습에
답변다는 제가 빡시긴하지만 그래도 뿌듯 합니다.^^
앞으로도 궁금하신거 있으시면 질문 주세요~~
감사합니다.