Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. #define TURN 10
  6.  
  7. typedef struct {
  8. int ans[3];
  9. int strike;
  10. int ball;
  11. } player;
  12.  
  13. void generateRandomAns(int arr[]) {
  14. arr[0] = arr[1] = arr[2] = 0;
  15. while (arr[0] == arr[1] || arr[1] == arr[2]) {
  16. arr[0] = rand() % 9 + 1;
  17. arr[1] = rand() % 9 + 1;
  18. arr[2] = rand() % 9 + 1;
  19. }
  20. }
  21.  
  22. player *init() {
  23. player *p = (player *) malloc(sizeof(player));
  24. p->strike = p->ball = 0;
  25. generateRandomAns(p->ans);
  26. return p;
  27. }
  28.  
  29. //guessing game
  30. //if s = 3 you win
  31. int nextTurn(player *p, int *gcount) {
  32. (*gcount)++;
  33. printf("Turn %d / %d\n", *gcount, TURN);
  34. printf("Input guessing number. ex) 123 :\n");
  35. //TODO
  36. //implement this
  37.  
  38.  
  39. if (p->strike == 3) {
  40. return 1;
  41. }
  42. return 0;
  43. }
  44.  
  45. void endGame(player *p) {
  46. int *ans = p->ans;
  47. printf("You lost! Answer is %d%d%d\n", ans[0], ans[1], ans[2]);
  48. }
  49. int main(void)
  50. {
  51. //init random
  52. srand((unsigned int)time(NULL));
  53. player *pc = init();
  54. int gcount = 0;
  55. while (gcount < TURN) {
  56. int ret = nextTurn(pc, &gcount);
  57. if (ret) {
  58. printf("You made the day! score is: %d\n", gcount);
  59. return 0;
  60. }
  61. }
  62. endGame(pc);
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement