task1
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
"""
|
||||
Card module for poker game
|
||||
"""
|
||||
|
||||
from functools import total_ordering
|
||||
from enum import Enum
|
||||
from typing import List, Tuple, Optional
|
||||
|
||||
@@ -15,7 +12,6 @@ class Suit(Enum):
|
||||
def __str__(self):
|
||||
return self.value
|
||||
|
||||
|
||||
class Rank(Enum):
|
||||
TWO = (2, '2')
|
||||
THREE = (3, '3')
|
||||
@@ -41,12 +37,21 @@ class Rank(Enum):
|
||||
def __str__(self):
|
||||
return self.symbol
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Rank):
|
||||
return self.numeric_value == other.numeric_value
|
||||
return NotImplemented
|
||||
|
||||
def __lt__(self, other):
|
||||
if not isinstance(other, Rank):
|
||||
return NotImplemented
|
||||
return self.numeric_value < other.numeric_value
|
||||
if isinstance(other, Rank):
|
||||
return self.numeric_value < other.numeric_value
|
||||
return NotImplemented
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.numeric_value)
|
||||
|
||||
|
||||
@total_ordering
|
||||
class Card:
|
||||
def __init__(self, rank: Rank, suit: Suit):
|
||||
self.rank = rank
|
||||
@@ -55,22 +60,20 @@ class Card:
|
||||
def __str__(self):
|
||||
return f"{self.rank}{self.suit}"
|
||||
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, Card):
|
||||
return False
|
||||
return self.rank == other.rank and self.suit == other.suit
|
||||
if isinstance(other, Card):
|
||||
return self.rank == other.rank and self.suit == other.suit
|
||||
return NotImplemented
|
||||
|
||||
def __lt__(self, other):
|
||||
if not isinstance(other, Card):
|
||||
return NotImplemented
|
||||
return self.rank.numeric_value < other.rank.numeric_value
|
||||
if isinstance(other, Card):
|
||||
if self.rank != other.rank:
|
||||
return self.rank < other.rank
|
||||
return self.suit.value < other.suit.value
|
||||
return NotImplemented
|
||||
|
||||
@classmethod
|
||||
def create_card(cls, card_str: str) -> 'Card':
|
||||
"""
|
||||
从字符串创建Card对象,例如 "As", "Kh", "2c"
|
||||
"""
|
||||
def createCard(cls, card_str) -> 'Card':
|
||||
if len(card_str) != 2:
|
||||
raise ValueError(f"Invalid card string: {card_str}")
|
||||
|
||||
@@ -100,9 +103,9 @@ class Card:
|
||||
return cls(rank, suit)
|
||||
|
||||
@classmethod
|
||||
def parse_cards(cls, cards_str: str) -> List['Card']:
|
||||
def parseCards(cls, cards_str) -> List['Card']:
|
||||
"""
|
||||
从字符串解析多张牌,例如 "AsKs AhAdAc6s7s"
|
||||
从字符串拆解多张牌, "AsKs AhAdAc6s7s"
|
||||
"""
|
||||
cards_str = cards_str.strip()
|
||||
if not cards_str:
|
||||
@@ -122,8 +125,4 @@ class Card:
|
||||
i += 2
|
||||
else:
|
||||
raise ValueError(f"Invalid card format at position {i}")
|
||||
result = []
|
||||
for card_str in card_strings:
|
||||
card_tmp = cls.create_card(card_str)
|
||||
result.append(card_tmp)
|
||||
return result
|
||||
return [cls.createCard(card_str) for card_str in card_strings]
|
||||
@@ -1,7 +1,3 @@
|
||||
"""
|
||||
Hand evaluator module for poker game
|
||||
"""
|
||||
|
||||
from typing import List, Tuple, Dict
|
||||
from collections import Counter
|
||||
from itertools import combinations
|
||||
@@ -10,14 +6,10 @@ from .hand_ranking import HandRanking, HandType
|
||||
|
||||
|
||||
class HandEvaluator:
|
||||
"""
|
||||
手牌评估器类,用于评估扑克手牌
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def evaluate_hand(cards: List[Card]) -> HandRanking:
|
||||
def evaluateHand(cards) -> HandRanking:
|
||||
"""
|
||||
从7张牌中评估出最好的5张牌组合
|
||||
从7张牌中找出最好的5张牌组合
|
||||
"""
|
||||
if len(cards) != 7:
|
||||
raise ValueError(f"Expected 7 cards, got {len(cards)}")
|
||||
@@ -25,23 +17,19 @@ class HandEvaluator:
|
||||
best_ranking = None
|
||||
best_cards = None
|
||||
|
||||
# 尝试所有可能的5张牌组合
|
||||
# 所有可能的5张牌组合
|
||||
for five_cards in combinations(cards, 5):
|
||||
ranking = HandEvaluator._evaluate_five_cards(list(five_cards))
|
||||
ranking = HandEvaluator.evaluate5Cards(list(five_cards))
|
||||
|
||||
if best_ranking is None or ranking > best_ranking:
|
||||
best_ranking = ranking
|
||||
best_cards = list(five_cards)
|
||||
|
||||
# 更新最佳ranking的cards
|
||||
best_ranking.cards = best_cards
|
||||
return best_ranking
|
||||
|
||||
@staticmethod
|
||||
def _evaluate_five_cards(cards: List[Card]) -> HandRanking:
|
||||
"""
|
||||
评估5张牌的手牌类型
|
||||
"""
|
||||
def evaluate5Cards(cards) -> HandRanking:
|
||||
if len(cards) != 5:
|
||||
raise ValueError(f"Expected 5 cards, got {len(cards)}")
|
||||
|
||||
@@ -54,11 +42,11 @@ class HandEvaluator:
|
||||
rank_counts = Counter(ranks)
|
||||
count_values = sorted(rank_counts.values(), reverse=True)
|
||||
|
||||
# 检查是否是同花
|
||||
# 同花
|
||||
is_flush = len(set(suits)) == 1
|
||||
|
||||
# 检查是否是顺子
|
||||
is_straight, straight_high = HandEvaluator._is_straight(ranks)
|
||||
# 顺子
|
||||
is_straight, straight_high = HandEvaluator._isStraight(ranks)
|
||||
|
||||
# 根据牌型返回相应的HandRanking
|
||||
if is_straight and is_flush:
|
||||
@@ -102,10 +90,7 @@ class HandEvaluator:
|
||||
return HandRanking(HandType.HIGH_CARD, ranks, sorted_cards)
|
||||
|
||||
@staticmethod
|
||||
def _is_straight(ranks: List[Rank]) -> Tuple[bool, Rank]:
|
||||
"""
|
||||
检查是否是顺子,返回(是否是顺子, 最高牌)
|
||||
"""
|
||||
def _isStraight(ranks) -> Tuple[bool, Rank]:
|
||||
# 排序点数值
|
||||
values = sorted([rank.numeric_value for rank in ranks], reverse=True)
|
||||
|
||||
@@ -125,16 +110,13 @@ class HandEvaluator:
|
||||
break
|
||||
return True, highest_rank
|
||||
|
||||
# 检查A-2-3-4-5的特殊顺子(轮子)
|
||||
# A-2-3-4-5特殊顺子
|
||||
if values == [14, 5, 4, 3, 2]: # A, 5, 4, 3, 2
|
||||
return True, Rank.FIVE # 在轮子中,5是最高牌
|
||||
return True, Rank.FIVE # 5是最高牌
|
||||
|
||||
return False, None
|
||||
|
||||
@staticmethod
|
||||
def evaluate_from_input(cards_str: str) -> HandRanking:
|
||||
"""
|
||||
从字符串评估手牌,例如 "AsKs AhAdAc6s7s"
|
||||
"""
|
||||
cards = Card.parse_cards(cards_str)
|
||||
return HandEvaluator.evaluate_hand(cards)
|
||||
def evaluateFromInput(cards_str) -> HandRanking:
|
||||
cards = Card.parseCards(cards_str)
|
||||
return HandEvaluator.evaluateHand(cards)
|
||||
@@ -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