简体   繁体   中英

C++ BlackJack Stuck trying to program Ace

I have to program a simple blackjack game for my intro to C++ class and the way the teacher wants us to build the deck has me confused with how I am supposed to program the Ace to automatically choose whether or not to be a value of 11 or 1.

From reading others possible solutions, there have been conflicting ideas to first set the value of the ace to be 11 and subtract 10 if you bust (How my professor would prefer), but most I see say to set the value to 1 and add 10 if needed.

below is how he wanted us to setup our card and deck classes:

#include <iostream>
#include <string>
#include <vector>
#include <ctime>

using namespace std;

class card
{
string rank;
string suit;
int value;

public:
string get_rank()
{
    return rank;
}
string get_suit()
{
    return suit;
}
int get_value()
{
    return value;
}

//Contstructor
card(string rk = " ", string st = " ", int val = 0)
{
    rank = rk;
    suit = st;
    value = val;
}


//print function
void print()
{
    cout << rank << " of " << suit << endl;
}
};

class deck
{
card a_deck[52];
int top;

public:
card deal_card()
{
    return a_deck[top++];
}

//Deck constructor
deck()
{
    top = 0;
    string ranks[13] = { "Ace", "King", "Queen", "Jack", "Ten", "Nine", 
"Eight", "Seven", "Six", "Five", "Four", "Three", "Two" };
    string suits[4] = { "Spades", "Diamonds", "Hearts", "Clubs" };
    int values[13] = { 11,10,10,10,10,9,8,7,6,5,4,3,2 };

    for (int i = 0; i < 52; i++)
        a_deck[i] = card(ranks[i % 13], suits[i % 4], values[i % 13]);

    srand(static_cast<size_t> (time(nullptr)));
}

void shuffle()
{
    for (int i = 0; i < 52; i++)
    {
        int j = rand() % 52;
        card temp = a_deck[i];
        a_deck[i] = a_deck[j];
        a_deck[j] = temp;

    }
}
};

Then he had us create a player class in which we build the hand into a vector

class player
{
string name;
vector<card>hand;
double cash;
double bet;

public:

//Constructor
player(double the_cash)
{
    cash = the_cash;
    bet = 0;
}

void hit(card c)
{
    hand.push_back(c);
}


int total_hand()
{
    int count = 0;
    for (int i = 0; i < hand.size(); i++)
    {
        card c = hand[i];
        count = count + c.get_value();
    }
    return count;

}
};

How would I go about coding the Ace? he just barely went over creating a "friend" last class. should I create a friend function within deck that changes the value of Ace in the array to one? My brain hurts, apologies if I'm not making sense.

You can implement it both ways.

To add 10 later, you can change your total_hand() function to become:

int total_hand
{
    int count = 0;
    int num_of_aces = 0;
    for (int i = 0; i < hand.size(); i++)
    {
        card c = hand[i];
        if (c.get_value() == 11){
            num_of_aces += 1;
            count += 1;
        }
        else
            count = count + c.get_value();
    }
    for (int i = 0; i < num_of_aces; i++){
        if (count + 10 <= 21)
            count += 10;
    return count;
}

To subtract 10 later (as your intuition suggests), you can do the following:

int total_hand
{
    int count = 0;
    int num_of_aces = 0;
    for (int i = 0; i < hand.size(); i++)
    {
        card c = hand[i];
        if (c.get_value() == 11)
            num_of_aces += 1;

        count = count + c.get_value();
    }

    while(count > 21 && num_of_aces--)
        count -=10;

    return count;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM