//BlackJack
// Plays a simple version of the casino style game of blackjack; 1 - 7 players
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
class Card
{
public:
enum rank {ACE = 1, TWO , THREE , FOUR , FIVE , SIX , SEVEN , EIGHT , NINE , TEN , JACK , QUEEN , KING};
enum suit {CLUBS , DIAMONDS , HEARTS , SPADES};
//overloading <<operator so can send the Card object to standart output friend ostream& os, const Card& aCard);
Card(rank r= ACE, suit s = SPADES , bool ifu = true);
//returns the value of a card , 1 - 11
int GetValue() const;
//flips a card; if face up, becomdes face down and vice versa
void Flip();
private:
rank m_Rank;
suit m_Suit;
bool m_IsFaceup;
};
Card::Card(rank r, suit s, bool ifu): m_Rank(r) , m_Suit(s) , m_IsFaceup(ifu)
{}
int Card::GetValue() const
{
//if a cards is face down, its value is 0
int value = 0;
if (m_IsFaceup)
{
//value is number showing on card
value = m_Rank;
//value is 10 for face cards
if (value > 10)
{
value = 10;
}
}
return value;
}
void Card::Flip()
{
m_IsFaceup = ! (m_IsFaceup);
}
class Hand
{
public:
Hand();
virtual ~Hand();
//adds a card to the hand
void Add(Card*pCard);
//clears hand of all cards
void Clear();
//gets hand total value, intelligently treats aces as 1 or 11
int GetTotal() const;
protected:
vector<Card*> m_Cards;
};
Hand::Hand()
{
m_Cards.reserve(7);
}
Hand::~Hand()
{
Clear();
}
void Hand::Add(Card*pCard)
{
m_Cards.push_back(pCard);
}
void Hand::Clear()
{
//iterate through vector , freeing all memory on the heap
vector<Card*>::iterator iter = m_Cards.begin();
for (iter = m_Cards.begin(); iter !=m_Cards.end(); ++iter)
{
delete *iter;
*iter = 0;
}
//clear vector of pointers
m_Cards.clear();
}
int Hand::GetTotal() const
{
//if no cards in hand, return 0
if (m_Cards.empty())
{
return 0;
}
//if a first card has a value of 0, then card is face down; return 0
if (m_Cards[0]->GetValue() == 0)
{
return 0;
}
//add up card values , treast each ace as 1
int total = 0;
vector<Card*>::const_iterator iter;
for (iter = m_Cards.begin(); iter!=m_Cards.end(); ++iter)
{
total += (*iter) -> GetValue();
}
// determine if hand contains ace
bool containsAce = false;
for (iter = m_Cards.begin(); iter !=m_Cards.end(); ++iter)
{
if ((*iter)->GetValue() == Card::ACE)
{
containsAce = true;
}
}
// if hand contains ace and total is low enough , treat ace as 11
if (containsAce && total <=11)
{
//add only 10 since we've already added 1 for the ace
total += 10;
}
return total;
}
class GenericPLayer: public Hand
{
friend ostream& operator<<(ostream& os,
const GenericPLayer& aGenericPlayer);
public:
GenericPLayer(const string&name = "");
virtual ~GenericPLayer();
//indicates whether or not generic player wants to keep hitting
virtual bool IsHitting() const = 0;
//returns whether generic player has busted - has a total greater than 21
bool IsBusted() const;
//announces that the generic player busts
void Bust() const;
protected:
string m_Name;
};
GenericPLayer::GenericPLayer(const string& name):
m_Name(name)
{}
GenericPLayer::~GenericPLayer()
{}
bool GenericPLayer::IsBusted()const
{
return (GetTotal()>21);
}
void GenericPLayer::Bust() const
{
cout << m_Name << "busts.\n";
}
class Player : public GenericPLayer
{
public:
Player(const string& name= "");
virtual ~Player();
//returns whether of not the player wants another it
virtual bool IsHitting() const;
//annouces that the player wins
void Win() const;
//anouces that the player loses
void Lose() const;
//announces that the player pushes
void push() const;
};
Player::Player(const string& name):
GenericPLayer(name)
{}
Player::~Player()
{}
bool Player::IsHitting() const
{
cout << m_Name << ", do you want a hit? (Y/N): ";
char response;
cin >> response;
return (response == 'y' || response == 'Y');
}
void Player::Lose() const
{
cout << m_Name << "loses.\n";
}
void Player::push() const
{
cout << m_Name << " pushes.\n";
}
class House : public GenericPLayer
{
public:
House(const string& name = "House");
virtual ~House();
//indicates whether house is hitting - will always hit on 16 or less
virtual bool IsHitting() const;
//flips over first card
void FlipFirstCard();
};
House::House(const string& name):
GenericPLayer(name)
{}
House::~House()
{}
bool House::IsHitting() const
{
return (GetTotal() <=16);
}
void House::FlipFirstCard()
{
if (!(m_Cards.empty()))
{
m_Cards[0] ->Flip();
}
else
{
cout << "No card to flip!\n";
}
}
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);
//gives 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()
{
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 isnt busted and
//wants another hit
while (!(aGenericPlayer.IsBusted())&&aGenericPlayer.IsHitting())
{
Deal(aGenericPlayer);
cout << aGenericPlayer << endl;
if (aGenericPlayer.IsBusted())
{
aGenericPlayer.Bust();
}
}
}
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 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 intial 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 houses first card
m_house.FlipFirstCard();
//display everyones 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 houses 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())
{
pPlayer->Win();
}
else if (pPlayer->GetTotal()<m_House.GetTotal())
{
pPlayer->Lose();
}
else
{
pPlayer->push();
}
}
}
}
//remove everyones cards
for (pPlayer = m_Players.begin(); pPlayer !=m_Players.end();++pPlayer)
{
pPlayer->Clear();
}
m_House.Clear();
}
//function prototypes
ostream& operator<<(ostream& os, const Card& aCard);
ostream& operator<<(ostream& OS, const GenericPLayer& aGenericPlayer);
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')
{
aGame.Play();
cout << "\nDo you want to play again?(Y/N):";
cin >> again;
}
return 0;
}
*Im Almost Done, just a couple of errors,
/Users/Mark/Desktop/C++ Code/BlackJack/BlackJack/Black Jack 2/Black Jack 2/main.cpp:273:5: Expected '(' or '{'
/Users/Mark/Desktop/C++ Code/BlackJack/BlackJack/Black Jack 2/Black Jack 2/main.cpp:288:9: Use of undeclared identifier 'Populate'
/Users/Mark/Desktop/C++ Code/BlackJack/BlackJack/Black Jack 2/Black Jack 2/main.cpp:294:16: Out-of-line definition of 'Populate' does not match any declaration in 'Deck'
/Users/Mark/Desktop/C++ Code/BlackJack/BlackJack/Black Jack 2/Black Jack 2/main.cpp:390:9: Expected expression
/Users/Mark/Desktop/C++ Code/BlackJack/BlackJack/Black Jack 2/Black Jack 2/main.cpp:490:15: No member named 'Play' in 'Game'
/Users/Mark/Desktop/C++ Code/BlackJack/BlackJack/Black Jack 2/Black Jack 2/main.cpp:495:5: Void function 'play' should not return a value
*Im sure most of them are easy, I just cannot figure them out, Thanks for the help!
Lilgenski16
0
Newbie Poster
Recommended Answers
Jump to Post
virtual ~Deck():
Should that be a semi-colon?m_Deck.Deal(m_House); )
Should that final
)
be a}
?
m_house.FlipFirstCard();
Case matters.There is more. Fix the above, see if the new errors are helpful to you.
Jump to PostLine 270:
virtual ~Deck():
Notice anything wrong with that?
Jump to PostTwo people independently told you about the colon at the end of that line, and your reply (in a comment on Nathan's response, no less) is that because the compiler didn't put a big neon sign on it, it must be correct?
I am genuinely astounded that you didn't even …
Jump to PostYou know Windows -> Accessories -> Char map utility? have Arial font mappings for the following extended ASCII: U+2660 ♠ , U+2665 ♥, U+2663 ♣, U+2666 ♦
test if your DOS prompt can render it with a simple 1 liner:
std::cout << std::string("\u2663") << std::endl;Otherwise you might need PDCurses …
All 15 Replies
Moschops
683
Practically a Master Poster
Featured Poster
NathanOliver
429
Veteran Poster
Featured Poster
Lilgenski16
commented:
That isnt an error, so there is not anything wrong with it, the : is correct instead of the ;
+0
Moschops
683
Practically a Master Poster
Featured Poster
Maritimo
15
Junior Poster in Training
Lilgenski16
0
Newbie Poster
Maritimo
15
Junior Poster in Training
Maritimo
15
Junior Poster in Training
マーズ maazu
2
Light Poster
Maritimo
15
Junior Poster in Training
マーズ maazu
2
Light Poster
Lilgenski16
commented:
Unfortunatley, im not on a windows machine, OSX -__
+0
Maritimo
15
Junior Poster in Training
マーズ maazu
2
Light Poster
Maritimo
15
Junior Poster in Training
Maritimo
15
Junior Poster in Training
マーズ maazu
2
Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.