Advertisement
BilakshanP

Untitled

Feb 26th, 2024 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #include "cards.h"
  6.  
  7. void assert_card_valid(card_t c) {
  8. int flag = 0;
  9. if (c.value < 2 || c.value > VALUE_ACE)
  10. flag = 1;
  11. if (c.suit < SPADES || c.suit > CLUBS)
  12. flag = 1;
  13. assert(flag == 0);
  14. }
  15.  
  16. const char* ranking_to_string(hand_ranking_t r) {
  17. switch (r) {
  18. case STRAIGHT_FLUSH:
  19. return "STRAIGHT_FLUSH";
  20. break;
  21. case FOUR_OF_A_KIND:
  22. return "FOUR_OF_A_KIND";
  23. break;
  24. case FULL_HOUSE:
  25. return "FULL_HOUSE";
  26. break;
  27. case FLUSH:
  28. return "FLUSH";
  29. break;
  30. case STRAIGHT:
  31. return "STRAIGHT";
  32. break;
  33. case THREE_OF_A_KIND:
  34. return "THREE_OF_A_KIND";
  35. break;
  36. case TWO_PAIR:
  37. return "TWO_PAIR";
  38. break;
  39. case PAIR:
  40. return "PAIR";
  41. break;
  42. case NOTHING:
  43. return "NOTHING";
  44. break;
  45. default:
  46. return "wrong";
  47. }
  48. }
  49.  
  50. char value_letter(card_t c) {
  51. switch (c.value) {
  52. case 2:
  53. return '2';
  54. break;
  55. case 3:
  56. return '3';
  57. break;
  58. case 4:
  59. return '4';
  60. break;
  61. case 5:
  62. return '5';
  63. break;
  64. case 6:
  65. return '6';
  66. break;
  67. case 7:
  68. return '7';
  69. break;
  70. case 8:
  71. return '8';
  72. break;
  73. case 9:
  74. return '9';
  75. break;
  76. case 10:
  77. return '0';
  78. break;
  79. case VALUE_JACK:
  80. return 'J';
  81. break;
  82. case VALUE_QUEEN:
  83. return 'Q';
  84. break;
  85. case VALUE_KING:
  86. return 'K';
  87. break;
  88. case VALUE_ACE:
  89. return 'A';
  90. break;
  91. default:
  92. return 'x';
  93. }
  94. }
  95.  
  96. char suit_letter(card_t c) {
  97. switch (c.suit) {
  98. case SPADES:
  99. return 's';
  100. break;
  101. case HEARTS:
  102. return 'h';
  103. break;
  104. case DIAMONDS:
  105. return 'd';
  106. break;
  107. case CLUBS:
  108. return 'c';
  109. break;
  110. default:
  111. return 'x';
  112. }
  113. }
  114.  
  115. void print_card(card_t c) {
  116. printf("%c", value_letter(c));
  117. printf("%c", suit_letter(c));
  118. }
  119.  
  120. card_t card_from_letters(char value_let, char suit_let) {
  121. card_t temp;
  122. switch (value_let) {
  123. case 'A':
  124. temp.value = VALUE_ACE;
  125. break;
  126. case 'J':
  127. temp.value = VALUE_JACK;
  128. break;
  129. case 'Q':
  130. temp.value = VALUE_QUEEN;
  131. break;
  132. case 'K':
  133. temp.value = VALUE_KING;
  134. break;
  135. case '0':
  136. temp.value = 10;
  137. break;
  138. default:
  139. temp.value = value_let - 48;
  140. }
  141. switch (suit_let) {
  142. case 's':
  143. temp.suit = SPADES;
  144. break;
  145. case 'h':
  146. temp.suit = HEARTS;
  147. break;
  148. case 'd':
  149. temp.suit = DIAMONDS;
  150. break;
  151. case 'c':
  152. temp.suit = CLUBS;
  153. break;
  154. default:
  155. temp.suit = NUM_SUITS;
  156. }
  157.  
  158. assert_card_valid(temp);
  159.  
  160. return temp;
  161. }
  162.  
  163. card_t card_from_num(unsigned c) {
  164. card_t temp;
  165. if (c >= 0 && c <= 12) {
  166. if (c % 13 == 0)
  167. temp.value = VALUE_KING;
  168. else if (c % 13 == 1)
  169. temp.value = VALUE_ACE;
  170. else if (c % 13 == 11)
  171. temp.value = VALUE_JACK;
  172. else if (c % 13 == 12)
  173. temp.value = VALUE_QUEEN;
  174. else
  175. temp.value = c % 13;
  176. temp.suit = SPADES;
  177. }
  178.  
  179. if (c >= 13 && c <= 25) {
  180. if (c % 13 == 0)
  181. temp.value = VALUE_KING;
  182. else if (c % 13 == 1)
  183. temp.value = VALUE_ACE;
  184. else if (c % 13 == 11)
  185. temp.value = VALUE_JACK;
  186. else if (c % 13 == 12)
  187. temp.value = VALUE_QUEEN;
  188. else
  189. temp.value = c % 13;
  190. temp.suit = HEARTS;
  191. }
  192.  
  193. if (c >= 26 && c <= 38) {
  194. if (c % 13 == 0)
  195. temp.value = VALUE_KING;
  196. else if (c % 13 == 1)
  197. temp.value = VALUE_ACE;
  198. else if (c % 13 == 11)
  199. temp.value = VALUE_JACK;
  200. else if (c % 13 == 12)
  201. temp.value = VALUE_QUEEN;
  202. else
  203. temp.value = c % 13;
  204.  
  205. temp.suit = DIAMONDS;
  206. }
  207.  
  208. if (c >= 39 && c <= 51) {
  209. if (c % 13 == 0)
  210. temp.value = VALUE_KING;
  211. else if (c % 13 == 1)
  212. temp.value = VALUE_ACE;
  213. else if (c % 13 == 11)
  214. temp.value = VALUE_JACK;
  215. else if (c % 13 == 12)
  216. temp.value = VALUE_QUEEN;
  217. else
  218. temp.value = c % 13;
  219. temp.suit = CLUBS;
  220. }
  221. return temp;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement