-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlay.py
471 lines (373 loc) · 16.3 KB
/
Play.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#
# Copyright (c) James Quintero 2022
#
# Last Modified: 12/2022
#
#Play Mississippi Stud manually
import json
import csv
import os
import time
import copy
import random
from HandStrength import HandStrength
from Simulate import Simulate
from Utils import Utils
class Play:
hand_strength = None
util = None
#Starting buy-in when sitting down at the table
starting_bankroll = 1000000
#min-bet is $15
bet_amount = 10
#[0] = Ante bet, [1] = 3rd street bet, [2] = 4th street bet, [3] = 5th street bet
bets = [0,0,0,0]
fold = False
play_type = 0 #0 is manual, 1 is basic strategy, 2 is custom strategy
deck = []
board = []
player_hand = []
other_players_hands = [] #2D list of other player's hands at the table
hand_strength_distribution = {}
#if true, print statements are printed
verbose = True
def __init__(self):
self.hand_strength = HandStrength()
self.simulate = Simulate()
self.util = Utils()
self.reset()
self.verbose = False
"""
Sets up global variables
"""
def reset(self):
self.reset_hand()
self.bankroll = self.starting_bankroll
self.hand_strength_distribution = {
0: 0,
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
7: 0,
8: 0,
9: 0,
}
def reset_hand(self):
self.board = []
self.player_hand = [""]*2
"""
Play a game of Mississippi Stud.
"""
def play(self):
# self.starting_bankroll = 200
# self.starting_bankroll = 1000000
self.starting_bankroll = 1000
self.reset()
num_other_players = int(input("Number of other players playing: "))
# auto_play = input("Auto Play? (y/n): ").lower() == "y"
self.play_type = self.play_type_choice()
num_rounds = 0
max_rounds = 10000
while self.bankroll >= self.bet_amount and num_rounds <= max_rounds:
self.play_round(num_other_players)
num_rounds += 1
if self.play_type == 1:
self.util.clear_screen()
self.util.print_current_state(self.board, self.player_hand, self.other_players_hands, self.bets, self.starting_bankroll, self.bankroll, self.bet_amount)
if self.play_type == 1:
choice = input("Press any key to play again, (n) to cash out: ")
if choice.lower() == "n":
break
print("Finished round {}".format(num_rounds))
self.reset_hand()
print("Final bankroll: ${}".format(self.bankroll))
print("Profit: ${}".format(self.bankroll - self.starting_bankroll))
print("Number of hands played: {}".format(num_rounds))
"""
Plays a single round of Mississippi Stud
auto_play is True if AI will play instead of the user.
"""
def play_round(self, num_other_players):
#shuffles the deck of cards
self.deck = self.util.initialize_deck()
#make bets
self.initial_bets()
#player gets random cards
self.player_hand[0] = self.deck.pop()
self.player_hand[1] = self.deck.pop()
#Deal to other players
self.other_players_hands = []
for x in range(num_other_players):
self.other_players_hands.append([self.deck.pop(), self.deck.pop()])
success = self.play_3rd_street()
if not success:
return self.player_lost()
success = self.play_4th_street()
if not success:
return self.player_lost()
success = self.play_5th_street()
if not success:
return self.player_lost()
player_hand_strength = self.hand_strength.determine_hand_strength(self.board, self.player_hand)
#player wins with pair of jacks or better
if player_hand_strength[0] >= 2 or (player_hand_strength[0] == 1 and player_hand_strength[1][0]>=11):
return self.player_won(player_hand_strength)
#pushes with pair of 6s to pair of 10s
elif player_hand_strength[0] == 1 and player_hand_strength[1][0]>=6:
return self.player_pushes(player_hand_strength)
#player lost
else:
return self.player_lost(player_hand_strength)
def player_won(self, player_hand_strength=None):
if player_hand_strength == None:
player_hand_strength = self.hand_strength.determine_hand_strength(self.board, self.player_hand)
self.hand_strength_distribution[player_hand_strength[0]] += 1
print("Won")
player_hand_strength = self.hand_strength.determine_hand_strength(self.board, self.player_hand)
payout_multiplier = self.util.payout[player_hand_strength[0]]
print("Payout {}x bets".format(payout_multiplier-1))
self.bankroll += sum(self.bets)*self.util.payout[player_hand_strength[0]]
return 1
def player_pushes(self, player_hand_strength=None):
if player_hand_strength == None:
player_hand_strength = self.hand_strength.determine_hand_strength(self.board, self.player_hand)
self.hand_strength_distribution[player_hand_strength[0]] += 1
print("Pushes")
self.bankroll += sum(self.bets)
return 0
def player_lost(self, player_hand_strength=None):
if player_hand_strength == None:
player_hand_strength = self.hand_strength.determine_hand_strength(self.board, self.player_hand)
#Only want to save high card hands, because any other hand is a losing pair, and don't want to save that.
if player_hand_strength[0] == 0:
self.hand_strength_distribution[player_hand_strength[0]] += 1
print("Lost")
return -1
"""
Plays 3rd street with the player betting and then the card being dealt
"""
def play_3rd_street(self):
## 3rd street
if self.play_type == 0:
self.util.clear_screen()
#Simulates possible outcomes
print("Simulating expected avg return...")
board_to_print = ",".join([ self.util.convert_card(card) for card in self.board ])
print("Board: {}".format(board_to_print))
# num_runs = 10000
# num_player_wins, num_dealer_wins, num_pushes, total_profit, bankroll, _ = self.simulate.simulate_many_runs(num_runs = num_runs, play_optimally=True, player_hand=copy.copy(self.player_hand), board=copy.copy(self.board), cards_to_remove=[ card for row in self.other_players_hands for card in row ])
# expected_return = total_profit/num_runs
# self.util.print_current_state(self.board, self.player_hand, self.other_players_hands, self.bets, self.starting_bankroll, self.bankroll, self.bet_amount)
# print("Recommended move: {}".format(self.recommended_move(expected_return)))
if self.play_type == 1 or self.play_type == 3:
start_time = time.perf_counter()
#Execution time for 100,000 runs on a single processor is 2.4 seconds for a foldable hand, and 3.8 seconds for a playable hand.
num_runs = 100000
num_player_wins, num_dealer_wins, num_pushes, total_profit, bankroll, _ = self.simulate.simulate_many_runs_multiprocessing(num_runs = num_runs, play_optimally=True, player_hand=copy.copy(self.player_hand), board=copy.copy(self.board), cards_to_remove=[ card for row in self.other_players_hands for card in row ])
expected_return = total_profit/num_runs
end_time = time.perf_counter()
elapsed_time = end_time - start_time
elapsed_time_milliseconds = elapsed_time * 1000
print("Elapsed simulation time: ", elapsed_time_milliseconds, " milliseconds")
# Manual play
if self.play_type == 1:
self.util.print_current_state(self.board, self.player_hand, self.other_players_hands, self.bets, self.starting_bankroll, self.bankroll, self.bet_amount)
print("Recommended move: {}".format(self.recommended_move(expected_return)))
choice = self.betting_choice()
# Betting strategy
elif self.play_type == 2:
choice = self.basic_strategy(street=3)
# Custom strategy
elif self.play_type == 3:
choice = self.recommended_move(expected_return)
else:
choice = self.betting_choice()
#Raise 1x
if choice == 1:
self.bet(1, self.bet_amount)
#Raise 3x
elif choice == 2:
self.bet(1, self.bet_amount*3)
#Fold
else:
return False
#Deals 3rd street
self.board.append(self.deck.pop())
return True
"""
Plays 4th street with the player betting and then the card being dealt
"""
def play_4th_street(self):
## 4th street
if self.play_type == 1:
self.util.clear_screen()
print("Simulating expected avg return...")
board_to_print = ",".join([ self.util.convert_card(card) for card in self.board ])
print("Board: {}".format(board_to_print))
# num_runs = 10000
# num_player_wins, num_dealer_wins, num_pushes, total_profit, bankroll, _ = self.simulate.simulate_many_runs(num_runs = num_runs, play_optimally=True, player_hand=copy.copy(self.player_hand), board=copy.copy(self.board), cards_to_remove=[ card for row in self.other_players_hands for card in row ])
# expected_return = total_profit/num_runs
# self.util.print_current_state(self.board, self.player_hand, self.other_players_hands, self.bets, self.starting_bankroll, self.bankroll, self.bet_amount)
# print("Recommended move: {}".format(self.recommended_move(expected_return)))
if self.play_type == 1 or self.play_type == 3:
num_runs = 100000
num_player_wins, num_dealer_wins, num_pushes, total_profit, bankroll, _ = self.simulate.simulate_many_runs_multiprocessing(num_runs = num_runs, play_optimally=True, player_hand=copy.copy(self.player_hand), board=copy.copy(self.board), cards_to_remove=[ card for row in self.other_players_hands for card in row ])
expected_return = total_profit/num_runs
# Manual play
if self.play_type == 1:
self.util.print_current_state(self.board, self.player_hand, self.other_players_hands, self.bets, self.starting_bankroll, self.bankroll, self.bet_amount)
print("Recommended move: {}".format(self.recommended_move(expected_return)))
choice = self.betting_choice()
# Betting strategy
elif self.play_type == 2:
choice = self.basic_strategy(street=4)
# Custom strategy
elif self.play_type == 3:
choice = self.recommended_move(expected_return)
else:
choice = self.betting_choice()
# input("Move AI is going to make: {}. Continue?".format(choice))
#Raise 1x
if choice == 1:
self.bet(2, self.bet_amount)
#Raise 3x
elif choice == 2:
self.bet(2, self.bet_amount*3)
#Fold
else:
return False
#Deals 4th street
self.board.append(self.deck.pop())
return True
"""
Plays 5th street with the player betting and then the card being dealt
"""
def play_5th_street(self):
## 5th street
if self.play_type == 1:
self.util.clear_screen()
print("Simulating expected avg return...")
board_to_print = ",".join([ self.util.convert_card(card) for card in self.board ])
print("Board: {}".format(board_to_print))
# num_runs = 10000
# num_player_wins, num_dealer_wins, num_pushes, total_profit, bankroll, _ = self.simulate.simulate_many_runs(num_runs = num_runs, play_optimally=True, player_hand=copy.copy(self.player_hand), board=copy.copy(self.board), cards_to_remove=[ card for row in self.other_players_hands for card in row ])
# expected_return = total_profit/num_runs
# self.util.print_current_state(self.board, self.player_hand, self.other_players_hands, self.bets, self.starting_bankroll, self.bankroll, self.bet_amount)
# print("Recommended move: {}".format(self.recommended_move(expected_return)))
if self.play_type == 1 or self.play_type == 3:
num_runs = 100000
num_player_wins, num_dealer_wins, num_pushes, total_profit, bankroll, _ = self.simulate.simulate_many_runs_multiprocessing(num_runs = num_runs, play_optimally=True, player_hand=copy.copy(self.player_hand), board=copy.copy(self.board), cards_to_remove=[ card for row in self.other_players_hands for card in row ])
expected_return = total_profit/num_runs
# Manual play
if self.play_type == 1:
self.util.print_current_state(self.board, self.player_hand, self.other_players_hands, self.bets, self.starting_bankroll, self.bankroll, self.bet_amount)
print("Recommended move: {}".format(self.recommended_move(expected_return)))
choice = self.betting_choice()
# Betting strategy
elif self.play_type == 2:
choice = self.basic_strategy(street=5)
# Custom strategy
elif self.play_type == 3:
choice = self.recommended_move(expected_return)
else:
choice = self.betting_choice()
# input("Move AI is going to make: {}. Continue?".format(choice))
#Raise 1x
if choice == 1:
self.bet(3, self.bet_amount)
#Raise 3x
elif choice == 2:
self.bet(3, self.bet_amount*3)
#Fold
else:
return False
#Deals 5th street
self.board.append(self.deck.pop())
return True
"""
expected_return is
Return 1 to 1x bet, 2 to 3x bet, -1 to fold.
"""
def recommended_move(self, expected_return):
print("Self.bets: {}".format(self.bets))
print("Negative: {}".format(-sum(self.bets)))
print("Expected return: {}".format(expected_return))
if -sum(self.bets) < expected_return:
print("Continue playing")
#Betting 3x is probably the winning move
# if sum(self.bets) <= expected_return:
if expected_return >= self.bet_amount:
print("Raise 3x")
return 2
#Betting 1x is the winning move
else:
return 1
#Fold
else:
return -1
def basic_strategy(self, street):
if street == 3:
sorted_player_hand = sorted(self.player_hand)
return self.simulate.move_3rd_street(sorted_player_hand, player_hand=self.player_hand, board=self.board)
elif street == 4:
return self.simulate.move_4th_street(player_hand=self.player_hand, board=self.board)
elif street == 5:
return self.simulate.move_5th_street(player_hand=self.player_hand, board=self.board, bets=self.bets)
else:
print("Invalid street specified for basic strategy")
return -1
"""
Gets user input on what action to taken in regards to betting
"""
def betting_choice(self):
choice = 0
while choice < 1 or choice > 3:
print()
print("Betting choice: ")
print("1) Raise 1x")
print("2) Raise 3x")
print("3) Fold")
try:
choice = int(input("Choice: "))
except:
choice = 0
return choice
"""
User chooses what play style they want to initiate
"""
def play_type_choice(self):
choice = 0
while choice < 1 or choice > 3:
print()
print("Play type choice: ")
print("1) Manual (Player chooses the moves)")
print("2) Auto basic strategy")
print("3) Auto custom strategy")
try:
choice = int(input("Choice: "))
except:
choice = 0
return choice
"""
places player's initial bets
"""
def initial_bets(self):
self.fold = False
#ante bet
self.bet(0, self.bet_amount)
#reset the rest of the streets
self.bets[1] = 0
self.bets[2] = 0
self.bets[3] = 0
"""
player makets bet of size amount at time street
"""
def bet(self, street, amount):
self.bets[street] = amount
self.bankroll -= amount
if __name__=="__main__":
play = Play()
play.play()