-
Notifications
You must be signed in to change notification settings - Fork 140
/
holdem_functions.py
300 lines (273 loc) · 11.5 KB
/
holdem_functions.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Constants
suit_index_dict = {"s": 0, "c": 1, "h": 2, "d": 3}
reverse_suit_index = ("s", "c", "h", "d")
val_string = "AKQJT98765432"
hand_rankings = ("High Card", "Pair", "Two Pair", "Three of a Kind",
"Straight", "Flush", "Full House", "Four of a Kind",
"Straight Flush", "Royal Flush")
suit_value_dict = {"T": 10, "J": 11, "Q": 12, "K": 13, "A": 14}
for num in xrange(2, 10):
suit_value_dict[str(num)] = num
class Card:
# Takes in strings of the format: "As", "Tc", "6d"
def __init__(self, card_string):
value, self.suit = card_string[0], card_string[1]
self.value = suit_value_dict[value]
self.suit_index = suit_index_dict[self.suit]
def __str__(self):
return val_string[14 - self.value] + self.suit
def __repr__(self):
return val_string[14 - self.value] + self.suit
def __eq__(self, other):
if self is None:
return other is None
elif other is None:
return False
return self.value == other.value and self.suit == other.suit
# Returns deck of cards with all hole cards and board cards removed
def generate_deck(hole_cards, board):
deck = []
for suit in reverse_suit_index:
for value in val_string:
deck.append(Card(value + suit))
taken_cards = []
for hole_card in hole_cards:
for card in hole_card:
if card is not None:
taken_cards.append(card)
if board and len(board) > 0:
taken_cards.extend(board)
for taken_card in taken_cards:
deck.remove(taken_card)
return tuple(deck)
# Generate all possible hole card combinations
def generate_hole_cards(deck):
import itertools
return itertools.combinations(deck, 2)
# Generate num_iterations random boards
def generate_random_boards(deck, num_iterations, board_length):
import random
import time
random.seed(time.time())
for _ in xrange(num_iterations):
yield random.sample(deck, 5 - board_length)
# Generate all possible boards
def generate_exhaustive_boards(deck, num_iterations, board_length):
import itertools
return itertools.combinations(deck, 5 - board_length)
# Returns a board of cards all with suit = flush_index
def generate_suit_board(flat_board, flush_index):
histogram = [card.value for card in flat_board
if card.suit_index == flush_index]
histogram.sort(reverse=True)
return histogram
# Returns a list of two tuples of the form: (value of card, frequency of card)
def preprocess(histogram):
return [(14 - index, frequency) for index, frequency in
enumerate(histogram) if frequency]
# Takes an iterable sequence and returns two items in a tuple:
# 1: 4-long list showing how often each card suit appears in the sequence
# 2: 13-long list showing how often each card value appears in the sequence
def preprocess_board(flat_board):
suit_histogram, histogram = [0] * 4, [0] * 13
# Reversing the order in histogram so in the future, we can traverse
# starting from index 0
for card in flat_board:
histogram[14 - card.value] += 1
suit_histogram[card.suit_index] += 1
return suit_histogram, histogram, max(suit_histogram)
# Returns tuple: (Is there a straight flush?, high card)
def detect_straight_flush(suit_board):
contiguous_length, fail_index = 1, len(suit_board) - 5
# Won't overflow list because we fail fast and check ahead
for index, elem in enumerate(suit_board):
current_val, next_val = elem, suit_board[index + 1]
if next_val == current_val - 1:
contiguous_length += 1
if contiguous_length == 5:
return True, current_val + 3
else:
# Fail fast if straight not possible
if index >= fail_index:
if (index == fail_index and next_val == 5 and
suit_board[0] == 14):
return True, 5
break
contiguous_length = 1
return False,
# Returns the highest kicker available
def detect_highest_quad_kicker(histogram_board):
for elem in histogram_board:
if elem[1] < 4:
return elem[0]
# Returns tuple: (Is there a straight?, high card)
def detect_straight(histogram_board):
contiguous_length, fail_index = 1, len(histogram_board) - 5
# Won't overflow list because we fail fast and check ahead
for index, elem in enumerate(histogram_board):
current_val, next_val = elem[0], histogram_board[index + 1][0]
if next_val == current_val - 1:
contiguous_length += 1
if contiguous_length == 5:
return True, current_val + 3
else:
# Fail fast if straight not possible
if index >= fail_index:
if (index == fail_index and next_val == 5 and
histogram_board[0][0] == 14):
return True, 5
break
contiguous_length = 1
return False,
# Returns tuple of the two highest kickers that result from the three of a kind
def detect_three_of_a_kind_kickers(histogram_board):
kicker1 = -1
for elem in histogram_board:
if elem[1] != 3:
if kicker1 == -1:
kicker1 = elem[0]
else:
return kicker1, elem[0]
# Returns the highest kicker available
def detect_highest_kicker(histogram_board):
for elem in histogram_board:
if elem[1] == 1:
return elem[0]
# Returns tuple: (kicker1, kicker2, kicker3)
def detect_pair_kickers(histogram_board):
kicker1, kicker2 = -1, -1
for elem in histogram_board:
if elem[1] != 2:
if kicker1 == -1:
kicker1 = elem[0]
elif kicker2 == -1:
kicker2 = elem[0]
else:
return kicker1, kicker2, elem[0]
# Returns a list of the five highest cards in the given board
# Note: Requires a sorted board to be given as an argument
def get_high_cards(histogram_board):
return histogram_board[:5]
# Return Values:
# Royal Flush: (9,)
# Straight Flush: (8, high card)
# Four of a Kind: (7, quad card, kicker)
# Full House: (6, trips card, pair card)
# Flush: (5, [flush high card, flush second high card, ..., flush low card])
# Straight: (4, high card)
# Three of a Kind: (3, trips card, (kicker high card, kicker low card))
# Two Pair: (2, high pair card, low pair card, kicker)
# Pair: (1, pair card, (kicker high card, kicker med card, kicker low card))
# High Card: (0, [high card, second high card, third high card, etc.])
def detect_hand(hole_cards, given_board, suit_histogram,
full_histogram, max_suit):
# Determine if flush possible. If yes, four of a kind and full house are
# impossible, so return royal, straight, or regular flush.
if max_suit >= 3:
flush_index = suit_histogram.index(max_suit)
for hole_card in hole_cards:
if hole_card.suit_index == flush_index:
max_suit += 1
if max_suit >= 5:
flat_board = list(given_board)
flat_board.extend(hole_cards)
suit_board = generate_suit_board(flat_board, flush_index)
result = detect_straight_flush(suit_board)
if result[0]:
return (8, result[1]) if result[1] != 14 else (9,)
return 5, get_high_cards(suit_board)
# Add hole cards to histogram data structure and process it
full_histogram = full_histogram[:]
for hole_card in hole_cards:
full_histogram[14 - hole_card.value] += 1
histogram_board = preprocess(full_histogram)
# Find which card value shows up the most and second most times
current_max, max_val, second_max, second_max_val = 0, 0, 0, 0
for item in histogram_board:
val, frequency = item[0], item[1]
if frequency > current_max:
second_max, second_max_val = current_max, max_val
current_max, max_val = frequency, val
elif frequency > second_max:
second_max, second_max_val = frequency, val
# Check to see if there is a four of a kind
if current_max == 4:
return 7, max_val, detect_highest_quad_kicker(histogram_board)
# Check to see if there is a full house
if current_max == 3 and second_max >= 2:
return 6, max_val, second_max_val
# Check to see if there is a straight
if len(histogram_board) >= 5:
result = detect_straight(histogram_board)
if result[0]:
return 4, result[1]
# Check to see if there is a three of a kind
if current_max == 3:
return 3, max_val, detect_three_of_a_kind_kickers(histogram_board)
if current_max == 2:
# Check to see if there is a two pair
if second_max == 2:
return 2, max_val, second_max_val, detect_highest_kicker(
histogram_board)
# Return pair
else:
return 1, max_val, detect_pair_kickers(histogram_board)
# Check for high cards
return 0, get_high_cards(histogram_board)
# Returns the index of the player with the winning hand
def compare_hands(result_list):
best_hand = max(result_list)
winning_player_index = result_list.index(best_hand) + 1
# Check for ties
if best_hand in result_list[winning_player_index:]:
return 0
return winning_player_index
# Print results
def print_results(hole_cards, winner_list, result_histograms):
float_iterations = float(sum(winner_list))
print "Winning Percentages:"
for index, hole_card in enumerate(hole_cards):
winning_percentage = float(winner_list[index + 1]) / float_iterations
if hole_card == (None, None):
print "(?, ?) : ", winning_percentage
else:
print hole_card, ": ", winning_percentage
print "Ties: ", float(winner_list[0]) / float_iterations, "\n"
for player_index, histogram in enumerate(result_histograms):
print "Player" + str(player_index + 1) + " Histogram: "
for index, elem in enumerate(histogram):
print hand_rankings[index], ": ", float(elem) / float_iterations
print
# Returns the winning percentages
def find_winning_percentage(winner_list):
float_iterations = float(sum(winner_list))
percentages = []
for num_wins in winner_list:
winning_percentage = float(num_wins) / float_iterations
percentages.append(winning_percentage)
return percentages
# Populate provided data structures with results from simulation
def find_winner(generate_boards, deck, hole_cards, num, board_length,
given_board, winner_list, result_histograms):
# Run simulations
result_list = [None] * len(hole_cards)
for remaining_board in generate_boards(deck, num, board_length):
# Generate a new board
if given_board:
board = given_board[:]
board.extend(remaining_board)
else:
board = remaining_board
# Find the best possible poker hand given the created board and the
# hole cards and save them in the results data structures
suit_histogram, histogram, max_suit = (
preprocess_board(board))
for index, hole_card in enumerate(hole_cards):
result_list[index] = detect_hand(hole_card, board, suit_histogram,
histogram, max_suit)
# Find the winner of the hand and tabulate results
winner_index = compare_hands(result_list)
winner_list[winner_index] += 1
# Increment what hand each player made
for index, result in enumerate(result_list):
result_histograms[index][result[0]] += 1