< Previous | Contents | Next >
The Deck class represents a deck of cards. It’s derived from Hand.
class Deck : public Hand
{
Introducing the Blackjack Game 371
public:
Deck();
virtual ~Deck();
//create a standard deck of 52 cards void Populate();
//shuffle cards void Shuffle();
//deal one card to a hand void Deal(Hand& aHand);
//give additional cards to a generic player
void AdditionalCards(GenericPlayer& aGenericPlayer);
};
Deck::Deck()
{
m_Cards.reserve(52); Populate();
}
Deck::~Deck()
{}
void Deck::Populate()
{
Clear();
//create standard deck
for (int s = Card::CLUBS; s <= Card::SPADES; ++s)
{
for (int r = Card::ACE; r <= Card::KING; ++r)
{
Add(new Card(static_cast<Card::rank>(r),
static_cast<Card::suit>(s)));
}
}
}
void Deck::Shuffle()
372 Chapter 10 n Inheritance and Polymorphism: Blackjack
{
random_shuffle(m_Cards.begin(), m_Cards.end());
}
void Deck::Deal(Hand& aHand)
{
if (!m_Cards.empty())
{
aHand.Add(m_Cards.back()); m_Cards.pop_back();
}
else
{
cout << "Out of cards. Unable to deal.";
}
}
void Deck::AdditionalCards(GenericPlayer& aGenericPlayer)
{
cout << endl;
//continue to deal a card as long as generic player isn’t busted and
//wants another hit
while ( !(aGenericPlayer.IsBusted()) && aGenericPlayer.IsHitting() )
{
Deal(aGenericPlayer);
cout << aGenericPlayer << endl;
if (aGenericPlayer.IsBusted())
{
aGenericPlayer.Bust();
}
}
}
Hi n t
Type casting is a way of converting a value of one type to a value of another type. One way to do type casting is to use static_cast. You use static_cast to return a value of a new type from a value of another type by specifying the new type you want between < and >, followed by the value from which you want to get a new value between parentheses. Here’s an example that returns the double value 5.0.
static_cast<double>(5);
Introducing the Blackjack Game 373
Populate() creates a standard deck of 52 cards. The member function loops through all of the possible combinations of Card::suit and Card::rank values. It uses static_cast to cast the int loop variables to the proper enumerated types defined in Card.
Shuffle() shuffles the cards in the deck. It randomly rearranges the pointers in m_Cards with random_shuffle() from the Standard Template Library. This is the reason I include the <algorithm> header file.
Deal() deals one card from the deck to a hand. It adds a copy of the pointer to the back of m_Cards to the object through the object’s Add() member function. Then, it removes the pointer at the back of m_Cards, effectively transferring the card. The powerful thing about Deal() is that it accepts a reference to a Hand object, which means it can work equally well with a Player or a House object. And through the magic of polymorphism, Deal() can call the object’s Add() member function without knowing the exact object type.
AdditionalCards() gives additional cards to a generic player until the generic player either stops hitting or busts. The member function accepts reference to a GenericPlayer object so you can pass a Player or House object to it. Again, through the magic of polymorphism, AdditionalCards() doesn’t have to know whether it’s working with a Player or a House object. It can call the IsBusted() and IsHitting() member functions for the object without knowing the object’s type, and the correct code will be executed.