< Previous | Contents | Next >
The Game class represents a game of Blackjack.
class Game
{
public:
Game(const vector<string>& names);
~Game();
//plays the game of blackjack void Play();
374 Chapter 10 n Inheritance and Polymorphism: Blackjack
private:
Deck m_Deck; House m_House;
vector<Player> m_Players;
};
Game::Game(const vector<string>& names)
{
//create a vector of players from a vector of names vector<string>::const_iterator pName;
for (pName = names.begin(); pName != names.end(); ++pName)
{
m_Players.push_back(Player(*pName));
}
//seed the random number generator srand(static_cast<unsigned int>(time(0))); m_Deck.Populate();
m_Deck.Shuffle();
}
Game::~Game()
{}
void Game::Play()
{
//deal initial 2 cards to everyone vector<Player>::iterator pPlayer; for (int i = 0; i < 2; ++i)
{
for (pPlayer = m_Players.begin(); pPlayer != m_Players.end();
++pPlayer)
{
m_Deck.Deal(*pPlayer);
}
m_Deck.Deal(m_House);
}
//hide house’s first card m_House.FlipFirstCard();
Introducing the Blackjack Game 375
//display everyone’s hand
for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
{
cout << *pPlayer << endl;
}
cout << m_House << endl;
//deal additional cards to players
for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
{
m_Deck.AdditionalCards(*pPlayer);
}
//reveal house’s first card m_House.FlipFirstCard(); cout << endl << m_House;
//deal additional cards to house m_Deck.AdditionalCards(m_House);
if (m_House.IsBusted())
{
//everyone still playing wins
for (pPlayer = m_Players.begin(); pPlayer != m_Players.end();
++pPlayer)
{
if ( !(pPlayer->IsBusted()) )
{
pPlayer->Win();
}
}
}
else
{
//compare each player still playing to house
for (pPlayer = m_Players.begin(); pPlayer != m_Players.end();
++pPlayer)
{
if ( !(pPlayer->IsBusted()) )
{
if (pPlayer->GetTotal() > m_House.GetTotal())
376 Chapter 10 n Inheritance and Polymorphism: Blackjack
{
pPlayer->Win();
}
else if (pPlayer->GetTotal() < m_House.GetTotal())
{
}
else
{
}
}
}
pPlayer->Lose();
pPlayer->Push();
//remove everyone’s cards
for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
{
pPlayer->Clear();
}
m_House.Clear();
}
The class constructor accepts a reference to a vector of string objects, which represent the names of the human players. The constructor instantiates a Player object with each name. Next, it seeds the random number generator, and then it populates and shuffles the deck.
The Play() member function faithfully implements the pseudocode I wrote earlier about how a round of play should be implemented.