-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand.h
More file actions
194 lines (181 loc) · 5.72 KB
/
hand.h
File metadata and controls
194 lines (181 loc) · 5.72 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "card.h"
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <iostream>
class Hand
{
public:
//input string hand format "AA BB CC DD EE FF GG"
Hand(std::string hand_string)
{
float hs = (hand_string.size()+1)/3.0;
int handsize = int(hs);
if(handsize != hs)
return;
for(int i=0; i<handsize; i++)
{
m_cards.push_back(Card(hand_string.substr(i*3,2)));
}
std::sort(m_cards.begin(),m_cards.end());
}
protected:
std::vector<Card> m_cards;
};
std::string PokerHandTypes[10] = {
"HIGH_CARD", // 0
"PAIR", // 1
"TWO_PAIRS", // 2
"THREE_KIND", // 3
"STRAIT", // 4
"FLUSH", // 5
"FULL_HOUSE", // 6
"FOUR_KIND", // 7
"STRAIT_FLUSH", // 8
"ROYAL_FLUSH" // 9
};
enum PokerHandType
{
HIGH_CARD, // 0
PAIR, // 1
TWO_PAIRS, // 2
THREE_KIND, // 3
STRAIT, // 4
FLUSH, // 5
FULL_HOUSE, // 6
FOUR_KIND, // 7
STRAIT_FLUSH, // 8
ROYAL_FLUSH // 9
};
class PokerHand : Hand
{
// High Card: Highest value card.
// One Pair: Two cards of the same value.
// Two Pairs: Two different pairs.
// Three of a Kind: Three cards of the same value.
// Straight: All cards are consecutive values.
// Flush: All cards of the same suit.
// Full House: Three of a kind and a pair.
// Four of a Kind: Four cards of the same value.
// Straight Flush: All cards are consecutive values of same suit.
// Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
public:
PokerHand(std::string hand_string)
: Hand(hand_string)
{
assign_type();
}
void assign_type()
{
m_hand_type = HIGH_CARD;
m_rank = m_cards[4].rank();
bool strait = false;
bool flush = false;
if (m_cards[0].rank() + 1 == m_cards[1].rank() &&
m_cards[0].rank() + 2 == m_cards[2].rank() &&
m_cards[0].rank() + 3 == m_cards[3].rank() &&
m_cards[0].rank() + 4 == m_cards[4].rank())
{
strait = true;
m_hand_type = STRAIT;
}
if (m_cards[0].suit() == m_cards[1].suit() &&
m_cards[0].suit() == m_cards[2].suit() &&
m_cards[0].suit() == m_cards[3].suit() &&
m_cards[0].suit() == m_cards[4].suit())
{
flush = true;
m_hand_type = FLUSH;
}
if (flush && strait)
{
if(m_cards[4].rank() == ACE)
m_hand_type = ROYAL_FLUSH;
else
m_hand_type = STRAIT_FLUSH;
return;
}
else if (flush || strait)
return;
std::map<Rank,int> hand_count;
for( std::vector<Card>::iterator ic = m_cards.begin();
ic != m_cards.end();
ic++)
{
hand_count[ic->rank()] += 1;
switch(hand_count[ic->rank()])
{
case 2:
switch(m_hand_type)
{
case HIGH_CARD:
m_hand_type = PAIR;
m_rank = ic->rank();
break;
case PAIR:
m_hand_type = TWO_PAIRS;
m_rank = std::max(m_rank,ic->rank());
break;
case THREE_KIND:
m_hand_type = FULL_HOUSE;
break;
default:
break;
}
break;
case 3:
switch(m_hand_type)
{
case PAIR:
m_hand_type = THREE_KIND;
break;
case TWO_PAIRS:
m_hand_type = FULL_HOUSE;
m_rank = ic->rank();
return;
break;
default:
break;
}
break;
case 4:
m_hand_type = FOUR_KIND;
return;
break;
default:
break;
}
}
}
PokerHandType getHandType() const {return m_hand_type;}
std::vector<Card> getCards() const {return m_cards;}
Rank getRank() const {return m_rank;}
private:
PokerHandType m_hand_type;
Rank m_rank;
};
inline bool operator< (const PokerHand& lhs, const PokerHand& rhs)
{
if( lhs.getHandType() != rhs.getHandType() )
return lhs.getHandType() < rhs.getHandType();
else if (lhs.getRank() != rhs.getRank())
return lhs.getRank() < rhs.getRank();
else
for(int i = 4; i >=0; --i)
if( lhs.getCards()[i].value() != rhs.getCards()[i].value() )
return lhs.getCards()[i].value() < rhs.getCards()[i].value();
return false;
}
inline bool operator> (const PokerHand& lhs, const PokerHand& rhs){return rhs < lhs;}
inline bool operator<=(const PokerHand& lhs, const PokerHand& rhs){return !(lhs > rhs);}
inline bool operator>=(const PokerHand& lhs, const PokerHand& rhs){return !(lhs < rhs);}
std::ostream& operator<<(std::ostream& out, const PokerHand& h){
std::cout << "Hand of " << h.getCards().size() << " cards: ";
for(std::vector<Card>::iterator it = h.getCards().begin();
it != h.getCards().end();
++it)
out << (*it) << ' ';
out << ": " << PokerHandTypes[h.getHandType()] << "\n";
return out;
}