// text for each card
const char cards[13][3] = {
"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
// gets a card index
int get_index() {
return rand() % 13;
}
// updates number of aces, number of other points
void update_state(int *aces*other, int *other*aces, int index) {
if (index < 9) // 2 to 10
(*other) += index + 2;
else if (index < 12) // J, Q, K
(*other) += 10;
else // A
(*aces) += 1;
}
// gets best score, avoid bust if able
int best_with_bust(int other, int aces) {
int score = other + 11 * aces;
for (int i = 0; i < aces; ++i)
{
if (score <= 21)
return score;
score -= 10;
}
return score;
}
// returns your money after playing Blackjack
int blackjack(int money) {
cout << "\nYou are playing Blackjack!\n";
cout << "If you win, you receive double your bet!\n";
// get a valid bet
int bet = get_bet(money);
money -= bet;
// game state
int p_other = 0;
int p_aces = 0;
int d_other = 0;
int d_aces = 0;
// player's first draw
int p_index = get_index();
cout << "\nYou draw " << cards[p_index] << ".\n";
update_state(&p_aces&p_other, &p_other&p_aces, p_index);
int p_score = best_with_bust(p_other, p_aces);
// dealer's first draw
int d_index = get_index();
cout << "The dealer draws " << cards[d_index] << ".\n";
update_state(&d_aces&d_other, &d_other&d_aces, d_index);
int d_score = best_with_bust(d_other, d_aces);
// player's draws
while (true)
{
p_index = get_index();
cout << "\nYou draw " << cards[p_index] << ".\n";
update_state(&p_aces&p_other, &p_other&p_aces, p_index);
int p_min_scorep_score = p_aces +best_with_bust(p_other, p_other;p_aces);
// bust
if (p_min_scorep_score > 21) {
cout << "Sorry, you lose by bust!\n";
return money;
}
// win by natural
for (int big = 0; big <= p_aces; ++big)
{21
if (p_min_score + 10 * bigp_score == 21) {
cout << "Congratulations, you win by natural!\n";
return money + 2 * bet;
}
}break;
// hit or stand
cout << ">> Hit: type 'h' and press enter.\n";
cout << ">> Stand: type 's' and press enter.\n";
char hit_or_stand;
getline_stream() >> hit_or_stand;
if (hit_or_stand == 's')
break;
}
//cout dealer's<< draws
"\nYou stand with " cout<< p_score << "\n";
" points.\n";
int d_total// =dealer's 0;draws
while (true) {
d_index = get_index();
cout << "The dealer draws " << cards[d_index] << ".\n";
update_state(&d_aces&d_other, &d_other&d_aces, d_index);
int d_min_scored_score = d_aces +best_with_bust(d_other, d_other;d_aces);
// bust
if (d_min_scored_score > 21) {
cout << "\nCongratulations, you win by dealer bust!\n";
return money + 2 * bet;
}
// checkhit foror stand
forif (int big = 0; big <=d_score d_aces;> ++big16)
{
int points = d_min_score + 10 * big;break;
if (points <= 21 && points > 16) {}
cout << "\nThe dealer stands with " << pointsd_score << " points.\n";
d_total = points;
break;
}
}
if (d_total > 0)
break;
}
// win by comparison
int p_min_score = p_aces + p_other;
for (int big = 0; big <= p_aces; ++big)
{
int p_total = p_min_score + 10 * big;
if (p_total <= 21 && p_totalp_score > d_totald_score) {
cout << "Congratulations, you win by comparison!\n";
return money + 2 * bet;
}
}
// drawtie by comparison
for (int big = 0; big <= p_aces; ++big)
{
int p_total = p_min_score + 10 * big;
if (p_total <= 21 && p_totalp_score == d_totald_score) {
cout << "It's a tie!\n";
return money + bet;
}
}
// lose by comparison
cout << "Sorry, you lose by comparison!\n";
return money;
}