-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.h
121 lines (110 loc) · 2.72 KB
/
blackjack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef BLACKJACK_H
#define BLACKJACK_H
#include <QString>
#include <array>
////////////////////////////////////////////////////
// Card
class Card{
public:
enum CardSuit{
SUIT_CLUB,
SUIT_DIAMOND,
SUIT_HEART,
SUIT_SPADE,
MAX_SUITS
};
enum CardRank{
RANK_2,
RANK_3,
RANK_4,
RANK_5,
RANK_6,
RANK_7,
RANK_8,
RANK_9,
RANK_10,
RANK_JACK,
RANK_QUEEN,
RANK_KING,
RANK_ACE,
MAX_RANKS
};
private:
CardSuit m_suit;
CardRank m_rank;
public:
Card(CardSuit suit = CardSuit::MAX_SUITS, CardRank rank = CardRank::MAX_RANKS);
const QString toString() const;
template<typename TPrintHelper>
void print() const{
TPrintHelper printfunc;
printfunc("card: "+toString());
}
std::array<int, 2> getValue() const;
};
Card::CardSuit& operator++(Card::CardSuit& suit);
Card::CardSuit operator++(Card::CardSuit& suit, int);
Card::CardRank& operator++(Card::CardRank& rank);
Card::CardRank operator++(Card::CardRank& rank, int);
////////////////////////////////////////////////////
// Deck
class Deck
{
std::array<Card, 52> m_deck;
Card* m_cardPtr;
public:
Deck();
void resetDeck();
void SwapCard(size_t index1, size_t index2);
void ShuffleDeck();
template<typename TPrintHelper>
void print() const{
TPrintHelper printfunc;
QString s{};
for(size_t index=0; index<m_deck.size();++index){
s += m_deck[index].toString()+" ";
if((index+1)%4 == 0)
s += "\n";
}
printfunc(s);
}
template<typename TPrintHelper>
void printTopCard() const{
m_cardPtr->print<TPrintHelper>();
}
const Card* dealCard();
template<typename TPrintHelper>
const Card* dealCard(){
if(m_cardPtr==m_deck.end())
return nullptr;
printTopCard<TPrintHelper>();
return m_cardPtr++;
}
};
////////////////////////////////////////////////////
// Player
class Player
{
QString m_name;
std::array<int, 2> m_cardValues;
public:
Player(const QString& name);
void resetValues();
int getMaxValue() const;
int getMinValue() const;
bool isLoss() const;
bool isIWin(const Player& anotherPlayer) const;
bool isDealerEnough() const;
const std::array<int, 2>& addCard(const Card& card);
template<typename TPrintHelper>
void printValues() const{
TPrintHelper printfunc;
QString s = "player "+m_name+"has ";
for(auto i : m_cardValues){
if(i!=0)
s += QString::number(i)+" ";
}
printfunc(s);
}
};
#endif // BLACKJACK_H