the_goth_cardgame_collection/games/blackjack/models/bot_ai.py

236 lines
8.7 KiB
Python

# -*- 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_STARTING_GOLD, BOT_DEITY_LEVEL, FACE_RANKS, SEVEN_RANK
)
class BlackjackBotAI:
"""
3-Stufen-Ablaufbaum KI (ohne Endlos-Bieten):
1. Mitgehen? (falls Erhöhung offen) -> 2. Karte ziehen / Keine Karte -> 3. Erhöhen?
Zieht im gesamten Tisch-Umlauf niemand eine Karte, wird automatisch aufgedeckt!
"""
def __init__(self):
self.running_count: int = 0
@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]
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: Offene Erhöhung mitgehen oder austreten? ---
def decide_step1_mitgehen(
self, bot: BlackjackBotSeat, highest_bet: int, dealer_upcard: Card | None = None, remaining_cards: int = 52
) -> bool:
to_call = max(0, highest_bet - bot.bet_in_pot)
if to_call <= 0 or bot.gold <= 0:
return True
val, is_soft = HandEvaluator.evaluate(bot.hand)
lvl = bot.ai_level
if lvl == 0:
return random.random() < 0.75
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)
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
# --- KNOTEN 2: Karte ziehen oder keine neue Karte? ---
def wants_double_down(
self, bot: BlackjackBotSeat, dealer_upcard: Card, highest_bet: int, remaining_cards: int = 52
) -> bool:
if bot.cards_locked or bot.doubled or len(bot.hand) != 2 or bot.ai_level < 2:
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 or bot.doubled:
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: Einsatz erhöhen? (nur im regulären Zug, nicht im reinen Ausgleich!) ---
def decide_step3_erhoehen(
self,
bot: BlackjackBotSeat,
chips: tuple[int, ...],
remaining_cards: int,
dealer_upcard: Card | None = None,
) -> int:
if 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
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:
return affordable[-1] if tc >= 0.0 else affordable[min(2, len(affordable) - 1)]
if val == 20:
return affordable[min(2, len(affordable) - 1)] if (up <= 9 or tc >= 1.0) else affordable[0]
if val == 19 and (2 <= up <= 6) and not is_soft:
return affordable[min(1, len(affordable) - 1)]
if (2 <= up <= 6) and val >= 18 and tc >= 2.0:
return affordable[0]
return 0
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 (Illustrious-18)
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