87 lines
3.0 KiB
Python
87 lines
3.0 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# games/poker/models/poker_evaluator.py
|
||
|
|
from collections import Counter
|
||
|
|
from itertools import combinations
|
||
|
|
from models.card import Card
|
||
|
|
|
||
|
|
RANK_VAL = {
|
||
|
|
"2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8,
|
||
|
|
"9": 9, "10": 10, "J": 11, "Q": 12, "K": 13, "A": 14,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
class PokerEvaluator:
|
||
|
|
"""Bewertet 2..7 Karten nach offiziellen Texas Hold'em Hand-Rängen (0..9)."""
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def card_val(cls, card: Card) -> int:
|
||
|
|
return RANK_VAL[card.rank]
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def evaluate_best(cls, cards: list[Card]) -> tuple[int, tuple[int, ...]]:
|
||
|
|
"""
|
||
|
|
Gibt (category_0_to_9, tiebreaker_tuple) zurück.
|
||
|
|
Größeres Tupel gewinnt immer direkt per Python-Tupelvergleich!
|
||
|
|
"""
|
||
|
|
if len(cards) < 5:
|
||
|
|
return cls._eval_partial(cards)
|
||
|
|
return max(cls._eval_five(combo) for combo in combinations(cards, 5))
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def _eval_partial(cls, cards: list[Card]) -> tuple[int, tuple[int, ...]]:
|
||
|
|
vals = sorted((RANK_VAL[c.rank] for c in cards), reverse=True)
|
||
|
|
counts = Counter(vals)
|
||
|
|
ordered = sorted(counts.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
|
||
|
|
top_cnt = ordered[0][1] if ordered else 0
|
||
|
|
kickers = tuple(v for v, _c in ordered)
|
||
|
|
if top_cnt == 4:
|
||
|
|
return (7, kickers)
|
||
|
|
if top_cnt == 3:
|
||
|
|
return (3, kickers)
|
||
|
|
if top_cnt == 2:
|
||
|
|
if len(ordered) > 1 and ordered[1][1] == 2:
|
||
|
|
return (2, kickers)
|
||
|
|
return (1, kickers)
|
||
|
|
return (0, tuple(vals))
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def _eval_five(cls, five_cards: tuple[Card, ...]) -> tuple[int, tuple[int, ...]]:
|
||
|
|
vals = sorted((RANK_VAL[c.rank] for c in five_cards), reverse=True)
|
||
|
|
suits = [c.suit for c in five_cards]
|
||
|
|
is_flush = len(set(suits)) == 1
|
||
|
|
|
||
|
|
is_straight = False
|
||
|
|
straight_high = 0
|
||
|
|
if len(set(vals)) == 5:
|
||
|
|
if vals[0] - vals[4] == 4:
|
||
|
|
is_straight = True
|
||
|
|
straight_high = vals[0]
|
||
|
|
elif vals == [14, 5, 4, 3, 2]:
|
||
|
|
is_straight = True
|
||
|
|
straight_high = 5
|
||
|
|
|
||
|
|
if is_flush and is_straight:
|
||
|
|
if straight_high == 14:
|
||
|
|
return (9, (14,))
|
||
|
|
return (8, (straight_high,))
|
||
|
|
|
||
|
|
counts = Counter(vals)
|
||
|
|
ordered = sorted(counts.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
|
||
|
|
pattern = [cnt for _val, cnt in ordered]
|
||
|
|
rank_tiebreak = tuple(val for val, _cnt in ordered)
|
||
|
|
|
||
|
|
if pattern == [4, 1]:
|
||
|
|
return (7, rank_tiebreak)
|
||
|
|
if pattern == [3, 2]:
|
||
|
|
return (6, rank_tiebreak)
|
||
|
|
if is_flush:
|
||
|
|
return (5, tuple(vals))
|
||
|
|
if is_straight:
|
||
|
|
return (4, (straight_high,))
|
||
|
|
if pattern == [3, 1, 1]:
|
||
|
|
return (3, rank_tiebreak)
|
||
|
|
if pattern == [2, 2, 1]:
|
||
|
|
return (2, rank_tiebreak)
|
||
|
|
if pattern == [2, 1, 1, 1]:
|
||
|
|
return (1, rank_tiebreak)
|
||
|
|
return (0, tuple(vals))
|