修改
This commit is contained in:
9
poker/__init__.py
Normal file
9
poker/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
"""
|
||||
Poker hand evaluation module
|
||||
"""
|
||||
|
||||
from .card import Card, Suit, Rank
|
||||
from .hand_ranking import HandRanking, HandType
|
||||
from .hand_evaluator import HandEvaluator
|
||||
|
||||
__all__ = ['Card', 'Suit', 'Rank', 'HandRanking', 'HandType', 'HandEvaluator']
|
||||
129
poker/card.py
Normal file
129
poker/card.py
Normal file
@@ -0,0 +1,129 @@
|
||||
"""
|
||||
Card module for poker game
|
||||
"""
|
||||
|
||||
from enum import Enum
|
||||
from typing import List, Tuple, Optional
|
||||
|
||||
|
||||
class Suit(Enum):
|
||||
SPADES = 's' # 黑桃
|
||||
HEARTS = 'h' # 红桃
|
||||
DIAMONDS = 'd' # 方块
|
||||
CLUBS = 'c' # 梅花
|
||||
|
||||
def __str__(self):
|
||||
return self.value
|
||||
|
||||
|
||||
class Rank(Enum):
|
||||
TWO = (2, '2')
|
||||
THREE = (3, '3')
|
||||
FOUR = (4, '4')
|
||||
FIVE = (5, '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 __lt__(self, other):
|
||||
if not isinstance(other, Rank):
|
||||
return NotImplemented
|
||||
return self.numeric_value < other.numeric_value
|
||||
|
||||
|
||||
class Card:
|
||||
def __init__(self, rank: Rank, suit: Suit):
|
||||
self.rank = rank
|
||||
self.suit = suit
|
||||
|
||||
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
|
||||
|
||||
def __lt__(self, other):
|
||||
if not isinstance(other, Card):
|
||||
return NotImplemented
|
||||
return self.rank.numeric_value < other.rank.numeric_value
|
||||
|
||||
@classmethod
|
||||
def create_card(cls, card_str: str) -> 'Card':
|
||||
"""
|
||||
从字符串创建Card对象,例如 "As", "Kh", "2c"
|
||||
"""
|
||||
if len(card_str) != 2:
|
||||
raise ValueError(f"Invalid card string: {card_str}")
|
||||
|
||||
rank_char = card_str[0]
|
||||
suit_char = card_str[1].lower()
|
||||
|
||||
# 查找rank
|
||||
rank = None
|
||||
for r in Rank:
|
||||
if r.symbol == rank_char:
|
||||
rank = r
|
||||
break
|
||||
|
||||
if rank is None:
|
||||
raise ValueError(f"Invalid rank: {rank_char}")
|
||||
|
||||
# 查找suit
|
||||
suit = None
|
||||
for s in Suit:
|
||||
if s.value == suit_char:
|
||||
suit = s
|
||||
break
|
||||
|
||||
if suit is None:
|
||||
raise ValueError(f"Invalid suit: {suit_char}")
|
||||
|
||||
return cls(rank, suit)
|
||||
|
||||
@classmethod
|
||||
def parse_cards(cls, cards_str: str) -> List['Card']:
|
||||
"""
|
||||
从字符串解析多张牌,例如 "AsKs AhAdAc6s7s"
|
||||
"""
|
||||
cards_str = cards_str.strip()
|
||||
if not cards_str:
|
||||
return []
|
||||
|
||||
# 将字符串分割成单独的牌
|
||||
card_strings = []
|
||||
i = 0
|
||||
while i < len(cards_str):
|
||||
if cards_str[i].isspace():
|
||||
i += 1
|
||||
continue
|
||||
|
||||
if i + 1 < len(cards_str):
|
||||
card_str = cards_str[i:i+2]
|
||||
card_strings.append(card_str)
|
||||
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
|
||||
140
poker/hand_evaluator.py
Normal file
140
poker/hand_evaluator.py
Normal file
@@ -0,0 +1,140 @@
|
||||
"""
|
||||
Hand evaluator module for poker game
|
||||
"""
|
||||
|
||||
from typing import List, Tuple, Dict
|
||||
from collections import Counter
|
||||
from itertools import combinations
|
||||
from .card import Card, Rank, Suit
|
||||
from .hand_ranking import HandRanking, HandType
|
||||
|
||||
|
||||
class HandEvaluator:
|
||||
"""
|
||||
手牌评估器类,用于评估扑克手牌
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def evaluate_hand(cards: List[Card]) -> HandRanking:
|
||||
"""
|
||||
从7张牌中评估出最好的5张牌组合
|
||||
"""
|
||||
if len(cards) != 7:
|
||||
raise ValueError(f"Expected 7 cards, got {len(cards)}")
|
||||
|
||||
best_ranking = None
|
||||
best_cards = None
|
||||
|
||||
# 尝试所有可能的5张牌组合
|
||||
for five_cards in combinations(cards, 5):
|
||||
ranking = HandEvaluator._evaluate_five_cards(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张牌的手牌类型
|
||||
"""
|
||||
if len(cards) != 5:
|
||||
raise ValueError(f"Expected 5 cards, got {len(cards)}")
|
||||
|
||||
# 按点数排序(降序)
|
||||
sorted_cards = sorted(cards, key=lambda c: c.rank.numeric_value, reverse=True)
|
||||
ranks = [card.rank for card in sorted_cards]
|
||||
suits = [card.suit for card in sorted_cards]
|
||||
|
||||
# 统计点数出现次数
|
||||
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)
|
||||
|
||||
# 根据牌型返回相应的HandRanking
|
||||
if is_straight and is_flush:
|
||||
if straight_high == Rank.ACE and ranks == [Rank.ACE, Rank.KING, Rank.QUEEN, Rank.JACK, Rank.TEN]:
|
||||
return HandRanking(HandType.ROYAL_FLUSH, [Rank.ACE], sorted_cards)
|
||||
else:
|
||||
return HandRanking(HandType.STRAIGHT_FLUSH, [straight_high], sorted_cards)
|
||||
|
||||
elif count_values == [4, 1]: # 四条
|
||||
quad_rank = [rank for rank, count in rank_counts.items() if count == 4][0]
|
||||
kicker = [rank for rank, count in rank_counts.items() if count == 1][0]
|
||||
return HandRanking(HandType.FOUR_OF_A_KIND, [quad_rank, kicker], sorted_cards)
|
||||
|
||||
elif count_values == [3, 2]: # 三条加一对
|
||||
trips_rank = [rank for rank, count in rank_counts.items() if count == 3][0]
|
||||
pair_rank = [rank for rank, count in rank_counts.items() if count == 2][0]
|
||||
return HandRanking(HandType.FULL_HOUSE, [trips_rank, pair_rank], sorted_cards)
|
||||
|
||||
elif is_flush: # 同花
|
||||
return HandRanking(HandType.FLUSH, ranks, sorted_cards)
|
||||
|
||||
elif is_straight: # 顺子
|
||||
return HandRanking(HandType.STRAIGHT, [straight_high], sorted_cards)
|
||||
|
||||
elif count_values == [3, 1, 1]: # 三条
|
||||
trips_rank = [rank for rank, count in rank_counts.items() if count == 3][0]
|
||||
kickers = sorted([rank for rank, count in rank_counts.items() if count == 1], reverse=True)
|
||||
return HandRanking(HandType.THREE_OF_A_KIND, [trips_rank] + kickers, sorted_cards)
|
||||
|
||||
elif count_values == [2, 2, 1]: # 两对
|
||||
pairs = sorted([rank for rank, count in rank_counts.items() if count == 2], reverse=True)
|
||||
kicker = [rank for rank, count in rank_counts.items() if count == 1][0]
|
||||
return HandRanking(HandType.TWO_PAIR, pairs + [kicker], sorted_cards)
|
||||
|
||||
elif count_values == [2, 1, 1, 1]: # 一对
|
||||
pair_rank = [rank for rank, count in rank_counts.items() if count == 2][0]
|
||||
kickers = sorted([rank for rank, count in rank_counts.items() if count == 1], reverse=True)
|
||||
return HandRanking(HandType.ONE_PAIR, [pair_rank] + kickers, sorted_cards)
|
||||
|
||||
else: # 高牌
|
||||
return HandRanking(HandType.HIGH_CARD, ranks, sorted_cards)
|
||||
|
||||
@staticmethod
|
||||
def _is_straight(ranks: List[Rank]) -> Tuple[bool, Rank]:
|
||||
"""
|
||||
检查是否是顺子,返回(是否是顺子, 最高牌)
|
||||
"""
|
||||
# 排序点数值
|
||||
values = sorted([rank.numeric_value for rank in ranks], reverse=True)
|
||||
|
||||
# 检查常规顺子
|
||||
is_regular_straight = True
|
||||
for i in range(1, len(values)):
|
||||
if values[i-1] - values[i] != 1:
|
||||
is_regular_straight = False
|
||||
break
|
||||
|
||||
if is_regular_straight:
|
||||
# 返回最高牌
|
||||
highest_rank = None
|
||||
for rank in ranks:
|
||||
if rank.numeric_value == values[0]:
|
||||
highest_rank = rank
|
||||
break
|
||||
return True, highest_rank
|
||||
|
||||
# 检查A-2-3-4-5的特殊顺子(轮子)
|
||||
if values == [14, 5, 4, 3, 2]: # A, 5, 4, 3, 2
|
||||
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)
|
||||
125
poker/hand_ranking.py
Normal file
125
poker/hand_ranking.py
Normal file
@@ -0,0 +1,125 @@
|
||||
"""
|
||||
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
|
||||
Reference in New Issue
Block a user