SHARE
TWEET

Untitled

a guest Nov 13th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  8. int dice_roll(){
  9.     return ((rand()%6)+1); /* +1 to avoid rolling 0 */
  10. }
  11.     void reset_dice(int *dice_pairs){
  12.     int i;
  13.     for(i = 0; i <= 7; i++) /* reset dice values */
  14.         dice_pairs[i] = 0;
  15. }
  16. void roll_multiple_dices(int *dice_pairs, int n){
  17.     int i;
  18.     reset_dice(dice_pairs);
  19.     for(i = 1; i < n+1; i++) /* rolls as many times as dices given (+1 because index 0 is skipped) */
  20.         dice_pairs[dice_roll()] += 1; /* store the amount of rolls for each type */
  21.     print_dice(n, dice_pairs);
  22. }
  23. void sum_of_all(int n, int dice_pairs, int *score){
  24.     if(dice_pairs > 5)
  25.         *score = (5 * n);
  26.     else
  27.         *score = (dice_pairs * n);
  28. }
  29. void sum_of_pair(int n, int dice_pairs[], int *score){
  30.     int index, amount = n, buff_score = 0;
  31.     for(index = 6; index > 0 && amount != 0; index--){
  32.         if(dice_pairs[index] >= 2){
  33.             buff_score += (index * 2);
  34.             amount--;
  35.         }
  36.     }
  37.     if(amount == 0)
  38.         *score += buff_score;
  39. }
  40. void sum_of_kind(int n, int dice_pairs[], int *score){ /* optimize */
  41.     int index;
  42.     for(index = 6; index>0; index--){
  43.         if(dice_pairs[index] >= n){
  44.             *score = (index*n);
  45.             index = 0;
  46.         }
  47.     }
  48. }
  49. void sum_of_straight(int start, int dice_pairs[], int *score){
  50.     int index, end = (start+5), buffer_score = 0;
  51.     for(index = start; index < end && dice_pairs[index] > 0; index++ )
  52.         buffer_score += index;
  53.     if(index == end)
  54.         *score = buffer_score;
  55. }
  56. void sum_of_house(int dice_pairs[], int *score){ /* messy */
  57.     int index, buff_score = 0;
  58.     for(index = 6; index>0; index--){
  59.         if(dice_pairs[index] >= 3){
  60.             *score = (index*3);
  61.             dice_pairs[index] = 0;
  62.             sum_of_pair(1 ,dice_pairs, &buff_score); /* already pointer */
  63.             if(buff_score > 0)
  64.                 *score += buff_score;
  65.             else
  66.                 *score = 0;
  67.             index = 0;
  68.         }
  69.     }
  70. }
  71. void sum_of_chance(int dice_pairs[], int *score){
  72.     int index, count = 0;
  73.     for(index = 6; index > 0 && count <5; index--){
  74.         if(dice_pairs[index]+count <= 5){
  75.             *score += (dice_pairs[index] * index);
  76.             count += dice_pairs[index];
  77.         }
  78.         else{
  79.             *score += (5-count) * index;
  80.             count = 5;
  81.         }
  82.     }
  83. }
  84. void sum_of_yatzy(int dice_pairs[], int *score){
  85.     sum_of_kind((5), dice_pairs, score);
  86.     if(*score > 0)
  87.         *score = 50;
  88. }
  89. void check_bonus_round(int i, int *score){
  90.     int bonus = 0;
  91.     if(i == BONUS_ROUND){
  92.         printf("\nBonus round\nCurrent score: %d: ", *score);
  93.         if(*score >= 63)
  94.             bonus = 50;
  95.         *score += bonus;
  96.         printf("Earned %d points, total score: %d\n",bonus, *score);
  97.     }
  98. }
  99. void calc_round(int i, int dice_pairs[], int *buff_score, int *score){
  100.     *buff_score = 0;
  101.     if(i <= 6)
  102.         sum_of_all(i, dice_pairs[(i)], buff_score);
  103.     else if(i <= 8)
  104.         sum_of_pair((i-6), dice_pairs, buff_score);
  105.     else if(i <= 10)
  106.         sum_of_kind((i-6), dice_pairs, buff_score);
  107.     else if(i <= 12)
  108.         sum_of_straight((i-10), dice_pairs, buff_score);
  109.     else if(i == 13)
  110.         sum_of_house(dice_pairs, buff_score);
  111.     else if(i == 14)
  112.         sum_of_chance(dice_pairs, buff_score);
  113.     else if(i == 15)
  114.         sum_of_yatzy(dice_pairs, buff_score);
  115.     *score += *buff_score;
  116. }
  117. void scan_data(int *n){
  118.     char skip;
  119.     printf("\ninsert number of dices (less than 5 to exit): ");
  120.     while( scanf(" %d", n) != 1){
  121.         printf("\ninvalid input, try again: ");
  122.         do{
  123.             scanf("%c", &skip);
  124.         }while(skip != '\n');
  125.     }
  126. }
  127. void print_dice(int n, int dice_pairs[]){
  128.     int i, j;
  129.     printf("\nDice:");
  130.     for(i = 1; i <= 6; i++ ){
  131.         for(j = dice_pairs[i]; j > 0; j--)
  132.             printf(" %d", i);
  133.     }
  134. }
  135. void print_round_name(const int *round_name, const int i){
  136.     printf("\nRolling for %s", round_name[i]);
  137. }
  138. void print_round_points(const int points){
  139.     printf(": Earned %2d points\n", points);
  140. }
  141.  
  142. int main(void){
  143.     int n, i;
  144.     int score = 0, buff_score = 0;
  145.     int dice_pairs[7] ={0, 0, 0, 0, 0, 0, 0};
  146.     const char *round_name[] =
  147.     {"NULL", "ones", "twos", "threes", "fours", "fives", "sixes",
  148.     "one pair", "two pairs", "three of a kind", "four of a kind",
  149.     "small straight", "large straight", "full house", "chance", "yatzy"};
  150.     srand(time(NULL));
  151.  
  152.     do {
  153.         scan_data(&n);
  154.         score = 0;
  155.         for(i = 1; i <= 15 && n >= 5; i++){
  156.             print_round_name(round_name, i);
  157.             roll_multiple_dices(dice_pairs, n);
  158.             calc_round(i , dice_pairs, &buff_score, &score);
  159.             print_round_points(buff_score);
  160.             check_bonus_round(i, &score);
  161.         }
  162.         printf("\nfinal score: %d\n", score);
  163.     }while(n >= 5);
  164.     return 0;
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top