Skip to content

Instantly share code, notes, and snippets.

@ensup
Created October 4, 2024 06:58
Show Gist options
  • Save ensup/9fb7a245141e4284eb23393c0bd77b64 to your computer and use it in GitHub Desktop.
Save ensup/9fb7a245141e4284eb23393c0bd77b64 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void user(int*); //사용자 입력
void computer(int*); //컴퓨터 숫자 생성
int mapping(int*, int*); //매핑
int main(void) {
int arr_com[4] = { 0 };
int arr_user[4] = { 0 };
srand((unsigned)time(NULL));
computer(arr_com); //컴퓨터 숫자 랜덤 생성
while (1) {
user(arr_user); //사용자 입력
if (mapping(arr_com, arr_user))
break;
}
return 0;
}
void computer(int* arr_c) {
srand((unsigned int)time(NULL));
for (int i = 0; i < 4; i++) {
arr_c[i] = rand() % 9 + 1;
for (int k = 0; k<i; k++)
if (arr_c[k] == arr_c[i]) {
i--;
continue;
}
}
}
void user(int* arr_u) {
for (int i = 0; i < 4; i++)
scanf("%d", &arr_u[i]);
}
int mapping(int* u, int* c) {
int strike[4] = {0},str_num=0,ball_num=0;
for(int i=0;i<4;i++)
if (u[i] == c[i]) {
strike[i] = 1;
str_num++;
}
for (int i = 0; i < 4; i++) {//i=>user
if (!(strike[i])) {
int key = 0;
for (int j = 0; j < 4; j++) //j=>cpu
if (u[i] == c[j])
key = 1;
if (key)
ball_num++;
}
}
if (str_num == 4) {
puts("YOU WIN!");
return 1;
}
else if (!ball_num)
puts("OUT!");
else
printf("%d ball, %d strike.\n", ball_num, str_num);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment