Advertisement
Guest User

Untitled

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