# -*- coding: utf-8 -*- # games/blackjack/models/bot_ai.py import random from games.blackjack.models.bot_seat import BlackjackBotSeat from games.blackjack.models.hand_evaluator import HandEvaluator from models.card import Card from resources.code_res.game_constants import ( ACE_RANK, BJ_MAX_RAISES_PER_ROUND, BJ_STARTING_GOLD, BOT_DEITY_LEVEL, FACE_RANKS, SEVEN_RANK ) class BlackjackBotAI: """ 4-Stufen-Ablaufbaum KI: - Stufe 0..5: Abgestufte Offline-Gegner - Stufe 6 (BOT_DEITY_LEVEL): Entfesselte Gottheit-KI (Online-Standard, kein Kindergarten!) """ def __init__(self): self.running_count: int = 0 @staticmethod def enforce_online_deity_bots(bot_configs: list[tuple[int, str]]) -> list[tuple[int, str]]: """Zwingt im Online-Modus ausnahmslos alle Bots auf Gottheit-Niveau (Stufe 6).""" return [(BOT_DEITY_LEVEL, name) for _lvl, name in bot_configs] def reset_count(self): self.running_count = 0 def observe_public_card(self, card: Card): if card.rank in ("2", "3", "4", "5", "6"): self.running_count += 1 elif card.rank in ("10", *FACE_RANKS, ACE_RANK): self.running_count -= 1 def get_true_count(self, remaining_cards: int) -> float: decks_left = max(0.5, remaining_cards / 52.0) return self.running_count / decks_left def ensure_ante_gold(self, bot: BlackjackBotSeat, min_bet: int, fallback_stack: int = BJ_STARTING_GOLD): if bot.gold < min_bet: bot.gold = fallback_stack bot.rebought = True else: bot.rebought = False # --- KNOTEN 1: Mitgehen? --- def decide_step1_mitgehen( self, bot: BlackjackBotSeat, highest_bet: int, dealer_upcard: Card | None = None, remaining_cards: int = 52 ) -> bool: val, is_soft = HandEvaluator.evaluate(bot.hand) to_call = max(0, highest_bet - bot.bet_in_pot) lvl = bot.ai_level if to_call > 0 and bot.gold > 0: if lvl == 0: return random.random() < 0.75 # Gottheit-Stufe (6): Rechnet Pot-Odds + Dealer-Upcard + True Count knallhart gegen! if lvl >= BOT_DEITY_LEVEL: if val >= 19 or (not is_soft and val in (10, 11)) or is_soft: return True up = dealer_upcard.blackjack_value if dealer_upcard else 10 tc = self.get_true_count(remaining_cards) # Gegen starke Dealer-Upcard (9, 10, A) mit steifer 14-16 und teurem Raise -> eiskalter Fold if 13 <= val <= 16 and up >= 8 and to_call >= max(10, highest_bet // 4): return False if val == 17 and up >= 10 and to_call >= max(15, highest_bet // 2) and tc < 1.0: return False return True if val >= 18 or val in (10, 11) or is_soft: return True if val <= 16 and to_call >= max(10, highest_bet // 3) and lvl >= 2: return random.random() < 0.30 return True if bot.cards_locked: # Gottheit bleibt mit 20/21 im Zug, falls noch Raisedruck aufgebaut werden kann! if lvl >= BOT_DEITY_LEVEL and val >= 20: return True return False if not is_soft and val >= 19 and 1 <= lvl < BOT_DEITY_LEVEL and random.random() < 0.65: return False return True # --- KNOTEN 2: Ziehen? (inkl. Double Down Option) --- def wants_double_down( self, bot: BlackjackBotSeat, dealer_upcard: Card, highest_bet: int, raises_in_round: int, remaining_cards: int = 52, ) -> bool: if bot.cards_locked or len(bot.hand) != 2 or bot.ai_level < 2: return False if raises_in_round >= BJ_MAX_RAISES_PER_ROUND: return False double_cost = max(highest_bet, bot.bet_in_pot) * 2 - bot.bet_in_pot if bot.gold < double_cost or double_cost <= 0: return False val, is_soft = HandEvaluator.evaluate(bot.hand) up = dealer_upcard.blackjack_value if bot.ai_level >= BOT_DEITY_LEVEL: tc = self.get_true_count(remaining_cards) if not is_soft and val == 11 and (up <= 10 or tc >= 1.0): return True if not is_soft and val == 10 and (up <= 9 or (up == 10 and tc >= 4.0)): return True if not is_soft and val == 9 and (3 <= up <= 6 or (up == 2 and tc >= 1.0)): return True if is_soft and val in (17, 18) and 3 <= up <= 6: return True return False if not is_soft and val == 11 and up <= 10: return True if not is_soft and val == 10 and up <= 9 and bot.ai_level >= 3: return True return False def decide_step2_ziehen( self, bot: BlackjackBotSeat, dealer_upcard: Card, remaining_cards: int ) -> bool: if bot.cards_locked: return False val, is_soft = HandEvaluator.evaluate(bot.hand) up = dealer_upcard.blackjack_value return self._wants_hit(bot.ai_level, bot.hand, val, is_soft, up, remaining_cards) # --- KNOTEN 3: Erhöhen? --- def decide_step3_erhoehen( self, bot: BlackjackBotSeat, chips: tuple[int, ...], remaining_cards: int, raises_in_round: int, dealer_upcard: Card | None = None, ) -> int: if raises_in_round >= BJ_MAX_RAISES_PER_ROUND or bot.gold < chips[0]: return 0 val, is_soft = HandEvaluator.evaluate(bot.hand) lvl = bot.ai_level if lvl == 0: return chips[0] if random.random() < 0.18 else 0 if lvl == 1: return chips[0] if val == 21 and random.random() < 0.35 else 0 if lvl in (2, 3): return chips[0] if val >= 20 and random.random() < 0.40 else 0 if lvl == 4: if val == 21 and random.random() < 0.60: return min(bot.gold, chips[min(1, len(chips) - 1)]) if val == 20 and random.random() < 0.35: return chips[0] return 0 tc = self.get_true_count(remaining_cards) if lvl == 5: if val == 21 and random.random() < 0.75: pick = chips[min(2, len(chips) - 1)] if tc >= 1.5 else chips[min(1, len(chips) - 1)] return min(bot.gold, pick) if val == 20 and random.random() < 0.50: return min(bot.gold, chips[0]) return 0 # --- STUFE 6: GOTTHEIT (Gnadenloser Druck bei Edge!) --- up = dealer_upcard.blackjack_value if dealer_upcard else 10 affordable = [c for c in chips if c <= bot.gold] if not affordable: return 0 if val == 21: # Mit 21 feuert Gottheit fast immer den höchsten bezahlbaren Chip rein! return affordable[-1] if tc >= 0.0 else affordable[min(2, len(affordable) - 1)] if val == 20: if up <= 9 or tc >= 1.0: return affordable[min(2, len(affordable) - 1)] return affordable[0] if val == 19 and (2 <= up <= 6) and not is_soft: # Dealer zeigt Bust-Karte (2-6) und Gottheit hält 19 -> Gegner bluten lassen return affordable[min(1, len(affordable) - 1)] if (2 <= up <= 6) and val >= 18 and tc >= 2.0: return affordable[0] return 0 # --- KNOTEN 4: Fertig? --- def decide_step4_fertig( self, bot: BlackjackBotSeat, dealer_upcard: Card, remaining_cards: int ) -> bool: if bot.cards_locked: return True val, is_soft = HandEvaluator.evaluate(bot.hand) if val >= 21: return True up = dealer_upcard.blackjack_value return not self._wants_hit(bot.ai_level, bot.hand, val, is_soft, up, remaining_cards) def _wants_hit( self, lvl: int, hand: list[Card], val: int, is_soft: bool, up: int, remaining_cards: int ) -> bool: if val >= 21: return False if len(hand) == 2 and sum(1 for c in hand if c.rank == SEVEN_RANK) == 2 and lvl >= 2: return True if lvl == 0: return val < 20 and random.choice([True, False]) if lvl == 1: return val < 15 if lvl == 2: return val < 17 if lvl == 3: if not is_soft and val >= 13 and 2 <= up <= 6: return False return val < 17 if lvl == 4: return self._basic_strategy_hit(val, is_soft, up) tc = self.get_true_count(remaining_cards) if lvl == 5: if not is_soft and val == 16 and up == 10 and tc >= 0.5: return False if not is_soft and val == 15 and up == 10 and tc >= 4.0: return False if not is_soft and val == 12 and up == 3 and tc >= 2.0: return False return self._basic_strategy_hit(val, is_soft, up) # --- STUFE 6: GOTTHEIT (Volle Illustrious-18 Index-Abweichungen) --- if not is_soft: if val == 16 and up == 10: return tc < 0.0 if val == 15 and up == 10: return tc < 4.0 if val == 16 and up == 9: return tc < 5.0 if val == 13 and up == 2: return tc < -1.0 if val == 13 and up == 3: return tc < -2.0 if val == 12 and up == 2: return tc < 3.0 if val == 12 and up == 3: return tc < 2.0 if val == 12 and up == 4: return tc < 0.0 if val == 12 and up == 5: return tc < -2.0 if val == 12 and up == 6: return tc < -1.0 return self._basic_strategy_hit(val, is_soft, up) @staticmethod def _basic_strategy_hit(val: int, is_soft: bool, up: int) -> bool: if is_soft: if val >= 19: return False if val == 18: return up >= 9 return True if val >= 17: return False if 13 <= val <= 16: return up >= 7 if val == 12: return not (4 <= up <= 6) return True