Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "blackjack.h"
- card_t cards[DECK * 4 * 13];
- fighter_t *new_fighter(void) {
- fighter_t * new = (fighter_t *)malloc(sizeof(fighter_t));
- new->value = 0;
- new->card = NULL;
- new->prev = NULL;
- return new;
- }
- void init_cards(void) {
- char * const suits[] = { "Spade", "Heart", "Diamond", "Club" };
- char * const face[] = {
- "dummy", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
- int count = 0;
- for (int i = 0; i < DECK; i++) {
- for (int j = 0; j < 4; j++) {
- for (int k = 1; k <= 13; k++) {
- cards[count].suit = suits[j];
- cards[count].face = face[k];
- cards[count].kind = k;
- if (k == Ace) cards[count].worth = 11;
- else if (k >= Ten) cards[count].worth = 10;
- else cards[count].worth = k;
- count++;
- }
- }
- }
- shuffle_cards();
- }
- void swap_cards(card_t *a, card_t *b) {
- card_t save = *a; *a = *b; *b = save;
- }
- void shuffle_cards(void) {
- // srand((unsigned int) time(NULL));
- srand(1UL);
- for (int i = DECK * 4 * 13 - 1; i > 0; i--) {
- int random = rand() % i; swap_cards(&cards[i], &cards[random]);
- }
- }
- fighter_t *pull(fighter_t *fighter) {
- static int front = 0;
- card_t card = cards[front++];
- fighter_t *new = new_fighter();
- new->prev = fighter;
- new->value = fighter->value + card.worth;
- new->card = (card_t *) malloc(sizeof(card_t));
- new->card->suit = card.suit;
- new->card->face = card.face;
- new->card->kind = card.kind;
- new->card->worth = card.worth;
- return new;
- }
- void print_fight_without_hole(fighter_t *dealer, fighter_t *player) {
- printf("Dealer up cards: ");
- print_up(dealer);
- puts("");
- print_player(player);
- puts("");
- }
- void print_up(fighter_t *dealer) {
- for (fighter_t *p = dealer; p->prev->prev != NULL; p = p->prev) {
- printf("%s %s", p->card->suit, p->card->face);
- if (p->prev->prev->prev != NULL) printf(", ");
- }
- }
- void print_player(fighter_t *player) {
- printf("Player value: %i, cards: ", player->value);
- print_hand(player);
- }
- void print_hand(fighter_t *fighter) {
- for (fighter_t *p = fighter; p->prev != NULL; p = p->prev) {
- printf("%s %s", p->card->suit, p->card->face);
- if (p->prev->prev != NULL) printf(", ");
- }
- }
- void print_fight(fighter_t *dealer, fighter_t *player) {
- switch (judge(dealer, player)) {
- case DealerBust:
- puts("Dealer Bust");
- break;
- case PlayerBust:
- puts("You Bust");
- break;
- case Even:
- puts("Even");
- break;
- case PlayerBlackjack:
- puts("You Blackjack");
- break;
- case DealerBlackjack:
- puts("Dealer Blackjack");
- break;
- default:
- if (player->value > dealer->value) puts("You Win");
- else if (player->value < dealer->value) puts("You Lose");
- else puts("Even");
- break;
- }
- printf("Dealer value: %i, cards: ", dealer->value);
- print_hand(dealer);
- puts("");
- print_player(player);
- puts("");
- }
- judge_t judge(fighter_t *dealer, fighter_t *player) {
- if (dealer->value > 21) return DealerBust;
- if (player->value > 21) return PlayerBust;
- if (dealer->value == 21 && player->value == 21) return Even;
- if (player->value == 21) return PlayerBlackjack;
- if (dealer->value == 21) return DealerBlackjack;
- if (dealer->value < 17) return DealerDuty;
- if (dealer->value >= 17 && dealer->value < 21) return DealerPass;
- return HitOrStay;
- }
- fighter_t *try_soft_ace(fighter_t *fighter) {
- for (fighter_t *p = fighter; p->prev != NULL; p = p->prev) {
- if (p->card->kind != Ace) continue;
- puts("Try Soft Ace");
- p->card->kind = One; fighter->value -= 10; return fighter;
- }
- return fighter;
- }
- bool yes_or_no(void) {
- char *yes = NULL; int n = scanf("%ms", &yes); if (n != 1) exit(1);
- if (strcmp(yes, "y") == 0 ) return true;
- return false;
- }
- fighter_t *hit_or_stay(fighter_t *player) {
- printf("Hit? (y/n): "); fflush(stdout);
- if (!yes_or_no()) return player;
- fighter_t *pulled = pull(player);
- printf("You Pulled %s %s\n", pulled->card->suit, pulled->card->face);
- return pulled;
- }
- int count_ace(fighter_t *fighter) {
- int count = 0;
- for (fighter_t *p = fighter; p->prev != NULL; p = p->prev) {
- if (p->card->kind == Ace) count++;
- }
- return count;
- }
- fighter_t *dealer_duty(fighter_t *dealer) {
- fighter_t *pulled = pull(dealer);
- printf("Dealer Pulled %s %s\n", pulled->card->suit, pulled->card->face);
- return pulled;
- }
- bool continued(void) {
- printf("Continue ? (y/n): "); fflush(stdout); return yes_or_no();
- }
- int main(int ac, char **av) {
- puts("Blackjack");
- init_cards();
- do {
- fighter_t *dealer = new_fighter(); fighter_t *player = new_fighter();
- for (int i = 0; i < 2; i++) {
- player = pull(player); dealer = pull(dealer);
- }
- while (1) {
- fighter_t *stay;
- print_fight_without_hole(dealer, player);
- switch (judge(dealer, player)) {
- case DealerBust:
- if (count_ace(dealer) > 0) { dealer = try_soft_ace(dealer); continue; }
- break;
- case PlayerBust:
- if (count_ace(player) > 0) { player = try_soft_ace(player); continue; }
- break;
- case DealerDuty:
- dealer = dealer_duty(dealer); continue;
- break;
- case HitOrStay:
- case DealerPass:
- stay = player; player = hit_or_stay(player); if (stay != player) continue;
- break;
- default:
- break;
- }
- print_fight(dealer, player); break;
- }
- } while (continued());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement