Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Kevin Brough
- //Yahtzee Program
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <fcntl.h>
- int d0, d1, d2, d3, d4;
- int dice[5];
- int one_score, two_score, three_score, four_score, five_score, six_score, up_score, up_bonus = 0;//all scores of both sections are at first 0
- int t_kind_score, f_kind_score, f_house_score, s_straight_score, l_straight_score, yahtzee_score, chance_score = 0;
- int score;
- int one_marked, two_marked, three_marked, four_marked, five_marked, six_marked = 0;//at first, all of the upper section is free
- int t_kind_marked, f_kind_marked, f_house_marked, s_straight_marked, l_straight_marked, yahtzee_marked, chance_marked = 0;//same witht the lower
- int sections = 0; //this will count how many times we've played,
- void trim_spaces(char*), print_sections(void), which_die(int), ones(int[]), twos(int[]), threes(int[]), fours(int[]), fives(int[]), sixes(int[]), three_of_a_kind(int[]), four_of_a_kind(int[]), full_house(int[]), small_straight(int[]), large_straight(int[]), yahtzee(int[]), chance(int[]);
- int intcmp(const void *, const void *);
- void clear (void){ while ( getchar() != '\n' ); }
- int main(){
- while(sections <= 13){//while we still have categories to use up
- srand((unsigned int)time(NULL));
- d0 = rand() % 6;//rolling the die
- d1 = rand() % 6;
- d2 = rand() % 6;
- d3 = rand() % 6;
- d4 = rand() % 6;
- dice[0] = d0+1;//then put them in an array for future use
- dice[1] = d1+1;
- dice[2] = d2+1;
- dice[3] = d3+1;
- dice[4] = d4+1;
- char re_dice [10];//the array that holds all the dice to reroll if the user wants to. 9 because max to reroll is 5 with 4 spaces and the null terminator
- int selection;
- printf("Your Roll:\n%d %d %d %d %d\n", dice[0], dice[1], dice[2], dice[3], dice[4]);
- printf("Which die to reroll?\n");
- scanf("%s", re_dice);//user inputs all the die to reroll, up to all 5
- trim_spaces(re_dice);//get rid of all the spaces
- if(re_dice[0] == 0){//if they don't want to reroll after their first roll
- break;
- }
- int i;
- for(i = 0; i < 5; i++){//let's go through that array
- if(re_dice[i] == '\0') break;
- int d = atoi(&re_dice[i]);
- which_die(d);//and assign the user's values to their respective die
- }
- printf("Your Second Roll:\n%d %d %d %d %d\n", dice[0], dice[1], dice[2], dice[3], dice[4]);
- printf("Which die to reroll?\n");
- clear();
- scanf("%s", re_dice);//user inputs all the die to reroll, up to all 5
- trim_spaces(re_dice);
- if(re_dice[0] == 0){//if they don't want to reroll after their first roll
- break;
- }
- int j;
- for(j = 0; j < 5; j++){//let's go through that array
- if(re_dice[j] == '\0') break;
- int d = atoi(&re_dice[j]);
- which_die(d);//and assign the user's values to their respective die
- }
- printf("Your Third Roll:\n%d %d %d %d %d\n", dice[0], dice[1], dice[2], dice[3], dice[4]);
- printf("Place dice into:\n1)Upper Section\n2) Lower Section\n\nSelection?\n");
- clear();
- scanf("%d", &selection);
- while(selection > 2 || selection < 1){//force user to input valid choices
- printf("Please enter a valid choice\n\nPlace dice into:\n1)Upper Section\n2) Lower Section\n\nSelection?\n");
- clear();
- scanf("%d", &selection);
- }
- if(selection == 1){//code for the upper section
- int up_selection;
- printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
- clear();
- scanf("%d", &up_selection);
- while(up_selection > 6 || up_selection < 1){//force user to input valid choices
- printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
- clear();
- scanf("%d", &up_selection);
- }
- if(up_selection == 1){
- if(one_marked == 0){//first we check if it's marked
- ones(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{//if it is marked, force them to choose something else
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
- clear();
- scanf("%d", &up_selection);
- }
- }
- if(up_selection == 2){
- if(two_marked == 0){
- twos(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
- clear();
- scanf("%d", &up_selection);
- }
- }
- if(up_selection == 3){
- if(three_marked == 0){
- threes(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
- clear();
- scanf("%d", &up_selection);
- }
- }
- if(up_selection == 4){
- if(four_marked == 0){
- fours(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
- clear();
- scanf("%d", &up_selection);
- }
- }
- if(up_selection == 5){
- if(five_marked == 0){
- fives(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
- clear();
- scanf("%d", &up_selection);
- }
- }
- if(up_selection == 6){
- if(six_marked == 0){
- sixes(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
- clear();
- scanf("%d", &up_selection);
- }
- }
- if(up_score >= 63){//if the sum total of the upper section is 63 or higher, user gets a 35 point bonus
- score += 35;
- up_bonus = 35;
- }
- }
- if(selection == 2){
- int lower_selection;
- printf("Place dice into:\n1)Three of a Kind\n2)Four of a Kind\n3)Full House\n4) Small Straight\n5) Large Straight\n6) Yahtzee\n7) Chance\n\nSelection?\n");
- clear();
- scanf("%d", &lower_selection);
- while(lower_selection > 7 || lower_selection < 1){//force user to input valid choices
- printf("Place dice into:\n1)Three of a Kind\n2)Four of a Kind\n3)Full House\n4) Small Straight\n5) Large Straight\n6) Yahtzee\n7) Chance\n\nSelection?\n");
- clear();
- scanf("%d", &lower_selection);
- }
- if(lower_selection == 1){
- if(t_kind_marked == 0){
- three_of_a_kind(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1)Three of a Kind\n2)Four of a Kind\n3)Full House\n4) Small Straight\n5) Large Straight\n6) Yahtzee\n7) Chance\n\nSelection?\n");
- clear();
- scanf("%d", &lower_selection);
- }
- }
- if(lower_selection == 2){
- if(f_kind_marked == 0){
- four_of_a_kind(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1)Three of a Kind\n2)Four of a Kind\n3)Full House\n4) Small Straight\n5) Large Straight\n6) Yahtzee\n7) Chance\n\nSelection?\n");
- clear();
- scanf("%d", &lower_selection);
- }
- }
- if(lower_selection == 3){
- if(f_house_marked == 0){
- full_house(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1)Three of a Kind\n2)Four of a Kind\n3)Full House\n4) Small Straight\n5) Large Straight\n6) Yahtzee\n7) Chance\n\nSelection?\n");
- clear();
- scanf("%d", &lower_selection);
- }
- }
- if(lower_selection == 4){
- if(s_straight_marked == 0){
- small_straight(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1)Three of a Kind\n2)Four of a Kind\n3)Full House\n4) Small Straight\n5) Large Straight\n6) Yahtzee\n7) Chance\n\nSelection?\n");
- clear();
- scanf("%d", &lower_selection);
- }
- }
- if(lower_selection == 5){
- if(l_straight_marked == 0){
- large_straight(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1)Three of a Kind\n2)Four of a Kind\n3)Full House\n4) Small Straight\n5) Large Straight\n6) Yahtzee\n7) Chance\n\nSelection?\n");
- clear();
- scanf("%d", &lower_selection);
- }
- }
- if(lower_selection == 6){
- if(yahtzee_marked == 0){
- yahtzee(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1)Three of a Kind\n2)Four of a Kind\n3)Full House\n4) Small Straight\n5) Large Straight\n6) Yahtzee\n7) Chance\n\nSelection?\n");
- clear();
- scanf("%d", &lower_selection);
- }
- }
- if(lower_selection == 7){
- if(chance_marked == 0){
- chance(dice);
- printf("\nYour score so far is: %d\n\n", score);
- print_sections();
- }
- else{
- printf("\nYou've already selected that, please choose another: \n\n");
- printf("Place dice into:\n1)Three of a Kind\n2)Four of a Kind\n3)Full House\n4) Small Straight\n5) Large Straight\n6) Yahtzee\n7) Chance\n\nSelection?\n");
- clear();
- scanf("%d", &lower_selection);
- }
- }
- }
- }
- return 0;//game ends when selections is 13
- }
- void ones(int dice[]){
- int i;
- for(i = 0; i < 5; i++){//summing the ones, this will be relative to the upper section selection, all the way through 6
- if(dice[i] == 1){
- one_score+=1;
- }
- }
- up_score += one_score;//up score is the running tally to see if we got above or equal to 63 point in the upper section
- score += one_score; //for individual score keeping,
- one_marked = 1;//we mark it so we don't get to come back
- sections++;//game ends when this is 13, or all sections have been played
- }
- void twos(int dice[]){
- int i;
- for(i = 0; i < 5; i++){
- if(dice[i] == 2){
- two_score+=2;
- }
- }
- up_score += two_score;
- score += two_score;
- two_marked = 1;//we mark it so we don't get to come back
- sections++;
- }
- void threes(int dice[]){
- int i;
- for(i = 0; i < 5; i++){
- if(dice[i] == 3){
- three_score+=3;
- }
- }
- up_score += three_score;
- score += three_score;
- three_marked = 1;//we mark it so we don't get to come back
- sections++;
- }
- void fours(int dice[]){
- int i;
- for(i = 0; i < 5; i++){
- if(dice[i] == 4){
- four_score+=4;
- }
- }
- up_score += four_score;//and seven years ago hahahahah!!!!!
- score += four_score;
- four_marked = 1;//we mark it so we don't get to come back
- sections++;
- }
- void fives(int dice[]){
- int i;
- for(i = 0; i < 5; i++){
- if(dice[i] == 5){
- five_score+=5;
- }
- }
- up_score += five_score;
- score += five_score;
- one_marked = 1;//we mark it so we don't get to come back
- sections++;
- }
- void sixes(int dice[]){
- int i;
- for(i = 0; i < 5; i++){
- if(dice[i] == 6){
- six_score+=6;
- }
- }
- up_score += six_score;
- score += six_score;
- one_marked = 1;//we mark it so we don't get to come back
- sections++;
- }
- void three_of_a_kind(int dice[]){
- int i, j, k;
- int have_t_kind = 0;//at first we don't know if we have a three of a kind
- for(i = 1; i < 6; i++){
- int count = 0;
- for(j = 0; j < 5; j++){
- if(dice[j] == i) count++;//check our current number against the next number
- if(count > 2) have_t_kind = 1;
- }
- }
- if(have_t_kind){//summing if indeed we have a three of a kind
- for(k = 0; k < 6; k++){
- t_kind_score += dice[k];
- }
- }
- score += t_kind_score;
- t_kind_marked = 1;
- sections++;
- }
- void four_of_a_kind(int dice[]){
- int i, j, k;
- int have_f_kind = 0;//at first we don't know if we have a four of a kind
- for(i = 1; i < 6; i++){
- int count = 0;
- for(j = 0; j < 5; j++){
- if(dice[j] == i) count++;//check our current number against the next number
- if(count > 3) have_f_kind = 1;
- }
- }
- if(have_f_kind){//summing if indeed we have a four of a kind
- for(k = 0; k < 6; k++){
- t_kind_score += dice[k];
- }
- }
- score += f_kind_score;
- f_kind_marked = 1;
- sections++;
- }
- void full_house(int dice[]){
- qsort(dice, 5, sizeof(int), intcmp);
- if( (((dice[0] == dice[1]) && (dice[1] == dice[2])) && // Three of a Kind
- (dice[3] == dice[4]) && // Two of a Kind
- (dice[2] != dice[3])) ||
- ((dice[0] == dice[1]) && // Two of a Kind
- ((dice[2] == dice[3]) && (dice[3] == dice[4])) && // Three of a Kind
- (dice[1] != dice[2])) ){
- f_house_score = 25;
- }
- score += f_house_score;
- f_house_marked = 1;
- sections++;
- }
- void small_straight(int dice[]){//sorting first makes it easier to implement
- qsort(dice, 5, sizeof(int), intcmp);
- int i;
- for(i = 0; i < 4; i++ ){//this is the edge case that arises if you have two of the same number, we'll just put it at the end
- int temp = 0;
- if( dice[i] == dice[i+1] ){
- temp = dice[i];
- int j;
- for(j = i; j < 4; j++ ){
- dice[j] = dice[j+1];
- }
- dice[4] = temp;
- }
- }
- if( ((dice[0] == 1) && (dice[1] == 2) && (dice[2] == 3) && (dice[3] == 4)) ||//all of the cases of a small staight
- ((dice[0] == 2) && (dice[1] == 3) && (dice[2] == 4) && (dice[3] == 5)) ||
- ((dice[0] == 3) && (dice[1] == 4) && (dice[2] == 5) && (dice[3] == 6)) ||
- ((dice[1] == 1) && (dice[2] == 2) && (dice[3] == 3) && (dice[4] == 4)) ||
- ((dice[1] == 2) && (dice[2] == 3) && (dice[3] == 4) && (dice[4] == 5)) ||
- ((dice[1] == 3) && (dice[2] == 4) && (dice[3] == 5) && (dice[4] == 6)) ){
- s_straight_score = 30;
- }
- score += s_straight_score;
- s_straight_marked = 1;
- sections++;
- }
- void large_straight(int dice[]){
- qsort(dice, 5, sizeof(int), intcmp);
- if( ((dice[0] == 1) &&//the only two instances of a large straight
- (dice[1] == 2) &&
- (dice[2] == 3) &&
- (dice[3] == 4) &&
- (dice[4] == 5)) ||
- ((dice[0] == 2) &&
- (dice[1] == 3) &&
- (dice[2] == 4) &&
- (dice[3] == 5) &&
- (dice[4] == 6)) ){
- l_straight_score = 40;
- }
- score += l_straight_score;
- l_straight_marked = 1;
- sections++;
- }
- void yahtzee(int dice[]){
- int i, j, k;
- for(i = 1; i < 6; i++){
- int count = 0;
- for(j = 0; j < 5; j++){
- if(dice[j] == i) count++;//check our current number against the next number
- if(count > 4) yahtzee_score = 50;
- }
- }
- score += yahtzee_score;
- yahtzee_marked = 1;
- sections++;
- }
- void chance(int dice[]){
- int i;
- for(i = 0; i < 5; i++){
- chance_score += dice[i];
- }
- score += chance_score;
- chance_marked = 1;
- sections++;
- }
- void which_die(int d){//since the user gets to put the die they want to reroll in any order on input, go through them and find which is which
- if(d == 1){
- d0 = rand() % 6;
- dice[0] = d0+1;
- }
- if(d == 2){
- d1 = rand() % 6;
- dice[1] = d1+1;
- }
- if(d == 3){
- d2 = rand() % 6;
- dice[2] = d2+1;
- }
- if(d == 4){
- d3 = rand() % 6;
- dice[3] = d3+1;
- }
- if(d == 5){
- d4 = rand() % 6;
- dice[4] = d4+1;
- }
- }
- void trim_spaces(char *str){//function to get rid of all the spaces in the string the user entered for the die they wanted to reroll
- char *ptr1 = str, *ptr2 = str;
- do
- while (*ptr2 == ' ')
- ptr2++;
- while (*ptr1++ = *ptr2++);
- }
- int intcmp(const void *aa, const void *bb){//our comarison function for the qsort
- const int *a = aa, *b = bb;
- return (*a < *b) ? -1 : (*a > *b);
- }
- void print_sections(){
- printf("Ones: %d\t\t\tFours: %d\nTwos: %d\t\t\tFives: %d\nThrees: %d\t\tSixes: %d\nUpper Section Bonus: %d\n", one_score, four_score, two_score, five_score, three_score, six_score, up_bonus);
- printf("Three of a Kind: %d\tFour of a Kind: %d\nSmall Straight: %d\tLarge Straight: %d\n", t_kind_score, f_kind_score, s_straight_score, l_straight_score);
- printf("Full House: %d\t\tYahtzee: %d\nChance %d\n\n", f_house_score, yahtzee_score, chance_score);
- }
Advertisement
Add Comment
Please, Sign In to add comment