task4
This commit is contained in:
112
poker/card.py
112
poker/card.py
@@ -1,6 +1,6 @@
|
||||
from functools import total_ordering
|
||||
from enum import Enum
|
||||
from typing import List, Tuple, Optional
|
||||
from typing import List, Tuple, Optional, Union, Any
|
||||
|
||||
|
||||
class Suit(Enum):
|
||||
@@ -51,29 +51,101 @@ class Rank(Enum):
|
||||
return hash(self.numeric_value)
|
||||
|
||||
|
||||
@total_ordering
|
||||
class ShortDeckRank(Enum):
|
||||
"""
|
||||
shortdeck不包含2,3,4,5点数
|
||||
"""
|
||||
SIX = (6, '6')
|
||||
SEVEN = (7, '7')
|
||||
EIGHT = (8, '8')
|
||||
NINE = (9, '9')
|
||||
TEN = (10, 'T')
|
||||
JACK = (11, 'J')
|
||||
QUEEN = (12, 'Q')
|
||||
KING = (13, 'K')
|
||||
ACE = (14, 'A')
|
||||
|
||||
def __new__(cls, value, symbol):
|
||||
obj = object.__new__(cls)
|
||||
obj._value_ = value
|
||||
obj.numeric_value = value
|
||||
obj.symbol = symbol
|
||||
return obj
|
||||
|
||||
def __str__(self):
|
||||
return self.symbol
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, ShortDeckRank):
|
||||
return self.numeric_value == other.numeric_value
|
||||
return NotImplemented
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, ShortDeckRank):
|
||||
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):
|
||||
"""
|
||||
通用卡片类,支持标准扑克和短牌扑克
|
||||
"""
|
||||
|
||||
def __init__(self, rank: Union[Rank, 'ShortDeckRank'], suit: Suit):
|
||||
self.rank = rank
|
||||
self.suit = suit
|
||||
|
||||
def get_rank(self) -> Union[Rank, 'ShortDeckRank']:
|
||||
return self.rank
|
||||
|
||||
def get_suit(self) -> Suit:
|
||||
return self.suit
|
||||
|
||||
def is_short_deck(self) -> bool:
|
||||
return isinstance(self.rank, ShortDeckRank)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.rank}{self.suit}"
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Card):
|
||||
return self.rank == other.rank and self.suit == other.suit
|
||||
self_value = getattr(self.rank, 'numeric_value', self.rank.value)
|
||||
other_value = getattr(other.rank, 'numeric_value', other.rank.value)
|
||||
return self_value == other_value and self.suit == other.suit
|
||||
return NotImplemented
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, Card):
|
||||
if self.rank != other.rank:
|
||||
return self.rank < other.rank
|
||||
self_value = getattr(self.rank, 'numeric_value', self.rank.value)
|
||||
other_value = getattr(other.rank, 'numeric_value', other.rank.value)
|
||||
|
||||
if self_value != other_value:
|
||||
return self_value < other_value
|
||||
return self.suit.value < other.suit.value
|
||||
return NotImplemented
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _find_rank(rank_char, is_short_deck):
|
||||
if is_short_deck:
|
||||
# 在ShortDeckRank中查找
|
||||
for r in ShortDeckRank:
|
||||
if r.symbol == rank_char:
|
||||
return r
|
||||
else:
|
||||
# 在Rank中查找
|
||||
for r in Rank:
|
||||
if r.symbol == rank_char:
|
||||
return r
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def createCard(cls, card_str) -> 'Card':
|
||||
def create_card(cls, card_str, is_short_deck=False) -> 'Card':
|
||||
"""创建卡片,支持标准扑克和短牌扑克"""
|
||||
if len(card_str) != 2:
|
||||
raise ValueError(f"Invalid card string: {card_str}")
|
||||
|
||||
@@ -81,14 +153,10 @@ class Card:
|
||||
suit_char = card_str[1].lower()
|
||||
|
||||
# 查找rank
|
||||
rank = None
|
||||
for r in Rank:
|
||||
if r.symbol == rank_char:
|
||||
rank = r
|
||||
break
|
||||
|
||||
rank = cls._find_rank(rank_char, is_short_deck)
|
||||
if rank is None:
|
||||
raise ValueError(f"Invalid rank: {rank_char}")
|
||||
rank_type = "short deck" if is_short_deck else "standard"
|
||||
raise ValueError(f"Invalid rank for {rank_type}: {rank_char}")
|
||||
|
||||
# 查找suit
|
||||
suit = None
|
||||
@@ -103,9 +171,10 @@ class Card:
|
||||
return cls(rank, suit)
|
||||
|
||||
@classmethod
|
||||
def parseCards(cls, cards_str) -> List['Card']:
|
||||
def parse_cards(cls, cards_str, is_short_deck=False) -> List['Card']:
|
||||
"""
|
||||
从字符串拆解多张牌, "AsKs AhAdAc6s7s"
|
||||
从字符串拆解多张牌,支持标准扑克和短牌扑克
|
||||
例如: "AsKs AhAdAc6s7s"
|
||||
"""
|
||||
cards_str = cards_str.strip()
|
||||
if not cards_str:
|
||||
@@ -125,4 +194,13 @@ class Card:
|
||||
i += 2
|
||||
else:
|
||||
raise ValueError(f"Invalid card format at position {i}")
|
||||
return [cls.createCard(card_str) for card_str in card_strings]
|
||||
|
||||
return [cls.create_card(card_str, is_short_deck) for card_str in card_strings]
|
||||
@classmethod
|
||||
def parseLongCards(cls, cards_str) -> List['Card']:
|
||||
return cls.parse_cards(cards_str, is_short_deck=False)
|
||||
|
||||
@classmethod
|
||||
def parse_short_deck_cards(cls, cards_str) -> List['Card']:
|
||||
return cls.parse_cards(cards_str, is_short_deck=True)
|
||||
|
||||
@@ -7,7 +7,7 @@ from .hand_ranking import HandRanking, HandType
|
||||
|
||||
class HandEvaluator:
|
||||
@staticmethod
|
||||
def evaluateHand(cards) -> HandRanking:
|
||||
def evaluate_hand(cards) -> HandRanking:
|
||||
"""
|
||||
从7张牌中找出最好的5张牌组合
|
||||
"""
|
||||
@@ -19,7 +19,7 @@ class HandEvaluator:
|
||||
|
||||
# 所有可能的5张牌组合
|
||||
for five_cards in combinations(cards, 5):
|
||||
ranking = HandEvaluator.evaluate5Cards(list(five_cards))
|
||||
ranking = HandEvaluator.evaluate_5_cards(list(five_cards))
|
||||
|
||||
if best_ranking is None or ranking > best_ranking:
|
||||
best_ranking = ranking
|
||||
@@ -28,11 +28,8 @@ class HandEvaluator:
|
||||
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)}")
|
||||
|
||||
@classmethod
|
||||
def _analyze_cards(cls, cards: List[Card]) -> tuple:
|
||||
# 按点数排序(降序)
|
||||
sorted_cards = sorted(cards, key=lambda c: c.rank.numeric_value, reverse=True)
|
||||
ranks = [card.rank for card in sorted_cards]
|
||||
@@ -45,8 +42,16 @@ class HandEvaluator:
|
||||
# 同花
|
||||
is_flush = len(set(suits)) == 1
|
||||
|
||||
# 顺子
|
||||
is_straight, straight_high = HandEvaluator._isStraight(ranks)
|
||||
is_straight, straight_high = cls._is_straight(ranks)
|
||||
|
||||
return sorted_cards, ranks, suits, rank_counts, count_values, is_flush, is_straight, straight_high
|
||||
|
||||
@staticmethod
|
||||
def evaluate_5_cards(cards) -> HandRanking:
|
||||
if len(cards) != 5:
|
||||
raise ValueError(f"Expected 5 cards, got {len(cards)}")
|
||||
|
||||
sorted_cards, ranks, suits, rank_counts, count_values, is_flush, is_straight, straight_high = HandEvaluator._analyze_cards(cards)
|
||||
|
||||
# 根据牌型返回相应的HandRanking
|
||||
if is_straight and is_flush:
|
||||
@@ -90,7 +95,7 @@ class HandEvaluator:
|
||||
return HandRanking(HandType.HIGH_CARD, ranks, sorted_cards)
|
||||
|
||||
@staticmethod
|
||||
def _isStraight(ranks) -> Tuple[bool, Rank]:
|
||||
def _is_straight(ranks) -> Tuple[bool, Rank]:
|
||||
# 排序点数值
|
||||
values = sorted([rank.numeric_value for rank in ranks], reverse=True)
|
||||
|
||||
@@ -117,6 +122,6 @@ class HandEvaluator:
|
||||
return False, None
|
||||
|
||||
@staticmethod
|
||||
def evaluateFromInput(cards_str) -> HandRanking:
|
||||
cards = Card.parseCards(cards_str)
|
||||
return HandEvaluator.evaluateHand(cards)
|
||||
def evaluate_from_input(cards_str) -> HandRanking:
|
||||
cards = Card.parse_cards(cards_str)
|
||||
return HandEvaluator.evaluate_hand(cards)
|
||||
@@ -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