Advertisement
cd62131

Blackjack in C

Oct 28th, 2013
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.52 KB | None | 0 0
  1. #include "blackjack.h"
  2.  
  3. card_t cards[DECK * 4 * 13];
  4.  
  5. fighter_t *new_fighter(void) {
  6.   fighter_t * new = (fighter_t *)malloc(sizeof(fighter_t));
  7.   new->value = 0;
  8.   new->card = NULL;
  9.   new->prev = NULL;
  10.   return new;
  11. }
  12.  
  13. void init_cards(void) {
  14.   char * const suits[] = { "Spade", "Heart", "Diamond", "Club" };
  15.   char * const face[] = {
  16.     "dummy", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
  17.   int count = 0;
  18.   for (int i = 0; i < DECK; i++) {
  19.     for (int j = 0; j < 4; j++) {
  20.       for (int k = 1; k <= 13; k++) {
  21.         cards[count].suit = suits[j];
  22.         cards[count].face = face[k];
  23.         cards[count].kind = k;
  24.         if (k == Ace) cards[count].worth = 11;
  25.         else if (k >= Ten) cards[count].worth = 10;
  26.         else cards[count].worth = k;
  27.         count++;
  28.       }
  29.     }
  30.   }
  31.   shuffle_cards();
  32. }
  33.  
  34. void swap_cards(card_t *a, card_t *b) {
  35.   card_t save = *a; *a = *b; *b = save;
  36. }
  37.  
  38. void shuffle_cards(void) {
  39.   //  srand((unsigned int) time(NULL));
  40.   srand(1UL);
  41.   for (int i = DECK * 4 * 13 - 1; i > 0; i--) {
  42.     int random = rand() % i; swap_cards(&cards[i], &cards[random]);
  43.   }
  44. }
  45.  
  46. fighter_t *pull(fighter_t *fighter) {
  47.   static int front = 0;
  48.   card_t card = cards[front++];
  49.   fighter_t *new = new_fighter();
  50.   new->prev = fighter;
  51.   new->value = fighter->value + card.worth;
  52.   new->card = (card_t *) malloc(sizeof(card_t));
  53.   new->card->suit = card.suit;
  54.   new->card->face = card.face;
  55.   new->card->kind = card.kind;
  56.   new->card->worth = card.worth;
  57.   return new;
  58. }
  59.  
  60. void print_fight_without_hole(fighter_t *dealer, fighter_t *player) {
  61.   printf("Dealer up cards: ");
  62.   print_up(dealer);
  63.   puts("");
  64.   print_player(player);
  65.   puts("");
  66. }
  67.  
  68. void print_up(fighter_t *dealer) {
  69.   for (fighter_t *p = dealer; p->prev->prev != NULL; p = p->prev) {
  70.     printf("%s %s", p->card->suit, p->card->face);
  71.     if (p->prev->prev->prev != NULL) printf(", ");
  72.   }
  73. }
  74.  
  75. void print_player(fighter_t *player) {
  76.   printf("Player value: %i, cards: ", player->value);
  77.   print_hand(player);
  78. }
  79.  
  80. void print_hand(fighter_t *fighter) {
  81.   for (fighter_t *p = fighter; p->prev != NULL; p = p->prev) {
  82.     printf("%s %s", p->card->suit, p->card->face);
  83.     if (p->prev->prev != NULL) printf(", ");
  84.   }
  85. }
  86.  
  87. void print_fight(fighter_t *dealer, fighter_t *player) {
  88.   switch (judge(dealer, player)) {
  89.     case DealerBust:
  90.       puts("Dealer Bust");
  91.       break;
  92.     case PlayerBust:
  93.       puts("You Bust");
  94.       break;
  95.     case Even:
  96.       puts("Even");
  97.       break;
  98.     case PlayerBlackjack:
  99.       puts("You Blackjack");
  100.       break;
  101.     case DealerBlackjack:
  102.       puts("Dealer Blackjack");
  103.       break;
  104.     default:
  105.       if (player->value > dealer->value) puts("You Win");
  106.       else if (player->value < dealer->value) puts("You Lose");
  107.       else puts("Even");
  108.       break;
  109.   }
  110.   printf("Dealer value: %i, cards: ", dealer->value);
  111.   print_hand(dealer);
  112.   puts("");
  113.   print_player(player);
  114.   puts("");
  115. }
  116.  
  117. judge_t judge(fighter_t *dealer, fighter_t *player) {
  118.   if (dealer->value > 21) return DealerBust;
  119.   if (player->value > 21) return PlayerBust;
  120.   if (dealer->value == 21 && player->value == 21) return Even;
  121.   if (player->value == 21) return PlayerBlackjack;
  122.   if (dealer->value == 21) return DealerBlackjack;
  123.   if (dealer->value < 17) return DealerDuty;
  124.   if (dealer->value >= 17 && dealer->value < 21) return DealerPass;
  125.   return HitOrStay;
  126. }
  127.  
  128. fighter_t *try_soft_ace(fighter_t *fighter) {
  129.   for (fighter_t *p = fighter; p->prev != NULL; p = p->prev) {
  130.     if (p->card->kind != Ace) continue;
  131.     puts("Try Soft Ace");
  132.     p->card->kind = One; fighter->value -= 10; return fighter;
  133.   }
  134.   return fighter;
  135. }
  136.  
  137. bool yes_or_no(void) {
  138.   char *yes = NULL; int n = scanf("%ms", &yes); if (n != 1) exit(1);
  139.   if (strcmp(yes, "y") == 0 ) return true;
  140.   return false;
  141. }
  142.  
  143. fighter_t *hit_or_stay(fighter_t *player) {
  144.   printf("Hit? (y/n): "); fflush(stdout);
  145.   if (!yes_or_no()) return player;
  146.   fighter_t *pulled = pull(player);
  147.   printf("You Pulled %s %s\n", pulled->card->suit, pulled->card->face);
  148.   return pulled;
  149. }
  150.  
  151. int count_ace(fighter_t *fighter) {
  152.   int count = 0;
  153.   for (fighter_t *p = fighter; p->prev != NULL; p = p->prev) {
  154.     if (p->card->kind == Ace) count++;
  155.   }
  156.   return count;
  157. }
  158.  
  159. fighter_t *dealer_duty(fighter_t *dealer) {
  160.   fighter_t *pulled = pull(dealer);
  161.   printf("Dealer Pulled %s %s\n", pulled->card->suit, pulled->card->face);
  162.   return pulled;
  163. }
  164.  
  165. bool continued(void) {
  166.   printf("Continue ? (y/n): "); fflush(stdout); return yes_or_no();
  167. }
  168.  
  169. int main(int ac, char **av) {
  170.   puts("Blackjack");
  171.   init_cards();
  172.   do {
  173.     fighter_t *dealer = new_fighter(); fighter_t *player = new_fighter();
  174.     for (int i = 0; i < 2; i++) {
  175.       player = pull(player); dealer = pull(dealer);
  176.     }
  177.     while (1) {
  178.       fighter_t *stay;
  179.       print_fight_without_hole(dealer, player);
  180.       switch (judge(dealer, player)) {
  181.         case DealerBust:
  182.           if (count_ace(dealer) > 0) { dealer = try_soft_ace(dealer); continue; }
  183.           break;
  184.         case PlayerBust:
  185.           if (count_ace(player) > 0) { player = try_soft_ace(player); continue; }
  186.           break;
  187.         case DealerDuty:
  188.           dealer = dealer_duty(dealer); continue;
  189.           break;
  190.         case HitOrStay:
  191.         case DealerPass:
  192.           stay = player; player = hit_or_stay(player); if (stay != player) continue;
  193.           break;
  194.         default:
  195.           break;
  196.       }
  197.       print_fight(dealer, player); break;
  198.     }
  199.   } while (continued());
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement