Skip to content

Instantly share code, notes, and snippets.

@honux77
Last active April 15, 2019 01:17
Show Gist options
  • Select an option

  • Save honux77/716efc67b40e0178372375ecbe059281 to your computer and use it in GitHub Desktop.

Select an option

Save honux77/716efc67b40e0178372375ecbe059281 to your computer and use it in GitHub Desktop.
숫자야구 C로 구현하기
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define TURN 10
typedef struct {
int ans[3];
int strike;
int ball;
} player;
void generateRandomAns(int arr[]) {
arr[0] = arr[1] = arr[2] = 0;
while (arr[0] == arr[1] || arr[1] == arr[2]) {
arr[0] = rand() % 9 + 1;
arr[1] = rand() % 9 + 1;
arr[2] = rand() % 9 + 1;
}
}
player *init() {
player *p = (player *) malloc(sizeof(player));
p->strike = p->ball = 0;
generateRandomAns(p->ans);
return p;
}
//guessing game
//if s = 3 you win
int nextTurn(player *p, int *gcount) {
(*gcount)++;
printf("Turn %d / %d\n", *gcount, TURN);
printf("Input guessing number. ex) 123 :\n");
//TODO
//implement this
if (p->strike == 3) {
return 1;
}
return 0;
}
void endGame(player *p) {
int *ans = p->ans;
printf("You lost! Answer is %d%d%d\n", ans[0], ans[1], ans[2]);
}
int main(void)
{
//init random
srand((unsigned int)time(NULL));
player *pc = init();
int gcount = 0;
while (gcount < TURN) {
int ret = nextTurn(pc, &gcount);
if (ret) {
printf("You made the day! score is: %d\n", gcount);
return 0;
}
}
endGame(pc);
return 0;
}
@honux77
Copy link
Author

honux77 commented Apr 14, 2019

//TODO 부분을 구현하면 완성됩니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment