-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
225 lines (195 loc) · 6.22 KB
/
util.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
from enum import Enum
from deck import Deck
from deuces import Card
from deuces import Evaluator
class Counter(dict):
def __getitem__(self, idx):
self.setdefault(idx, 0.0)
return dict.__getitem__(self, idx)
def incrementAll(self, keys, count):
for key in keys:
self[key] += count
def arg_max(self):
if len(self.keys()) == 0:
return None
all = self.items()
values = [x[1] for x in all]
maxIndex = values.index(max(values))
return all[maxIndex][0]
def sortedKeys(self):
sortedItems = self.items()
compare = lambda x, y: sign(y[1] - x[1])
sortedItems.sort(cmp=compare)
return [x[0] for x in sortedItems]
def totalCount(self):
return sum(self.values())
def normalize(self):
total = float(self.totalCount())
if total == 0: return
for key in self.keys():
self[key] = self[key] / total
def divideAll(self, divisor):
divisor = float(divisor)
for key in self:
self[key] /= divisor
def copy(self):
return Counter(dict.copy(self))
def __mul__(self, y):
sum = 0
x = self
if len(x) > len(y):
x, y = y, x
for key in x:
if key not in y:
continue
sum += x[key] * y[key]
return sum
def __radd__(self, y):
for key, value in y.items():
self[key] += value
def __add__(self, y):
addend = Counter()
for key in self:
if key in y:
addend[key] = self[key] + y[key]
else:
addend[key] = self[key]
for key in y:
if key in self:
continue
addend[key] = y[key]
return addend
def __sub__(self, y):
addend = Counter()
for key in self:
if key in y:
addend[key] = self[key] - y[key]
else:
addend[key] = self[key]
for key in y:
if key in self:
continue
addend[key] = -1 * y[key]
return addend
class Actions(Enum):
FOLD = 0
CALL = 1
RAISE = 2
"""
Returns a value between 1 and 7462 for a 5 card poker hand out of 5, 6, or 7 cards
"""
def evalHand(hand, communal_cards):
if communal_cards:
communal_cards_strs = [str(card) for card in communal_cards]
hand_cards_str = [str(card) for card in hand]
board = []
handList = []
if communal_cards:
for card in communal_cards_strs:
board.append(Card.new(card))
for card in hand_cards_str:
handList.append(Card.new(card))
evaluator = Evaluator()
return evaluator.evaluate(board, handList)
def get_rank(score):
evaluator = Evaluator()
rank = evaluator.get_rank_class(score)
#print(evaluator.class_to_string(rank))
return rank
def percentHandStrength(score):
return score / float(7462)
def possibleFlush(cards):
if len(cards) == 5:
return (len(set([card.suit for card in cards])) >= 3)
elif len(cards) == 6:
return (len(set([card.suit for card in cards])) >= 4)
else:
return False
def possibleStraight(cards):
card_values = []
for card in cards:
cardval = card.value
if cardval == 1:
cardval = 14
card_values.append(cardval)
if len(cards) == 5:
for i in range(2,15):
card_values.append(i)
for j in range(2,15):
card_values.append(j)
card_values.sort()
possibleStraight = True
for k in range(len(card_values) - 1):
if card_values[k] + 1 != card_values[k+1]:
possibleStraight = False
if possibleStraight:
return True
card_values.remove(j)
card_values.remove(i)
return False
elif len(cards) == 6:
for i in range(2,15):
card_values.append(i)
card_values.sort()
possibleStraight = True
for k in range(len(card_values) - 1):
if card_values[k] + 1 != card_values[k+1]:
possibleStraight = False
if possibleStraight:
return True
card_values.remove(i)
return False
else:
return False
class PreflopEvaluator:
@staticmethod
def get_range_score(hand):
"""
evaluates the range of two cards in preflop hand.
Cards that are closer together are better, and a max range of 5, so this
returns 6 - diff(card values) or 0 if they are greater than 5 apart.
:param hand: list of two cards
:return:
"""
value0 = hand[0].value
value1 = hand[1].value
#check aces
if value0 == 1:
value0 = 14
if value1 == 1:
value1 = 14
return 6 - abs(value0 - value1)
@staticmethod
def get_pair_score(hand):
"""
:param hand: list of two cards
:return: returns 1 if pocket pairs else 0
"""
return hand[0].value == hand[1].value
@staticmethod
def get_flush_score(hand):
"""
:param hand: list of two cards
:return: returns 1 if same suit else 0
"""
return hand[0].suit == hand[1].suit
@staticmethod
def get_high_card(hand):
return max([14 if hand[0].value == 1 else hand[0].value,14 if hand[1].value == 1 else hand[1].value])
@staticmethod
def get_sum_card(hand):
return sum([14 if hand[0].value == 1 else hand[0].value, 14 if hand[1].value == 1 else hand[1].value])
@staticmethod
def evaluate_cards(hand):
card_stats = Counter()
hand = list(hand)
card_stats['range-score'] = PreflopEvaluator.get_range_score(hand)
card_stats['pair-score'] = PreflopEvaluator.get_pair_score(hand)
card_stats['flush-score'] = PreflopEvaluator.get_flush_score(hand)
card_stats['high-card-score'] = PreflopEvaluator.get_high_card(hand) / 100.0
card_stats['card-sum'] = PreflopEvaluator.get_sum_card(hand)
return card_stats
class BiddingRound(Enum):
PREFLOP = 0
POST_FLOP = 1
SHOWDOWN = 2