Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.77 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 7
  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 num){
  17.     int i;
  18.     reset_dice(dice_pairs);
  19.     for(i = 1; i < num+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. }
  22. void sum_of_all(int n, int dice_pairs, int *score){
  23.     if(dice_pairs > 5)
  24.         *score = (5 * (n));
  25.     else
  26.         *score = (dice_pairs * n);
  27. }
  28. void sum_of_pair(int n, int dice_pairs[], int *score){
  29.     int index, amount = n, buff_score = 0;
  30.     for(index = 6; index > 0 && amount != 0; index--){
  31.         if(dice_pairs[index] >= 2){
  32.             buff_score += (index*2);
  33.             amount--;
  34.             if(amount == 0){
  35.                 *score += buff_score;
  36.             }
  37.         }
  38.     }
  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){
  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.         if(*score >= 63)
  93.             bonus = 50;
  94.         *score += bonus;
  95.         printf("\nBonus round: Earned %d points, total score: %d\n",bonus, *score);
  96.     }
  97. }
  98. void calc_round(int i, int dice_pairs[], int *buff_score){
  99.     if(i <= 6)
  100.         sum_of_all(i, dice_pairs[(i)], buff_score);
  101.     else if(i <= 8)
  102.         sum_of_pair((i-6), dice_pairs, buff_score);
  103.     else if(i <= 10)
  104.         sum_of_kind((i-6), dice_pairs, buff_score);
  105.     else if(i <= 12)
  106.         sum_of_straight((i-10), dice_pairs, buff_score);
  107.     else if(i == 13)
  108.         sum_of_house(dice_pairs, buff_score);
  109.     else if(i == 14)
  110.         sum_of_chance(dice_pairs, buff_score);
  111.     else if(i == 15)
  112.         sum_of_yatzy(dice_pairs, buff_score);
  113. }
  114. void print_dice(int n, int dice_pairs[]){
  115.     int i, j;
  116.     printf("\nDice:");
  117.     for(i = 1; i <= 6; i++ ){
  118.         for(j = dice_pairs[i]; j > 0; j--)
  119.             printf(" %d", i);
  120.     }
  121. }
  122. void scan_data(int *n){
  123.     char skip;
  124.     printf("\ninsert number of dices (less than 5 to exit): ");
  125.     while( scanf(" %d", n) != 1){
  126.         printf("\ninvalid input, try again: ");
  127.         do{
  128.             scanf("%c", &skip);
  129.         }while(skip != '\n');
  130.     }
  131. }
  132.  
  133. int main(void){
  134.     int n, i;
  135.     int score = 0, buff_score = 0;
  136.     int dice_pairs[7] ={0, 0, 0, 0, 0, 0, 0};
  137.     const char *round_name[] =
  138.     {"NULL", "ones", "twos", "threes", "fours", "fives", "sixes",
  139.     "one pair", "two pairs", "three of a kind", "four of a kind",
  140.     "small straight", "large straight", "full house", "chance", "yatzy"};
  141.     srand(time(NULL));
  142.  
  143.     do {
  144.         scan_data(&n);
  145.         score = 0;
  146.         for(i = 1; i <= 15 && n >= 5; i++){
  147.  
  148.             roll_multiple_dices(dice_pairs, n);
  149.             printf("\nRolling for %s", round_name[i]);
  150.             print_dice(n, dice_pairs);
  151.  
  152.             calc_round(i , dice_pairs, &buff_score);
  153.  
  154.             score += buff_score;
  155.             printf(": Earned %2d points, total score: %d\n", buff_score, score);
  156.             buff_score = 0;
  157.  
  158.             check_bonus_round(i, &score);
  159.         }
  160.     }while(n >= 5);
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement