U E D R , A S I H C RSS

Number Baseball Game/영동

~cpp 
//숫자 야구 놀이
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
int main()
{
	int i, j;
	int input, input_1, input_2, input_3;
	int pitcher[3]={0,};
	int batter[3]={0,};
	int strike=0;
	int ball=0;

	//투수의 숫자 설정
	srand((unsigned)time(NULL));
	pitcher[0]=rand()%9+1;
	do{
		pitcher[1]=rand()%9+1;
	}while(pitcher[1]==pitcher[0]);
	do{
		pitcher[2]=rand()%9+1;
	}while(pitcher[2]==pitcher[1] || pitcher[2]==pitcher[0]);
	for(i=0;i<3;i++)
		cout<<pitcher[i];
	cout<<"\n";

	//타자의 숫자 입력	
	do{
		cout<<"숫자를 입력해라.\n";
		cin>>input;
		input_1=input/100;
		input_2=(input%100)/10;
		input_3=(input%100)%10;
	}while(input==0 || input>987 || input_1==input_2 || input_2==input_3 || input_1==input_3);
	batter[0]=input_1;
	batter[1]=input_2;
	batter[2]=input_3;

	//투수와 타자의 숫자 비교
	for(i=0;i<3;i++){
		if(pitcher[i]==batter[i])
			strike++;}
	for(i=0;i<3;i++){
		for(j=0;j<3;j++){
			if(pitcher[i]== batter[j]){
				if(i!=j)
					ball++;
			}
		}
	}
	cout<<strike<<" strike~~~!"<<endl;
	cout<<ball<<" ball~~~!"<<endl;
	if(strike==3)
		cout<<"\nYou Win!\n";
	else 
		cout<<"\nYou Lose!\n";
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:52
Processing time 0.0105 sec