-
Notifications
You must be signed in to change notification settings - Fork 1
/
stupid.py
270 lines (238 loc) · 9.09 KB
/
stupid.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
import random
import copy
# Constants for hand evaluation
ROYAL_FLUSH_VALUE = 900000014
STRAIGHT_FLUSH_VALUE = 900000000
FOUR_OF_A_KIND_VALUE = 800000000
FULL_HOUSE_VALUE = 700000000
FLUSH_VALUE = 600000000
STRAIGHT_VALUE = 500000000
THREE_OF_A_KIND_VALUE = 400000000
TWO_PAIR_VALUE = 300000000
PAIR_VALUE = 200000000
class Deck():
def __init__(self):
self.original_deck = ["2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "T♠", "J♠", "Q♠", "K♠", "A♠",
"2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥", "T♥", "J♥", "Q♥", "K♥", "A♥",
"2♦", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦", "9♦", "T♦", "J♦", "Q♦", "K♦", "A♦",
"2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "T♣", "J♣", "Q♣", "K♣", "A♣"]
self.deck = copy.deepcopy(self.original_deck)
self.shuffle_and_reset()
def shuffle_and_reset(self):
self.deck = copy.deepcopy(self.original_deck)
random.shuffle(self.deck)
def draw_card(self):
assert len(self.deck) > 0, "\n\nDeck is empty, cannot draw card"
return self.deck.pop(0)
class botPlayer():
def __init__(self):
self.cards = []
def assign_value_to_hand(hand, table_cards):
highest_hand = 0
highest_hand_type = "High card"
tmp_cards = hand + table_cards
cards = []
for card in tmp_cards:
tmp_card_val = card[0]
if tmp_card_val == "T":
tmp_card_val = "10"
elif tmp_card_val == "J":
tmp_card_val = "11"
elif tmp_card_val == "Q":
tmp_card_val = "12"
elif tmp_card_val == "K":
tmp_card_val = "13"
elif tmp_card_val == "A":
tmp_card_val = "14"
else:
tmp_card_val = "0" + str(tmp_card_val)
cards.append(tmp_card_val + card[-1])
cards.sort(key=lambda x: int(x[:-1]))
# Look for flush
suits = {}
for card in cards:
if card[-1] in suits:
suits[card[-1]] += 1
else:
suits[card[-1]] = 1
most_common_suit = max(suits, key=suits.get)
most_common_suit_n = suits[most_common_suit]
if most_common_suit_n >= 5:
straight_flush = []
for card in cards:
if card[-1] == most_common_suit:
if len(straight_flush) > 0:
if int(card[:-1]) == int(straight_flush[-1][:-1]) + 1:
straight_flush.append(card)
else:
straight_flush = [card]
else:
straight_flush.append(card)
if len(straight_flush) >= 5:
handValue = STRAIGHT_FLUSH_VALUE + int(straight_flush[-1][:-1])
if handValue == ROYAL_FLUSH_VALUE:
if handValue > highest_hand:
highest_hand_type = "Royal straight flush"
highest_hand = handValue
else:
if handValue > highest_hand:
highest_hand_type = "Straight flush"
highest_hand = handValue
handValue = FLUSH_VALUE
multiplier = 10000
for card in reversed(cards):
if card[-1] == most_common_suit:
handValue += int(card[:-1])*multiplier
multiplier = int(multiplier / 10)
if multiplier == 0:
break
if handValue > highest_hand:
highest_hand_type = "Flush"
highest_hand = handValue
values = {}
for card in cards:
if card[:-1] in values:
values[card[:-1]] += 1
else:
values[card[:-1]] = 1
most_common_value = max(values, key=values.get)
most_common_value_n = values[most_common_value]
if most_common_value_n == 4:
highest_card = 0
for card in cards:
if card[:-1] != most_common_value:
if int(card[:-1]) > highest_card:
highest_card = int(card[:-1])
handValue = FOUR_OF_A_KIND_VALUE + int(most_common_value)*100 + highest_card
if handValue > highest_hand:
highest_hand_type = "Four of a kind"
highest_hand = handValue
if most_common_value_n == 3:
for card in cards:
if card[:-1] != most_common_value:
if values[card[:-1]] == 2:
handValue = FULL_HOUSE_VALUE + int(most_common_value)*100 + int(card[:-1])
if handValue > highest_hand:
highest_hand_type = "Full house"
highest_hand = handValue
highest_card = 0
second_highest_card = 0
for card in cards:
if card[:-1] != most_common_value:
if int(card[:-1]) > highest_card:
second_highest_card = highest_card
highest_card = int(card[:-1])
elif int(card[:-1]) > second_highest_card:
second_highest_card = int(card[:-1])
handValue = THREE_OF_A_KIND_VALUE + int(most_common_value)*100 + highest_card*10 + second_highest_card
if handValue > highest_hand:
highest_hand_type = "Three of a kind"
highest_hand = handValue
if most_common_value_n == 2:
second_most_common_value = 0
for card in cards:
if card[:-1] != most_common_value:
if values[card[:-1]] == 2:
second_most_common_value = card[:-1]
break
highest_card = 0
for card in cards:
if card[:-1] != most_common_value and card[:-1] != second_most_common_value:
if int(card[:-1]) > highest_card:
highest_card = int(card[:-1])
if second_most_common_value != 0:
handValue = TWO_PAIR_VALUE + int(most_common_value)*100 + int(second_most_common_value)*100 + highest_card
if handValue > highest_hand:
highest_hand_type = "Two pair"
highest_hand = handValue
else:
handValue = PAIR_VALUE + int(most_common_value)*10000 + highest_card
multiplier = 1000
for card in reversed(cards):
if card[:-1] != most_common_value:
handValue += int(card[:-1])*multiplier
multiplier = int(multiplier / 10)
if multiplier == 0:
break
if handValue > highest_hand:
highest_hand_type = "Pair"
highest_hand = handValue
straight = []
for card in cards:
if len(straight) > 0:
if int(card[:-1]) == int(straight[-1][:-1]) + 1:
straight.append(card)
else:
straight = [card]
else:
straight.append(card)
if len(straight) >= 5:
handValue = STRAIGHT_VALUE + int(straight[-1][:-1])
if handValue > highest_hand:
highest_hand_type = "Straight"
highest_hand = handValue
handValue = 0
multiplier = 10000
for card in reversed(cards):
handValue += int(card[:-1])*multiplier
multiplier = int(multiplier / 10)
if multiplier == 0:
break
if handValue > highest_hand:
highest_hand = handValue
return highest_hand, highest_hand_type
def yourBot(gameState, yourPlayersIndex, yourHand):
move: str = "call"
amount: int = 0
deck = Deck()
wins = 0
handCards = yourHand
n_players = len(gameState["players"])
sim_players = []
for i in range(n_players):
bot_player = botPlayer()
sim_players.append(bot_player)
for _ in range(1000):
deck.shuffle_and_reset()
tablecards = gameState["table_cards"]
for card in handCards:
deck.deck.remove(card)
for card in tablecards:
if card:
deck.deck.remove(card)
for i in range(len(tablecards)):
if tablecards[i] is None:
tablecards[i] = deck.draw_card()
for i in range(len(sim_players)):
sim_players[i].cards = [deck.draw_card(), deck.draw_card()]
your_hand_val, _ = assign_value_to_hand(handCards, tablecards)
did_win = True
for player in sim_players:
hand_val, _ = assign_value_to_hand(player.cards, tablecards)
if hand_val > your_hand_val:
did_win = False
break
if did_win:
wins += 1
check_amount = 100
call_amount = 350
raise_amount = 400
allin_amount = 550
skew = 2
check_amount = int(check_amount * skew)
call_amount = int(call_amount * skew)
raise_amount = int(raise_amount * skew)
allin_amount = int(allin_amount * skew)
if wins > allin_amount:
move = "allin"
amount = 0
elif wins > raise_amount:
move = "raise"
amount = gameState["min_raise"] if gameState["min_raise"] > 300 else 300
elif wins > call_amount:
move = "call"
amount = 0
else:
move = "check"
amount = 0
return move, amount