Skip to main content
correct out-of-money error
Source Link
Justin Chang
  • 2.4k
  • 3
  • 14
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>

using namespace std;

// type input and press enter
stringstream getline_stream() {
    string user_input;
    getline(cin, user_input);
    return stringstream(user_input);
}

// asks the user to enter a bet
int get_bet(int money) {
    while (true) {
        cout << "\nYou have " << money << " dollars.\n";
        cout << "Please enter your bet (integer >= 0).\n";

        int bet;
        getline_stream() >> bet;

        if (bet > money) {
            cout << "You do not have enough money!\n";
            continue;
        }

        if (bet < 0) {
            cout << "You cannot bet a negative amount!\n";
            continue;
        }

        return bet;
    }
}

// returns your money after playing Guess The Number
int guess_the_number(int money) {
    cout << "\nYou are playing Guess The Number!\n";
    cout << "If you win, you receive double your bet!\n";
    int answer = (rand() % 10) + 1;

    // get a valid bet
    int bet = get_bet(money);
    money -= bet;

    // get the guess
    cout << "\nPlease type your guess and press enter.\n";
    cout << "The answer is an integer from 1 to 10 inclusive.\n";
    int guess;
    getline_stream() >> guess;

    cout << "\nThe correct answer was " << answer << ".\n";
    if (guess == answer) {
        cout << "Congratulations, you win!\n";
        return money + 2 * bet;
    }
    else {
        cout << "Sorry, you lose!\n";
        return money;
    }
}

int main()
{
    // initialize the random number generator
    srand(static_cast<unsigned int>(time(0)));

    // welcome message
    cout << "Hello! Welcome to my casino.\n";
    int money = 100;

    // dialogue loop
    while (true) {
        cout << "\nYou have " << money << " dollars.\n";
        cout << ">> Quit: type 'q' and press enter.\n";
        cout << ">> Reset: type 'r' and press enter.\n";
 
        if (money > 0)
        {
            cout << ">> Guess The Number: type '1' and press enter.\n";
            cout << ">> Blackjack: type '2' and press enter.\n";
        }
        else
        {
            cout << "You do not have enough money to play any games.\n";
        }

        char choice;
        getline_stream() >> choice;

        switch (choice) {
        case 'q':
            return 0;
        case 'r':
            money = 100;
            break;
        case '1':
            money = guess_the_number(money);
            break;
        case '2':
            // money = blackjack(money);
            break;
        default:
            cout << "\nYour input could not be parsed.\n";
        }
    }
}
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>

using namespace std;

// type input and press enter
stringstream getline_stream() {
    string user_input;
    getline(cin, user_input);
    return stringstream(user_input);
}

// asks the user to enter a bet
int get_bet(int money) {
    while (true) {
        cout << "\nYou have " << money << " dollars.\n";
        cout << "Please enter your bet (integer >= 0).\n";

        int bet;
        getline_stream() >> bet;

        if (bet > money) {
            cout << "You do not have enough money!\n";
            continue;
        }

        if (bet < 0) {
            cout << "You cannot bet a negative amount!\n";
            continue;
        }

        return bet;
    }
}

// returns your money after playing Guess The Number
int guess_the_number(int money) {
    cout << "\nYou are playing Guess The Number!\n";
    cout << "If you win, you receive double your bet!\n";
    int answer = (rand() % 10) + 1;

    // get a valid bet
    int bet = get_bet(money);
    money -= bet;

    // get the guess
    cout << "\nPlease type your guess and press enter.\n";
    cout << "The answer is an integer from 1 to 10 inclusive.\n";
    int guess;
    getline_stream() >> guess;

    cout << "\nThe correct answer was " << answer << ".\n";
    if (guess == answer) {
        cout << "Congratulations, you win!\n";
        return money + 2 * bet;
    }
    else {
        cout << "Sorry, you lose!\n";
        return money;
    }
}

int main()
{
    // initialize the random number generator
    srand(static_cast<unsigned int>(time(0)));

    // welcome message
    cout << "Hello! Welcome to my casino.\n";
    int money = 100;

    // dialogue loop
    while (true) {
        cout << "\nYou have " << money << " dollars.\n";
        cout << ">> Quit: type 'q' and press enter.\n";
        cout << ">> Reset: type 'r' and press enter.\n";
 
        if (money > 0)
        {
            cout << ">> Guess The Number: type '1' and press enter.\n";
            cout << ">> Blackjack: type '2' and press enter.\n";
        }
        else
        {
            cout << "You do not have enough money to play any games.\n";
        }

        char choice;
        getline_stream() >> choice;

        switch (choice) {
        case 'q':
            return 0;
        case 'r':
            money = 100;
            break;
        case '1':
            money = guess_the_number(money);
            break;
        case '2':
            // money = blackjack(money);
            break;
        default:
            cout << "\nYour input could not be parsed.\n";
        }
    }
}
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>

using namespace std;

// type input and press enter
stringstream getline_stream() {
    string user_input;
    getline(cin, user_input);
    return stringstream(user_input);
}

// asks the user to enter a bet
int get_bet(int money) {
    while (true) {
        cout << "\nYou have " << money << " dollars.\n";
        cout << "Please enter your bet (integer >= 0).\n";

        int bet;
        getline_stream() >> bet;

        if (bet > money) {
            cout << "You do not have enough money!\n";
            continue;
        }

        if (bet < 0) {
            cout << "You cannot bet a negative amount!\n";
            continue;
        }

        return bet;
    }
}

// returns your money after playing Guess The Number
int guess_the_number(int money) {
    cout << "\nYou are playing Guess The Number!\n";
    cout << "If you win, you receive double your bet!\n";
    int answer = (rand() % 10) + 1;

    // get a valid bet
    int bet = get_bet(money);
    money -= bet;

    // get the guess
    cout << "\nPlease type your guess and press enter.\n";
    cout << "The answer is an integer from 1 to 10 inclusive.\n";
    int guess;
    getline_stream() >> guess;

    cout << "\nThe correct answer was " << answer << ".\n";
    if (guess == answer) {
        cout << "Congratulations, you win!\n";
        return money + 2 * bet;
    }
    else {
        cout << "Sorry, you lose!\n";
        return money;
    }
}

int main()
{
    // initialize the random number generator
    srand(static_cast<unsigned int>(time(0)));

    // welcome message
    cout << "Hello! Welcome to my casino.\n";
    int money = 100;

    // dialogue loop
    while (true) {
        cout << "\nYou have " << money << " dollars.\n";
        cout << ">> Quit: type 'q' and press enter.\n";
        cout << ">> Reset: type 'r' and press enter.\n";
        cout << ">> Guess The Number: type '1' and press enter.\n";
        cout << ">> Blackjack: type '2' and press enter.\n";

        char choice;
        getline_stream() >> choice;

        switch (choice) {
        case 'q':
            return 0;
        case 'r':
            money = 100;
            break;
        case '1':
            money = guess_the_number(money);
            break;
        case '2':
            money = blackjack(money);
            break;
        default:
            cout << "\nYour input could not be parsed.\n";
        }
    }
}
better performance
Source Link
Justin Chang
  • 2.4k
  • 3
  • 14
// 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 *other, int *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_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_other, &d_aces, d_index);
   
  int d_score =// best_with_bust(d_other,player's d_aces);
draws
    // player'sint drawsp_score;
    while (true)
    {
        p_index = get_index();
        cout << "\nYou draw " << cards[p_index] << ".\n";
        update_state(&p_other, &p_aces, p_index);
        p_score = best_with_bust(p_other, p_aces);

        // bust
        if (p_score > 21) {
            cout << "Sorry, you lose by bust!\n";
            return money;
        }

        // natural 21
        if (p_score == 21)
            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 << "\nYou stand with " << p_score << " points.\n";

    // dealer's draws
    int d_score;
    while (true) {
        d_index = get_index();
        cout << "The dealer draws " << cards[d_index] << ".\n";
        update_state(&d_other, &d_aces, d_index);
        d_score = best_with_bust(d_other, d_aces);

        // bust
        if (d_score > 21) {
            cout << "\nCongratulations, you win by dealer bust!\n";
            return money + 2 * bet;
        }

        // hit or stand
        if (d_score > 16)
            break;
    }

    cout << "\nThe dealer stands with " << d_score << " points.\n";

    // win by comparison
    if (p_score > d_score) {
        cout << "Congratulations, you win!\n";
        return money + 2 * bet;
    }

    // tie by comparison
    if (p_score == d_score) {
        cout << "It's a tie!\n";
        return money + bet;
    }

    // lose by comparison
    cout << "Sorry, you lose!\n";
    return money;
}
// 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 *other, int *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_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_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_other, &p_aces, p_index);
        p_score = best_with_bust(p_other, p_aces);

        // bust
        if (p_score > 21) {
            cout << "Sorry, you lose by bust!\n";
            return money;
        }

        // natural 21
        if (p_score == 21)
            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 << "\nYou stand with " << p_score << " points.\n";

    // dealer's draws
    while (true) {
        d_index = get_index();
        cout << "The dealer draws " << cards[d_index] << ".\n";
        update_state(&d_other, &d_aces, d_index);
        d_score = best_with_bust(d_other, d_aces);

        // bust
        if (d_score > 21) {
            cout << "\nCongratulations, you win by dealer bust!\n";
            return money + 2 * bet;
        }

        // hit or stand
        if (d_score > 16)
            break;
    }

    cout << "\nThe dealer stands with " << d_score << " points.\n";

    // win by comparison
    if (p_score > d_score) {
        cout << "Congratulations, you win!\n";
        return money + 2 * bet;
    }

    // tie by comparison
    if (p_score == d_score) {
        cout << "It's a tie!\n";
        return money + bet;
    }

    // lose by comparison
    cout << "Sorry, you lose!\n";
    return money;
}
// 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 *other, int *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_other, &p_aces, p_index);

    // dealer's first draw
    int d_index = get_index();
    cout << "The dealer draws " << cards[d_index] << ".\n";
    update_state(&d_other, &d_aces, d_index);
 
    // player's draws
    int p_score;
    while (true)
    {
        p_index = get_index();
        cout << "\nYou draw " << cards[p_index] << ".\n";
        update_state(&p_other, &p_aces, p_index);
        p_score = best_with_bust(p_other, p_aces);

        // bust
        if (p_score > 21) {
            cout << "Sorry, you lose by bust!\n";
            return money;
        }

        // natural 21
        if (p_score == 21)
            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 << "\nYou stand with " << p_score << " points.\n";

    // dealer's draws
    int d_score;
    while (true) {
        d_index = get_index();
        cout << "The dealer draws " << cards[d_index] << ".\n";
        update_state(&d_other, &d_aces, d_index);
        d_score = best_with_bust(d_other, d_aces);

        // bust
        if (d_score > 21) {
            cout << "\nCongratulations, you win by dealer bust!\n";
            return money + 2 * bet;
        }

        // hit or stand
        if (d_score > 16)
            break;
    }

    cout << "\nThe dealer stands with " << d_score << " points.\n";

    // win by comparison
    if (p_score > d_score) {
        cout << "Congratulations, you win!\n";
        return money + 2 * bet;
    }

    // tie by comparison
    if (p_score == d_score) {
        cout << "It's a tie!\n";
        return money + bet;
    }

    // lose by comparison
    cout << "Sorry, you lose!\n";
    return money;
}
clean up a space
Source Link
Justin Chang
  • 2.4k
  • 3
  • 14
// 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;
}
// 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, int *other, int index) {
    if (index < 9) // 2 to 10
        (*other) += index + 2;
    else if (index < 12) // J, Q, K
        (*other) += 10;
    else // A
        (*aces) += 1;
}

// 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_index);

    // dealer's first draw
    int d_index = get_index();
    cout << "The dealer draws " << cards[d_index] << ".\n";
    update_state(&d_aces, &d_other, d_index);

    // player's draws
    while (true)
    {
        p_index = get_index();
        cout << "\nYou draw " << cards[p_index] << ".\n";
        update_state(&p_aces, &p_other, p_index);
        int p_min_score = p_aces + p_other;

        // bust
        if (p_min_score > 21) {
            cout << "Sorry, you lose by bust!\n";
            return money;
        }

        // win by natural
        for (int big = 0; big <= p_aces; ++big)
        {
            if (p_min_score + 10 * big == 21) {
                cout << "Congratulations, you win by natural!\n";
                return money + 2 * bet;
            }
        }

        // 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;
    }

    // dealer's draws
    cout << "\n";
    int d_total = 0;
    while (true) {
        d_index = get_index();
        cout << "The dealer draws " << cards[d_index] << ".\n";
        update_state(&d_aces, &d_other, d_index);
        int d_min_score = d_aces + d_other;

        // bust
        if (d_min_score > 21) {
            cout << "\nCongratulations, you win by dealer bust!\n";
            return money + 2 * bet;
        }

        // check for stand
        for (int big = 0; big <= d_aces; ++big)
        {
            int points = d_min_score + 10 * big;
            if (points <= 21 && points > 16) {
                cout << "\nThe dealer stands with " << points << " 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_total > d_total) {
            cout << "Congratulations, you win by comparison!\n";
            return money + 2 * bet;
        }
    }

    // draw by comparison
    for (int big = 0; big <= p_aces; ++big)
    {
        int p_total = p_min_score + 10 * big;
        if (p_total <= 21 && p_total == d_total) {
            cout << "It's a tie!\n";
            return money + bet;
        }
    }

    // lose by comparison
    cout << "Sorry, you lose by comparison!\n";
    return money;
}
// 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 *other, int *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_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_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_other, &p_aces, p_index);
        p_score = best_with_bust(p_other, p_aces);

        // bust
        if (p_score > 21) {
            cout << "Sorry, you lose by bust!\n";
            return money;
        }

        // natural 21
        if (p_score == 21)
            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 << "\nYou stand with " << p_score << " points.\n";

    // dealer's draws
    while (true) {
        d_index = get_index();
        cout << "The dealer draws " << cards[d_index] << ".\n";
        update_state(&d_other, &d_aces, d_index);
        d_score = best_with_bust(d_other, d_aces);

        // bust
        if (d_score > 21) {
            cout << "\nCongratulations, you win by dealer bust!\n";
            return money + 2 * bet;
        }

        // hit or stand
        if (d_score > 16)
            break;
    }
 
    cout << "\nThe dealer stands with " << d_score << " points.\n";

    // win by comparison
    if (p_score > d_score) {
        cout << "Congratulations, you win!\n";
        return money + 2 * bet;
    }

    // tie by comparison
    if (p_score == d_score) {
        cout << "It's a tie!\n";
        return money + bet;
    }

    // lose by comparison
    cout << "Sorry, you lose!\n";
    return money;
}
break statement
Source Link
Justin Chang
  • 2.4k
  • 3
  • 14
Loading
added 198 characters in body
Source Link
Justin Chang
  • 2.4k
  • 3
  • 14
Loading
deleted 15 characters in body
Source Link
Justin Chang
  • 2.4k
  • 3
  • 14
Loading
edited body
Source Link
Justin Chang
  • 2.4k
  • 3
  • 14
Loading
blackjack
Source Link
Justin Chang
  • 2.4k
  • 3
  • 14
Loading
added 120 characters in body
Source Link
Justin Chang
  • 2.4k
  • 3
  • 14
Loading
Source Link
Justin Chang
  • 2.4k
  • 3
  • 14
Loading