Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Khai Quang Ho, kho19@student.aau.dk, A402, SW1 */
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #define BONUS_ROUND 7
- int dice_roll(){
- return ((rand()%6)+1); /* +1 to avoid rolling 0 */
- }
- void reset_dice(int *dice_pairs){
- int i;
- for(i = 0; i <= 7; i++) /* reset dice values */
- dice_pairs[i] = 0;
- }
- void roll_multiple_dices(int *dice_pairs, int num){
- int i;
- reset_dice(dice_pairs);
- for(i = 1; i < num+1; i++) /* rolls as many times as dices given (+1 because index 0 is skipped) */
- dice_pairs[dice_roll()] += 1; /* store the amount of rolls for each type */
- }
- void sum_of_all(int n, int dice_pairs, int *score){
- if(dice_pairs > 5)
- *score = (5 * (n));
- else
- *score = (dice_pairs * n);
- }
- void sum_of_pair(int n, int dice_pairs[], int *score){
- int index, amount = n, buff_score = 0;
- for(index = 6; index > 0 && amount != 0; index--){
- if(dice_pairs[index] >= 2){
- buff_score += (index*2);
- amount--;
- if(amount == 0){
- *score += buff_score;
- }
- }
- }
- }
- void sum_of_kind(int n, int dice_pairs[], int *score){ /* optimize */
- int index;
- for(index = 6; index>0; index--){
- if(dice_pairs[index] >= n){
- *score = (index*n);
- index = 0;
- }
- }
- }
- void sum_of_straight(int start, int dice_pairs[], int *score){
- int index, end = (start+5), buffer_score = 0;
- for(index = start; index < end && dice_pairs[index] > 0; index++ )
- buffer_score += index;
- if(index == end)
- *score = buffer_score;
- }
- void sum_of_house(int dice_pairs[], int *score){
- int index, buff_score = 0;
- for(index = 6; index>0; index--){
- if(dice_pairs[index] >= 3){
- *score = (index*3);
- dice_pairs[index] = 0;
- sum_of_pair(1 ,dice_pairs, &buff_score); /* already pointer */
- if(buff_score > 0)
- *score += buff_score;
- else
- *score = 0;
- index = 0;
- }
- }
- }
- void sum_of_chance(int dice_pairs[], int *score){
- int index, count = 0;
- for(index = 6; index > 0 && count <5; index--){
- if(dice_pairs[index]+count <= 5){
- *score += (dice_pairs[index] * index);
- count += dice_pairs[index];
- }
- else{
- *score += (5-count) * index;
- count = 5;
- }
- }
- }
- void sum_of_yatzy(int dice_pairs[], int *score){
- sum_of_kind((5), dice_pairs, score);
- if(*score > 0)
- *score = 50;
- }
- void check_bonus_round(int i, int *score){
- int bonus = 0;
- if(i == BONUS_ROUND){
- if(*score >= 63)
- bonus = 50;
- *score += bonus;
- printf("\nBonus round: Earned %d points, total score: %d\n",bonus, *score);
- }
- }
- void calc_round(int i, int dice_pairs[], int *buff_score){
- if(i <= 6)
- sum_of_all(i, dice_pairs[(i)], buff_score);
- else if(i <= 8)
- sum_of_pair((i-6), dice_pairs, buff_score);
- else if(i <= 10)
- sum_of_kind((i-6), dice_pairs, buff_score);
- else if(i <= 12)
- sum_of_straight((i-10), dice_pairs, buff_score);
- else if(i == 13)
- sum_of_house(dice_pairs, buff_score);
- else if(i == 14)
- sum_of_chance(dice_pairs, buff_score);
- else if(i == 15)
- sum_of_yatzy(dice_pairs, buff_score);
- }
- void print_dice(int n, int dice_pairs[]){
- int i, j;
- printf("\nDice:");
- for(i = 1; i <= 6; i++ ){
- for(j = dice_pairs[i]; j > 0; j--)
- printf(" %d", i);
- }
- }
- void scan_data(int *n){
- char skip;
- printf("\ninsert number of dices (less than 5 to exit): ");
- while( scanf(" %d", n) != 1){
- printf("\ninvalid input, try again: ");
- do{
- scanf("%c", &skip);
- }while(skip != '\n');
- }
- }
- int main(void){
- int n, i;
- int score = 0, buff_score = 0;
- int dice_pairs[7] ={0, 0, 0, 0, 0, 0, 0};
- const char *round_name[] =
- {"NULL", "ones", "twos", "threes", "fours", "fives", "sixes",
- "one pair", "two pairs", "three of a kind", "four of a kind",
- "small straight", "large straight", "full house", "chance", "yatzy"};
- srand(time(NULL));
- do {
- scan_data(&n);
- score = 0;
- for(i = 1; i <= 15 && n >= 5; i++){
- roll_multiple_dices(dice_pairs, n);
- printf("\nRolling for %s", round_name[i]);
- print_dice(n, dice_pairs);
- calc_round(i , dice_pairs, &buff_score);
- score += buff_score;
- printf(": Earned %2d points, total score: %d\n", buff_score, score);
- buff_score = 0;
- check_bonus_round(i, &score);
- }
- }while(n >= 5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement