Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <ctype.h>
  5.  
  6. #define ACE 0
  7. #define CARDS 13
  8. #define SUITS 4
  9. #define MAX_CARDS 5
  10. #define SCREENLINECOUNT 25
  11. #define BUF_NAME 15
  12. #define NO_WINNER 0
  13.  
  14. struct table {
  15.  
  16. int cards[MAX_CARDS];
  17.  
  18. char name[BUF_NAME]; /* Holds the user's first name. */
  19.  
  20. int points[MAX_CARDS]; /* Points for each hand */
  21.  
  22. int total; /* total points */
  23.  
  24. int hand; /* number of cards dealt */
  25.  
  26. int aces; /* N° of aces: These variable are used to attribute values 1/11 to Aces*/
  27.  
  28. int credit, bet;
  29. };
  30.  
  31. void draw_cards(struct table *dealer, struct table *player);
  32. void print_cards(struct table *dealer, struct table *player, char check_winner);
  33. void init(struct table *dealer, struct table *player);
  34. int findWinner(struct table *dealer, struct table *player, char check_stand);
  35. void assign_points(struct table *dealer, struct table *player, char ans);
  36. void bet (struct table *player);
  37.  
  38. int main()
  39. {
  40. srand((unsigned)time(NULL));
  41. struct table player = {0};
  42. struct table dealer = {0};
  43.  
  44. player.credit = 50;
  45.  
  46. char check_winner; /* to check the presence of a winner */
  47. char check_stand; /* to check wheter user entered stand */
  48. check_stand = check_winner = 0;
  49.  
  50. char ans = 'M'; /* For Hit/S answers */
  51.  
  52. for(;;) {
  53.  
  54. if (player.hand == 0) {
  55.  
  56. init(&dealer, &player);
  57. draw_cards(&dealer, &player);
  58.  
  59. assign_points(&dealer, &player, 'H');
  60.  
  61. assign_points(&dealer, &player, 'H');
  62. }
  63.  
  64. print_cards(&dealer, &player, check_winner);
  65.  
  66. check_winner = findWinner(&dealer, &player, check_stand);
  67.  
  68. if (check_winner != NO_WINNER) {
  69.  
  70. if (check_winner == 4) /* Game Over */
  71. break;
  72.  
  73. else {
  74. check_stand = 0;
  75. check_winner = 0;
  76. ans = 'M';
  77. continue;
  78. }
  79. }
  80.  
  81. else {
  82.  
  83. printf("Hit or stand (H/S)? ");
  84.  
  85. do {
  86. ans = toupper(getchar());
  87.  
  88. if (ans == 'H' || ans == 'S')
  89. break;
  90.  
  91. else
  92. continue;
  93.  
  94. } while(1);
  95.  
  96. if (ans == 'S' && dealer.total >= 17)
  97. check_stand = 1;
  98.  
  99. assign_points(&dealer, &player, ans);
  100. }
  101. } /* end of for(;;) */
  102.  
  103. return 0;
  104. }
  105.  
  106. void draw_cards(struct table *dealer, struct table *player) {
  107.  
  108. int card_pick, suit_pick;
  109. int i = 0;
  110.  
  111. int drawn_card[SUITS][CARDS] = {0};
  112.  
  113. while (i < MAX_CARDS) { /* draw dealer's cards */
  114.  
  115. suit_pick = rand() % SUITS;
  116. card_pick = rand() % CARDS;
  117.  
  118. if (drawn_card[suit_pick][card_pick] == 0) {
  119.  
  120. dealer->cards[i] = suit_pick * CARDS + card_pick;
  121. drawn_card[suit_pick][card_pick] = 1;
  122.  
  123. if (card_pick == ACE) { /* Here points are defined but not still assigned */
  124. dealer->points[i] = 11;
  125. dealer->aces++;
  126. }
  127.  
  128. else if (card_pick >= 9)
  129. dealer->points[i] = 10;
  130.  
  131. else
  132. dealer->points[i] = card_pick + 1;
  133.  
  134. i++;
  135. }
  136. }
  137.  
  138. i = 0;
  139.  
  140. while (i < MAX_CARDS) { /* draw player's cards */
  141.  
  142. card_pick = rand() % CARDS;
  143. suit_pick = rand() % SUITS;
  144.  
  145. if (drawn_card[suit_pick][card_pick] == 0) {
  146.  
  147. player->cards[i] = suit_pick * CARDS + card_pick;
  148. drawn_card[suit_pick][card_pick] = 1;
  149.  
  150. if (card_pick == ACE) { /* Here points are defined but not still assigned */
  151. player->points[i] = 11;
  152. player->aces++;
  153. }
  154.  
  155. else if (card_pick >= 9)
  156. player->points[i] = 10;
  157.  
  158. else
  159. player->points[i] = card_pick + 1;
  160.  
  161. i++;
  162. }
  163. }
  164.  
  165. return;
  166. }
  167.  
  168. void assign_points(struct table *dealer, struct table *player, char ans) {
  169.  
  170. if (dealer->total < 17) { /* Assing dealer's points: Dealer has to stand at 17 */
  171.  
  172. if (dealer->aces > 0 && dealer->total > 21) {
  173. dealer->total = dealer->total - 10;
  174. dealer->aces--;
  175. }
  176.  
  177. else
  178. dealer->total += dealer->points[dealer->hand];
  179.  
  180. dealer->hand++;
  181. }
  182.  
  183. if (ans == 'H') { /* Assign player's points */
  184.  
  185. if (player->aces > 0 && player->total > 21) {
  186. player->total = player->total - 10;
  187. player->aces--;
  188. }
  189.  
  190. else
  191. player->total += player->points[player->hand];
  192.  
  193. player->hand++;
  194. }
  195. }
  196.  
  197. void print_cards(struct table *dealer, struct table *player, char check_winner) {
  198.  
  199. const char *deck[SUITS][CARDS] = {{"🂡","🂢","🂣","🂤","🂥","🂦","🂧","🂨","🂩","🂪","🂫","🂭","🂮"},
  200. {"🂱","🂲","🂳","🂴","🂵","🂶","🂷","🂸","🂹","🂺","🂻","🂽","🂾"},
  201. {"🃁","🃂","🃃","🃄","🃅","🃆","🃇","🃈","🃉","🃊","🃋","🃍","🃎"},
  202. {"🃑","🃒","🃓","🃔","🃕","🃖","🃗","🃘","🃙","🃚","🃛","🃝","🃞"}};
  203.  
  204. int i;
  205. int suit_pick, card_pick;
  206. printf("Dealer's cards:\n\n");
  207.  
  208. if (check_winner != 0) {
  209. suit_pick = dealer->cards[0] / CARDS;
  210. card_pick = dealer->cards[0] % CARDS;
  211. printf("%s ", deck[suit_pick][card_pick]);
  212. }
  213.  
  214. else
  215. printf("%s ", "🂠");
  216.  
  217. for (i = 0; i < dealer->hand; i++) { /* print dealer's cards */
  218.  
  219. if (i == 0)
  220. continue;
  221. else {
  222. suit_pick = dealer->cards[i] / CARDS;
  223. card_pick = dealer->cards[i] % CARDS;
  224. printf("%s ", deck[suit_pick][card_pick]);
  225. }
  226. }
  227.  
  228. printf("\n\n");
  229.  
  230. printf("Your cards:\n\n");
  231.  
  232. for (i = 0; i < player->hand; i++) { /* print player's cards */
  233. suit_pick = player->cards[i] / CARDS;
  234. card_pick = player->cards[i] % CARDS;
  235. printf("%s ", deck[suit_pick][card_pick]);
  236. }
  237.  
  238. while (i < 17) {
  239. printf("\n"); /* Clears screen by printing a number of blank lines. */
  240. i++;
  241. }
  242. }
  243.  
  244. void init(struct table *dealer, struct table *player) {
  245.  
  246. player->total = 0;
  247. dealer->total = 0;
  248.  
  249. printf("\n\nStep right up to the Blackjack tables\n\n");
  250.  
  251. if (player->name[0] == 0) {
  252. printf("\nWhat is your first name? ");
  253.  
  254. do {
  255. fgets(player->name, BUF_NAME, stdin);
  256.  
  257. if(!isprint(player->name[0])) {
  258. printf("Please enter a valid name\n");
  259. continue;
  260. }
  261.  
  262. else
  263. break;
  264.  
  265. } while (1);
  266.  
  267. printf("Ok, %sGet ready for casino action * Blackjack pays 3 to 2 * You're credit is 50$ * Press enter to start\n\n", player->name);
  268. bet(player);
  269. }
  270. }
  271.  
  272. int findWinner (struct table *dealer, struct table *player, char check_stand) {
  273.  
  274. char winner;
  275.  
  276. if (check_stand == 1) {
  277.  
  278. if (dealer->total == player->total) /* Tie */
  279. winner = 5;
  280.  
  281. else if ( (player->total <= 21) && (player->total > 17) ) /* Player wins */
  282. winner = 2;
  283.  
  284. else /* Dealer wins */
  285. winner = 3;
  286. }
  287.  
  288. else {
  289.  
  290. if (player->total > 21) /* If the player exceeds a sum of 21 "busts", loses, even if the dealer also exceeds 21 */
  291. winner = 3;
  292.  
  293. else if ( (player->total == 21) && (dealer->total == 21) ) /* A tie */
  294. winner = 5;
  295.  
  296. else if ( (player->total == 21) && (dealer->total != 21) ) /* Player wins with Blackjack */
  297. winner = 1;
  298.  
  299. else if ( (player->total <= 21) && (dealer->total > 21) ) /* Player wins */
  300. winner = 2;
  301.  
  302. else if ( (dealer->total == 21) && (player->total != 21) ) /* Dealer wins */
  303. winner = 3;
  304.  
  305. else
  306. winner = NO_WINNER; /* no winner found */
  307. }
  308.  
  309. if (winner != NO_WINNER) {
  310.  
  311. print_cards(dealer, player, winner);
  312.  
  313. player->hand = dealer->hand = 0; /* zero number of cards dealt */
  314. player->aces = dealer->aces = 0; /* zero number of aces */
  315.  
  316. if (winner == 1) {
  317. printf("Congratulation you win!\n");
  318. player->credit += (player->bet * 3)/2;
  319. }
  320.  
  321. else if (winner == 2) {
  322. printf("Congratulation you win!\n");
  323. player->credit += player->bet;
  324. }
  325.  
  326. else if (winner == 3) {
  327. printf("Sorry, Dealer wins\n");
  328. player->credit -= player->bet;
  329. }
  330.  
  331. else
  332. printf("No one wins\n");
  333.  
  334. printf("credit: %d$\n", player->credit);
  335.  
  336. if (player->credit <= 0) {
  337. printf("Sorry you bankrupted\n");
  338. return 4; /* Game Over */
  339. }
  340.  
  341. else {
  342. char ans;
  343. printf("Play again (Y/N)?\n");
  344.  
  345. do {
  346. ans = toupper(getchar());
  347.  
  348. if(ans == 'Y') {
  349. bet(player);
  350. return 1;
  351. }
  352.  
  353. else if (ans == 'N') {
  354. printf("Goodbye!\n");
  355. return 4; /* Game Over */
  356. }
  357.  
  358. else
  359. continue;
  360. } while(1);
  361. }
  362. }
  363.  
  364. print_cards(dealer, player, winner);
  365.  
  366. return 0;
  367. }
  368.  
  369. void bet (struct table *player) {
  370.  
  371. printf("What's your bet?\n");
  372.  
  373. while ( (scanf("%d", &player->bet) != 1) || player->bet > player->credit)
  374. {
  375. while (getchar() != '\n');
  376. printf ("Enter a valid sum, your credit is %d$:\n", player->credit);
  377. }
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement