/
pokerhand.cpp
158 lines (146 loc) · 3.62 KB
/
pokerhand.cpp
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
#include "pokerhand.h"
#define SUIT(cardval) (cardval % 4)
#define RANK(cardval) ((cardval-1) / 4)
bool is_royal(int* hand)
{
for (int i=0; i<5; i++) {
if (hand[i] > 20)
return false; // contains card lower than 10
if (SUIT(hand[i]) != SUIT(hand[0]))
return false; // contains mismatched suits
}
return true;
}
bool is_four(int* rankcount)
{
for (int i=0; i<13; i++)
if (rankcount[i] == 4)
return true;
return false;
}
bool is_full(int* rankcount)
{
bool found_3 = false;
bool found_2 = false;
for (int i=0; i<13; i++) {
if (rankcount[i] == 3)
found_3 = true;
else if (rankcount[i] == 2)
found_2 = true;
}
return found_3 && found_2;
}
bool is_straight(int* rankcount)
{
int lowest = -1;
int highest;
//skipping over ace
for (int i=1; i< 13; i++) {
if (rankcount[i] == 1) {
if (lowest == -1)
lowest = i;
highest = i;
}
if (rankcount[i] > 1)
return false;
}
if (rankcount[0] > 1)
return false;
// we know every rank is 0 or 1
// middle straight or we allow ACE (0) at bottom or top
if ((highest - lowest == 4 && rankcount[0] == 0) ||
(highest == 4) || (lowest == 9))
return true;
return false;
}
bool is_flush(int* hand)
{
for (int i =1; i< 5; i++)
if (SUIT(hand[i]) != SUIT(hand[0]))
return false;
return true;
}
bool is_straight_flush(int* hand, int* rankcount)
{
return is_straight(rankcount) && is_flush(hand);
}
bool is_three(int* rankcount)
{
for (int i=0; i< 13; i++)
if (rankcount[i] == 3)
return true;
return false;
}
bool is_TwoPair(int* rankcount)
{
int count = 0;
for (int i=0; i< 13; i++)
if (rankcount[i] == 2)
count++;
return count > 1;
}
bool is_Pair(int* rankcount)
{
// note only Jacks or better
for (int i=0; i< 4; i++)
if (rankcount[i] == 2)
return true;
return false;
}
HandRank EvaluateHand(int hand[5])
{
int rankcount[13]= {0};
for (int i=0; i< 5; i++)
rankcount[RANK(hand[i])]++;
if (is_royal(hand))
return eRoyal;
if (is_straight_flush(hand, rankcount))
return eStraightFlush;
if (is_four(rankcount))
return eFour;
if (is_full(rankcount))
return eFull;
if (is_flush(hand))
return eFlush;
if (is_straight(rankcount))
return eStraight;
if (is_three(rankcount))
return eThree;
if (is_TwoPair(rankcount))
return eTwoPair;
if (is_Pair(rankcount))
return ePair;
return eBust;
}
int BasePayout(HandRank rank)
{
switch(rank)
{
case ePair : return 1;
case eTwoPair : return 2;
case eThree : return 3;
case eStraight : return 4;
case eFlush : return 6;
case eFull : return 9;
case eFour : return 25;
case eStraightFlush : return 50;
case eRoyal : return 250;
}
return 0;
}
const char* RankToText(HandRank rank)
{
switch(rank)
{
case ePair : return "Jacks or better";
case eTwoPair : return "two pair";
case eThree : return "three of a kind";
case eStraight : return "straight";
case eFlush : return "flush";
case eFull : return "FULL HOUSE";
case eFour : return "FOUR OF A KIND";
case eStraightFlush : return "STRAIGHT FLUSH";
case eRoyal : return "ROYAL FLUSH";
}
return 0;
}