Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.85 KB | None | 0 0
  1. /* Khai Quang Ho, kho19@student.aau.dk, A402, SW1 */
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #define BONUS_ROUND 6
  7. #define MAX_ROUND 15
  8. #define MAX_DICE_EYES 6
  9.  
  10. void flush(void);
  11. void scan_data(int *n);
  12. void reset_dice(int *dice_list);
  13.  int dice_roll(void);
  14. void roll_multiple_dices(int dice_list[], int n);
  15. void sum_of_all(int n, int dice_list, int *score);
  16. void sum_of_pair(int n, int dice_list[], int *score);
  17. void sum_of_kind(int kind_size, int dice_list[], int *score);
  18. void sum_of_straight(int start, int dice_list[], int *score);
  19. void sum_of_house(int dice_list[], int *score);
  20. void sum_of_chance(int dice_list[], int *score);
  21. void sum_of_yatzy(int dice_list[], int *score);
  22. void calc_round(int i, int dice_list[], int *points, int *score);
  23. void check_bonus_round(int i, int n, int *score);
  24. void print_round_name(const char **round_name, int i);
  25. void print_dices(int dice_list[]);
  26. void print_points(int points);
  27. void print_score(int *score);
  28. void print_round(int *dice_list, int round, const char **round_name, int points, int *score);
  29. /* prompt user for dice amount, then plays game of yatzy with given dices, repeat until input less than 5*/
  30. int main(void){
  31.     int dices = 0, round = 1, score = 0, points = 0;
  32.     int dice_list[MAX_DICE_EYES + 1]; /* +1 because index 0 is ignored */
  33.     const char *round_name[] =
  34.     {"NULL", "Ones", "Twos", "Threes", "Fours", "Fives", "Sixes",
  35.     "One pair", "Two pairs", "Three of a kind", "Four of a kind",
  36.     "Small straight", "Large straight", "Full house", "Chance", "Yatzy"};
  37.     srand(time(NULL)); /*  seed random number gen. with time since Jan 1. 1970 */
  38.  
  39.     do {
  40.         scan_data(&dices);
  41.         for(round = 1; round <= MAX_ROUND && dices >= 5; round++){
  42.  
  43.             roll_multiple_dices(dice_list, dices);
  44.             calc_round(round , dice_list, &points, &score);
  45.             check_bonus_round(round, dices, &score);
  46.             print_round(dice_list, round, round_name, points, &score);
  47.         }
  48.     }while(dices >= 5);
  49.     return 0;
  50. }/* TODO fix 3 of a kind deleting stuff */
  51.  
  52. /* prompts user for input, re-prompt if input is not int */
  53. void scan_data(int *dices){
  54.     printf("\nInsert number of Dices:(less than 5 to exit): ");
  55.  
  56.     while(scanf(" %d", dices) != 1){ /* if scanf doesnt return 1, the input didn't contain int */
  57.         printf("\ninvalid input, try again: ");
  58.         flush();
  59.     }
  60.     flush(); /* flush one last time in case multiple valid inputs was given (input: 10 5 14) */
  61. }
  62. /* flushes all non int stored in scanf */
  63. void flush(void){
  64.     char skip;
  65.     do{
  66.         scanf("%c", &skip);
  67.     }while(skip != '\n');
  68. }
  69. /* prints the equivalent string of a given round */
  70. void print_round_name(const char **round_name, int round){
  71.     printf("%-16s ", round_name[round]);
  72. }
  73. /* rolls dices and stores them in the array dice_list */
  74. void roll_multiple_dices(int dice_list[], int n){
  75.     int i;
  76.  
  77.     reset_dice(dice_list);
  78.     for(i = 1; i < (n + 1); i++) /* rolls as many times as dices given (+1 because index 0 is skipped) */
  79.         dice_list[dice_roll()]++; /* store the amount of rolls for each type (1-6) */
  80. }
  81. /* reset dice values */
  82. void reset_dice(int *dice_list){
  83.     int i;
  84.  
  85.     for(i = 0; i <= MAX_DICE_EYES+1; i++) /* +1 because 0 index is ignored */
  86.         dice_list[i] = 0;
  87. }
  88. /* generates random num 1-6 */
  89. int dice_roll(void){
  90.     return ((rand() % MAX_DICE_EYES) + 1); /* +1 to avoid rolling 0 */
  91. }
  92. /* prints dice rolls */
  93. void print_dices(int dice_list[]){ /* put in main? */
  94.     int dice_face, sum;
  95.  
  96.     printf("Dices:");
  97.     for(dice_face = 1; dice_face <= MAX_DICE_EYES; dice_face++ ){ /* loops for each dice face 1-6 */
  98.         for(sum = dice_list[dice_face]; sum > 0; sum--) /* dice_list[i] is the number of dices with the face i (1-6) */
  99.             printf(" %d", dice_face);
  100.     }
  101. }
  102. /* decides function to be called for each round and formats function parameters*/
  103. void calc_round(int i, int dice_list[], int *points, int *score){
  104.     *points = 0;
  105.     if(i <= 6)
  106.         sum_of_all(i, dice_list[(i)], points);
  107.     else if(i <= 8)
  108.         sum_of_pair((i-6), dice_list, points); /* i-6 because the parameter is size of pair */
  109.     else if(i <= 10)
  110.         sum_of_kind((i-6), dice_list, points);
  111.     else if(i <= 12)
  112.         sum_of_straight((i-10), dice_list, points);
  113.     else if(i == 13)
  114.         sum_of_house(dice_list, points);
  115.     else if(i == 14)
  116.         sum_of_chance(dice_list, points);
  117.     else if(i == 15)
  118.         sum_of_yatzy(dice_list, points);
  119.     *score += *points;
  120. }
  121. /* calculates sum of all dices with given face (1-6), but max 5 dices */
  122. void sum_of_all(int n, int dice_list, int *score){
  123.  
  124.     if(dice_list > 5)
  125.         *score = (5 * n);
  126.     else
  127.         *score = (dice_list * n);
  128. }
  129. /* calculates sum of pair of a give size */
  130. void sum_of_pair(int n, int dice_list[], int *score){
  131.     int index, amount = n, points = 0;
  132.  
  133.     for(index = MAX_DICE_EYES; index > 0 && amount != 0; index--){
  134.         if(dice_list[index] >= 2){
  135.             points += (index * 2);
  136.             amount--;
  137.         }
  138.     }
  139.     if(amount == 0)
  140.         *score += points;
  141. }
  142. /* calculates sum "n" of a kind, n varying on input */
  143. void sum_of_kind(int kind_size, int dice_list[], int *score){
  144.     int index;
  145.  
  146.     for(index = MAX_DICE_EYES; index > 0; index--){
  147.         if(dice_list[index] >= kind_size){
  148.             *score = (index * kind_size);
  149.             dice_list[index] = 0; /* remove the dice type to prevent it being reused */
  150.             index = 0;
  151.         }
  152.     }
  153. }
  154. /* calculates sum of either small or large straight depending on input */
  155. void sum_of_straight(int start, int dice_list[], int *score){
  156.     int index, end = (start + 5), buffer_score = 0;
  157.  
  158.     for(index = start; index < end && dice_list[index] > 0; index++ )
  159.         buffer_score += index;
  160.  
  161.     if(index == end)
  162.         *score = buffer_score;
  163. }
  164. /* calculates sum of full house */
  165. void sum_of_house(int dice_list[], int *score){
  166.     int points1 = 0, points2 = 0;
  167.  
  168.     sum_of_kind(3, dice_list, &points1);/* once 3 of kind is found, dice_list element is removed to avoid overlap with function below*/
  169.     sum_of_pair(1 ,dice_list, &points2);
  170.  
  171.     if(points1 > 0 && points2 > 0) /* if both functions found a match */
  172.         *score += points1 + points2;
  173. }
  174. /* calculates sum of 5 largest dices */
  175. void sum_of_chance(int dice_list[], int *score){
  176.     int index, count = 0;
  177.  
  178.     for(index = MAX_DICE_EYES; dice_list[index]+count <= 5; index--){
  179.         *score += (dice_list[index] * index);
  180.         count += dice_list[index];
  181.     }
  182.     *score += (5-count) * index; /* adds remainder, if count is < 5 and dice_list[index]+count > 5 */
  183. }
  184. /* calculates sum of yatzy using sum_of_kind */
  185. void sum_of_yatzy(int dice_list[], int *score){
  186.  
  187.     sum_of_kind(5, dice_list, score);
  188.  
  189.     if(*score > 0)
  190.         *score = 50;
  191. }
  192. /* self containing module, check if round is bonus round, check score and calculate points, prints result*/
  193. void check_bonus_round(int i, int n, int *score){
  194.     int bonus = 0;
  195.  
  196.     if(i == BONUS_ROUND){
  197.         printf("\n%-16s Score: %-*d","Bonus round", (n*2)-1, *score); /*(n*2)-1, is the length of print_dice function -1 (one is used on left side) */
  198.  
  199.         if(*score >= 63)
  200.             bonus = 50;
  201.  
  202.         printf("| Earned %2d points\n\n",bonus);
  203.         *score += bonus;
  204.     }
  205. }
  206. /* prints points scored for current round, used each round */
  207. void print_points(int points){
  208.     printf("| Earned %2d points\n", points);
  209. }
  210. /* print score and reset it once printed, used once per game*/
  211. void print_score(int *score){
  212.     printf("\nFinal score: %d\n", *score);
  213.     *score = 0;
  214. }
  215. void print_round(int *dice_list, int round, const char **round_name, int points, int *score){
  216.     print_round_name(round_name, round);
  217.     print_dices(dice_list);
  218.     print_points(points);
  219.  
  220.     if(round == MAX_ROUND)
  221.         print_score(score);
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement