task1
This commit is contained in:
@@ -1,16 +1,11 @@
|
||||
"""
|
||||
Hand ranking module for poker game
|
||||
"""
|
||||
|
||||
from enum import Enum
|
||||
from functools import total_ordering
|
||||
from typing import List, Tuple
|
||||
from .card import Card, Rank
|
||||
|
||||
|
||||
@total_ordering
|
||||
class HandType(Enum):
|
||||
"""
|
||||
手牌类型枚举,按强度排序
|
||||
"""
|
||||
HIGH_CARD = (1, "High Card")
|
||||
ONE_PAIR = (2, "Pair")
|
||||
TWO_PAIR = (3, "Two Pair")
|
||||
@@ -33,40 +28,26 @@ class HandType(Enum):
|
||||
return self.type_name
|
||||
|
||||
def __lt__(self, other):
|
||||
if not isinstance(other, HandType):
|
||||
return NotImplemented
|
||||
return self.strength < other.strength
|
||||
if isinstance(other, HandType):
|
||||
return self.strength < other.strength
|
||||
return NotImplemented
|
||||
|
||||
def __le__(self, other):
|
||||
if not isinstance(other, HandType):
|
||||
return NotImplemented
|
||||
return self.strength <= other.strength
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, HandType):
|
||||
return self.strength == other.strength
|
||||
return NotImplemented
|
||||
|
||||
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
|
||||
def __hash__(self):
|
||||
return hash(self.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张牌
|
||||
self.cards = cards # 组成这个ranking的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:
|
||||
@@ -91,35 +72,18 @@ class HandRanking:
|
||||
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)
|
||||
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
|
||||
return False
|
||||
Reference in New Issue
Block a user