Ejemplo n.º 1
0
def find_straight(cards: List[Card]):
    ranks = [card.rank for card in cards]
    unique_ranks = list(dict.fromkeys(ranks))
    ranks_sorted = sorted(unique_ranks, reverse=True)
    straight = try_find_straight(ranks_sorted)
    if straight:
        return Combination(5, straight)
Ejemplo n.º 2
0
 def test__is_royal_flush(self):
     expected = Combination(
         10, [Rank.Ace, Rank.King, Rank.Queen, Rank.Jack, Rank.r10])
     sc = Suit.clubs
     sd = Suit.diamonds
     # when clubs
     test_input = [
         Card(Rank.Ace, sc),
         Card(Rank.r10, sc),
         Card(Rank.Queen, sc),
         Card(Rank.Jack, sc),
         Card(Rank.King, sc)
     ]
     result = combination_finder.find(test_input)
     self.assertEqual(expected.strength, result.strength)
     self.assertEqual(expected.kickers, result.kickers)
     # when diamonds
     test_input = [
         Card(Rank.Ace, sd),
         Card(Rank.r10, sd),
         Card(Rank.King, sd),
         Card(Rank.Queen, sd),
         Card(Rank.Jack, sd)
     ]
     result = combination_finder.find(test_input)
     self.assertEqual(expected.strength, result.strength)
     self.assertEqual(expected.kickers, result.kickers)
Ejemplo n.º 3
0
    def test__find_full_house(self):
        sc = Suit.clubs
        sd = Suit.diamonds
        ss = Suit.spades
        sh = Suit.hearts
        # when full house found then returns combo ordered by kickers
        test_input = [
            Card(Rank.Ace, sc),
            Card(Rank.Ace, sd),
            Card(Rank.Ace, ss),
            Card(Rank.King, sc),
            Card(Rank.King, sh)
        ]
        result = combination_finder.find(test_input)
        expected = Combination(
            7, [Rank.Ace, Rank.Ace, Rank.Ace, Rank.King, Rank.King])
        self.assertEqual(expected.strength, result.strength)
        self.assertEqual(expected.kickers, result.kickers)

        # when full house found then returns combo ordered by kickers
        test_input = [
            Card(Rank.Jack, sc),
            Card(Rank.Jack, sd),
            Card(Rank.Jack, ss),
            Card(Rank.r10, sc),
            Card(Rank.r10, sh)
        ]
        result = combination_finder.find(test_input)
        expected = Combination(
            7, [Rank.Jack, Rank.Jack, Rank.Jack, Rank.r10, Rank.r10])
        self.assertEqual(expected.strength, result.strength)
        self.assertEqual(expected.kickers, result.kickers)

        # when two full houses available then returns the higher
        test_input = [
            Card(Rank.r2, sh),
            Card(Rank.r3, sc),
            Card(Rank.r2, sc),
            Card(Rank.r3, sd),
            Card(Rank.r3, ss),
            Card(Rank.r2, sd)
        ]
        result = combination_finder.find(test_input)
        expected = Combination(7,
                               [Rank.r3, Rank.r3, Rank.r3, Rank.r2, Rank.r2])
        self.assertEqual(expected.strength, result.strength)
        self.assertEqual(expected.kickers, result.kickers)
Ejemplo n.º 4
0
def find_pair(cards: List[Card]):
    ranks = [card.rank for card in cards]
    ranks_sorted = sorted(ranks, reverse=True)
    for i in range(len(ranks_sorted) - 1):
        if ranks_sorted[i] == ranks_sorted[i + 1]:
            hand = [ranks_sorted[i], ranks_sorted[i]]
            hand += pick_highest_kickers(hand, ranks_sorted, 3)
            return Combination(2, hand)
Ejemplo n.º 5
0
def find_four_of_a_kind(cards: Iterable[Card]):
    ranks = [card.rank for card in cards]
    rank_occurrences = count_rank_occurrences(ranks)
    for rank, count in rank_occurrences.items():
        if count is 4:
            hand = 4 * [rank]
            ranks_sorted = sorted(ranks, reverse=True)
            hand += pick_highest_kickers(hand, ranks_sorted, 1)
            return Combination(8, hand)
Ejemplo n.º 6
0
def find_full_house(cards: List[Card]):
    three_of_a_kind = find_three_of_a_kind(cards)
    if not three_of_a_kind:
        return
    hand = three_of_a_kind.kickers[:3]
    remaining_cards = [card for card in cards if card.rank not in hand]
    pair = find_pair(remaining_cards)
    if pair:
        hand += pair.kickers[:2]
        return Combination(7, hand)
Ejemplo n.º 7
0
def find_flush(cards: List[Card]):
    suit_occurrences = defaultdict(list)
    for card in cards:
        suit_occurrences[card.suit].append(card.rank)
    for suit in suit_occurrences:
        grouped_by_suit = suit_occurrences[suit]
        if len(grouped_by_suit) > 4:
            sorted_ranks = sorted(grouped_by_suit, reverse=True)
            top_5 = sorted_ranks[:5]
            return Combination(6, top_5)
Ejemplo n.º 8
0
def find_two_pairs(cards: List[Card]):
    ranks = [card.rank for card in cards]
    rank_occurrences = count_rank_occurrences(ranks)
    ranks_that_can_form_pair = [rank for rank in rank_occurrences if rank_occurrences[rank] == 2]
    if len(ranks_that_can_form_pair) < 2:
        return
    hand = 2 * [ranks_that_can_form_pair[0]] + 2 * [ranks_that_can_form_pair[1]]
    ranks_sorted = sorted(ranks, reverse=True)
    hand += pick_highest_kickers(hand, ranks_sorted, 1)
    return Combination(3, hand)
Ejemplo n.º 9
0
def find_straight_flush(cards: List[Card]):
    sort_by_suit = defaultdict(list)
    for card in cards:
        sort_by_suit[card.suit].append(card.rank)
    for suited_ranks in sort_by_suit.values():
        if len(suited_ranks) > 4:
            sorted_ranks = sorted(suited_ranks, reverse=True)
            straight = try_find_straight(sorted_ranks)
            if straight:
                return Combination(9, straight)
Ejemplo n.º 10
0
def find_three_of_a_kind(cards: List[Card]):
    ranks = [card.rank for card in cards]
    rank_occurrences = count_rank_occurrences(ranks)
    ranks_that_can_form_three_of_a_kind = [rank for rank in rank_occurrences if rank_occurrences[rank] == 3]
    if len(ranks_that_can_form_three_of_a_kind) is 0:
        return
    highest_three_of_a_kind_available = max(ranks_that_can_form_three_of_a_kind)
    hand = 3 * [highest_three_of_a_kind_available]
    ranks_sorted = sorted(ranks, reverse=True)
    hand += pick_highest_kickers(hand, ranks_sorted, 2)
    return Combination(4, hand)
Ejemplo n.º 11
0
 def test_to_string_when_straight_flush(self):
     combination = Combination(9, [Rank.r6, Rank.r5, Rank.r4, Rank.r3, Rank.r2])
     result = str(combination)
     self.assertEqual("Straight flush [6,5,4,3,2]", result)
Ejemplo n.º 12
0
 def test_to_string_when_four_of_a_kind(self):
     combination = Combination(8, [Rank.r3, Rank.r3, Rank.r3, Rank.r3, Rank.r2])
     result = str(combination)
     self.assertEqual("Four of a kind [3,3,3,3,2]", result)
Ejemplo n.º 13
0
 def test_to_string_when_full_house(self):
     combination = Combination(7, [Rank.r3, Rank.r3, Rank.r3, Rank.r2, Rank.r2])
     result = str(combination)
     self.assertEqual("Full house [3,3,3,2,2]", result)
Ejemplo n.º 14
0
 def test_to_string_when_flush(self):
     combination = Combination(6, [Rank.r7, Rank.r5, Rank.r4, Rank.r3, Rank.r2])
     result = str(combination)
     self.assertEqual("Flush [7,5,4,3,2]", result)
Ejemplo n.º 15
0
 def test_to_string_when_three_of_a_kind(self):
     combination = Combination(4, [Rank.r3, Rank.r3, Rank.r3, Rank.r4, Rank.r2])
     result = str(combination)
     self.assertEqual("Three of a kind [3,3,3,4,2]", result)
Ejemplo n.º 16
0
 def test_to_string_when_two_pairs(self):
     combination = Combination(3, [Rank.r3, Rank.r3, Rank.r2, Rank.r2, Rank.r4])
     result = str(combination)
     self.assertEqual("Two pairs [3,3,2,2,4]", result)
Ejemplo n.º 17
0
 def test_to_string_when_pair(self):
     combination = Combination(2, [Rank.r2, Rank.r2, Rank.r5, Rank.r4, Rank.r3])
     result = str(combination)
     self.assertEqual("Pair [2,2,5,4,3]", result)
Ejemplo n.º 18
0
def find_high_card(cards: Iterable[Card]):
    ranks = [card.rank for card in cards]
    ranks_sorted = sorted(ranks, reverse=True)
    return Combination(1, ranks_sorted[:5])
Ejemplo n.º 19
0
 def test_to_string_when_royal_flush(self):
     combination = Combination(10, [Rank.Ace, Rank.King, Rank.Queen, Rank.Jack, Rank.r10])
     result = str(combination)
     self.assertEqual("Royal flush [Ace,King,Queen,Jack,10]", result)
Ejemplo n.º 20
0
 def test_to_string_when_high_card(self):
     combination = Combination(1, [Rank.r7, Rank.r5, Rank.r4, Rank.r3, Rank.r2])
     result = str(combination)
     self.assertEqual("High card [7,5,4,3,2]", result)
Ejemplo n.º 21
0
def find_royal_flush(cards: List[Card]):
    straight_flush = find_straight_flush(cards)
    if not straight_flush:
        return
    if straight_flush.kickers[0] is Rank.Ace:
        return Combination(10, straight_flush.kickers)