printf("ho_tari\n");

baseball game 본문

C

baseball game

호타리 2023. 8. 31. 13:26
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
	int question[3];

	srand(time(NULL));

	question[0] = rand() % 9 + 1;

	do
	{
		question[1] = rand() % 9 + 1;
	}while(question[0] == question[1]);

	do
	{
		question[2] = rand() % 9 + 1;
	}while(question[0] == question[2] || question[1] == question[2]);

	printf("question: %d %d %d\n", question[0], question[1], question[2]);

	int count = 0;

	for(;;)   // while(1)
	{
		++count;

		int strike, ball;
        
		strike = ball = 0;

		int answer[3];

		printf("answer : ");

		scanf("%d %d %d", &answer[0], &answer[1], &answer[2]);

		for (int i = 0; i < 3; ++i)
		{
			for (int j = 0; j < 3; ++j)
			{
				if (question[i] == answer[j])
				{
					if (i == j)
					{
						++strike;
					}
					else
					{
						++ball;
					}
				}
			}
		}

		printf("#strike : %d\t#ball : %d\n", strike, ball);

		printf("-------------------------------\n");

		if (strike == 3)
			break;
	}

	printf("Congraturation!! your counts : %d\n", count);

	return 0;
}

 

<compile 결과>

'C' 카테고리의 다른 글

bubbleSorting  (0) 2023.08.31
bigLetter  (0) 2023.08.31
average  (0) 2023.08.31
arrayCompare  (0) 2023.08.31
arrayAssignment  (0) 2023.08.31