int get_bet(player_t human, int currentHibet) { int bet = UNINIT; // Turn me into a string bean! // I should not code at 4AM, especially after enjoying brandy char betText[99] = "Bet ("; char betHelp[44] = ""; if (currentHibet) { strlcat(betHelp, "must beat ", sizeof(betHelp)); strlcat(betHelp, numStr_int(currentHibet), sizeof(betHelp)); strlcat(betText, betHelp, sizeof(betText)); } else { strlcat(betHelp, "6 or greater", sizeof(betHelp)); strlcat(betText, betHelp, sizeof(betText)); } strlcat(betText, "): ", sizeof(betText)); // Don't go bidding 4 if (!currentHibet) currentHibet = 5; show_hand(human); while (bet == UNINIT) { bet = get_number_in_range(betText, -2, 12, 2); if (bet == ENTERED * UNINIT || bet == -2) return choose_bet(human, currentHibet); if (bet == HELP * UNINIT) h_betting(); if (bet == -1 || bet == NOTHING * UNINIT) return random_euchre_bet(); if (bet < 0) bet = UNINIT; if (bet > 0 && bet <= currentHibet) { if (currentHibet==5) printf("\tMinimum bid is 6.\n"); else printf("\tMust beat the current high bet. Enter 0 to pass.\n"); bet = UNINIT; } } return bet; }
int main(void) { Card cards[NUMBER_CARDS]; Card *aCard = cards; uint roundCount = NUMBER_ROUNDS_SHUFFLE, gamesPlayed = 0, gamesWon = 0; char buffer[80]; /* Load the cards */ uint i = 0; do { uint j = 0; do { aCard->suit = i; aCard->rank = j; ++aCard; } while (++j < NUMBER_RANKS); } while (++i < NUMBER_SUITS); /* Play */ do { uint aces = 0, playerPoints = 0, dealerPoints = 0; Card *playerHand[MAXIMUM_HAND]; Card **aHandCard = playerHand; /* Shuffle the cards after a specific number of rounds */ if (++roundCount >= NUMBER_ROUNDS_SHUFFLE) { shuffle(cards); aCard = cards; roundCount = 0; } /* Player plays */ do { /* Add the card's rank to player point */ if (aCard->rank == ACE) { playerPoints += 11; ++aces; } else if (aCard->rank < JACK) playerPoints += aCard->rank + 1; else playerPoints += 10; /* Print the new card */ printf("New Card... %s of %s\n", rank_names[aCard->rank], suit_names[aCard->suit]); *aHandCard = aCard; ++aCard; assert(aCard != cards + NUMBER_CARDS); /* Let the second card be dealt */ if (aHandCard == playerHand) { ++aHandCard; *buffer = 'h'; continue; } ++aHandCard; /* Convert Aces to 1 */ while (playerPoints > 21 && aces > 0) { playerPoints -= 10; --aces; } /* Print the hand */ show_hand(playerHand, aHandCard, playerPoints); /* On a bust, dealer doesn't need to play. * This single "goto" simplifies the code. */ if (playerPoints > 21) { printf("Bust!\n"); goto done; } /* On Blackjack, let the dealer play */ if (playerPoints == 21) { printf("Blackjack!\n"); break; } /* Hit or stand? */ do { printf("Hit or stand? (h/s) : "); scanf("%s", buffer); } while (*buffer != 'h' && *buffer != 'H' && *buffer != 's' && *buffer != 'S'); } while (*buffer == 'h' || *buffer == 'H'); /* Dealer plays */ aHandCard = playerHand; aces = 0; printf("Dealer plays...\n"); do { /* Add the card's rank to dealer point */ if (aCard->rank == ACE) { dealerPoints += 11; ++aces; } else if (aCard->rank < JACK) dealerPoints += aCard->rank + 1; else dealerPoints += 10; /* Print the new card */ printf("New Card... %s of %s\n", rank_names[aCard->rank], suit_names[aCard->suit]); *aHandCard = aCard; ++aCard; assert(aCard != cards + NUMBER_CARDS); /* Let the second card be dealt */ if (aHandCard == playerHand) { ++aHandCard; continue; } ++aHandCard; /* Convert Aces to 1 */ while (dealerPoints > 21 && aces > 0) { dealerPoints -= 10; --aces; } /* Print the hand */ show_hand(playerHand, aHandCard, dealerPoints); if (dealerPoints > 21) { printf("Bust!\n"); break; } /* Blackjack */ if (dealerPoints == 21) { printf("Blackjack!\n"); break; } printf("Hit or stand? (h/s) : "); /* Dealer must hit on soft 17 */ if (dealerPoints < 17 || (dealerPoints == 17 && aces)) { printf("hit\n"); continue; } printf("stand\n"); break; } while (1); done: /* Keep track of win percentage for the player. */ if (playerPoints <= 21 && (playerPoints > dealerPoints || dealerPoints > 21)) { ++gamesWon; printf("Player wins!\n"); } else if (playerPoints <= 21 && playerPoints == dealerPoints) printf("Tie!\n"); else printf("Dealer wins!\n"); ++gamesPlayed; printf("Win percentage - %u%%\n", (gamesWon * 100) / gamesPlayed); /* Ask to play again. */ do { printf("Play again? (y/n) : "); scanf("%s", buffer); } while (*buffer != 'n' && *buffer != 'N' && *buffer != 'y' && *buffer != 'Y'); } while (*buffer == 'y' || *buffer == 'Y'); return 0; }