import gradio as gr import random import matplotlib.pyplot as plt from itertools import combinations from collections import Counter # Detailed poker hand strength evaluator 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) # Check for pairs, three-of-a-kind, etc. value_counts = Counter(values) most_common = value_counts.most_common() if len(set(card[1] for card in cards)) == 1: # Flush return (5, values) if most_common[0][1] == 4: # Four of a kind return (7, most_common[0][0], most_common[1][0]) if most_common[0][1] == 3 and most_common[1][1] == 2: # Full house return (6, most_common[0][0], most_common[1][0]) if len(set(values)) == 5 and values[0] - values[4] == 4: # Straight return (4, values[0]) if most_common[0][1] == 3: # Three of a kind return (3, most_common[0][0], values) if most_common[0][1] == 2 and most_common[1][1] == 2: # Two pair return (2, most_common[0][0], most_common[1][0], values) if most_common[0][1] == 2: # One pair return (1, most_common[0][0], values) return (0, values) # High card 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: # Discourage raising with weak hands 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): 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=1000, 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%}' # Gradio interface inputs = [ gr.inputs.Textbox(label="Starting Hand (comma-separated, e.g., 'AS,KD')"), gr.inputs.Textbox(label="Pot Size"), gr.inputs.Textbox(label="Player's Amount of Chips"), gr.inputs.Textbox(label="Number of Opponents"), gr.inputs.Textbox(label="Flop Cards (comma-separated, e.g., '2H,7D,9C')") ] outputs = [ gr.outputs.Textbox(label="Optimal Strategy"), gr.outputs.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()