fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define SUITS 4
  7. #define FACES 13
  8.  
  9. void shuffel(int deck[][FACES]);
  10. void deal(int deck[][FACES], const char* face[], const char* suit[], int handRow[], int handCol[]);
  11. int containsPair(int handCol[]);
  12. int containsTwoPairs(int handCol[]);
  13. int containsThreeOfKind(int handCol[]);
  14. int containsFourOfKind(int handCol[]);
  15. int containsFlush(int handRow[]);
  16. int containsStraight(int handCol[]);
  17.  
  18.  
  19. int main(void)
  20. {
  21. int deck[SUITS][FACES] = { 0 };
  22.  
  23. srand((unsigned)time(NULL));
  24.  
  25. const char* suit[SUITS] = {
  26. "Hearts", "Diamonds", "Clubs", "Spades"
  27. };
  28.  
  29. const char* face[FACES] = {
  30. "Ace", "Deuce", "Three", "Four", "Five",
  31. "Six", "Seven", "Eight", "Nine", "Ten",
  32. "Jack", "Queen", "King"
  33. };
  34.  
  35. int handRow[5];
  36. int handCol[5];
  37.  
  38. shuffel(deck);
  39. deal(deck, face, suit, handRow, handCol);
  40.  
  41.  
  42. }
  43.  
  44. void shuffel(int deck[][FACES])
  45. {
  46. for (int card = 1; card <= 52; card++)
  47. {
  48. int row, col;
  49.  
  50. do
  51. {
  52. row = rand() % SUITS;
  53. col = rand() % FACES;
  54. } while (deck[row][col] != 0);
  55.  
  56. deck[row][col] = card;
  57. }
  58. }
  59.  
  60. void deal(int deck[][FACES], const char* face[], const char* suit[], int handRow[], int handCol[])
  61. {
  62. for (int card = 1; card <= 5; card++)
  63. {
  64. for (int i = 0; i < SUITS; i++)
  65. {
  66. for (int j = 0; j < FACES; j++)
  67. {
  68. if (deck[i][j] == card)
  69. {
  70. printf("%5s of %-8s %c", face[j], suit[i],
  71. card % 5 == 0 ? '\n' : ' ');
  72. handRow[card - 1] = i;
  73. handCol[card - 1] = j;
  74. }
  75. }
  76. }
  77.  
  78. }
  79. }
  80.  
  81. int containsPair(int handCol[])
  82. {
  83. for (int card = 0; card < 5; card++)
  84. {
  85. for (int i = card + 1; i < 5; i++)
  86. {
  87. if (handCol[card] == handCol[i])
  88. {
  89. return 1;
  90. }
  91. }
  92. }
  93. return 0;
  94. }
  95.  
  96. int containsTwoPairs(int handCol[])
  97. {
  98. int count[13] = { 0 };
  99. int pairs = 0;
  100.  
  101. for (int i = 0; i < 5; i++)
  102. {
  103. count[handCol[i]]++;
  104. }
  105.  
  106. for (int i = 0; i < 13; i++)
  107. {
  108. if (count[i] == 2)
  109. {
  110. pairs++;
  111. }
  112. }
  113.  
  114. return pairs == 2;
  115. }
  116.  
  117. int containsThreeOfKind(int handCol[])
  118. {
  119. int count[13] = { 0 };
  120.  
  121. for (int i = 0; i < 5; i++)
  122. {
  123. count[handCol[i]]++;
  124. }
  125.  
  126. for (int i = 0; i < 13; i++)
  127. {
  128. if (count[i] == 3)
  129. {
  130. return 1;
  131.  
  132. }
  133. }
  134.  
  135. return 0;
  136. }
  137.  
  138. int containsFourOfKind(int handCol[])
  139. {
  140. int count[13] = { 0 };
  141.  
  142. for (int i = 0; i < 5; i++)
  143. {
  144. count[handCol[i]]++;
  145. }
  146.  
  147. for (int i = 0; i < 13; i++)
  148. {
  149. if (count[i] == 4)
  150. {
  151. return 1;
  152.  
  153. }
  154. }
  155.  
  156. return 0;
  157. }
  158.  
  159. int containsFlush(int handRow[])
  160. {
  161.  
  162. for (int i = 1; i < 5; i++)
  163. {
  164. if (handRow[i] != handRow[0])
  165. {
  166. return 0;
  167. }
  168. }
  169. return 1;
  170. }
  171.  
  172.  
  173. int containsStraight(int handCol[])
  174. {
  175. int temp[5];
  176.  
  177.  
  178. for (int i = 0; i < 5; i++)
  179. {
  180. temp[i] = handCol[i];
  181. }
  182.  
  183.  
  184. for (int i = 0; i < 4; i++)
  185. {
  186. for (int j = i + 1; j < 5; j++)
  187. {
  188. if (temp[i] > temp[j])
  189. {
  190. int swap = temp[i];
  191. temp[i] = temp[j];
  192. temp[j] = swap;
  193. }
  194. }
  195. }
  196.  
  197.  
  198. for (int i = 0; i < 4; i++)
  199. {
  200. if (temp[i + 1] != temp[i] + 1)
  201. {
  202. return 0;
  203. }
  204. }
  205.  
  206. return 1;
  207. }
  208.  
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
Deuce of Hearts       Ace of Hearts     Seven of Diamonds    Five of Clubs       Nine of Clubs