task4
This commit is contained in:
@@ -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)
|
||||
Reference in New Issue
Block a user