[C++] 숫자야구 게임

2022. 6. 3. 01:05코딩 1막 <C++개념편>/코딩 1막 <C++응용편>

728x90

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

void main()
{
	srand(time(NULL));

	int randomNumber1 = rand() % 10;
	int randomNumber2 = rand() % 10;
	int randomNumber3 = rand() % 10;

	while (randomNumber1 == randomNumber2 || randomNumber2 == randomNumber3 || randomNumber1 == randomNumber3)
	{
		int randomNumber1 = rand() % 10;
		int randomNumber2 = rand() % 10;
		int randomNumber3 = rand() % 10;
	}

	int comRandomNumber[3] = { randomNumber1, randomNumber2, randomNumber3 };
	int myNumber;
	int strike, ball;
	int count = 1;



	cout << randomNumber1 << randomNumber2 << randomNumber3 << endl;

	cout << "!!숫자야구게임!!" << endl;
	while (1)
	{
		strike = ball = 0;

		cout << count << "번 째" << endl;

		cout << "숫자를 입력해주세요 : ";
		cin >> myNumber;

		if (myNumber >= 1000) cout << "입력 범위를 넘어섰습니다.\n\n" << endl;
		else if (!cin)
		{
			cout << "!!!!숫자만 입력해주세요!!!!" << endl;
			cin.clear();
			cin.ignore(INT_MAX, '\n');
			cout << "\n\n";
		}
		else {
			if (myNumber / 100 == comRandomNumber[0]) strike++;
			else if (myNumber / 100 == comRandomNumber[1]) ball++;
			else if (myNumber / 100 == comRandomNumber[2]) ball++;

			if (myNumber % 100 / 10 == comRandomNumber[1]) strike++;
			else if (myNumber % 100 / 10 == comRandomNumber[0]) ball++;
			else if (myNumber % 100 / 10 == comRandomNumber[2]) ball++;

			if (myNumber % 10 == comRandomNumber[2]) strike++;
			else if (myNumber % 10 == comRandomNumber[0]) ball++;
			else if (myNumber % 10 == comRandomNumber[1]) ball++;


			if (strike == 3)
			{
				cout << "정답입니다. 컴퓨터의 숫자는 " << randomNumber1 << randomNumber2 << randomNumber3 << "입니다." << endl;
				cout << count << "번 만에 성공했습니다." << endl;
				break;
			}
			else
				cout << strike << "S " << ball << "B" << endl;

			count++;
			cout << "\n\n\n";
		}
	}
}

출력 결과

여러분도 함 해보세요 ㅋㅋㅋ 개꿀잼 3트만에 성공

728x90