정확한 이름은 잘 모르겠습니다.
그냥 아는 분이 이런걸 만드셔서 재미있어 보여서 따라 만들어봤습니다.
// 야구 숫자 게임.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "stdafx.h"
typedef struct
{
int strike;
int ball;
int foul;
}Result;
void InputNum(int*);
void SetAnswerNum(int*);
int CheckOverlap(const int, const int*);
Result CheckAnswer(const int*, const int*);
void PrintResult(const Result*);
int main()
{
int user_num[3];
int answer_num[3];
Result result;
int count = 0;;
srand((int)time(NULL));
SetAnswerNum(answer_num);
do
{
printf("숫자 3개를 입력하세요 :");
InputNum(user_num);
result = CheckAnswer(user_num, answer_num);
PrintResult(&result);
count++;
} while (result.strike!=3);
printf("\n축하드립니다. 정답입니다.\n");
printf("%d 회 시도하여 성공하셨습니다.", count);
system("pause");
return 0;
}
void InputNum(int* num)
{
int i;
for (i = 0; i < 3; i++)
{
scanf("%d", &num[i]);
}
}
void SetAnswerNum(int* num)
{
int i;
int rand_num[3] = { -1 };
for (i = 0; i < 3; i++)
{
do
{
num[i] = rand() % 10;
} while (CheckOverlap(num[i], rand_num));
rand_num[i] = num[i];
}
}
int CheckOverlap(const int num, const int *nums_list)
{
int nums_size = sizeof(nums_list) / sizeof(int*);
int i;
for (i = 0; i < nums_size; i++)
{
if (nums_list[i] == num)
{
return 1;
}
}
return 0;
}
Result CheckAnswer(const int* user_input, const int* answer)
{
int i, j;
int input_size = 3;
Result temp_answer;
temp_answer.ball = 0;
temp_answer.strike = 0;
temp_answer.foul = 0;
//파울 체크
for (i = 0; i < input_size; i++)
{
for (j = 0; j < input_size; j++)
{
if (!(i == j))
{
if (user_input[i] == answer[j])
{
temp_answer.ball++;
}
}
}
}
//스트라이크 체크
for (i = 0; i < input_size; i++)
{
if (user_input[i] == answer[i])
{
temp_answer.strike++;
}
}
//아웃 계산
temp_answer.foul = input_size - (temp_answer.ball + temp_answer.strike);
return temp_answer;
}
void PrintResult(const Result* result)
{
printf("%d 스트라이크, %d 볼\n", (*result).strike, (*result).ball);
}
여러 수를 받을 수 있게 좀 더 수정이 용이하게 작성하려고 했는데 매개 변수로 배열을 받고 이 녀석의 크기가 얼마나되는지 측정이 실패해서 제가 능력이 다 부족한 탓이겠지만...
일단 숫자가 중복되면 안된다고 해서 중복 방지기능이랑 등등.. 그런 부분을 조금만 고치면 숫자 늘려서 가능합니다.
위는 다운로드하는거구요. 뭐 바이러스 이런거 없습니다. 의심되시면 위에 소스로 컴파일 해보시던지요.
주석이 부실하지만 뭐 단순한 단어니까 패쓰.
ps. 다운로드 관련 태그를 만들어놔야겠네요. 너무 안예뻐요 ㅠㅠ...
'프로그래밍 > C' 카테고리의 다른 글
[C] 리눅스 환경에서 컴파일 (0) | 2016.08.09 |
---|---|
[C] 각종 자료형들 크기 알아보기 (0) | 2016.07.08 |
[C] 2차원 이동 예제(측면에서 바라보는 시점) (0) | 2016.07.08 |
[C] 2차원 이동 예제(위에서 바라보는 시점) (0) | 2016.06.25 |
[C] 간단한 Up AND Down 게임 (0) | 2016.06.25 |