""" Hand ranking module for poker game """ from enum import Enum from typing import List, Tuple from .card import Card, Rank class HandType(Enum): """ 手牌类型枚举,按强度排序 """ HIGH_CARD = (1, "High Card") ONE_PAIR = (2, "Pair") TWO_PAIR = (3, "Two Pair") THREE_OF_A_KIND = (4, "Three of a Kind") STRAIGHT = (5, "Straight") FLUSH = (6, "Flush") FULL_HOUSE = (7, "Full House") FOUR_OF_A_KIND = (8, "Four of a Kind") STRAIGHT_FLUSH = (9, "Straight Flush") ROYAL_FLUSH = (10, "Royal Flush") def __new__(cls, strength, name): obj = object.__new__(cls) obj._value_ = strength obj.strength = strength obj.type_name = name return obj def __str__(self): return self.type_name def __lt__(self, other): if not isinstance(other, HandType): return NotImplemented return self.strength < other.strength def __le__(self, other): if not isinstance(other, HandType): return NotImplemented return self.strength <= other.strength def __gt__(self, other): if not isinstance(other, HandType): return NotImplemented return self.strength > other.strength def __ge__(self, other): if not isinstance(other, HandType): return NotImplemented return self.strength >= other.strength class HandRanking: """ 手牌排名类,包含手牌类型和关键牌 """ def __init__(self, hand_type: HandType, key_ranks: List[Rank], cards: List[Card]): self.hand_type = hand_type self.key_ranks = key_ranks # 用于比较的关键点数 self.cards = cards # 组成这个排名的5张牌 def __str__(self): """ 返回手牌排名的字符串表示 """ if self.hand_type == HandType.FOUR_OF_A_KIND: return f"Quad({self.key_ranks[0].symbol})" elif self.hand_type == HandType.FULL_HOUSE: return f"Full House({self.key_ranks[0].symbol} over {self.key_ranks[1].symbol})" elif self.hand_type == HandType.FLUSH: return f"Flush({self.key_ranks[0].symbol} high)" elif self.hand_type == HandType.STRAIGHT: return f"Straight({self.key_ranks[0].symbol} high)" elif self.hand_type == HandType.STRAIGHT_FLUSH: if self.key_ranks[0] == Rank.ACE: return "Royal Flush" else: return f"Straight Flush({self.key_ranks[0].symbol} high)" elif self.hand_type == HandType.ROYAL_FLUSH: return "Royal Flush" elif self.hand_type == HandType.THREE_OF_A_KIND: return f"Three of a Kind({self.key_ranks[0].symbol})" elif self.hand_type == HandType.TWO_PAIR: return f"Two Pair({self.key_ranks[0].symbol} and {self.key_ranks[1].symbol})" elif self.hand_type == HandType.ONE_PAIR: return f"Pair({self.key_ranks[0].symbol})" else: # HIGH_CARD return f"High Card({self.key_ranks[0].symbol})" def __repr__(self): return f"HandRanking({self.hand_type}, {[r.symbol for r in self.key_ranks]})" def __eq__(self, other): if not isinstance(other, HandRanking): return False return (self.hand_type == other.hand_type and self.key_ranks == other.key_ranks) def __lt__(self, other): if not isinstance(other, HandRanking): return NotImplemented # 首先比较手牌类型 if self.hand_type != other.hand_type: return self.hand_type < other.hand_type # 如果手牌类型相同,比较关键点数 for self_rank, other_rank in zip(self.key_ranks, other.key_ranks): if self_rank != other_rank: return self_rank < other_rank return False # 完全相等 def __le__(self, other): return self == other or self < other def __gt__(self, other): return not self <= other def __ge__(self, other): return not self < other