task4
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
from enum import Enum
|
||||
from functools import total_ordering
|
||||
from typing import List, Tuple
|
||||
from .card import Card, Rank
|
||||
from typing import List, Tuple, Union, TypeVar, Any
|
||||
from .card import Card, Rank, ShortDeckRank
|
||||
|
||||
HandTypeVar = TypeVar('HandTypeVar', bound=Enum)
|
||||
|
||||
|
||||
@total_ordering
|
||||
@@ -42,32 +44,36 @@ class HandType(Enum):
|
||||
|
||||
|
||||
class HandRanking:
|
||||
def __init__(self, hand_type: HandType, key_ranks: List[Rank], cards: List[Card]):
|
||||
"""通用手牌排名类,支持标准扑克和短牌扑克的手牌类型"""
|
||||
|
||||
def __init__(self, hand_type: Any, key_ranks: List[Union[Rank, ShortDeckRank]], cards: List[Card]):
|
||||
self.hand_type = hand_type
|
||||
self.key_ranks = key_ranks # 用于比较的关键点数
|
||||
self.cards = cards # 组成这个ranking的5张牌
|
||||
|
||||
def __str__(self):
|
||||
if self.hand_type == HandType.FOUR_OF_A_KIND:
|
||||
hand_type_name = self.hand_type.name if hasattr(self.hand_type, 'name') else str(self.hand_type)
|
||||
|
||||
if hand_type_name == "FOUR_OF_A_KIND":
|
||||
return f"Quad({self.key_ranks[0].symbol})"
|
||||
elif self.hand_type == HandType.FULL_HOUSE:
|
||||
elif hand_type_name == "FULL_HOUSE":
|
||||
return f"Full House({self.key_ranks[0].symbol} over {self.key_ranks[1].symbol})"
|
||||
elif self.hand_type == HandType.FLUSH:
|
||||
elif hand_type_name == "FLUSH":
|
||||
return f"Flush({self.key_ranks[0].symbol} high)"
|
||||
elif self.hand_type == HandType.STRAIGHT:
|
||||
elif hand_type_name == "STRAIGHT":
|
||||
return f"Straight({self.key_ranks[0].symbol} high)"
|
||||
elif self.hand_type == HandType.STRAIGHT_FLUSH:
|
||||
if self.key_ranks[0] == Rank.ACE:
|
||||
elif hand_type_name == "STRAIGHT_FLUSH":
|
||||
if hasattr(self.key_ranks[0], 'symbol') and self.key_ranks[0].symbol == 'A':
|
||||
return "Royal Flush"
|
||||
else:
|
||||
return f"Straight Flush({self.key_ranks[0].symbol} high)"
|
||||
elif self.hand_type == HandType.ROYAL_FLUSH:
|
||||
elif hand_type_name == "ROYAL_FLUSH":
|
||||
return "Royal Flush"
|
||||
elif self.hand_type == HandType.THREE_OF_A_KIND:
|
||||
elif hand_type_name == "THREE_OF_A_KIND":
|
||||
return f"Three of a Kind({self.key_ranks[0].symbol})"
|
||||
elif self.hand_type == HandType.TWO_PAIR:
|
||||
elif hand_type_name == "TWO_PAIR":
|
||||
return f"Two Pair({self.key_ranks[0].symbol} and {self.key_ranks[1].symbol})"
|
||||
elif self.hand_type == HandType.ONE_PAIR:
|
||||
elif hand_type_name == "ONE_PAIR":
|
||||
return f"Pair({self.key_ranks[0].symbol})"
|
||||
else: # HIGH_CARD
|
||||
return f"High Card({self.key_ranks[0].symbol})"
|
||||
@@ -75,14 +81,20 @@ class HandRanking:
|
||||
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)
|
||||
self_strength = getattr(self.hand_type, 'strength', self.hand_type.value[0] if hasattr(self.hand_type.value, '__getitem__') else self.hand_type.value)
|
||||
other_strength = getattr(other.hand_type, 'strength', other.hand_type.value[0] if hasattr(other.hand_type.value, '__getitem__') else other.hand_type.value)
|
||||
return (self_strength == other_strength 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
|
||||
# 手牌类型相同比较点数
|
||||
|
||||
self_strength = getattr(self.hand_type, 'strength', self.hand_type.value[0] if hasattr(self.hand_type.value, '__getitem__') else self.hand_type.value)
|
||||
other_strength = getattr(other.hand_type, 'strength', other.hand_type.value[0] if hasattr(other.hand_type.value, '__getitem__') else other.hand_type.value)
|
||||
|
||||
if self_strength != other_strength:
|
||||
return self_strength < other_strength
|
||||
|
||||
for self_rank, other_rank in zip(self.key_ranks, other.key_ranks):
|
||||
if self_rank != other_rank:
|
||||
return self_rank < other_rank
|
||||
|
||||
Reference in New Issue
Block a user