//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');
}
Lilgenski16 0 Newbie Poster
Lilgenski16 0 Newbie Poster
NathanOliver 429 Veteran Poster Featured Poster
Lilgenski16 0 Newbie Poster
NathanOliver 429 Veteran Poster Featured Poster
Lilgenski16 0 Newbie 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.