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 6
- #define MAX_ROUND 15
- #define MAX_DICE_EYES 6
- void flush(void);
- void scan_data(int *n);
- void reset_dice(int *dice_list);
- int dice_roll();
- void roll_multiple_dices(int dice_list[], int n);
- void print_round_name(const char **round_name, int i);
- void print_dices(int n, int dice_list[]);
- void sum_of_all(int n, int dice_list, int *score);
- void sum_of_pair(int n, int dice_list[], int *score);
- void sum_of_kind(int kind_size, int dice_list[], int *score);
- void sum_of_straight(int start, int dice_list[], int *score);
- void sum_of_house(int dice_list[], int *score);
- void sum_of_chance(int dice_list[], int *score);
- void sum_of_yatzy(int dice_list[], int *score);
- void calc_round(int i, int dice_list[], int *points, int *score);
- void print_round_points(const int points);
- void check_bonus_round(int i, int n, int *score);
- void print_score(int *score);
- /* prompt user for dice amount, then plays game of yatzy with given dices, repeat until input less than 5*/
- int main(void){
- int dices = 0, round = 1, score = 0, points = 0;
- int dice_list[MAX_DICE_EYES + 1]; /* +1 because index 0 is ignored */
- 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)); /* seed random number gen. with time since Jan 1. 1970 */
- do {
- scan_data(&dices);
- for(round = 1; round <= MAX_ROUND && dices >= 5; round++){
- roll_multiple_dices(dice_list, dices);
- print_round_name(round_name, round);
- print_dices(dices, dice_list);
- calc_round(round , dice_list, &points, &score);
- print_round_points(points);
- check_bonus_round(round, dices, &score);
- if(round == MAX_ROUND)
- print_score(&score);
- }
- }while(dices >= 5);
- return 0;
- }/* TODO less void functions? */
- /* prompts user for input, re-prompt if input is not int. Assigns user input to the var dices */
- void scan_data(int *dices){
- printf("\nInsert number of Dices:(less than 5 to exit): ");
- while(scanf(" %d", dices) != 1){ /* if scanf doesnt return 1, the input didn't contain int */
- printf("\ninvalid input, try again: ");
- flush();
- }
- flush(); /* flush one last time in case multiple valid inputs was given (input: 10 5 14) */
- }
- /* flushes all non int stored in scanf */
- void flush(void){
- char skip;
- do{
- scanf("%c", &skip);
- }while(skip != '\n');
- }
- /* rolls dices and stores them in the array dice_list */
- void roll_multiple_dices(int dice_list[], int n){
- int i;
- reset_dice(dice_list);
- for(i = 1; i < (n + 1); i++) /* rolls as many times as dices given (+1 because index 0 is skipped) */
- dice_list[dice_roll()]++; /* store the amount of rolls for each type (1-6) */
- }
- /* reset dice values */
- void reset_dice(int *dice_list){
- int i;
- for(i = 0; i <= MAX_DICE_EYES+1; i++) /* +1 because 0 index is ignored */
- dice_list[i] = 0;
- }
- /* generates random num 1-6 */
- int dice_roll(){
- return ((rand() % MAX_DICE_EYES) + 1); /* +1 to avoid rolling 0 */
- }
- /* prints the equivalent string of a given round */
- void print_round_name(const char **round_name, int round){
- printf("%-16s ", round_name[round]);
- }
- /* prints dice rolls */
- void print_dices(int n, int dice_list[]){ /* put in main? */
- int dice_face, sum;
- printf("Dices:");
- for(dice_face = 1; dice_face <= MAX_DICE_EYES; dice_face++ ){ /* loops for each dice face 1-6 */
- for(sum = dice_list[dice_face]; sum > 0; sum--) /* dice_list[i] is the number of dices with the face i (1-6) */
- printf(" %d", dice_face);
- }
- }
- /* decides function to be called for each round and formats function parameters*/
- void calc_round(int i, int dice_list[], int *points, int *score){
- *points = 0;
- if(i <= 6)
- sum_of_all(i, dice_list[(i)], points);
- else if(i <= 8)
- sum_of_pair((i-6), dice_list, points); /* i-6 because the parameter is size of pair */
- else if(i <= 10)
- sum_of_kind((i-6), dice_list, points);
- else if(i <= 12)
- sum_of_straight((i-10), dice_list, points);
- else if(i == 13)
- sum_of_house(dice_list, points);
- else if(i == 14)
- sum_of_chance(dice_list, points);
- else if(i == 15)
- sum_of_yatzy(dice_list, points);
- *score += *points;
- }
- /* calculates sum of all dices with given face (1-6), but max 5 dices */
- void sum_of_all(int n, int dice_list, int *score){
- if(dice_list > 5)
- *score = (5 * n);
- else
- *score = (dice_list * n);
- }
- /* calculates sum of pair of a give size */
- void sum_of_pair(int n, int dice_list[], int *score){
- int index, amount = n, points = 0;
- for(index = MAX_DICE_EYES; index > 0 && amount != 0; index--){
- if(dice_list[index] >= 2){
- points += (index * 2);
- amount--;
- }
- }
- if(amount == 0)
- *score += points;
- }
- /* calculates sum "n" of a kind, n varying on input */
- void sum_of_kind(int kind_size, int dice_list[], int *score){
- int index;
- for(index = MAX_DICE_EYES; index > 0; index--){
- if(dice_list[index] >= kind_size){
- *score = (index * kind_size);
- dice_list[index] = 0; /* remove the dice type to prevent it being reused */
- index = 0;
- }
- }
- }
- /* calculates sum of either small or large straight depending on input */
- void sum_of_straight(int start, int dice_list[], int *score){
- int index, end = (start + 5), buffer_score = 0;
- for(index = start; index < end && dice_list[index] > 0; index++ )
- buffer_score += index;
- if(index == end)
- *score = buffer_score;
- }
- /* calculates sum of full house */
- void sum_of_house(int dice_list[], int *score){
- int points1 = 0, points2 = 0;
- sum_of_kind(3, dice_list, &points1);/* once 3 of kind is found, dice_list element is removed to avoid overlap with function below*/
- sum_of_pair(1 ,dice_list, &points2);
- if(points1 > 0 && points2 > 0) /* if both functions found a match */
- *score += points1 + points2;
- }
- /* calculates sum of 5 largest dices */
- void sum_of_chance(int dice_list[], int *score){
- int index, count = 0;
- for(index = MAX_DICE_EYES; dice_list[index]+count <= 5; index--){
- *score += (dice_list[index] * index);
- count += dice_list[index];
- }
- *score += (5-count) * index; /* adds remainder, if count is < 5 and dice_list[index]+count > 5 */
- }
- /* calculates sum of yatzy using sum_of_kind */
- void sum_of_yatzy(int dice_list[], int *score){
- sum_of_kind(5, dice_list, score);
- if(*score > 0)
- *score = 50;
- }
- /* self containing module, check if round is bonus round, check score and calculate points, prints result*/
- void check_bonus_round(int i, int n, int *score){
- int bonus = 0;
- if(i == BONUS_ROUND){
- 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) */
- if(*score >= 63)
- bonus = 50;
- printf("| Earned %2d points\n\n",bonus);
- *score += bonus;
- }
- }
- /* prints points scored for current round, used each round */
- void print_round_points(const int points){
- printf("| Earned %2d points\n", points);
- }
- /* print score and reset it once printed, used once per game*/
- void print_score(int *score){
- printf("\nFinal score: %d\n", *score);
- *score = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement