def test_expected(self): one_pair_hand = Hand( [ Two(Suit.Clubs), Two(Suit.Diamonds), Three(Suit.Clubs), Four(Suit.Diamonds), Five(Suit.Hearts) ] ) self.assertTrue(OnePairVerifier.verify_hand_ranking(one_pair_hand)) two_pair_hand = Hand( [ Two(Suit.Clubs), Two(Suit.Diamonds), Three(Suit.Clubs), Three(Suit.Diamonds), Five(Suit.Clubs) ] ) self.assertFalse(OnePairVerifier.verify_hand_ranking(two_pair_hand)) full_house_hand = Hand( [ Two(Suit.Clubs), Two(Suit.Diamonds), Three(Suit.Clubs), Three(Suit.Diamonds), Three(Suit.Clubs) ] ) self.assertFalse(OnePairVerifier.verify_hand_ranking(full_house_hand)) three_of_a_kind_hand = Hand( [ Ace(Suit.Clubs), Two(Suit.Diamonds), Three(Suit.Clubs), Three(Suit.Diamonds), Three(Suit.Clubs) ] ) self.assertFalse(OnePairVerifier.verify_hand_ranking(three_of_a_kind_hand)) high_card_hand = Hand( [ Ace(Suit.Hearts), Two(Suit.Diamonds), King(Suit.Hearts), Four(Suit.Clubs), Five(Suit.Spades) ] ) self.assertFalse(OnePairVerifier.verify_hand_ranking(high_card_hand))
def calculate_hand_ranking(hand): if StraightFlushVerifier.verify_hand_ranking(hand): return StraightFlushCalculator.calculate_hand_ranking(hand) else: if FourOfAKindVerifier.verify_hand_ranking(hand): return FourOfAKindCalculator.calculate_hand_ranking(hand) else: if FullHouseVerifier.verify_hand_ranking(hand): return FullHouseCalculator.calculate_hand_ranking(hand) else: if FlushVerifier.verify_hand_ranking(hand): return FlushCalculator.calculate_hand_ranking(hand) else: if StraightVerifier.verify_hand_ranking(hand): return StraightCalculator.calculate_hand_ranking(hand) else: if ThreeOfAKindVerifier.verify_hand_ranking(hand): return ThreeOfAKindCalculator.calculate_hand_ranking(hand) else: if TwoPairVerifier.verify_hand_ranking(hand): return TwoPairCalculator.calculate_hand_ranking(hand) else: if OnePairVerifier.verify_hand_ranking(hand): return OnePairCalculator.calculate_hand_ranking(hand) else: if HighCardVerifier.verify_hand_ranking(hand): return HighCardCalculator.calculate_hand_ranking(hand) else: raise RuntimeError("unknown hand ranking")
def calculate_hand_ranking(hand): if OnePairVerifier.verify_hand_ranking(hand): hand_value_counter = Counter([card.value for card in hand.cards]) hand_most_common_values = hand_value_counter.most_common() pair_value = hand_most_common_values[0][0] sorted_values = sorted([card.value for card in hand.cards if card.value != pair_value], reverse=True) return OnePair( pair_value, sorted_values[0], sorted_values[1], sorted_values[2] ) else: raise RuntimeError("hand is not one pair")