U E D R , A S I H C RSS

Number Baseball Game/인수

  • 애들 시키려고 한번 만들어봤음

~cpp 
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;

const int DIGIT = 3;

void GenerateRandomNumber(int real[]);
void Process(int real[]);
bool IsExistSameNumber(int nth, int real[]);
void Calculate(int expected[], int real[], int& strike, int& ball);

int main()
{
	int real[3] = {0,};
	GenerateRandomNumber(real);
	Process(real);
	return 0;
}

void GenerateRandomNumber(int real[])
{
	srand(time(0));
	real[0] = rand() % 9 + 1;	
	for(int i = 1 ; i < DIGIT ; )
	{
		real[i] = rand() % 9;
		if(!IsExistSameNumber(i, real))
			++i;
	}
}

void Process(int real[])
{
	int expected[3] = {0,};
	for(int i = 0 ; i < 10 ; ++i)
	{
		cout << ">>";
		cin >> expected[0] >> expected[1] >> expected[2];
		int strike = 0;
		int ball = 0;
		Calculate(expected, real, strike, ball);
		if( !(strike == 0 && ball == 0) )
		{
			cout << strike << "스트라이크 " << ball << "볼" << endl;
			if(strike == 3)
			{
				cout << "축하합니다. " << endl;
				break;
			}
		}
		else
			cout << "아웃" << endl;		
	}
}

bool IsExistSameNumber(int nth, int real[])
{
	for(int j = 0 ; j < nth ; ++j)
	{
		if(nth != j && real[nth] == real[j])
			return true;	
	}
	return false;
}

void Calculate(int expected[], int real[], int& strike, int& ball)
{
	for(int i = 0 ; i < DIGIT; ++i)	
	{
		for(int j = 0 ; j < DIGIT ; ++j)
		{
			if(expected[i] == real[j])
			{
				if(i == j)
					++strike;
				else
					++ball;
				break;
			}
		}
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:51
Processing time 0.0103 sec