192 lines
7.6 KiB
Python
192 lines
7.6 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# games/poker/models/poker_bot_ai.py
|
||
|
|
import random
|
||
|
|
from games.poker.models.poker_evaluator import PokerEvaluator
|
||
|
|
from games.poker.models.poker_seat import PokerSeat
|
||
|
|
from models.card import Card
|
||
|
|
from resources.code_res.game_constants import BOT_DEITY_LEVEL, PK_STARTING_GOLD, RANKS, SUITS
|
||
|
|
|
||
|
|
|
||
|
|
class PokerBotAI:
|
||
|
|
"""
|
||
|
|
6-Max No-Limit Hold'em KI:
|
||
|
|
- Stufe 0..5: Vom Zufalls-Fisch bis zum tight-aggressiven Haifisch
|
||
|
|
- Stufe 6 (BOT_DEITY_LEVEL): Monte-Carlo Equity vs. exakte Pot-Odds + kalkulierter Semi-Bluff-Druck
|
||
|
|
"""
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def enforce_online_deity_bots(bot_configs: list[tuple[int, str]]) -> list[tuple[int, str]]:
|
||
|
|
return [(BOT_DEITY_LEVEL, name) for _lvl, name in bot_configs]
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def ensure_bot_stack(seat: PokerSeat, min_entry: int, fallback_stack: int = PK_STARTING_GOLD):
|
||
|
|
if seat.stack < min_entry:
|
||
|
|
seat.stack = fallback_stack
|
||
|
|
seat.rebought = True
|
||
|
|
else:
|
||
|
|
seat.rebought = False
|
||
|
|
|
||
|
|
def decide_action(
|
||
|
|
self,
|
||
|
|
bot: PokerSeat,
|
||
|
|
board: list[Card],
|
||
|
|
current_bet: int,
|
||
|
|
pot: int,
|
||
|
|
min_raise_delta: int,
|
||
|
|
chips: tuple[int, ...],
|
||
|
|
active_opponents: int,
|
||
|
|
) -> tuple[str, int]:
|
||
|
|
"""
|
||
|
|
Gibt ('FOLD' | 'CHECK' | 'CALL' | 'RAISE' | 'ALLIN', raise_delta) zurück.
|
||
|
|
"""
|
||
|
|
to_call = max(0, current_bet - bot.street_bet)
|
||
|
|
lvl = bot.ai_level
|
||
|
|
|
||
|
|
# Stufe 0: Verwirrte Seele (Zufall)
|
||
|
|
if lvl == 0:
|
||
|
|
if to_call == 0:
|
||
|
|
if bot.stack >= chips[0] and random.random() < 0.22:
|
||
|
|
return ("RAISE", max(min_raise_delta, chips[0]))
|
||
|
|
return ("CHECK", 0)
|
||
|
|
if random.random() < 0.28:
|
||
|
|
return ("FOLD", 0)
|
||
|
|
if to_call >= bot.stack:
|
||
|
|
return ("ALLIN", 0)
|
||
|
|
if bot.stack > to_call + chips[0] and random.random() < 0.15:
|
||
|
|
return ("RAISE", max(min_raise_delta, chips[0]))
|
||
|
|
return ("CALL", 0)
|
||
|
|
|
||
|
|
# Equity / Handstärke [0.0 .. 1.0] berechnen
|
||
|
|
samples = {1: 18, 2: 28, 3: 45, 4: 70, 5: 100}.get(lvl, 150)
|
||
|
|
equity = self._estimate_equity(bot.hole_cards, board, max(1, active_opponents), samples)
|
||
|
|
|
||
|
|
# Effektive Pot-Odds berechnen
|
||
|
|
eff_call = min(bot.stack, to_call)
|
||
|
|
pot_odds = eff_call / max(1, pot + eff_call) if eff_call > 0 else 0.0
|
||
|
|
|
||
|
|
# Stufen-Charakteristik
|
||
|
|
if lvl == 1:
|
||
|
|
# Vorsichtiger Akolyth (spielt passiv, callt zu oft kleine Bets, erhöht selten)
|
||
|
|
if to_call > 0 and equity + 0.08 < pot_odds and random.random() < 0.80:
|
||
|
|
return ("FOLD", 0)
|
||
|
|
if equity > 0.78 and bot.stack > to_call + chips[0] and random.random() < 0.45:
|
||
|
|
return ("RAISE", max(min_raise_delta, chips[0]))
|
||
|
|
return self._check_or_call(bot, to_call)
|
||
|
|
|
||
|
|
if lvl in (2, 3):
|
||
|
|
margin = 0.04 if lvl == 2 else 0.01
|
||
|
|
if to_call > 0 and equity + margin < pot_odds:
|
||
|
|
return ("FOLD", 0)
|
||
|
|
if equity >= 0.68 and bot.stack > to_call:
|
||
|
|
return self._pick_raise(bot, to_call, min_raise_delta, chips, pot, equity, aggressive=(lvl == 3))
|
||
|
|
return self._check_or_call(bot, to_call)
|
||
|
|
|
||
|
|
if lvl in (4, 5):
|
||
|
|
# TAG / Kartenzähler-Hai
|
||
|
|
bluff_chance = 0.08 if lvl == 4 else 0.13
|
||
|
|
if to_call > 0:
|
||
|
|
if equity < pot_odds - 0.02:
|
||
|
|
if to_call <= chips[0] and random.random() < bluff_chance and bot.stack > to_call + min_raise_delta:
|
||
|
|
return ("RAISE", max(min_raise_delta, chips[0]))
|
||
|
|
return ("FOLD", 0)
|
||
|
|
if equity >= 0.62 or (to_call == 0 and random.random() < bluff_chance):
|
||
|
|
return self._pick_raise(bot, to_call, min_raise_delta, chips, pot, equity, aggressive=True)
|
||
|
|
return self._check_or_call(bot, to_call)
|
||
|
|
|
||
|
|
# --- STUFE 6: GOTTHEIT (Gnadenlose EV-Maximierung + Polarized Bluffs) ---
|
||
|
|
opp_scale = 1.0 / max(1, active_opponents)
|
||
|
|
strong_thresh = max(0.54, opp_scale + 0.20)
|
||
|
|
|
||
|
|
if to_call > 0:
|
||
|
|
# Exakter Pot-Odds + Implied-Odds Cut
|
||
|
|
implied_bonus = 0.035 if len(board) < 5 else 0.0
|
||
|
|
if equity + implied_bonus < pot_odds:
|
||
|
|
# Gezielter Re-Steal Bluff bei kleinem Einsatz auf späten Straßen
|
||
|
|
if eff_call <= max(chips[0], pot // 4) and bot.stack > to_call + min_raise_delta and random.random() < 0.14:
|
||
|
|
return ("RAISE", max(min_raise_delta, chips[min(1, len(chips) - 1)]))
|
||
|
|
return ("FOLD", 0)
|
||
|
|
|
||
|
|
if equity >= strong_thresh + 0.14 and bot.stack > to_call:
|
||
|
|
if equity >= 0.84 and random.random() < 0.45:
|
||
|
|
return ("ALLIN", 0)
|
||
|
|
return self._pick_raise(bot, to_call, min_raise_delta, chips, pot, equity, aggressive=True)
|
||
|
|
return self._check_or_call(bot, to_call)
|
||
|
|
|
||
|
|
# Gottheit ohne offenen Einsatz (Check oder Attacke)
|
||
|
|
if equity >= strong_thresh or random.random() < 0.18:
|
||
|
|
return self._pick_raise(bot, 0, min_raise_delta, chips, pot, equity, aggressive=True)
|
||
|
|
return ("CHECK", 0)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _check_or_call(bot: PokerSeat, to_call: int) -> tuple[str, int]:
|
||
|
|
if to_call <= 0:
|
||
|
|
return ("CHECK", 0)
|
||
|
|
if to_call >= bot.stack:
|
||
|
|
return ("ALLIN", 0)
|
||
|
|
return ("CALL", 0)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _pick_raise(
|
||
|
|
bot: PokerSeat,
|
||
|
|
to_call: int,
|
||
|
|
min_raise_delta: int,
|
||
|
|
chips: tuple[int, ...],
|
||
|
|
pot: int,
|
||
|
|
equity: float,
|
||
|
|
aggressive: bool,
|
||
|
|
) -> tuple[str, int]:
|
||
|
|
avail_for_raise = bot.stack - to_call
|
||
|
|
if avail_for_raise <= 0:
|
||
|
|
return ("ALLIN", 0) if to_call >= bot.stack else ("CALL", 0)
|
||
|
|
if avail_for_raise <= min_raise_delta:
|
||
|
|
return ("ALLIN", 0)
|
||
|
|
|
||
|
|
target = max(min_raise_delta, int(pot * (0.75 if aggressive else 0.5)))
|
||
|
|
affordable = [c for c in chips if min_raise_delta <= c <= avail_for_raise]
|
||
|
|
if affordable:
|
||
|
|
chosen = min(affordable, key=lambda c: abs(c - target))
|
||
|
|
if equity >= 0.82 and aggressive:
|
||
|
|
chosen = affordable[min(len(affordable) - 1, 2)]
|
||
|
|
else:
|
||
|
|
chosen = min_raise_delta
|
||
|
|
|
||
|
|
if chosen >= avail_for_raise:
|
||
|
|
return ("ALLIN", 0)
|
||
|
|
return ("RAISE", chosen)
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def _estimate_equity(cls, hole: list[Card], board: list[Card], num_opps: int, iterations: int) -> float:
|
||
|
|
if len(hole) < 2:
|
||
|
|
return 0.0
|
||
|
|
|
||
|
|
known = {(c.suit, c.rank) for c in hole} | {(c.suit, c.rank) for c in board}
|
||
|
|
rem_deck = [Card(s, r) for s in SUITS for r in RANKS if (s, r) not in known]
|
||
|
|
need_board = 5 - len(board)
|
||
|
|
cards_per_sim = need_board + 2 * num_opps
|
||
|
|
|
||
|
|
if len(rem_deck) < cards_per_sim:
|
||
|
|
return 0.5
|
||
|
|
|
||
|
|
wins = 0.0
|
||
|
|
for _ in range(iterations):
|
||
|
|
sampled = random.sample(rem_deck, cards_per_sim)
|
||
|
|
sim_board = board + sampled[:need_board]
|
||
|
|
my_score = PokerEvaluator.evaluate_best(hole + sim_board)
|
||
|
|
|
||
|
|
idx = need_board
|
||
|
|
beaten = False
|
||
|
|
tied = 0
|
||
|
|
for _o in range(num_opps):
|
||
|
|
opp_hole = sampled[idx:idx + 2]
|
||
|
|
idx += 2
|
||
|
|
opp_score = PokerEvaluator.evaluate_best(opp_hole + sim_board)
|
||
|
|
if opp_score > my_score:
|
||
|
|
beaten = True
|
||
|
|
break
|
||
|
|
if opp_score == my_score:
|
||
|
|
tied += 1
|
||
|
|
|
||
|
|
if not beaten:
|
||
|
|
wins += 1.0 / (1 + tied)
|
||
|
|
|
||
|
|
return wins / iterations
|