숫자 야구 게임(배열 이용)

#include <iostream>
#include <time.h>
#include <Windows.h>
using namespace std;
//숫자야구 배열로 만들어라.
int main()
{
srand(time(NULL));
clock();
cout << "[[ 숫자 야구 게임 ]]" << endl;
cout << "[[ 숫자 출제 중입니다. ]]" << endl;
//1. 컴퓨터가 숫자를 뽑아라
//숫자 3개 뽑기(0~9범위)
int com[3];
while (true)
{
com[0] = rand() % 10;
com[1] = rand() % 10;
com[2] = rand() % 10;
//컴 숫자 중복 확인
if (com[0] == com[1] || com[1] == com[2] || com[2] == com[0]) continue;
break;
}
Sleep(10);
cout << "치트 : " << com[0] << com[1] << com[2] << endl << endl;
int strike = 0;
int ball = 0;
int out = 0;
int i = 0;
while (strike != 3)
{
//몇번째 시도인가
i = i++;
cout << i << " 번째 시도입니다." << endl;
//2. 유저가 숫자를 선택 하라
int user[3];
while (true)
{
cout << "첫번째 숫자 : ";
cin >> user[0];
cout << "두번째 숫자 : ";
cin >> user[1];
cout << "세번째 숫자 : ";
cin >> user[2];
//유저 숫자 중복 확인
if (user[0] == user[1] || user[1] == user[2] || user[3] == user[1])
{
cout << "같은 수가 있습니다. 세 수는 서로 다른 수를 넣으세요" << endl << endl;
continue;
}
if (user[0] < 0 || user[0] > 9 || user[1] < 0 || user[1] > 9 || user[2] < 0 || user[2] > 9)
{
cout << "0~9 사이의 수를 입력하세용" << endl << endl;
continue;
}
break;
}
strike = 0;
ball = 0;
out = 0;
//3. 컴퓨터와 유저 숫자를 비교하라.
//스트라이크, 볼, 아웃 카운트
if (user[0] == com[0]) strike++;
if (user[1] == com[1]) strike++;
if (user[2] == com[2]) strike++;
if (user[0] == com[1]) ball++;
if (user[0] == com[2]) ball++;
if (user[1] == com[3]) ball++;
if (user[1] == com[2]) ball++;
if (user[2] == com[3]) ball++;
if (user[2] == com[1]) ball++;
if (user[0] != com[0] && user[0] != com[1] && user[0] != com[2]) out++;
if (user[1] != com[0] && user[1] != com[1] && user[1] != com[2]) out++;
if (user[2] != com[0] && user[2] != com[1] && user[2] != com[2]) out++;
cout << strike << "-strike\t" << ball << "-ball\t" << out << "-out" << endl << endl;
if(strike == 3) cout << endl << "★ 즈엉다압 ★~~~~~~~~~" << endl << endl << endl;
//마지막으로 전체 게임 반복 처리
}//end of while(반복게임)
return 1127;
}//end of main
댓글
댓글 쓰기