|
import gradio as gr |
|
import random |
|
import matplotlib.pyplot as plt |
|
from itertools import combinations |
|
from collections import Counter |
|
|
|
|
|
def hand_rank(cards): |
|
ranks = '23456789TJQKA' |
|
rank_dict = {r: i for i, r in enumerate(ranks)} |
|
values = sorted([rank_dict[card[0]] for card in cards], reverse=True) |
|
|
|
|
|
value_counts = Counter(values) |
|
most_common = value_counts.most_common() |
|
|
|
if len(set(card[1] for card in cards)) == 1: |
|
return (5, values) |
|
if most_common[0][1] == 4: |
|
return (7, most_common[0][0], most_common[1][0]) |
|
if most_common[0][1] == 3 and most_common[1][1] == 2: |
|
return (6, most_common[0][0], most_common[1][0]) |
|
if len(set(values)) == 5 and values[0] - values[4] == 4: |
|
return (4, values[0]) |
|
if most_common[0][1] == 3: |
|
return (3, most_common[0][0], values) |
|
if most_common[0][1] == 2 and most_common[1][1] == 2: |
|
return (2, most_common[0][0], most_common[1][0], values) |
|
if most_common[0][1] == 2: |
|
return (1, most_common[0][0], values) |
|
return (0, values) |
|
|
|
class PokerSwarm: |
|
def __init__(self, n_particles, n_iterations, n_simulations, player_hand, pot_size, chips, n_opponents, flop_cards): |
|
self.n_particles = n_particles |
|
self.n_iterations = n_iterations |
|
self.n_simulations = n_simulations |
|
self.player_hand = player_hand |
|
self.pot_size = pot_size |
|
self.chips = chips |
|
self.n_opponents = n_opponents |
|
self.flop_cards = flop_cards |
|
self.swarm = [self.initialize_particle() for _ in range(self.n_particles)] |
|
|
|
def initialize_particle(self): |
|
return {'strategy': random.choice(['check', 'bet', 'raise', 'fold']), 'win_prob': 0} |
|
|
|
def simulate_hand(self, strategy): |
|
hand_strength = hand_rank(self.player_hand + self.flop_cards) |
|
opponent_strengths = [hand_rank(random.sample(deck, 2) + self.flop_cards) for _ in range(self.n_opponents)] |
|
|
|
if strategy == 'fold': |
|
return 0 |
|
player_wins = all(hand_strength > opponent_strength for opponent_strength in opponent_strengths) |
|
|
|
if strategy == 'check': |
|
return 1 if player_wins else 0 |
|
elif strategy == 'bet': |
|
return 1 if player_wins else 0 |
|
elif strategy == 'raise': |
|
if hand_strength[0] < 2: |
|
return 0 |
|
return 1 if player_wins else 0 |
|
return 0 |
|
|
|
def evaluate_strategy(self, strategy): |
|
wins = 0 |
|
for _ in range(self.n_simulations): |
|
result = self.simulate_hand(strategy) |
|
wins += result |
|
return wins / self.n_simulations |
|
|
|
def optimize(self): |
|
for iteration in range(self.n_iterations): |
|
for particle in self.swarm: |
|
particle['win_prob'] = self.evaluate_strategy(particle['strategy']) |
|
best_particle = max(self.swarm, key=lambda x: x['win_prob']) |
|
for particle in self.swarm: |
|
if particle != best_particle: |
|
particle['strategy'] = best_particle['strategy'] |
|
|
|
best_strategy = max(self.swarm, key=lambda x: x['win_prob']) |
|
return best_strategy['strategy'], best_strategy['win_prob'] |
|
|
|
def predict_optimal_strategy(player_hand, pot_size, chips, n_opponents, flop_cards, n_simulations): |
|
player_hand = player_hand.split(',') |
|
flop_cards = flop_cards.split(',') |
|
|
|
global deck |
|
deck = [r + s for r in '23456789TJQKA' for s in 'SHDC'] |
|
deck = [card for card in deck if card not in player_hand + flop_cards] |
|
|
|
poker_swarm = PokerSwarm(n_particles=30, n_iterations=50, n_simulations=int(n_simulations), player_hand=player_hand, pot_size=int(pot_size), chips=int(chips), n_opponents=int(n_opponents), flop_cards=flop_cards) |
|
optimal_strategy, optimal_win_prob = poker_swarm.optimize() |
|
return optimal_strategy, f'{optimal_win_prob:.2%}' |
|
|
|
|
|
inputs = [ |
|
gr.components.Textbox(label="Starting Hand (comma-separated, e.g., 'AS,KD')"), |
|
gr.components.Textbox(label="Pot Size"), |
|
gr.components.Textbox(label="Player's Amount of Chips"), |
|
gr.components.Textbox(label="Number of Opponents"), |
|
gr.components.Textbox(label="Flop Cards (comma-separated, e.g., '2H,7D,9C')"), |
|
gr.components.Textbox(label="Number of Simulated Hands") |
|
] |
|
|
|
outputs = [ |
|
gr.components.Textbox(label="Optimal Strategy"), |
|
gr.components.Textbox(label="Win Probability") |
|
] |
|
|
|
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() |