Advertisement
Guest User

Untitled

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