kmb180

Yahtzee Program

Feb 12th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.68 KB | None | 0 0
  1. //Kevin Brough
  2. //Yahtzee Program
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9.  
  10. int d0, d1, d2, d3, d4;
  11.  
  12. int dice[5];
  13. 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
  14. int t_kind_score, f_kind_score, f_house_score, s_straight_score, l_straight_score, yahtzee_score, chance_score = 0;
  15. int score;
  16. int one_marked, two_marked, three_marked, four_marked, five_marked, six_marked = 0;//at first, all of the upper section is free
  17. 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
  18. int sections = 0; //this will count how many times we've played,
  19. 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[]);
  20. int intcmp(const void *, const void *);
  21. void clear (void){ while ( getchar() != '\n' ); }
  22. int main(){
  23.     while(sections <= 13){//while we still have categories to use up
  24.        
  25.        
  26.         srand((unsigned int)time(NULL));
  27.         d0 = rand() % 6;//rolling the die
  28.         d1 = rand() % 6;
  29.         d2 = rand() % 6;
  30.         d3 = rand() % 6;
  31.         d4 = rand() % 6;
  32.         dice[0] = d0+1;//then put them in an array for future use
  33.         dice[1] = d1+1;
  34.         dice[2] = d2+1;
  35.         dice[3] = d3+1;
  36.         dice[4] = d4+1;
  37.         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
  38.        
  39.         int selection;
  40.         printf("Your Roll:\n%d %d %d %d %d\n", dice[0], dice[1], dice[2], dice[3], dice[4]);
  41.         printf("Which die to reroll?\n");
  42.         scanf("%s", re_dice);//user inputs all the die to reroll, up to all 5
  43.         trim_spaces(re_dice);//get rid of all the spaces
  44.         if(re_dice[0] == 0){//if they don't want to reroll after their first roll
  45.             break;
  46.         }
  47.         int i;
  48.         for(i = 0; i < 5; i++){//let's go through that array
  49.             if(re_dice[i] == '\0') break;
  50.             int d = atoi(&re_dice[i]);
  51.             which_die(d);//and assign the user's values to their respective die
  52.         }  
  53.         printf("Your Second Roll:\n%d %d %d %d %d\n", dice[0], dice[1], dice[2], dice[3], dice[4]);
  54.        
  55.         printf("Which die to reroll?\n");
  56.         clear();
  57.         scanf("%s", re_dice);//user inputs all the die to reroll, up to all 5
  58.         trim_spaces(re_dice);
  59.         if(re_dice[0] == 0){//if they don't want to reroll after their first roll
  60.             break;
  61.         }
  62.         int j;
  63.         for(j = 0; j < 5; j++){//let's go through that array
  64.             if(re_dice[j] == '\0') break;
  65.             int d = atoi(&re_dice[j]);
  66.             which_die(d);//and assign the user's values to their respective die
  67.         }  
  68.         printf("Your Third Roll:\n%d %d %d %d %d\n", dice[0], dice[1], dice[2], dice[3], dice[4]);
  69.        
  70.         printf("Place dice into:\n1)Upper Section\n2) Lower Section\n\nSelection?\n");
  71.         clear();
  72.         scanf("%d", &selection);
  73.         while(selection > 2 || selection < 1){//force user to input valid choices
  74.             printf("Please enter a valid choice\n\nPlace dice into:\n1)Upper Section\n2) Lower Section\n\nSelection?\n");
  75.             clear();
  76.             scanf("%d", &selection);
  77.         }
  78.         if(selection == 1){//code for the upper section
  79.             int up_selection;
  80.             printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
  81.             clear();
  82.             scanf("%d", &up_selection);
  83.             while(up_selection > 6 || up_selection < 1){//force user to input valid choices
  84.                 printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
  85.                 clear();
  86.                 scanf("%d", &up_selection);
  87.             }
  88.             if(up_selection == 1){
  89.                 if(one_marked == 0){//first we check if it's marked
  90.                     ones(dice);
  91.                     printf("\nYour score so far is: %d\n\n", score);
  92.                     print_sections();
  93.                 }
  94.                 else{//if it is marked, force them to choose something else
  95.                     printf("\nYou've already selected that, please choose another: \n\n");
  96.                     printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
  97.                     clear();
  98.                     scanf("%d", &up_selection);
  99.                 }  
  100.             }
  101.             if(up_selection == 2){
  102.                 if(two_marked == 0){   
  103.                     twos(dice);
  104.                     printf("\nYour score so far is: %d\n\n", score);
  105.                     print_sections();
  106.                 }              
  107.                 else{
  108.                     printf("\nYou've already selected that, please choose another: \n\n");
  109.                     printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
  110.                     clear();
  111.                     scanf("%d", &up_selection);
  112.                 }
  113.             }
  114.             if(up_selection == 3){
  115.                 if(three_marked == 0){
  116.                     threes(dice);
  117.                     printf("\nYour score so far is: %d\n\n", score);
  118.                     print_sections();
  119.                 }  
  120.                 else{
  121.                     printf("\nYou've already selected that, please choose another: \n\n");
  122.                     printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
  123.                     clear();
  124.                     scanf("%d", &up_selection);
  125.                 }
  126.             }
  127.             if(up_selection == 4){
  128.                 if(four_marked == 0){
  129.                     fours(dice);
  130.                     printf("\nYour score so far is: %d\n\n", score);
  131.                     print_sections();
  132.                 }  
  133.                 else{
  134.                     printf("\nYou've already selected that, please choose another: \n\n");
  135.                     printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
  136.                     clear();
  137.                     scanf("%d", &up_selection);
  138.                 }
  139.             }
  140.             if(up_selection == 5){
  141.                 if(five_marked == 0){
  142.                     fives(dice);
  143.                     printf("\nYour score so far is: %d\n\n", score);
  144.                     print_sections();
  145.                 }  
  146.                 else{
  147.                     printf("\nYou've already selected that, please choose another: \n\n");
  148.                     printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
  149.                     clear();
  150.                     scanf("%d", &up_selection);
  151.                 }
  152.             }
  153.             if(up_selection == 6){
  154.                 if(six_marked == 0){
  155.                     sixes(dice);
  156.                     printf("\nYour score so far is: %d\n\n", score);
  157.                     print_sections();
  158.                 }  
  159.                 else{
  160.                     printf("\nYou've already selected that, please choose another: \n\n");
  161.                     printf("Place dice into:\n1) Ones\n2) Twos\n3) Threes\n4) Fours\n5) Fives\n6) Sixes\n\nSelection?\n");
  162.                     clear();
  163.                     scanf("%d", &up_selection);
  164.                 }
  165.             }
  166.             if(up_score >= 63){//if the sum total of the upper section is 63 or higher, user gets a 35 point bonus
  167.                 score += 35;
  168.                 up_bonus = 35;
  169.             }
  170.         }
  171.         if(selection == 2){
  172.             int lower_selection;
  173.             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");
  174.             clear();
  175.             scanf("%d", &lower_selection);
  176.             while(lower_selection > 7 || lower_selection < 1){//force user to input valid choices
  177.                 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");
  178.                 clear();
  179.                 scanf("%d", &lower_selection);
  180.             }
  181.             if(lower_selection == 1){
  182.                 if(t_kind_marked == 0){
  183.                     three_of_a_kind(dice);
  184.                     printf("\nYour score so far is: %d\n\n", score);
  185.                     print_sections();
  186.                 }  
  187.                 else{
  188.                     printf("\nYou've already selected that, please choose another: \n\n");
  189.                     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");
  190.                     clear();
  191.                     scanf("%d", &lower_selection);
  192.                 }
  193.             }
  194.             if(lower_selection == 2){
  195.                 if(f_kind_marked == 0){
  196.                     four_of_a_kind(dice);
  197.                     printf("\nYour score so far is: %d\n\n", score);
  198.                     print_sections();
  199.                 }  
  200.                 else{
  201.                     printf("\nYou've already selected that, please choose another: \n\n");
  202.                     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");
  203.                     clear();
  204.                     scanf("%d", &lower_selection);
  205.                 }
  206.             }
  207.             if(lower_selection == 3){
  208.                 if(f_house_marked == 0){
  209.                     full_house(dice);
  210.                     printf("\nYour score so far is: %d\n\n", score);
  211.                     print_sections();
  212.                 }  
  213.                 else{
  214.                     printf("\nYou've already selected that, please choose another: \n\n");
  215.                     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");
  216.                     clear();
  217.                     scanf("%d", &lower_selection);
  218.                 }
  219.             }
  220.             if(lower_selection == 4){
  221.                 if(s_straight_marked == 0){
  222.                     small_straight(dice);
  223.                     printf("\nYour score so far is: %d\n\n", score);
  224.                     print_sections();
  225.                 }  
  226.                 else{
  227.                     printf("\nYou've already selected that, please choose another: \n\n");
  228.                     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");
  229.                     clear();
  230.                     scanf("%d", &lower_selection);
  231.                 }
  232.             }
  233.             if(lower_selection == 5){
  234.                 if(l_straight_marked == 0){
  235.                     large_straight(dice);
  236.                     printf("\nYour score so far is: %d\n\n", score);
  237.                     print_sections();
  238.                 }  
  239.                 else{
  240.                     printf("\nYou've already selected that, please choose another: \n\n");
  241.                     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");
  242.                     clear();
  243.                     scanf("%d", &lower_selection);
  244.                 }
  245.             }
  246.             if(lower_selection == 6){
  247.                 if(yahtzee_marked == 0){
  248.                     yahtzee(dice);
  249.                     printf("\nYour score so far is: %d\n\n", score);
  250.                     print_sections();
  251.                 }  
  252.                 else{
  253.                     printf("\nYou've already selected that, please choose another: \n\n");
  254.                     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");
  255.                     clear();
  256.                     scanf("%d", &lower_selection);
  257.                 }
  258.             }
  259.             if(lower_selection == 7){
  260.                 if(chance_marked == 0){
  261.                     chance(dice);
  262.                     printf("\nYour score so far is: %d\n\n", score);
  263.                     print_sections();
  264.                 }  
  265.                 else{
  266.                     printf("\nYou've already selected that, please choose another: \n\n");
  267.                     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");
  268.                     clear();
  269.                     scanf("%d", &lower_selection);
  270.                 }
  271.             }
  272.         }
  273.     }  
  274.     return 0;//game ends when selections is 13
  275. }
  276. void ones(int dice[]){
  277.     int i;
  278.     for(i = 0; i < 5; i++){//summing the ones, this will be relative to the upper section selection, all the way through 6
  279.         if(dice[i] == 1){
  280.             one_score+=1;
  281.         }
  282.     }
  283.     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
  284.     score += one_score; //for individual score keeping,
  285.     one_marked = 1;//we mark it so we don't get to come back
  286.     sections++;//game ends when this is 13, or all sections have been played
  287. }
  288. void twos(int dice[]){
  289.     int i;
  290.     for(i = 0; i < 5; i++){
  291.         if(dice[i] == 2){
  292.             two_score+=2;
  293.         }
  294.     }
  295.     up_score += two_score;
  296.     score += two_score;
  297.     two_marked = 1;//we mark it so we don't get to come back
  298.     sections++;
  299. }
  300. void threes(int dice[]){
  301.     int i;
  302.     for(i = 0; i < 5; i++){
  303.         if(dice[i] == 3){
  304.             three_score+=3;
  305.         }
  306.     }
  307.     up_score += three_score;
  308.     score += three_score;
  309.     three_marked = 1;//we mark it so we don't get to come back
  310.     sections++;
  311. }
  312. void fours(int dice[]){
  313.     int i;
  314.     for(i = 0; i < 5; i++){
  315.         if(dice[i] == 4){
  316.             four_score+=4;
  317.         }
  318.     }
  319.     up_score += four_score;//and seven years ago hahahahah!!!!!
  320.     score += four_score;
  321.     four_marked = 1;//we mark it so we don't get to come back
  322.     sections++;
  323. }
  324. void fives(int dice[]){
  325.     int i;
  326.     for(i = 0; i < 5; i++){
  327.         if(dice[i] == 5){
  328.             five_score+=5;
  329.         }
  330.     }
  331.     up_score += five_score;
  332.     score += five_score;
  333.     one_marked = 1;//we mark it so we don't get to come back
  334.     sections++;
  335. }
  336. void sixes(int dice[]){
  337.     int i;
  338.     for(i = 0; i < 5; i++){
  339.         if(dice[i] == 6){
  340.             six_score+=6;
  341.         }
  342.     }
  343.     up_score += six_score;
  344.     score += six_score;
  345.     one_marked = 1;//we mark it so we don't get to come back
  346.     sections++;
  347. }
  348. void three_of_a_kind(int dice[]){
  349.     int i, j, k;
  350.     int have_t_kind = 0;//at first we don't know if we have a three of a kind
  351.     for(i = 1; i < 6; i++){
  352.         int count = 0;
  353.         for(j = 0; j < 5; j++){
  354.             if(dice[j] == i) count++;//check our current number against the next number
  355.             if(count > 2) have_t_kind = 1;
  356.         }
  357.     }
  358.     if(have_t_kind){//summing if indeed we have a three of a kind
  359.         for(k = 0; k < 6; k++){
  360.             t_kind_score += dice[k];
  361.         }
  362.     }
  363.     score += t_kind_score;
  364.     t_kind_marked = 1;
  365.     sections++;
  366. }
  367. void four_of_a_kind(int dice[]){
  368.     int i, j, k;
  369.     int have_f_kind = 0;//at first we don't know if we have a four of a kind
  370.     for(i = 1; i < 6; i++){
  371.         int count = 0;
  372.         for(j = 0; j < 5; j++){
  373.             if(dice[j] == i) count++;//check our current number against the next number
  374.             if(count > 3) have_f_kind = 1;
  375.         }
  376.     }
  377.     if(have_f_kind){//summing if indeed we have a four of a kind
  378.         for(k = 0; k < 6; k++){
  379.             t_kind_score += dice[k];
  380.         }
  381.     }
  382.     score += f_kind_score;
  383.     f_kind_marked = 1;
  384.     sections++;
  385. }
  386. void full_house(int dice[]){
  387.     qsort(dice, 5, sizeof(int), intcmp);
  388.     if( (((dice[0] == dice[1]) && (dice[1] == dice[2])) && // Three of a Kind
  389.         (dice[3] == dice[4]) && // Two of a Kind
  390.         (dice[2] != dice[3])) ||
  391.         ((dice[0] == dice[1]) && // Two of a Kind
  392.         ((dice[2] == dice[3]) && (dice[3] == dice[4])) && // Three of a Kind
  393.         (dice[1] != dice[2])) ){
  394.             f_house_score = 25;
  395.     }
  396.     score += f_house_score;
  397.     f_house_marked = 1;
  398.     sections++;
  399. }
  400. void small_straight(int dice[]){//sorting first makes it easier to implement
  401.     qsort(dice, 5, sizeof(int), intcmp);
  402.     int i;
  403.     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
  404.         int temp = 0;
  405.         if( dice[i] == dice[i+1] ){
  406.             temp = dice[i];
  407.             int j;
  408.             for(j = i; j < 4; j++ ){
  409.                 dice[j] = dice[j+1];
  410.             }
  411.             dice[4] = temp;
  412.         }
  413.     }
  414.     if( ((dice[0] == 1) && (dice[1] == 2) && (dice[2] == 3) && (dice[3] == 4)) ||//all of the cases of a small staight
  415.       ((dice[0] == 2) && (dice[1] == 3) && (dice[2] == 4) && (dice[3] == 5)) ||
  416.       ((dice[0] == 3) && (dice[1] == 4) && (dice[2] == 5) && (dice[3] == 6)) ||
  417.       ((dice[1] == 1) && (dice[2] == 2) && (dice[3] == 3) && (dice[4] == 4)) ||
  418.       ((dice[1] == 2) && (dice[2] == 3) && (dice[3] == 4) && (dice[4] == 5)) ||
  419.       ((dice[1] == 3) && (dice[2] == 4) && (dice[3] == 5) && (dice[4] == 6)) ){
  420.         s_straight_score = 30;
  421.     }
  422.     score += s_straight_score;
  423.     s_straight_marked = 1;
  424.     sections++;
  425. }
  426. void large_straight(int dice[]){
  427.     qsort(dice, 5, sizeof(int), intcmp);
  428.     if( ((dice[0] == 1) &&//the only two instances of a large straight
  429.        (dice[1] == 2) &&
  430.        (dice[2] == 3) &&
  431.        (dice[3] == 4) &&
  432.        (dice[4] == 5)) ||
  433.       ((dice[0] == 2) &&
  434.        (dice[1] == 3) &&
  435.        (dice[2] == 4) &&
  436.        (dice[3] == 5) &&
  437.        (dice[4] == 6)) ){
  438.             l_straight_score = 40;
  439.     }
  440.     score += l_straight_score;
  441.     l_straight_marked = 1;
  442.     sections++;
  443. }
  444. void yahtzee(int dice[]){
  445.     int i, j, k;
  446.     for(i = 1; i < 6; i++){
  447.         int count = 0;
  448.         for(j = 0; j < 5; j++){
  449.             if(dice[j] == i) count++;//check our current number against the next number
  450.             if(count > 4) yahtzee_score = 50;
  451.         }
  452.     }
  453.     score += yahtzee_score;
  454.     yahtzee_marked = 1;
  455.     sections++;
  456. }
  457. void chance(int dice[]){
  458.     int i;
  459.     for(i = 0; i < 5; i++){
  460.         chance_score += dice[i];
  461.     }  
  462.     score += chance_score;
  463.     chance_marked = 1;
  464.     sections++;
  465. }
  466. 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
  467.     if(d == 1){
  468.         d0 = rand() % 6;
  469.         dice[0] = d0+1;
  470.     }
  471.     if(d == 2){
  472.         d1 = rand() % 6;
  473.         dice[1] = d1+1;
  474.     }
  475.     if(d == 3){
  476.         d2 = rand() % 6;
  477.         dice[2] = d2+1;
  478.     }
  479.     if(d == 4){
  480.         d3 = rand() % 6;
  481.         dice[3] = d3+1;
  482.     }
  483.     if(d == 5){
  484.         d4 = rand() % 6;
  485.         dice[4] = d4+1;
  486.     }
  487. }
  488. 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
  489.   char *ptr1 = str, *ptr2 = str;
  490.   do
  491.     while (*ptr2 == ' ')
  492.       ptr2++;
  493.   while (*ptr1++ = *ptr2++);
  494. }
  495. int intcmp(const void *aa, const void *bb){//our comarison function for the qsort
  496.     const int *a = aa, *b = bb;
  497.     return (*a < *b) ? -1 : (*a > *b);
  498. }
  499. void print_sections(){
  500.     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);
  501.     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);
  502.     printf("Full House: %d\t\tYahtzee: %d\nChance %d\n\n", f_house_score, yahtzee_score, chance_score);
  503. }
Advertisement
Add Comment
Please, Sign In to add comment