def test_bigger_flush(self): for i in range(num_tests): cards_a = Stack() cards_b = Stack() suit = random.choice(SUITS[:2]) values = random.sample(VALUES, 5) for value in values: cards_a.add(Card(value, suit)) if get_cards_type(cards_a) == STRAIGHT_FLUSH: continue if suit == "Hearts": suit = "Spades" else: suit = random.choice(SUITS[(SUITS.index(suit) + 1):]) values = random.sample(VALUES, 5) for value in values: cards_b.add(Card(value, suit)) if get_cards_type(cards_b) == STRAIGHT_FLUSH: continue self.assertTrue(are_cards_bigger(cards_a, cards_b))
def test_diff_suit_dragon(self): for i in range(num_tests): cards = Stack(cards=[Card(VALUES[0], SUITS[0]), Card(VALUES[1], SUITS[1])]) for value in VALUES[2:]: cards.add(Card(value, random.choice(SUITS))) self.assertEqual(get_cards_type(cards), DRAGON)
def test_pair(self): for i in range(num_tests): cards = Stack() value = random.choice(VALUES) for suit in random.sample(SUITS, 2): cards.add(Card(value, suit)) self.assertEqual(get_cards_type(cards), PAIR)
def test_same_suit_dragon(self): for i in range(num_tests): cards = Stack() suit = random.choice(SUITS) for value in VALUES: cards.add(Card(value, suit)) self.assertEqual(get_cards_type(cards), SAME_SUIT_DRAGON)
def test_three_of_a_kind(self): for i in range(num_tests): cards = Stack() value = random.choice(VALUES) for suit in random.sample(SUITS, 3): cards.add(Card(value, suit)) self.assertEqual(get_cards_type(cards), THREE_OF_A_KIND)
def test_flush(self): for i in range(num_tests): cards = Stack() suit = random.choice(SUITS) for value in random.sample(VALUES, 5): cards.add(Card(value, suit)) if get_cards_type(cards) == STRAIGHT_FLUSH: continue self.assertEqual(get_cards_type(cards), FLUSH)
def test_four_of_a_kind(self): for i in range(num_tests): cards = Stack() values = list(VALUES) four_value = random.choice(values) values.remove(four_value) extra_value = random.choice(values) for suit in SUITS: cards.add(Card(four_value, suit)) cards.add(Card(extra_value, random.choice(SUITS))) self.assertEqual(get_cards_type(cards), FOUR_OF_A_KIND)
def test_bigger_full_house(self): for i in range(num_tests): cards_a = Stack() cards_b = Stack() values = list(VALUES[1:]) three_value = random.choice(values) index = values.index(three_value) values.remove(three_value) two_value = random.choice(values) for suit in random.sample(SUITS, 3): cards_a.add(Card(three_value, suit)) for suit in random.sample(SUITS, 2): cards_a.add(Card(two_value, suit)) if three_value == "Ace": three_value = "2" else: three_value = random.choice(values[index:]) values.remove(three_value) two_value = random.choice(values) for suit in random.sample(SUITS, 3): cards_b.add(Card(three_value, suit)) for suit in random.sample(SUITS, 2): cards_b.add(Card(two_value, suit)) self.assertTrue(are_cards_bigger(cards_a, cards_b))
def test_bigger_four_of_a_kind(self): for i in range(num_tests): cards_a = Stack() cards_b = Stack() values = list(VALUES[1:]) four_value = random.choice(values) index = values.index(four_value) values.remove(four_value) extra_value = random.choice(values) for suit in SUITS: cards_a.add(Card(four_value, suit)) cards_a.add(Card(extra_value, random.choice(SUITS))) if four_value == "Ace": four_value = "2" else: four_value = random.choice(values[index:]) values.remove(four_value) extra_value = random.choice(values) for suit in SUITS: cards_b.add(Card(four_value, suit)) cards_b.add(Card(extra_value, random.choice(SUITS))) self.assertTrue(are_cards_bigger(cards_a, cards_b))
def test_full_house(self): for i in range(num_tests): cards = Stack() values = list(VALUES) three_value = random.choice(values) values.remove(three_value) two_value = random.choice(values) for suit in random.sample(SUITS, 3): cards.add(Card(three_value, suit)) for suit in random.sample(SUITS, 2): cards.add(Card(two_value, suit, )) self.assertEqual(get_cards_type(cards), FULL_HOUSE)
def is_quads(self, sorted_cards: Stack): best_five = Stack() freq_counter = Counter() for card in sorted_cards: freq_counter[card.value] += 1 most_freq_card = freq_counter.most_common()[0] if most_freq_card[1] < 4: return False, None for card in sorted_cards: if card.value == most_freq_card[0]: best_five.add(card) for card in sorted_cards: if card.value != most_freq_card[0]: best_five.add(card) return True, best_five[0:5]
def test_bigger_pair(self): for i in range(num_tests): cards_a = Stack() cards_b = Stack() value = random.choice(VALUES[1:]) for suit in random.sample(SUITS, 2): cards_a.add(Card(value, suit)) if value == "Ace": value = "2" else: value = random.choice(VALUES[(VALUES.index(value) + 1):]) for suit in random.sample(SUITS, 2): cards_b.add(Card(value, suit)) self.assertTrue(are_cards_bigger(cards_a, cards_b))
def test_bigger_straight_flush(self): for i in range(num_tests): cards_a = Stack() cards_b = Stack() suit = random.choice(SUITS[:3]) start_index = random.randint(1, 8) for index in range(start_index, start_index + 5): cards_a.add(Card(VALUES[index], suit)) if suit == "Hearts": suit = "Spades" else: suit = random.choice(SUITS[(SUITS.index(suit) + 1):]) start_index = random.randint(1, 8) for index in range(start_index, start_index + 5): cards_b.add(Card(VALUES[index], suit)) self.assertTrue(are_cards_bigger(cards_a, cards_b))
def test_straight_flush(self): for i in range(num_tests): suit = random.choice(SUITS) cards = Stack(cards=[Card("Ace", suit), Card("2", suit), Card("3", suit), Card("4", suit), Card("5", suit)]) self.assertEqual(get_cards_type(cards), STRAIGHT_FLUSH) cards = Stack(cards=[Card("2", suit), Card("3", suit), Card("4", suit), Card("5", suit), Card("6", suit)]) self.assertEqual(get_cards_type(cards), STRAIGHT_FLUSH) cards = Stack(cards=[Card("Jack", suit), Card("Queen", suit), Card("King", suit), Card("Ace", suit), Card("2", suit)]) self.assertEqual(get_cards_type(cards), STRAIGHT_FLUSH) cards = Stack() start_index = random.randint(1, 8) for index in range(start_index, start_index + 5): cards.add(Card(VALUES[index], suit)) self.assertEqual(get_cards_type(cards), STRAIGHT_FLUSH)
def is_two_pair(self, sorted_cards): best_five = Stack() freq_counter = Counter() for card in sorted_cards: freq_counter[card.value] += 1 most_freq_card = freq_counter.most_common()[0] second_most_freq_card = freq_counter.most_common()[1] third_most_freq_card = freq_counter.most_common()[2] if most_freq_card[1] < 2 or second_most_freq_card[1] < 2: return False, None if most_freq_card[1] == second_most_freq_card[1] == third_most_freq_card[1]: freqs = { most_freq_card[0]: self.ranks[freq_counter.most_common()[0][0]], second_most_freq_card[0]: self.ranks[freq_counter.most_common()[1][0]], third_most_freq_card[0]: self.ranks[freq_counter.most_common()[2][0]] } sorted_freqs = sorted(freqs, key=lambda k: freqs[k])[::-1] tp1 = sorted_freqs[0] tp2 = sorted_freqs[1] else: tp1, tp2 = most_freq_card[0], second_most_freq_card[0] for card in sorted_cards: if card.value == tp1: best_five.add(card) for card in sorted_cards: if card.value == tp2: best_five.add(card) for card in sorted_cards: if card.value not in [tp1, tp2]: best_five.add(card) return True, best_five[0:5]
def test_straight(self): for i in range(num_tests): cards = Stack(cards=[Card("Ace", SUITS[0]), Card("2", SUITS[1]), Card("3", random.choice(SUITS)), Card("4", random.choice(SUITS)), Card("5", random.choice(SUITS))]) self.assertEqual(get_cards_type(cards), STRAIGHT) cards = Stack(cards=[Card("2", SUITS[0]), Card("3", SUITS[1]), Card("4", random.choice(SUITS)), Card("5", random.choice(SUITS)), Card("6", random.choice(SUITS))]) self.assertEqual(get_cards_type(cards), STRAIGHT) cards = Stack(cards=[Card("Jack", SUITS[0]), Card("Queen", SUITS[1]), Card("King", random.choice(SUITS)), Card("Ace", random.choice(SUITS)), Card("2", random.choice(SUITS))]) self.assertEqual(get_cards_type(cards), STRAIGHT) cards = Stack() start_index = random.randint(1, 8) cards.add([Card(VALUES[start_index], SUITS[0]), Card(VALUES[start_index + 1], SUITS[1])]) for index in range(start_index + 2, start_index + 5): cards.add(Card(VALUES[index], random.choice(SUITS))) self.assertEqual(get_cards_type(cards), STRAIGHT)
def is_full_house(self, sorted_cards): best_five = Stack() freq_counter = Counter() for card in sorted_cards: freq_counter[card.value] += 1 most_freq_card = freq_counter.most_common()[0] second_most_freq_card = freq_counter.most_common()[1] if most_freq_card[1] < 3 or second_most_freq_card[1] < 2: return False, None if most_freq_card[1] == second_most_freq_card[1] and \ self.ranks[most_freq_card[0]] < self.ranks[second_most_freq_card[0]]: boat_1, boat_2 = second_most_freq_card[0], most_freq_card[0] else: boat_1, boat_2 = most_freq_card[0], second_most_freq_card[0] for card in sorted_cards: if card.value == boat_1: best_five.add(card) for card in sorted_cards: if card.value == boat_2: best_five.add(card) return True, best_five[0:5]
def test_bigger_straight(self): for i in range(num_tests): cards_a = Stack() cards_b = Stack() start_index = random.randint(1, 7) cards_a.add([Card(VALUES[start_index], SUITS[0]), Card(VALUES[start_index + 1], SUITS[1])]) for index in range(start_index + 2, start_index + 5): cards_a.add(Card(VALUES[index], random.choice(SUITS))) if start_index + 1 == 8: start_index = 8 else: start_index = random.randint(start_index + 1, 7) cards_b.add([Card(VALUES[start_index], SUITS[0]), Card(VALUES[start_index + 1], SUITS[1])]) for index in range(start_index + 2, start_index + 5): cards_b.add(Card(VALUES[index], random.choice(SUITS))) self.assertTrue(are_cards_bigger(cards_a, cards_b))
def is_high_card(self, sorted_cards): best_five = Stack() for card in sorted_cards: best_five.add(card) return True, best_five[0:5]