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