122 lines
2.0 KiB
C++
122 lines
2.0 KiB
C++
|
#ifndef BLACKJACK_HPP_
|
||
|
#define BLACKJACK_HPP_
|
||
|
|
||
|
#include <iostream>
|
||
|
#include <vector>
|
||
|
|
||
|
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};
|
||
|
|
||
|
Card(rank r = ACE, suit s, bool ifu = true): m_Rank(r), m_Suit(s), m_IsFaceUp(ifu) { }
|
||
|
|
||
|
int GetValue() const
|
||
|
{
|
||
|
int value = 0;
|
||
|
if (m_IsFaceUp)
|
||
|
{
|
||
|
value = m_Rank;
|
||
|
if (value > 10)
|
||
|
value = 10;
|
||
|
}
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
void Flip()
|
||
|
{
|
||
|
m_IsFaceUp = !m_IsFaceUp;
|
||
|
}
|
||
|
|
||
|
friend std::ostream& operator<<(std::ostream& s, const Card& aCard);
|
||
|
|
||
|
private:
|
||
|
rank m_Rank;
|
||
|
suit m_Suit;
|
||
|
bool m_IsFaceUp;
|
||
|
};
|
||
|
|
||
|
class Hand
|
||
|
{
|
||
|
public:
|
||
|
Hand()
|
||
|
{
|
||
|
m_Cards.reserve(7);
|
||
|
}
|
||
|
|
||
|
virtual ~Hand()
|
||
|
{
|
||
|
Clear();
|
||
|
}
|
||
|
|
||
|
void Add(Card* pCard)
|
||
|
{
|
||
|
m_Cards.push_back(pCard);
|
||
|
}
|
||
|
|
||
|
void Clear()
|
||
|
{
|
||
|
std::vector<Card*>::iterator iter = m_Cards.begin();
|
||
|
for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
|
||
|
{
|
||
|
delete *iter;
|
||
|
*iter = nullptr;
|
||
|
}
|
||
|
|
||
|
m_Cards.clear();
|
||
|
}
|
||
|
|
||
|
int GetTotal() const
|
||
|
{
|
||
|
if (m_Cards.empty() || m_Cards[0]->GetValue() == 0)
|
||
|
return 0;
|
||
|
|
||
|
int total = 0;
|
||
|
std::vector<Card*>::const_iterator iter;
|
||
|
|
||
|
for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
|
||
|
total += (*iter)->GetValue();
|
||
|
|
||
|
bool containsAce = false;
|
||
|
for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
|
||
|
if ((*iter)->GetValue() == Card::ACE)
|
||
|
containsAce = true;
|
||
|
|
||
|
if (containsAce && total <= 11) total += 10;
|
||
|
|
||
|
return total;
|
||
|
}
|
||
|
|
||
|
protected:
|
||
|
std::vector<Card*> m_Cards;
|
||
|
};
|
||
|
|
||
|
class GenericPlayer : public Hand
|
||
|
{
|
||
|
private:
|
||
|
friend ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);
|
||
|
protected:
|
||
|
string m_Name;
|
||
|
public:
|
||
|
GenericPlayer(const string& name = "") : m_Name(name) { }
|
||
|
|
||
|
virtual ~GenericPlayer();
|
||
|
|
||
|
virtual bool IsHitting() const = 0;
|
||
|
|
||
|
bool IsBusted() const
|
||
|
{
|
||
|
return (GetTotal() > 21);
|
||
|
}
|
||
|
|
||
|
void Bust() const
|
||
|
{
|
||
|
cout << m_Name << " перебор.\n";
|
||
|
}
|
||
|
};
|
||
|
|
||
|
#endif
|