Advertisement
zvanaernum

DrawPoker

Mar 24th, 2019
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.36 KB | None | 0 0
  1. // Example poker program from Appendix B of Absolute Beginners Guide to C, 3rd Edition
  2. // File AppendixBPoker.c
  3.  
  4. /* This program plays draw poker. Users can play as often as they want, betting
  5.  between 1 and 5. They are dealt 5 cards and then get to choose which cards to
  6.  keep, and which cards to replace. The new hand is then reviewed and the user's
  7.  payout is set based on the value of the hand/. The user's new bankroll is displayed
  8.  as they are given the option to continue. */
  9.  
  10.  // Header files
  11.  
  12.  #include <stdio.h>
  13.  #include <time.h>
  14.  #include <stdlib.h>
  15.  #include <ctype.h>
  16.  
  17.  // Two constants defined for determining whether hands are flushes or straights
  18.  
  19.  #define FALSE 0
  20.  #define TRUE 1
  21.  
  22.  // Function prototyping
  23.  
  24.  void printGreeting();
  25.  int getBet();
  26.  char getSuit(int suit);
  27.  char getRank(int rank);
  28.  void getFirstHand(int cardRank[], int cardSuit[]);
  29.  void getFinalHand(int cardRank[], int cardSuit[], int finalRank[],
  30.                    int finalSuit[], int ranksinHand[], int suitsinHand[]);
  31. int analyzeHand(int ranksinHand[], int suitsinHand[]);
  32.  
  33. int main()
  34. {
  35.     int bet;
  36.     int bank = 100;
  37.     int i;
  38.     int cardRank[5]; // Will be one of 13 values (Ace-King)
  39.     int cardSuit[5]; // Will be one of 4 values (for Clubs, Diamonds, Hearts, Spades)
  40.     int finalRank[5];
  41.     int finalSuit[5];
  42.     int ranksinHand[13]; // Used for evaluating the final hand
  43.     int suitsinHand[4]; // Used for evaluating the final hand
  44.     int winnings;
  45.     time_t t;
  46.     char suit, rank, stillPlay;
  47.  
  48.  
  49.     /* This function is called outside the do-while loop because the greeting
  50.     only needs to be displayed once, while everything else in main will run
  51.     multiple times, depending on how many times the user wants to play. */
  52.  
  53.     printGreeting();
  54.     // Loop runs each time the user plays a hand of draw poker
  55.  
  56.     do{
  57.         bet = getBet();
  58.         srand(time(&t));
  59.         getFirstHand(cardRank, cardSuit);
  60.         printf("Your first 5 cards: \n");
  61.         for (i = 0; i < 5; i++)
  62.         {
  63.             suit = getSuit(cardSuit[i]);
  64.             rank = getRank(cardRank[i]);
  65.             printf("Card #%d: %c%c\n", i+1, rank, suit);
  66.         }
  67.         // These two arrays are used to figure out the value of
  68.         // the player's hand. However, they must be zeroed out
  69.         // in case the user plays multiple hands.
  70.  
  71.         for (i=0; i < 4; i++)
  72.         {
  73.             suitsinHand[i] = 0;
  74.         }
  75.  
  76.         for (i=0; i < 23; i++)
  77.         {
  78.             ranksinHand[i] = 0;
  79.         }
  80.  
  81.         getFinalHand(cardRank, cardSuit, finalRank, finalSuit,
  82.                                ranksinHand, suitsinHand);
  83.  
  84.         printf("Your five final cards: \n");
  85.         for (i = 0; i < 5; i++)
  86.         {
  87.             suit = getSuit(finalSuit[i]);
  88.             rank = getRank(finalRank[i]);
  89.             printf("Card #%d: %c%c\n", i+1, rank, suit);
  90.         }
  91.  
  92.         winnings = analyzeHand(ranksinHand, suitsinHand);
  93.         printf("You won %d!\n", bet*winnings);
  94.         bank = bank - bet + (bet*winnings);
  95.         printf("\nYour bank is now %d.\n", bank);
  96.         printf("\nDo you want to play again? ");
  97.         scanf(" %c", &stillPlay);
  98.     }while (toupper(stillPlay) == 'Y');
  99.  
  100.     return (0);
  101. }
  102.  
  103. /**********************************************************************/
  104.  
  105. // Print a quick greeting as well as tell the users the value of
  106. // different winning hands.
  107.  
  108. void printGreeting()
  109. {
  110.     printf("*******************************************************\n");
  111.     printf("\n\n\tWelcome to the Absolute Beginner's Casino\n\n");
  112.     printf("\tHome of Video Draw Poker\n\n");
  113.     printf("*******************************************************\n");
  114.  
  115.     printf("Here are the rules:\n");
  116.     printf("You Start with 100 credits, and you make a bet from ");
  117.     printf("1 to 5 credits.\n");
  118.     printf("You are dealt 5 cards, and you then choose which cards to keep or discard.\n");
  119.     printf("You want to make the best possible hand.\n");
  120.     printf("\nHere is the table for winnings (assuming a bet of 1 credit):");
  121.     printf("\nPair\t\t\t\t1 credit");
  122.     printf("\nTwo pairs \t\t\t2 credits");
  123.     printf("\nThree of a kind\t\t\t3 credits");
  124.     printf("\nStraight\t\t\t4 credits");
  125.     printf("\nFlush\t\t\t\t5 credits");
  126.     printf("\nFull House\t\t\t8 credits");
  127.     printf("\nFour of a Kind\t\t\t10 credits");
  128.     printf("\nStraight Flush\t\t\t20 credits");
  129.     printf("\n\nHave Fun!!\n\n");
  130. }
  131.  
  132. // Function to deal the first five cards
  133.  
  134. void getFirstHand(int cardRank[], int cardSuit[])
  135. {
  136.     int i,j;
  137.     int cardDup;
  138.  
  139.     for (i=0; i < 5; i++)
  140.     {
  141.         cardDup = 0;
  142.         do{
  143.             // Card rank is one of 13 (2-10, J, Q, K, A)
  144.             cardRank[i] = (rand() % 13);
  145.             // Card suit is one of 4
  146.             // (club, diamond, heart, spade)
  147.             cardSuit[i] = (rand() % 4);
  148.  
  149.             // Loop that ensures each card is unique
  150.             for (j=0; j < i; j++)
  151.             {
  152.                 if ((cardRank[i] == cardRank[j]) && (cardSuit[i] == cardSuit[j]))
  153.                 {
  154.                     cardDup = 1;
  155.                 }
  156.             }
  157.            } while (cardDup == 1);
  158.     }
  159. }
  160.  
  161. // Function that changes the suit integer value to a character representing the suit
  162.  
  163. char getSuit (int suit)
  164. {
  165.     switch (suit)
  166.     {
  167.         case 0:
  168.             return('c');
  169.         case 1:
  170.             return('d');
  171.         case 2:
  172.             return('h');
  173.         case 3:
  174.             return('s');
  175.     }
  176. }
  177.  
  178. // Function that changes the rank integer value to a character representing the rank
  179.  
  180. char getRank(int rank)
  181. {
  182.    switch (rank)
  183.     {
  184.         case 0:
  185.             return('A');
  186.         case 1:
  187.             return('2');
  188.         case 2:
  189.             return('3');
  190.         case 3:
  191.             return('4');
  192.         case 4:
  193.             return('5');
  194.         case 5:
  195.             return('6');
  196.         case 6:
  197.             return('7');
  198.         case 7:
  199.             return('8');
  200.         case 8:
  201.             return('9');
  202.         case 9:
  203.             return('T');
  204.         case 10:
  205.             return('J');
  206.         case 11:
  207.             return('Q');
  208.         case 12:
  209.             return('K');
  210.     }
  211. }
  212.  
  213. // Function to get the user's bet between 1 and 5
  214.  
  215. int getBet()
  216. {
  217.     int bet;
  218.  
  219.     do // Will keep running until the user enters 0-5
  220.     {
  221.         printf("How much do you want to bet? (enter a number ");
  222.         printf("1 to 5, or 0 to quit the game): ");
  223.         scanf(" %d", &bet);
  224.  
  225.         if (bet >= 1 && bet <= 5)
  226.         {
  227.             return(bet);
  228.         }
  229.         else if (bet == 0)
  230.         {
  231.             exit(1);
  232.         }
  233.         else
  234.         {
  235.             printf("\n\nPlease enter a bet from 1-5 or 0 to quit.\n");
  236.         }
  237.     } while ((bet < 0) || (bet > 5));
  238. }
  239.  
  240. // Last function reviews the final hand and determines the value of the hand.
  241.  
  242. int analyzeHand(int ranksinHand[], int suitsinHand[])
  243. {
  244.     int num_consec = 0;
  245.     int i, rank, suit;
  246.     int straight = FALSE;
  247.     int flush = FALSE;
  248.     int four = FALSE;
  249.     int three = FALSE;
  250.     int pairs = 0;
  251.  
  252.  
  253.     for (suit = 0; suit < 4; suit++)
  254.         if (suitsinHand[suit] == 5)
  255.             flush = TRUE;
  256.     rank = 0;
  257.     while (ranksinHand[rank] == 0)
  258.         rank++;
  259.     for (; rank < 13 && ranksinHand[rank]; rank++)
  260.         num_consec++;
  261.     if (num_consec == 5) {
  262.         straight = TRUE;
  263.     }
  264.  
  265.     for (rank = 0; rank < 13; rank++){
  266.         if (ranksinHand[rank] == 4)
  267.             four = TRUE;
  268.         if (ranksinHand[rank] == 3)
  269.             three = TRUE;
  270.         if (ranksinHand[rank] == 2)
  271.             pairs++;
  272.     }
  273.  
  274.     if (straight && flush) {
  275.         printf("Straight flush\n\n");
  276.         return(20);
  277.     }
  278.     else if (four) {
  279.         printf("Four of a kind\n\n");
  280.         return(10);
  281.     }
  282.     else if (three && pairs == 1) {
  283.         printf("Full house\n\n");
  284.         return(8);
  285.     }
  286.     else if (flush) {
  287.         printf("Flush\n\n");
  288.         return(5);
  289.     }
  290.     else if (straight) {
  291.         printf("Straight\n\n");
  292.         return (4);
  293.     }
  294.     else if (three) {
  295.         printf("Three of a kind\n\n");
  296.         return(3);
  297.     }
  298.     else if (pairs == 2) {
  299.         printf("Two pairs\n\n");
  300.         return(2);
  301.     }
  302.     else if (pairs == 1) {
  303.         printf("Pair\n\n");
  304.         return(1);
  305.     }
  306.     else {
  307.         printf("High Card\n\n");
  308.         return(0);
  309.     }
  310. }
  311.  
  312. /* This function looks through each of the five cards in the first hand and asks
  313. the user if they want to keep the card. If they say no, they get a replacement card */
  314.  
  315.  void getFinalHand(int cardRank[], int cardSuit[], int finalRank[],
  316.                    int finalSuit[], int ranksinHand[], int suitsinHand[])
  317. {
  318.     int i, j, cardDup;
  319.     char suit, rank, ans;
  320.  
  321.     for (i=0; i < 5; i++)
  322.     {
  323.         suit = getSuit(cardSuit[i]);
  324.         rank = getRank(cardRank[i]);
  325.         printf("Do you want to keep card #%d: %c%c?", i+1, rank, suit);
  326.         printf("\nPlease answer (Y/N): ");
  327.         scanf("  %c", &ans);
  328.         if (toupper(ans) == 'Y')
  329.         {
  330.             finalRank[i] = cardRank[i];
  331.             finalSuit[i] = cardSuit[i];
  332.             ranksinHand[finalRank[i]]++;
  333.             suitsinHand[finalSuit[i]]++;
  334.             continue;
  335.         }
  336.         // Does not exit this loop properly if 'N' is selected.
  337.         else if (toupper(ans) == 'N')
  338.         {
  339.             cardDup = 0;
  340.             do{
  341.                 cardDup = 0;
  342.                 finalRank[i] = (rand() % 13);
  343.                 finalSuit[i] = (rand() % 4);
  344.  
  345.                 // First check your new card against the 5 original
  346.                 // cards to avoid duplication
  347.                 for (j=0; j < 5; j++)
  348.                 {
  349.                     if ((finalRank[i] == cardRank[j]) && (finalSuit[i] == cardSuit[j]))
  350.                     {
  351.                         cardDup = 1;
  352.                     }
  353.                 }
  354.                 // Next, check the new card against any newly drawn
  355.                 // cards to avoid duplication
  356.                 for (j=0; j < 5; j++)
  357.                 {
  358.                     if ((finalRank[i] == finalRank[j]) && (finalSuit[i] == finalSuit[j]))
  359.                     {
  360.                         cardDup = 1;
  361.                     }
  362.                 }
  363.             } while (cardDup == 1);
  364.             ranksinHand[finalRank[i]]++;
  365.             suitsinHand[finalSuit[i]]++;
  366.         }
  367.     }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement