Pella86

Untitled

Nov 4th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.13 KB | None | 0 0
  1. /* Classifies a poker hand */
  2.  
  3. #include <stdbool.h>  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define NUM_RANKS 13
  8. #define NUM_SUITS 4
  9. #define NUM_CARDS 5
  10.  
  11. /* external variables */
  12. int num_in_rank[NUM_RANKS];
  13. int num_in_suit[NUM_SUITS];
  14. bool straight, flush, four, three;
  15. int pairs;   /* can be 0, 1, or 2 */
  16.  
  17. /* prototypes */
  18.  
  19. /**********************************************************
  20.  * main                             *
  21.  **********************************************************/
  22. int main(void)
  23. {
  24.  
  25.  
  26.  
  27. /**********************************************************
  28.  * read_cards: Reads the cards into the external          *
  29.  *             variables num_in_rank and num_in_suit;     *
  30.  *             checks for bad cards and duplicate cards.  *
  31.  **********************************************************/
  32.  
  33.   bool card_exists[NUM_RANKS][NUM_SUITS];
  34.   char ch, rank_ch, suit_ch;
  35.   int rank, suit;
  36.   bool bad_card;
  37.   int cards_read = 0;
  38.  
  39.   for (rank = 0; rank < NUM_RANKS; rank++) {
  40.     num_in_rank[rank] = 0;
  41.     for (suit = 0; suit < NUM_SUITS; suit++)
  42.       card_exists[rank][suit] = false;
  43.   }
  44.  
  45.   for (suit = 0; suit < NUM_SUITS; suit++)
  46.     num_in_suit[suit] = 0;
  47.  
  48.   while (cards_read < NUM_CARDS) {
  49.     bad_card = false;
  50.  
  51.     printf("Enter a card: ");
  52.  
  53.     rank_ch = getchar();
  54.     switch (rank_ch) {
  55.       case '0':           exit(EXIT_SUCCESS);
  56.       case '2':           rank = 0; break;
  57.       case '3':           rank = 1; break;
  58.       case '4':           rank = 2; break;
  59.       case '5':           rank = 3; break;
  60.       case '6':           rank = 4; break;
  61.       case '7':           rank = 5; break;
  62.       case '8':           rank = 6; break;
  63.       case '9':           rank = 7; break;
  64.       case 't': case 'T': rank = 8; break;
  65.       case 'j': case 'J': rank = 9; break;
  66.       case 'q': case 'Q': rank = 10; break;
  67.       case 'k': case 'K': rank = 11; break;
  68.       case 'a': case 'A': rank = 12; break;
  69.       default:            bad_card = true;
  70.     }
  71.  
  72.     /*suit_ch = getchar();
  73.     switch (suit_ch) {
  74.       case 'c': case 'C': suit = 0; break;
  75.       case 'd': case 'D': suit = 1; break;
  76.       case 'h': case 'H': suit = 2; break;
  77.       case 's': case 'S': suit = 3; break;
  78.       default:            bad_card = true;
  79.     }*/
  80.  
  81.     while ((ch = getchar()) != '\n')
  82.       if (ch != ' ') bad_card = true;
  83.  
  84.     if (bad_card)
  85.       printf("Bad card; ignored.\n");
  86.     /*else if (card_exists[rank][suit])
  87.       printf("Duplicate card; ignored.\n");*/
  88.     else {
  89.       num_in_rank[rank]++;
  90.       num_in_suit[suit]++;
  91.       card_exists[rank][suit] = true;
  92.       cards_read++;
  93.     }
  94.   }
  95.    
  96. /**********************************************************
  97.  
  98.  
  99.  
  100. /**********************************************************
  101.  
  102. /**********************************************************
  103.  * analyze_hand: Determines whether the hand contains a   *
  104.  *               straight, a flush, four-of-a-kind,       *
  105.  *               and/or three-of-a-kind; determines the   *
  106.  *               number of pairs; stores the results into *
  107.  *               the external variables straight, flush,  *
  108.  *               four, three, and pairs.                  *
  109.  **********************************************************/
  110. {
  111.   int num_consec = 0;
  112.  
  113.   bool straights = false;
  114.   flush = false;
  115.   four = false;
  116.   three = false;
  117.   pairs = 0;
  118.  
  119.   /* check for straight */
  120.   rank = 0;
  121.   while (num_in_rank[rank] == 0) rank++;
  122.   for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)
  123.     num_consec++;
  124.   if (num_consec == NUM_CARDS) {
  125.     straight = true;
  126.   }
  127.  
  128.   /* check for 4-of-a-kind, 3-of-a-kind, and pairs */
  129.   for (rank = 0; rank < NUM_RANKS; rank++) {
  130.     if (num_in_rank[rank] == 4) four = true;
  131.     if (num_in_rank[rank] == 3) three = true;
  132.     if (num_in_rank[rank] == 2) pairs++;
  133.   }
  134. }
  135.  
  136. /**********************************************************
  137.  * print_result                  *
  138.  **********************************************************/
  139.  
  140.   if (four)         printf("Four of a kind");
  141.   else if (three &&
  142.            pairs == 1)   printf("Full house");
  143.   else if (flush)        printf("Flush");
  144.   else if (straight)     printf("Straight");
  145.   else if (three)        printf("Three of a kind");
  146.   else if (pairs == 2)   printf("Two pairs");
  147.   else if (pairs == 1)   printf("Pair");
  148.   else                   printf("High card");
  149.  
  150. getch();
  151.   printf("\n\n");
  152.    
  153. }
Advertisement
Add Comment
Please, Sign In to add comment