숫자 야구 게임



#include <iostream>
#include <time.h>
#include <Windows.h>

using namespace std;

//컴퓨터 중복 안된 3개 뽑기 (0~9)까지
//유저 입력 3개
//결과 비교

int main()
{
//컴터 랜덤 초기화
srand(time(NULL));

//컴터 중복 안된 숫자 3개 뽑기
int num1;
int num2;
int num3;
//
while (1) {
//문제 출제
cout << "★ Number Baseball Game ★" << endl << "[[ Making match... ]]" << endl;
num1 = rand() % 10;
num2 = rand() % 10;
num3 = rand() % 10;
//cout << num1 << num2 << num3 << endl;
if (num1 == num2 || num2 == num3 || num3 == num1)
{
cout << endl << "!! WARNING !! find same number. make the match again" << endl;
Sleep(2000);
system("cls");
continue;
}//end of if
break;
}//end of while



Sleep(1000); //1.5초 지연
cout << "[[ Finish making match. ]]\n[[ Put three numbers between 0 ~ 9 to play ]]" << endl << endl;


//결과 변수 선언
int strike = 0;
int ball = 0;
int out = 0;

//반복 회수 선언
int count = 0;

//반복
while (strike != 3)
{
count++;
cout << "[[ " << count << " try ]]" << endl;
//유저 입력 3개 받기(답안 제출)
int numU1;
int numU2;
int numU3;
cin >> numU1 >> numU2 >> numU3;
cout << "Your number : " << numU1 << " " << numU2 << " " << numU3 << endl;
//유저 입력 범위 초과시 예외 처리
if (numU1 < 0 || numU1 > 9)
{
cout << "입력범위를 초과 하였습니다. 0~9 사이 숫자를 입력 하세요" << endl << endl;
continue;
}
if (numU2 < 0 || numU2 > 9)
{
cout << "입력범위를 초과 하였습니다. 0~9 사이 숫자를 입력 하세요" << endl << endl;
continue;
}
if (numU3 < 0 || numU3 > 9)
{
cout << "입력범위를 초과 하였습니다. 0~9 사이 숫자를 입력 하세요" << endl << endl;
continue;
}

//유저 입력 중복 안되게 처리
if (numU1 == numU2 || numU2 == numU3 || numU3 == numU1)
{
cout << "don't put same number. try again " << endl << endl;
continue;
}
//결과값 초기화
strike = 0;
ball = 0;
out = 0;

//문제와 답안 비교
//스트라이크 확인
if (num1 == numU1)
{
strike++;
}
if (num2 == numU2)
{
strike++;
}
if (num3 == numU3)
{
strike++;
}

//볼 확인
if (numU1 == num2)
{
ball++;
}
if (numU1 == num3)
{
ball++;
}
if (numU2 == num1)
{
ball++;
}
if (numU2 == num3)
{
ball++;
}
if (numU3 == num1)
{
ball++;
}
if (numU3 == num2)
{
ball++;
}//end of if of ball

//out 확인
if (numU1 != num1 && numU1 != num2 && numU1 != num3)
{
out++;
}
if (numU2 != num1 && numU2 != num2 && numU2 != num3)
{
out++;
}
if (numU3 != num1 && numU3 != num2 && numU3 != num3)
{
out++;
}
cout << strike << "-Strike\t" << ball << "-Ball\t\t" << out << "-Out" << endl << endl;
}//end of while
cout << endl << "[[ Conglatulation! [" << count << "] try, you find! ]] 오늘은 치킨이다~~~" << endl;

}//end of main






댓글