I am attempting to alter a Blackjack game so that the game throws an exception and repopulates the deck when their are less than 3 cards per player. I am pretty sure I am on the right track but after 3 hours of trial and error I am still unable top get it to compile. Any and all help is appreciated.
// This is the beginning Deck sub-class
class Deck : public Hand
{
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);
//checklevel function
void checklevel();
};
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()
{
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();
}
}
//Beginning of code that concerns the exceptions handling in question. checklevel() takes the number of players entered in the main() and multiplies it by the minimum number of cards(3). If it is greater than or equal to the number of cards left in the deck the game deals again, if not then it repopulates the deck.
void Deck::checklevel()
{
int numPlayers = 0;
const int minCards = 3;
int remaining = count(m_Cards.begin(), m_Cards.end(), !0);
if(numPlayers * minCards >= remaining)
{
Populate();
throw "Too few cards remaining in the deck. Repopulating the deck.";
}
}
//end of Deck class
class Game
{
public:
Game(const vector<string>& names);
~Game();
//plays the game of blackjack
void Play();
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));
srand(time(0)); //seed the random number generator
m_Deck.Populate();
m_Deck.Shuffle();
}
Game::~Game()
{}
void Game::Play()
{
//calling the checklevel() right before dealing to ensure that enough cards remain.
m_Deck.checklevel();
//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);
}
.
.
.
.
.
int numPlayers;
int main()
{
cout << "\t\tWelcome to Blackjack!\n\n";
//int numPlayers = 0;
while (numPlayers < 1 || numPlayers > 7)
{
cout << "How many players? (1 - 7): ";
cin >> numPlayers;
}
vector<string> names;
string name;
for (int i = 0; i < numPlayers; ++i)
{
cout << "Enter player name: ";
cin >> name;
names.push_back(name);
}
cout << endl;
//the game loop
Game aGame(names);
char again = 'y';
while (again != 'n' && again != 'N')
{
// beginning of the try, catch blocks. Play() is invoked and if there is not 3 cards for each player in the deck, the getlevel() throws an exception, which is caught and the game loop begins again.
try
{
aGame.Play();
}
catch(char * s)
{
cout << s << endl;
}
cout << "\nDo you want to play again? (Y/N): ";
cin >> again;
}
return 0;
I did my best to take out whatever was not relevant, but I included the whole code as an attachment just in case. Thank in advance.