File size: 4,821 Bytes
7e7eb87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab9fb22
 
 
 
 
7e7eb87
 
 
ab9fb22
 
7e7eb87
 
ab9fb22
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
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.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')")
]

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()