-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandStrength.py
272 lines (216 loc) · 7.69 KB
/
HandStrength.py
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#
# Copyright (c) James Quintero 2020
#
# Last Modified: 12/2022
#
# Handles determining hand strength
class HandStrength:
def __init__(self):
self.verbose = False
"""
returns 1 if hand1 won, -1 if hand2 won, and 0 if split
"""
def determine_better_hand(self, hand1_strength, hand2_strength):
if hand1_strength[0] > hand2_strength[0]:
return 1
elif hand1_strength[0] < hand2_strength[0]:
return -1
#if same hand
else:
#if returned hand data doesn't include lists
if hand1_strength[0] == 8 or hand1_strength[0] == 5 or hand1_strength[0] == 4 or hand1_strength[0] == 9:
if hand1_strength[1] > hand2_strength[1]:
return 1
elif hand1_strength[1] < hand2_strength[1]:
return -1
else:
return 0
else:
for x in range(0, len(hand1_strength[1])):
if hand1_strength[1][x] > hand2_strength[1][x]:
return 1
elif hand1_strength[1][x] < hand2_strength[1][x]:
return -1
return 0
"""
Given the hand strength, convert to english version
"""
def convert_hand_strength(self, hand_strength):
to_return = {
0: "High card",
1: "Pair",
2: "Two pair",
3: "Trips",
4: "Straight",
5: "Flush",
6: "Full House",
7: "Quads",
8: "Straight Flush",
9: "Royal Flush"
}
#If provided in format of HandStrength
if isinstance(hand_strength, list):
return to_return[hand_strength[0]]
else:
return to_return[hand_strength]
"""
Returns the hand strength along with kickers
"""
def determine_hand_strength(self, board, hand):
#0 = high card
#1 = pair
#2 = 2-pair
#3 = trips
#4 = straight
#5 = flush
#6 = full house
#7 = quads
#8 = straight flush
#9 = royal flush
#will return [#, [list of information like how high straight or flush is]]
#starts at 0 spot (0 and 1 aren't used), and ends at Ace
cards=[0]*15
#spade, club, heart, diamond
#list of cards that had that suit
suits={"s": [], "c": [], "h": [], "d": []}
#adds hand data to lists
value1=int(hand[0][0:len(hand[0])-1])
suit1=hand[0][-1:]
value2=int(hand[1][0:len(hand[1])-1])
suit2=hand[1][-1:]
cards[value1]+=1
suits[suit1].append(value1)
cards[value2]+=1
suits[suit2].append(value2)
#adds board data to lists
for x in range(0, len(board)):
value=int(board[x][0:len(board[x])-1])
suit=board[x][-1:]
cards[value]+=1
suits[suit].append(value)
has_straight = self.has_straight(cards)
#if straight
if has_straight[0] == True:
str_height = has_straight[1]
#if 5 of same suits in play and they're all in the straight
for key in suits.keys():
if len(suits[key])>=5 and str_height-4 in suits[key] and str_height-3 in suits[key] and str_height-2 in suits[key] and str_height-1 in suits[key] and str_height in suits[key]:
#royal flush
if str_height==14:
return [9, 0]
#straight flush
return [8, str_height]
#if quads
if 4 in cards:
quads=cards.index(4)
#doesn't include quads in getting kicker
cards[quads]=0
#gets highest card in play
kicker=self.get_kicker_indices(cards, 1)
# kicker=kicker[0]
extra = [quads]
for x in range(0, min(1, len(kicker))):
extra.append(kicker)
return [7, extra]
#if full house
if (3 in cards and 2 in cards) or cards.count(3)==2:
#gets highest 3 in play
highest3=0
for x in range(0, len(cards)):
if cards[x]==3:
highest3=x
#doesn't include set of boat in getting other part
cards[highest3]=0
#gets highest 2 in play
highest2=0
for x in range(0, len(cards)):
if cards[x]>=2:
highest2=x
#Aces of Kings full house will return [6, [14, 13]]
return [6, [highest3, highest2]]
#if flush
for key in suits.keys():
if len(suits[key])>=5:
#returns highest player's flush if they have 2 of the suits
if suit1==key and suit2==key:
flush=max([value1, value2])
#return player's flush if they have 1 of the suits or 0 if board plays
if suit1==key:
flush=value1
elif suit2==key:
flush=value2
else:
flush=0
#returns highest card in flush
return [5, flush]
#if straight
if has_straight[0] == True:
return [4, has_straight[1]]
#if trips
if 3 in cards:
trips=cards.index(3)
#doesn't include trips in getting kicker
cards[trips]=0
#returns highest 2 cards not part of trips
kickers=self.get_kicker_indices(cards, 2)
extra = [trips]
for x in range(0, min(2, len(kickers))):
extra.append(kickers[x])
return [3, extra]
#if 2 pair
if cards.count(2)>=2:
#gets highest two pair
highest1=0
highest2=0
for x in range(0, len(cards)):
if cards[x]==2:
highest2=highest1
highest1=x
#doesn't include 2 pair in getting kicker
cards[highest1]=0
cards[highest2]=0
#gets high card
kicker = self.get_kicker_indices(cards, 1)
# kicker = kicker[0]
extra = [highest1, highest2]
for x in range(0, min(1, len(kicker))):
extra.append(kicker[x])
return [2, extra]
#if regular pair
if cards.count(2)==1:
pair=cards.index(2)
#doesn't include pair in getting kickers
cards[pair]=0
#get 3 other highest cards
kickers=self.get_kicker_indices(cards, 3)
values=[pair]
for x in range(0, len(kickers)):
if x<3:
values.append(kickers[x])
return [1, values]
#if high card
kickers=self.get_kicker_indices(cards, 5)
return [0, kickers]
"""
Gets indices for num_kickers
"""
def get_kicker_indices(self, temp_cards, num_kickers):
temp=[]
for x in range(len(temp_cards)-1, -1, -1):
if temp_cards[x]!=0 and len(temp)<num_kickers:
temp.append(x)
return temp
"""
returns [True, straight_height] if has straight
"""
def has_straight(self, cards):
for x in range(2, len(cards)-4):
if cards[x]>=1 and cards[x+1]>=1 and cards[x+2]>=1 and cards[x+3]>=1 and cards[x+4]>=1:
return [True, x+4]
#wheel straight
if cards[2]>=1 and cards[3]>=1 and cards[4]>=1 and cards[5]>=1 and cards[14]>=1:
return [True, 5]
return [False, 0]
if __name__=="__main__":
hand_strength = HandStrength()
# mississippi_stud.run()