Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "blackjack.h"
- extern card_t *cards;
- char * const suits[] = { "Spade", "Heart", "Diamond", "Club" };
- char * const face[] = {
- "dummy", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
- fighter_t *new_fighter(void) {
- fighter_t * new = (fighter_t *) malloc(sizeof(fighter_t));
- new->value = 0;
- new->card = (card_t *) NULL;
- new->prev = (fighter_t *) NULL;
- return new;
- }
- void init_cards(void) {
- cards = (card_t *) malloc(sizeof(card_t) * DECK * 4 * 13);
- 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 == 1) cards[count].worth = 11;
- else if (k < 10) cards[count].worth = k;
- else cards[count].worth = 10;
- count++;
- }
- }
- }
- shuffle();
- }
- void shuffle(void) {
- srand((unsigned int) time(NULL));
- for (int i = DECK * 4 * 13 - 1; i > 0; i--) {
- int random = rand() % i;
- card_t swap = cards[i];
- cards[i] = cards[random];
- cards[random] = swap;
- }
- }
- card_t *pull_card(void) {
- static int front = 0;
- return &cards[front++];
- }
- fighter_t *new_state(fighter_t *fighter, card_t *card) {
- 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);
- }
- 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);
- puts("");
- }
- 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) {
- if (dealer->value > 21) puts("Dealer Bust");
- else if (player->value > 21) puts("You Bust");
- else if (dealer->value == player->value) puts("Even");
- else if (player->value == 21) puts("You Blackjack");
- else if (dealer->value == 21) puts("Dealer Blackjack");
- else if (dealer->value > player->value) puts("You Lose");
- else if (dealer->value < player->value) puts("You Win");
- else puts("Even");
- printf("Dealer value: %i, cards: ", dealer->value);
- print_hand(dealer);
- puts("");
- print_player(player);
- }
- judge_t judge(fighter_t *dealer, fighter_t *player) {
- if (dealer->value == 21 && player->value == 21) return Even;
- if (dealer->value == 21) return DealerBlackjack;
- if (dealer->value < 17) return DealerDuty;
- if (dealer->value >= 17 && dealer->value < 21) return DealerPass;
- if (dealer->value > 21) return DealerBust;
- if (player->value == 21) return PlayerBlackjack;
- if (player->value > 21) return PlayerBust;
- return HitOrStay;
- }
- fighter_t *pull(fighter_t *fighter) {
- card_t *card = pull_card();
- return new_state(fighter, card);
- }
- 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;
- p->card->kind = One;
- fighter->value -= 10;
- puts("TrySoftAce");
- return fighter;
- }
- return fighter;
- }
- fighter_t *hit_or_stay(fighter_t *player) {
- int n;
- char *yes_or_no = NULL;
- printf("Hit? (y/n); ");
- n = scanf("%ms", &yes_or_no);
- if (n != 1) exit(1);
- if (strcmp(yes_or_no, "y")) return player;
- return pull(player);
- }
- 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;
- }
- int main(int ac, char **av) {
- puts("Blackjack");
- init_cards();
- fighter_t *dealer = new_fighter();
- fighter_t *player = new_fighter();
- for (int i = 0; i < 2; i++) {
- card_t *card = pull_card();
- player = new_state(player, card);
- card = pull_card();
- dealer = new_state(dealer, card);
- }
- while (1) {
- fighter_t *same = NULL;
- print_fight_without_hole(dealer, player);
- switch (judge(dealer, player)) {
- case DealerBust:
- if (count_ace(dealer) > 0) {
- dealer = try_soft_ace(dealer);
- continue;
- } // wall through
- case DealerBlackjack:
- case PlayerBlackjack:
- case Even:
- print_fight(dealer, player);
- exit(0);
- break;
- case PlayerBust:
- if (count_ace(player) > 0) {
- player = try_soft_ace(player);
- continue;
- }
- print_fight(dealer, player);
- exit(0);
- break;
- case DealerDuty:
- puts("Dealer Duty");
- dealer = pull(dealer);
- break;
- case HitOrStay:
- pull(dealer); // walk through
- case DealerPass:
- same = player;
- player = hit_or_stay(player);
- if (player != same) continue;
- print_fight(dealer, player);
- exit(0);
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement