-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulate.py
1214 lines (955 loc) · 39.7 KB
/
Simulate.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (c) James Quintero 2020
#
# Last Modified: 12/2022
#
# Simulates playing with certain hands
import json
import csv
import os
import copy
import random
from HandStrength import HandStrength
from Utils import Utils
class Simulate:
hand_strength = 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
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.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
"""
Simulates the player having certain types of hands, like pairs, or one high card and one low card, etc. The stats should be the same between Kx5x and Jx2x since they are both one high and one low card.
"""
def simulate_important_cards(self):
player_hands = [
["13s", "13c"], #Winning pair
["13s", "12c"], #Two winning cards
["13c", "12c"], #Two winning cards suited
["13s", "9c"], #Winning card and push card
["13c", "9c"], #Winning card and push card suited
["13s", "5c"], #Winning card and losing card
["13c", "5c"], #Winning card and losing card suited
["9s", "9c"], #Pushing pair
["9s", "8c"], #Two pushing cards
["9c", "8c"], #Two pushing cards suited
["9s", "5c"], #Push card and losing card
["9c", "5c"], #Push card and losing card suited
["5s", "5c"], #Losing pair
["5s", "4c"], #Two losing cards
["5c", "4c"], #Two losing cards suited
]
cards_to_remove = [
[]
] * len(player_hands)
num_runs = int(input("Num runs: "))
self.simulate(num_runs, player_hands, cards_to_remove)
"""
Simulate change in odds if player has middle cards and other players have a lot of over cards.
"""
def simulate_over_cards(self):
player_hands = [
["10c", "9d"], #Two middle cards
["10c", "9d"], #Two middle cards
["10c", "9d"], #Two middle cards
["10c", "9d"], #Two middle cards
["10c", "9d"], #Two middle cards
["10c", "9d"], #Two middle cards
]
cards_to_remove = [
[],
["11x"], #Sees 1 over card
["11x", "14x", "13x"], #Sees 3 over cards
["11x", "14x", "13x", "12x", "11x", "14x"], #Sees 6 over cards
["11x", "14x", "13x", "12x", "11x", "14x", "13x", "12x"], #Sees 8 over cards
["11x", "14x", "13x", "12x", "11x", "14x", "13x", "12x", "11x", "14x"], #Sees 10 over cards
]
num_runs = int(input("Num runs: "))
play_optimally = input("Play optimally? (y/n): ").lower() == "y"
play_style = "optimal_play" if play_optimally else "play_to_end"
save_path = "./Results/simulate_{}_runs_over_cards_{}.csv".format(num_runs, play_style)
self.simulate(num_runs, player_hands, cards_to_remove, play_optimally=play_optimally, save_path=save_path)
"""
Simulate change in odds if player has middle cards and other players have a lot of lower cards
"""
def simulate_under_cards(self):
player_hands = [
["10c", "9d"], #Two middle cards
["10c", "9d"], #Two middle cards
["10c", "9d"], #Two middle cards
["10c", "9d"], #Two middle cards
["10c", "9d"], #Two middle cards
["10c", "9d"], #Two middle cards
]
cards_to_remove = [
[],
["2x"], #Sees 1 under card
["2x", "5x", "3x"], #Sees 3 under cards
["2x", "5x", "3x", "4x", "2x", "5x"], #Sees 6 under cards
["2x", "5x", "3x", "4x", "2x", "5x", "3x", "4x"], #Sees 8 under cards
["2x", "5x", "3x", "4x", "2x", "5x", "3x", "4x", "2x", "5x"], #Sees 10 under cards
]
num_runs = int(input("Num runs: "))
play_optimally = input("Play optimally? (y/n): ").lower() == "y"
play_style = "optimal_play" if play_optimally else "play_to_end"
save_path = "./Results/simulate_{}_runs_under_cards_{}.csv".format(num_runs, play_style)
self.simulate(num_runs, player_hands, cards_to_remove, play_optimally=play_optimally, save_path=save_path)
"""
Simulates change in odds if you see whether other players do or do not have the same high card.
"""
def simulate_high_cards(self):
player_hands = [
["13c", "11d"], #Two high cards
["13c", "11d"], #Two high card
["13c", "11d"], #Two high card
["13c", "11d"], #Two high card
["13c", "11d"], #Two high card
["13c", "11d"], #Two high card
["13c", "11d"], #Two high card
["13c", "11d"], #Two high card
["13c", "11d"], #Two high card
["13c", "2d"], #One high card
["13c", "2d"], #One high card
["13c", "2d"], #One high card
["13c", "2d"], #One high card
["13c", "2d"], #One high card
["13c", "2d"], #One high card
["13c", "2d"], #One high card
["13c", "2d"], #One high card
["13c", "2d"], #One high card
]
cards_to_remove = [
[],
["13x"], #See 1 card is same
["13x", "13x"], #See 2 cards are the same
["13x", "13x", "13x"], #See 3 other cards are the same
["2x"], #See 1 card not same type
["2x", "14x", "3x"], #See 3 cards not same type
["2x", "14x", "3x", "12x", "4x", "10x"], #See 6 cards not same type
["2x", "14x", "3x", "12x", "4x", "10x", "5x", "9x"], #See 8 cards not same type
["2x", "14x", "3x", "12x", "4x", "10x", "5x", "9x", "6x", "8x"], #See 10 cards not same type
[],
["13x"], #See 1 card is same
["13x", "13x"], #See 2 cards are the same
["13x", "13x", "13x"], #See 3 other cards are the same
["2x"], #See 1 card not same type
["2x", "14x", "3x"], #See 3 cards not same type
["2x", "14x", "3x", "12x", "4x", "10x"], #See 6 cards not same type
["2x", "14x", "3x", "12x", "4x", "10x", "5x", "9x"], #See 8 cards not same type
["2x", "14x", "3x", "12x", "4x", "10x", "5x", "9x", "6x", "8x"], #See 10 cards not same type
]
num_runs = int(input("Num runs: "))
play_optimally = input("Play optimally? (y/n): ").lower() == "y"
play_style = "optimal_play" if play_optimally else "play_to_end"
save_path = "./Results/simulate_{}_runs_high_cards_{}.csv".format(num_runs, play_style)
self.simulate(num_runs, player_hands, cards_to_remove, play_optimally=play_optimally, save_path=save_path)
"""
Simulates change in odds if you see whether other players do or do not have the same middle cards.
"""
def simulate_middle_cards(self):
player_hands = [
["10c", "6d"], #Two middle cards
["10c", "6d"], #Two middle cards
["10c", "6d"], #Two middle cards
["10c", "6d"], #Two middle cards
["10c", "6d"], #Two middle cards
["10c", "6d"], #Two middle cards
["10c", "6d"], #Two middle cards
["10c", "6d"], #Two middle cards
["10c", "6d"], #Two middle cards
["10c", "6d"], #Two middle cards
["10c", "6d"], #Two middle cards
]
cards_to_remove = [
[],
["10x"], #See 1 card is same
["10x", "10x"], #See 2 cards are the same
["10x", "10x", "10x"], #See 3 other cards are the same
["10x", "6x"], #See 2 cards are the same
["10x", "6x", "10x", "6x"], #See 3 other cards are the same
["2x"], #See 1 card not same type
["2x", "14x", "3x"], #See 3 cards not same type
["2x", "14x", "3x", "12x", "4x", "11x"], #See 6 cards not same type
["2x", "14x", "3x", "12x", "4x", "11x", "5x", "9x"], #See 8 cards not same type
["2x", "14x", "3x", "12x", "4x", "11x", "5x", "9x", "7x", "8x"], #See 10 cards not same type
]
num_runs = int(input("Num runs: "))
play_optimally = input("Play optimally? (y/n): ").lower() == "y"
play_style = "optimal_play" if play_optimally else "play_to_end"
save_path = "./Results/simulate_{}_runs_middle_cards_{}.csv".format(num_runs, play_style)
self.simulate(num_runs, player_hands, cards_to_remove, play_optimally=play_optimally, save_path=save_path)
"""
Simulates change in odds if you see whether other players do or do not have the same middle cards.
"""
def simulate_low_cards(self):
player_hands = [
["5c", "2d"], #Two low cards
["5c", "2d"], #Two low cards
["5c", "2d"], #Two low cards
["5c", "2d"], #Two low cards
["5c", "2d"], #Two low cards
["5c", "2d"], #Two low cards
]
cards_to_remove = [
[],
["3x"], #See 1 card not same type
["3x", "14x", "3x"], #See 3 cards not same type
["3x", "14x", "3x", "12x", "4x", "11x"], #See 6 cards not same type
["3x", "14x", "3x", "12x", "4x", "11x", "6x", "9x"], #See 8 cards not same type
["3x", "14x", "3x", "12x", "4x", "11x", "6x", "9x", "7x", "8x"], #See 10 cards not same type
]
num_runs = int(input("Num runs: "))
play_optimally = input("Play optimally? (y/n): ").lower() == "y"
play_style = "optimal_play" if play_optimally else "play_to_end"
save_path = "./Results/simulate_{}_runs_low_cards_{}.csv".format(num_runs, play_style)
self.simulate(num_runs, player_hands, cards_to_remove, play_optimally=play_optimally, save_path=save_path)
"""
No matter the hand, each suited hand has the same chance of getting a flush, so simulate that chance depending on seeing other player's cards.
"""
def simulate_flushes(self):
player_hands = [
["5c", "4c"], #Two losing cards suited
["5c", "4c"], #Two losing cards suited
["5c", "4c"], #Two losing cards suited
["5c", "4c"], #Two losing cards suited
["5c", "4c"], #Two losing cards suited
["5c", "4c"], #Two losing cards suited
["5c", "4c"], #Two losing cards suited
["5c", "4c"], #Two losing cards suited
["5c", "4c"], #Two losing cards suited
]
cards_to_remove = [
[],
["Xc"], #See 1 card with same suit
["Xc", "Xc", "Xc"], #See 3 cards with same suit
["Xc", "Xc", "Xc", "Xc", "Xc", "Xc"], #See 6 cards with same suit
["Xd"], #See 1 card without same suit
["Xd", "Xs", "Xh"], #See 3 cards with same suit
["Xd", "Xs", "Xh", "Xd", "Xs", "Xh"], #See 6 cards with same suit
["Xd", "Xs", "Xh", "Xd", "Xs", "Xh", "Xd", "Xs"], #See 8 cards with same suit
["Xd", "Xs", "Xh", "Xd", "Xs", "Xh", "Xd", "Xs", "Xh", "Xd"], #See 10 cards with same suit
]
num_runs = int(input("Num runs: "))
play_optimally = input("Play optimally? (y/n): ").lower() == "y"
play_style = "optimal_play" if play_optimally else "play_to_end"
save_path = "./Results/simulate_{}_runs_flush_cards_{}.csv".format(num_runs, play_style)
self.simulate(num_runs, player_hands, cards_to_remove, play_optimally=play_optimally, save_path=save_path)
"""
Simulates the player having all combinations of hands.
"""
def simulate_all_cards(self):
player_hands = [
["14s", "14c"],
["14s", "13c"],
["14s", "12c"],
["14s", "11c"],
["14s", "10c"],
["14s", "9c"],
["14s", "8c"],
["14s", "7c"],
["14s", "6c"],
["14s", "5c"],
["14s", "4c"],
["14s", "3c"],
["14s", "2c"],
["13s", "13c"],
["13s", "12c"],
["13s", "11c"],
["13s", "10c"],
["13s", "9c"],
["13s", "8c"],
["13s", "7c"],
["13s", "6c"],
["13s", "5c"],
["13s", "4c"],
["13s", "3c"],
["13s", "2c"],
["12s", "12c"],
["12s", "11c"],
["12s", "10c"],
["12s", "9c"],
["12s", "8c"],
["12s", "7c"],
["12s", "6c"],
["12s", "5c"],
["12s", "4c"],
["12s", "3c"],
["12s", "2c"],
["11s", "11c"],
["11s", "10c"],
["11s", "9c"],
["11s", "8c"],
["11s", "7c"],
["11s", "6c"],
["11s", "5c"],
["11s", "4c"],
["11s", "3c"],
["11s", "2c"],
["10s", "10c"],
["10s", "9c"],
["10s", "8c"],
["10s", "7c"],
["10s", "6c"],
["10s", "5c"],
["10s", "4c"],
["10s", "3c"],
["10s", "2c"],
["9s", "9c"],
["9s", "8c"],
["9s", "7c"],
["9s", "6c"],
["9s", "5c"],
["9s", "4c"],
["9s", "3c"],
["9s", "2c"],
["8s", "8c"],
["8s", "7c"],
["8s", "6c"],
["8s", "5c"],
["8s", "4c"],
["8s", "3c"],
["8s", "2c"],
["7s", "7c"],
["7s", "6c"],
["7s", "5c"],
["7s", "4c"],
["7s", "3c"],
["7s", "2c"],
["6s", "6c"],
["6s", "5c"],
["6s", "4c"],
["6s", "3c"],
["6s", "2c"],
["5s", "5c"],
["5s", "4c"],
["5s", "3c"],
["5s", "2c"],
["4s", "4c"],
["4s", "3c"],
["4s", "2c"],
["3s", "3c"],
["3s", "2c"],
["2s", "2c"],
#Suited
["14c", "13c"],
["14c", "12c"],
["14c", "11c"],
["14c", "10c"],
["14c", "9c"],
["14c", "8c"],
["14c", "7c"],
["14c", "6c"],
["14c", "5c"],
["14c", "4c"],
["14c", "3c"],
["14c", "2c"],
["13c", "12c"],
["13c", "11c"],
["13c", "10c"],
["13c", "9c"],
["13c", "8c"],
["13c", "7c"],
["13c", "6c"],
["13c", "5c"],
["13c", "4c"],
["13c", "3c"],
["13c", "2c"],
["12c", "11c"],
["12c", "10c"],
["12c", "9c"],
["12c", "8c"],
["12c", "7c"],
["12c", "6c"],
["12c", "5c"],
["12c", "4c"],
["12c", "3c"],
["12c", "2c"],
["11c", "10c"],
["11c", "9c"],
["11c", "8c"],
["11c", "7c"],
["11c", "6c"],
["11c", "5c"],
["11c", "4c"],
["11c", "3c"],
["11c", "2c"],
["10c", "9c"],
["10c", "8c"],
["10c", "7c"],
["10c", "6c"],
["10c", "5c"],
["10c", "4c"],
["10c", "3c"],
["10c", "2c"],
["9c", "8c"],
["9c", "7c"],
["9c", "6c"],
["9c", "5c"],
["9c", "4c"],
["9c", "3c"],
["9c", "2c"],
["8c", "7c"],
["8c", "6c"],
["8c", "5c"],
["8c", "4c"],
["8c", "3c"],
["8c", "2c"],
["7c", "6c"],
["7c", "5c"],
["7c", "4c"],
["7c", "3c"],
["7c", "2c"],
["6c", "5c"],
["6c", "4c"],
["6c", "3c"],
["6c", "2c"],
["5c", "4c"],
["5c", "3c"],
["5c", "2c"],
["4c", "3c"],
["4c", "2c"],
["3c", "2c"]
]
cards_to_remove = [
[]
] * len(player_hands)
num_runs = int(input("Num runs: "))
play_optimally = input("Play optimally? (y/n): ").lower() == "y"
self.simulate(num_runs, player_hands, cards_to_remove=cards_to_remove, play_optimally=play_optimally)
"""
Runs through all provided player hands to simulate many games for each hand.
"""
def simulate(self, num_runs = None, player_hands=[], board=[], cards_to_remove=[], play_optimally=None, save_path=None):
if num_runs == None:
num_runs = int(input("Num runs: "))
if play_optimally == None:
play_optimally = input("Play optimally? (y/n): ").lower() == "y"
to_save = [self.util.get_header()]
for x in range(0, len(player_hands)):
num_player_wins, num_dealer_wins, num_pushes, total_profit, ending_bankroll, hand_strengths = self.simulate_many_runs(num_runs, play_optimally, player_hand=player_hands[x], board=board, cards_to_remove=cards_to_remove[x])
row = [
','.join(self.util.convert_cards(player_hands[x])),
','.join(self.util.convert_cards(cards_to_remove[x])),
self.util.convert_number(num_player_wins/num_runs),
self.util.convert_number(num_dealer_wins/num_runs),
self.util.convert_number(num_pushes/num_runs),
self.util.convert_number(total_profit/num_runs),
ending_bankroll
]
row.extend([ (hand_strengths[key]/num_runs*100) for key in hand_strengths ])
to_save.append(row)
if play_optimally:
play_style = "optimal_play"
else:
play_style = "play_to_end"
if save_path == None:
save_path = "./Results/simulate_{}_runs_{}_cards_{}.csv".format(num_runs, len(player_hands), play_style)
self.util.save_to_csv(save_path, to_save)
"""
Simulates one game many times given the starting player_hand
"""
def simulate_many_runs(self, num_runs = 1000000, play_optimally=True, player_hand=[], board=[], cards_to_remove=[]):
if len(player_hand) != 0:
print("Starting player hand: "+str(self.util.convert_cards(player_hand)))
if len(cards_to_remove) != 0:
print("Cards in other player's hand: {}".format(self.util.convert_cards(cards_to_remove)))
self.reset()
num_player_wins = 0
num_dealer_wins = 0
num_pushes = 0
total_profit = 0
for x in range(0, num_runs):
starting_bankroll = self.bankroll
winner = self.simulate_single(play_optimally, player_hand=player_hand, board=board, cards_to_remove=cards_to_remove)
#player wins with pair of jacks or better
if winner == 1:
num_player_wins += 1
player_hand_strength = self.hand_strength.determine_hand_strength(self.board, self.player_hand)
self.bankroll += sum(self.bets)*self.util.payout[player_hand_strength[0]]
#pushes with pair of 6s to pair of 10s
elif winner == 0:
num_pushes += 1
self.bankroll += sum(self.bets)
#player lost
else:
num_dealer_wins += 1
ending_bankroll = self.bankroll
bankroll_diff = ending_bankroll - starting_bankroll
total_profit += bankroll_diff
if x != 0 and x % 100000 == 0:
print("Hand #"+str(x))
self.util.print_bankroll_state(self.starting_bankroll, self.bankroll, self.bet_amount)
print("Num player wins: {:,}".format(num_player_wins))
print("Num dealer wins: {:,}".format(num_dealer_wins))
print("Num pushes: {:,}".format(num_pushes))
print("Win %: {:.2f}%".format(num_player_wins/num_runs*100))
print("Lose %: {:.2f}%".format(num_dealer_wins/num_runs*100))
print("Push %: {:.2f}%".format(num_pushes/num_runs*100))
print("Avg return per hand: ${:,.2f}".format(total_profit/num_runs))
print()
self.util.print_hand_strength_distribution(self.hand_strength_distribution, num_runs)
print()
return num_player_wins, num_dealer_wins, num_pushes, total_profit, self.bankroll, copy.deepcopy(self.hand_strength_distribution)
"""
Simulates one game many times given the starting player_hand
"""
def simulate_many_runs_multiprocessing(self, num_runs = 1000, play_optimally=True, player_hand=[], board=[], cards_to_remove=[]):
if player_hand != None and len(player_hand) != 0:
print("Starting player hand: "+str(self.util.convert_cards(player_hand)))
if cards_to_remove != None and len(cards_to_remove) != 0:
print("Cards in other player's hand: {}".format(self.util.convert_cards(cards_to_remove)))
self.reset()
num_player_wins = 0
num_dealer_wins = 0
num_pushes = 0
total_profit = 0
import multiprocessing
parameters = [(play_optimally, player_hand, board, cards_to_remove) for _ in range(num_runs)]
num_processors = 32
pool = multiprocessing.Pool(num_processors)
results = pool.starmap(self.simulate_single_full, parameters)
pool.close()
pool.join()
# Initialize the variables
num_player_wins = 0
num_dealer_wins = 0
num_pushes = 0
total_profit = 0
# Iterate over the results and update the variables
for result in results:
winner, bankroll_diff, player_hand_strength = result
#player wins with pair of jacks or better
if winner == 1:
self.hand_strength_distribution[player_hand_strength] += 1
num_player_wins += 1
#pushes with pair of 6s to pair of 10s
elif winner == 0:
self.hand_strength_distribution[player_hand_strength] += 1
num_pushes += 1
#player lost
else:
num_dealer_wins += 1
self.bankroll += bankroll_diff
total_profit += bankroll_diff
# ending_bankroll = self.bankroll
# bankroll_diff = ending_bankroll - starting_bankroll
# total_profit += bankroll_diff
# if x != 0 and x % 100000 == 0:
# print("Hand #"+str(x))
# self.util.print_bankroll_state(self.starting_bankroll, self.bankroll, self.bet_amount)
# print("Num player wins: {:,}".format(num_player_wins))
# print("Num dealer wins: {:,}".format(num_dealer_wins))
# print("Num pushes: {:,}".format(num_pushes))
# print("Win %: {:.2f}%".format(num_player_wins/num_runs*100))
# print("Lose %: {:.2f}%".format(num_dealer_wins/num_runs*100))
# print("Push %: {:.2f}%".format(num_pushes/num_runs*100))
# print("Avg return per hand: ${:,.2f}".format(total_profit/num_runs))
# print()
# self.util.print_hand_strength_distribution(self.hand_strength_distribution, num_runs)
# print()
return num_player_wins, num_dealer_wins, num_pushes, total_profit, self.bankroll, copy.deepcopy(self.hand_strength_distribution)
"""
Returns change in bankroll
"""
def simulate_single_full(self, play_optimally=True, player_hand=None, board=[], cards_to_remove=[]):
starting_bankroll = self.bankroll
winner = self.simulate_single(play_optimally, player_hand=player_hand, board=board, cards_to_remove=cards_to_remove)
player_hand_strength = self.hand_strength.determine_hand_strength(self.board, self.player_hand)
# player_hand_strength = [0, 0]
#player wins with pair of jacks or better
if winner == 1:
self.bankroll += sum(self.bets)*self.util.payout[player_hand_strength[0]]
ending_bankroll = self.bankroll
bankroll_diff = ending_bankroll - starting_bankroll
return 1, bankroll_diff, player_hand_strength[0]
#pushes with pair of 6s to pair of 10s
elif winner == 0:
self.bankroll += sum(self.bets)
ending_bankroll = self.bankroll
bankroll_diff = ending_bankroll - starting_bankroll
return 0, bankroll_diff, player_hand_strength[0]
#player lost
else:
return -1, -sum(self.bets), 0
"""
Simulates a single game. Returns 1 if player wins, -1 if dealer wins, and 0 if push
"""
def simulate_single(self, play_optimally=True, player_hand=None, board=[], cards_to_remove=[]):
#shuffles the deck of cards
self.deck = self.util.initialize_deck()
#make bets
self.initial_bets()
#player gets random cards
if player_hand == None or len(player_hand) == 0:
self.player_hand[0] = self.deck.pop()
self.player_hand[1] = self.deck.pop()
#player gets certain cards
else:
self.player_hand = player_hand
self.deck.pop(self.deck.index(self.player_hand[0]))
self.deck.pop(self.deck.index(self.player_hand[1]))
#Remove cards that you would see in other player's hands
for card in cards_to_remove:
try:
#If type of card is wild, choose random card of that suit
if "X" in card:
card = self.util.get_cards_same_suit(self.deck, num_cards=1, suit=self.util.get_suit(card))[0]
#If suit is wild, choose random card of that type
elif "x" in card:
card = self.util.get_cards_same_type(self.deck, num_cards=1, type=self.util.get_type(card))[0]
self.deck.pop(self.deck.index(card))
except Exception as error:
input("Error: {}".format(error))
pass
#Shuffles since we might have removed flush cards from the bottom of the deck.
random.shuffle(self.deck)
#deal rest of cards
self.deal(initial_board=board)
if play_optimally:
#player bets optimally, according to wizard-of-odds
self.play_optimally()
else:
# self.play_3rd_str_then_optimally()
self.play_until_end()
#if player folded, just skip to the next hand
if self.fold:
return -1
# dealer_hand_strength = self.determine_hand_strength(self.board, self.dealer_hand)
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):
self.hand_strength_distribution[player_hand_strength[0]] += 1
return 1
#pushes with pair of 6s to pair of 10s
elif player_hand_strength[0] == 1 and player_hand_strength[1][0]>=6:
self.hand_strength_distribution[player_hand_strength[0]] += 1
return 0
#player lost
else:
#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
return -1
"""
Plays optimally according to wizard-of-odds
Looking at player's hand, every street is bet in a way to simulate actual gameplay
Ante is already bet
"""
def play_optimally(self):
if self.verbose:
print("Player's hand: "+str(self.player_hand))
board_to_print = ",".join([ self.util.convert_card(card) for card in self.board ])
print("Board: {}".format(board_to_print))
sorted_player_hand = sorted(self.player_hand)
success = self.bet_3rd_street(sorted_player_hand)
if not success:
return
success = self.bet_4th_street()
if not success:
return
success = self.bet_5th_street()
if not success:
return
"""
Plays 3rd street no matter what, then plays optimally according to wizard-of-odds
Looking at player's hand, every street is bet in a way to simulate actual gameplay
Ante is already bet
"""
def play_3rd_str_then_optimally(self):
if self.verbose:
print("Player's hand: "+str(self.player_hand))
board_to_print = ",".join([ self.util.convert_card(card) for card in self.board ])
print("Board: {}".format(board_to_print))
#Bets 3rd street no matter what
self.bet(1, self.bet_amount)
success = self.bet_4th_street()
if not success:
return
success = self.bet_5th_street()
if not success:
return
"""
Plays and never folds.
Bets minimum, although this method should only be used to calculate win-rate and not avg return.
"""
def play_until_end(self):
if self.verbose:
print("Player's hand: "+str(self.player_hand))
board_to_print = ",".join([ self.util.convert_card(card) for card in self.board ])
print("Board: {}".format(board_to_print))
sorted_player_hand = sorted(self.player_hand)
self.bet(1, self.bet_amount)
self.bet(2, self.bet_amount)
self.bet(3, self.bet_amount)
"""
Returns the basic strategy move for 3rd street (1st community card)
Strategy from wizardofodds.com
"""
def move_3rd_street(self, sorted_player_hand, player_hand, board):
###
#bets 3rd street
#To bet 3rd street, you can only see your cards, which is street="ante"
# Strategy
# - Raise 3x with any pair.
# - Raise 1x with at least two points.
# - Raise 1x with 6/5 suited.
# Fold all others.
points = self.get_num_points(street="ante", player_hand=player_hand, board=board)
hand_strength = self.hand_strength.determine_hand_strength(board=[], hand=player_hand)
#Raise 3x with any pair
if hand_strength[0] == 1:
return 2
elif hand_strength[0] >= 2 or (hand_strength[0]==1 and hand_strength[1][0]>=6):
return 2
#raise 1x with at least 2 points
elif points>=2:
return 1
#Raise 1x with 6/5 suited
elif sorted_player_hand[0][0]==5 and sorted_player_hand[1][0]==6 and sorted_player_hand[0][1]==sorted_player_hand[1][1]:
return 1
else:
return -1
def move_4th_street(self, player_hand, board):
###
# bets 4th card (4th street)
# To bet 2nd street, you can only see your cards and the first board card, which is street="3rd"
# - Raise 3x with any made hand (mid pair or higher).
# ?- Raise 3x with royal flush draw.
# ?- Raise 3x with straight flush draw, with no gaps, 567 or higher.
# ?- Raise 3x with straight flush draw, with one gap, and at least one high card.
# ?- Raise 3x with straight flush draw, with two gaps, and at least two high cards.
# - Raise 1x with any other three suited cards.
# - Raise 1x with a low pair.
# - Raise 1x with at least three points.
# Raise 1x with a straight draw, with no gaps, 456 or higher.
# Raise 1x with a straight draw, with one gap, and two mid cards.
# - Fold all others.
points = self.get_num_points(street="3rd", player_hand=player_hand, board=board)
hand_strength = self.hand_strength.determine_hand_strength(board=[board[0]], hand=player_hand)
#Raise 3x with any made hand (mid pair or higher)
if hand_strength[0] >= 2 or (hand_strength[0]==1 and hand_strength[1][0]>=6):
return 2
#Raise 1x with a low pair
elif hand_strength[0]==1:
return 1
#raise 1x with at least 3 points
elif points>=3:
return 1
#Raise 1x with any other three suited cards
elif self.util.get_suit(player_hand[0])==self.util.get_suit(player_hand[1]) and self.util.get_suit(player_hand[1])==self.util.get_suit(board[0]):
return 1
#Fold all others
else:
return -1
def move_5th_street(self, player_hand, board, bets):
###
# bets 5th card (5th street)
# - Raise 3x with any made hand (mid pair or higher).
# - Raise 3x with any four to a flush.
# Raise 3x with four to an outside straight, 8 high or better.
# Raise 1x with any other straight draw.
# - Raise 1x with a low pair.
# - Raise 1x with at least four points.
# - Raise 1x with three mid cards and at least one previous 3x raise.
# - Fold all others.
if len(board) < 2:
print("Error when determing move for 5th street, board is not proper length. {} vs expected 2".format(len(board)))
points = self.get_num_points(street="4th", player_hand=player_hand, board=board)