109 lines
4.6 KiB
Python
109 lines
4.6 KiB
Python
from typing import List, Tuple, Dict
|
|
from collections import Counter
|
|
from itertools import combinations
|
|
from .card import Card, Rank, Suit
|
|
from .hand_ranking import HandRanking, HandType
|
|
|
|
|
|
class HandEvaluator:
|
|
@staticmethod
|
|
def evaluateHand(cards) -> HandRanking:
|
|
if len(cards) != 7:
|
|
raise ValueError(f"Expected 7 cards, got {len(cards)}")
|
|
|
|
best_ranking = None
|
|
best_cards = None
|
|
|
|
# 所有可能的5张牌组合
|
|
for five_cards in combinations(cards, 5):
|
|
ranking = HandEvaluator.evaluate5Cards(list(five_cards))
|
|
|
|
if best_ranking is None or ranking > best_ranking:
|
|
best_ranking = ranking
|
|
best_cards = list(five_cards)
|
|
best_ranking.cards = best_cards
|
|
return best_ranking
|
|
|
|
@staticmethod
|
|
def evaluate5Cards(cards) -> HandRanking:
|
|
|
|
if len(cards) != 5:
|
|
raise ValueError(f"Expected 5 cards, got {len(cards)}")
|
|
|
|
# 按点数排序(降序)
|
|
sorted_cards = sorted(cards, key=lambda c: c.rank.numeric_value, reverse=True)
|
|
ranks = [card.rank for card in sorted_cards]
|
|
suits = [card.suit for card in sorted_cards]
|
|
|
|
# 统计点数出现次数
|
|
rank_counts = Counter(ranks)
|
|
count_values = sorted(rank_counts.values(), reverse=True)
|
|
|
|
# 同花
|
|
is_flush = len(set(suits)) == 1
|
|
|
|
# 顺子
|
|
is_straight, straight_high = HandEvaluator._isStraight(ranks)
|
|
|
|
# 根据牌型返回相应的HandRanking
|
|
if is_straight and is_flush:
|
|
if straight_high == Rank.RA and ranks == [Rank.RA, Rank.RK, Rank.RQ, Rank.RJ, Rank.RT]:
|
|
return HandRanking(HandType.ROYAL_FLUSH, [Rank.RA], sorted_cards) # 皇家同花顺
|
|
else:
|
|
return HandRanking(HandType.STRAIGHT_FLUSH, [straight_high], sorted_cards) # 同花顺
|
|
|
|
elif count_values == [4, 1]: # 四条
|
|
quad_rank = [rank for rank, count in rank_counts.items() if count == 4][0]
|
|
kicker = [rank for rank, count in rank_counts.items() if count == 1][0]
|
|
return HandRanking(HandType.FOUR_OF_A_KIND, [quad_rank, kicker], sorted_cards)
|
|
|
|
elif count_values == [3, 2]: # 三条+一对
|
|
trips_rank = [rank for rank, count in rank_counts.items() if count == 3][0]
|
|
pair_rank = [rank for rank, count in rank_counts.items() if count == 2][0]
|
|
return HandRanking(HandType.FULL_HOUSE, [trips_rank, pair_rank], sorted_cards)
|
|
|
|
elif is_flush: # 同花
|
|
return HandRanking(HandType.FLUSH, ranks, sorted_cards)
|
|
|
|
elif is_straight: # 顺子
|
|
return HandRanking(HandType.STRAIGHT, [straight_high], sorted_cards)
|
|
|
|
elif count_values == [3, 1, 1]: # 三条
|
|
trips_rank = [rank for rank, count in rank_counts.items() if count == 3][0]
|
|
kickers = sorted([rank for rank, count in rank_counts.items() if count == 1], reverse=True)
|
|
return HandRanking(HandType.THREE_OF_A_KIND, [trips_rank] + kickers, sorted_cards)
|
|
|
|
elif count_values == [2, 2, 1]: # 两对
|
|
pairs = sorted([rank for rank, count in rank_counts.items() if count == 2], reverse=True)
|
|
kicker = [rank for rank, count in rank_counts.items() if count == 1][0]
|
|
return HandRanking(HandType.TWO_PAIR, pairs + [kicker], sorted_cards)
|
|
|
|
elif count_values == [2, 1, 1, 1]: # 一对
|
|
pair_rank = [rank for rank, count in rank_counts.items() if count == 2][0]
|
|
kickers = sorted([rank for rank, count in rank_counts.items() if count == 1], reverse=True)
|
|
return HandRanking(HandType.ONE_PAIR, [pair_rank] + kickers, sorted_cards)
|
|
|
|
else: # 高牌
|
|
return HandRanking(HandType.HIGH_CARD, ranks, sorted_cards)
|
|
|
|
@staticmethod
|
|
def _isStraight(ranks: List[Rank]) -> Tuple[bool, Rank]:
|
|
values = sorted([rank.numeric_value for rank in ranks], reverse=True)
|
|
|
|
is_regular_straight = True
|
|
for i in range(1, len(values)):
|
|
if values[i-1] - values[i] != 1:
|
|
is_regular_straight = False
|
|
break
|
|
|
|
if is_regular_straight:
|
|
highest_rank = None
|
|
for rank in ranks:
|
|
if rank.numeric_value == values[0]:
|
|
highest_rank = rank
|
|
break
|
|
return True, highest_rank
|
|
|
|
if values == {14, 9, 8, 7, 6}: # A, 9, 8, 7, 6
|
|
return True, Rank.R9
|
|
return False, None |