44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# games/blackjack/models/hand_evaluator.py
|
||
|
|
from models.card import Card
|
||
|
|
from resources.code_res.game_constants import ACE_RANK, BJ_TARGET_SCORE, SEVEN_RANK
|
||
|
|
|
||
|
|
|
||
|
|
class HandEvaluator:
|
||
|
|
@staticmethod
|
||
|
|
def evaluate(hand: list[Card]) -> tuple[int, bool]:
|
||
|
|
total = sum(c.blackjack_value for c in hand)
|
||
|
|
aces = sum(1 for c in hand if c.rank == ACE_RANK)
|
||
|
|
while total > BJ_TARGET_SCORE and aces > 0:
|
||
|
|
total -= 10
|
||
|
|
aces -= 1
|
||
|
|
is_soft = aces > 0 and total <= BJ_TARGET_SCORE
|
||
|
|
return total, is_soft
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def is_natural_blackjack(cls, hand: list[Card]) -> bool:
|
||
|
|
val, _ = cls.evaluate(hand)
|
||
|
|
return len(hand) == 2 and val == BJ_TARGET_SCORE
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def is_triple_seven(hand: list[Card]) -> bool:
|
||
|
|
"""3 Siebener auf der Hand = Auto-Blackjack & Sofortgewinn!"""
|
||
|
|
return sum(1 for c in hand if c.rank == SEVEN_RANK) >= 3
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def showdown_rank(cls, hand: list[Card]) -> tuple[int, int]:
|
||
|
|
"""
|
||
|
|
Vergleichswert für den Showdown (nur Hände <= 21):
|
||
|
|
(3, 21) = 7-7-7 Auto-Blackjack
|
||
|
|
(2, 21) = Natural Blackjack (2 Karten = 21)
|
||
|
|
(1, val) = Reguläre Punktzahl <= 21
|
||
|
|
(0, 0) = Überkauft (> 21)
|
||
|
|
"""
|
||
|
|
val, _ = cls.evaluate(hand)
|
||
|
|
if cls.is_triple_seven(hand):
|
||
|
|
return (3, 21)
|
||
|
|
if val > BJ_TARGET_SCORE:
|
||
|
|
return (0, 0)
|
||
|
|
if cls.is_natural_blackjack(hand):
|
||
|
|
return (2, 21)
|
||
|
|
return (1, val)
|