TuringsSolutions commited on
Commit
7e7eb87
1 Parent(s): 6331052

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ import matplotlib.pyplot as plt
4
+ from itertools import combinations
5
+ from collections import Counter
6
+
7
+ # Detailed poker hand strength evaluator
8
+ def hand_rank(cards):
9
+ ranks = '23456789TJQKA'
10
+ rank_dict = {r: i for i, r in enumerate(ranks)}
11
+ values = sorted([rank_dict[card[0]] for card in cards], reverse=True)
12
+
13
+ # Check for pairs, three-of-a-kind, etc.
14
+ value_counts = Counter(values)
15
+ most_common = value_counts.most_common()
16
+
17
+ if len(set(card[1] for card in cards)) == 1: # Flush
18
+ return (5, values)
19
+ if most_common[0][1] == 4: # Four of a kind
20
+ return (7, most_common[0][0], most_common[1][0])
21
+ if most_common[0][1] == 3 and most_common[1][1] == 2: # Full house
22
+ return (6, most_common[0][0], most_common[1][0])
23
+ if len(set(values)) == 5 and values[0] - values[4] == 4: # Straight
24
+ return (4, values[0])
25
+ if most_common[0][1] == 3: # Three of a kind
26
+ return (3, most_common[0][0], values)
27
+ if most_common[0][1] == 2 and most_common[1][1] == 2: # Two pair
28
+ return (2, most_common[0][0], most_common[1][0], values)
29
+ if most_common[0][1] == 2: # One pair
30
+ return (1, most_common[0][0], values)
31
+ return (0, values) # High card
32
+
33
+ class PokerSwarm:
34
+ def __init__(self, n_particles, n_iterations, n_simulations, player_hand, pot_size, chips, n_opponents, flop_cards):
35
+ self.n_particles = n_particles
36
+ self.n_iterations = n_iterations
37
+ self.n_simulations = n_simulations
38
+ self.player_hand = player_hand
39
+ self.pot_size = pot_size
40
+ self.chips = chips
41
+ self.n_opponents = n_opponents
42
+ self.flop_cards = flop_cards
43
+ self.swarm = [self.initialize_particle() for _ in range(self.n_particles)]
44
+
45
+ def initialize_particle(self):
46
+ return {'strategy': random.choice(['check', 'bet', 'raise', 'fold']), 'win_prob': 0}
47
+
48
+ def simulate_hand(self, strategy):
49
+ hand_strength = hand_rank(self.player_hand + self.flop_cards)
50
+ opponent_strengths = [hand_rank(random.sample(deck, 2) + self.flop_cards) for _ in range(self.n_opponents)]
51
+
52
+ if strategy == 'fold':
53
+ return 0
54
+ player_wins = all(hand_strength > opponent_strength for opponent_strength in opponent_strengths)
55
+
56
+ if strategy == 'check':
57
+ return 1 if player_wins else 0
58
+ elif strategy == 'bet':
59
+ return 1 if player_wins else 0
60
+ elif strategy == 'raise':
61
+ if hand_strength[0] < 2: # Discourage raising with weak hands
62
+ return 0
63
+ return 1 if player_wins else 0
64
+ return 0
65
+
66
+ def evaluate_strategy(self, strategy):
67
+ wins = 0
68
+ for _ in range(self.n_simulations):
69
+ result = self.simulate_hand(strategy)
70
+ wins += result
71
+ return wins / self.n_simulations
72
+
73
+ def optimize(self):
74
+ for iteration in range(self.n_iterations):
75
+ for particle in self.swarm:
76
+ particle['win_prob'] = self.evaluate_strategy(particle['strategy'])
77
+ best_particle = max(self.swarm, key=lambda x: x['win_prob'])
78
+ for particle in self.swarm:
79
+ if particle != best_particle:
80
+ particle['strategy'] = best_particle['strategy']
81
+
82
+ best_strategy = max(self.swarm, key=lambda x: x['win_prob'])
83
+ return best_strategy['strategy'], best_strategy['win_prob']
84
+
85
+ def predict_optimal_strategy(player_hand, pot_size, chips, n_opponents, flop_cards):
86
+ player_hand = player_hand.split(',')
87
+ flop_cards = flop_cards.split(',')
88
+
89
+ global deck
90
+ deck = [r + s for r in '23456789TJQKA' for s in 'SHDC']
91
+ deck = [card for card in deck if card not in player_hand + flop_cards]
92
+
93
+ poker_swarm = PokerSwarm(n_particles=30, n_iterations=50, n_simulations=1000, player_hand=player_hand, pot_size=int(pot_size), chips=int(chips), n_opponents=int(n_opponents), flop_cards=flop_cards)
94
+ optimal_strategy, optimal_win_prob = poker_swarm.optimize()
95
+ return optimal_strategy, f'{optimal_win_prob:.2%}'
96
+
97
+ # Gradio interface
98
+ inputs = [
99
+ gr.inputs.Textbox(label="Starting Hand (comma-separated, e.g., 'AS,KD')"),
100
+ gr.inputs.Textbox(label="Pot Size"),
101
+ gr.inputs.Textbox(label="Player's Amount of Chips"),
102
+ gr.inputs.Textbox(label="Number of Opponents"),
103
+ gr.inputs.Textbox(label="Flop Cards (comma-separated, e.g., '2H,7D,9C')")
104
+ ]
105
+
106
+ outputs = [
107
+ gr.outputs.Textbox(label="Optimal Strategy"),
108
+ gr.outputs.Textbox(label="Win Probability")
109
+ ]
110
+
111
+ gr.Interface(fn=predict_optimal_strategy, inputs=inputs, outputs=outputs, title="Poker Strategy Optimizer", description="Enter your poker hand and other details to get the optimal strategy and win probability.").launch()