def test_comparing_flush(self): ordered_hands = [ Hand(list(map(Card, ['2H', '4H', '6H', '8H', 'TH']))), Hand(list(map(Card, ['3H', '4H', '6H', '8H', 'TH']))), Hand(list(map(Card, ['2H', '5H', '6H', '8H', 'TH']))), Hand(list(map(Card, ['2H', '4H', '7H', '8H', 'TH']))), Hand(list(map(Card, ['2H', '4H', '6H', '9H', 'TH']))), Hand(list(map(Card, ['2H', '4H', '6H', '8H', 'JH']))), ] for left_index in range(0, len(ordered_hands)): for right_index in range(left_index + 1, len(ordered_hands)): self.assertTrue( ordered_hands[right_index] > ordered_hands[left_index]) equal_hands = [ Hand(list(map(Card, ['2H', '4H', '6H', '8H', 'TH']))), Hand(list(map(Card, ['2D', '4D', '6D', '8D', 'TD']))), Hand(list(map(Card, ['2S', '4S', '6S', '8S', 'TS']))), Hand(list(map(Card, ['2C', '4C', '6C', '8C', 'TC']))), ] for left_index in range(0, len(equal_hands)): for right_index in range(left_index + 1, len(equal_hands)): self.assertEqual(equal_hands[right_index], equal_hands[left_index])
def test_figures_out_flush_is_best_card(self): cards = [ Card(rank=rank, suit="Hearts") for rank in ["2", "5", "10", "Queen", "King"] ] hand = Hand() hand.add_cards(cards) self.assertEqual(hand.best_rank(), "Flush")
def test_comparing_straight_flush(self): hands = [ Hand(list(map(Card, ['3H', '4H', '5H', '6H', '7H']))), Hand(list(map(Card, ['3S', '4S', '5S', '6S', '7S']))), Hand(list(map(Card, ['6D', '7D', '8D', '9D', 'TD']))), ] self.assertEqual(hands[0], hands[1]) self.assertTrue(hands[2] > hands[1])
def test_three_of_kind_of_8_third_to_last_pos(self): card_list = [ Card("C","9"), Card("H","10"), Card("H","8"), Card("D","8"), Card("S","8"), ] hand = Hand(card_list) self.assertEqual(hand.result(), "Three of a kind")
def test_three_of_kind_of_7_first_until_third_pos(self): card_list = [ Card("H","7"), Card("D","7"), Card("S","7"), Card("C","9"), Card("H","10"), ] hand = Hand(card_list) self.assertEqual(hand.result(), "Three of a kind")
def test_two_pairs_7_and_9_at_first_until_four_pos(self): card_list = [ Card("H","7"), Card("S","7"), Card("H","9"), Card("C","9"), Card("H","10"), ] hand = Hand(card_list) self.assertEqual(hand.result(), "Two Pairs")
def test_pair_6_at_first_and_second_position(self): card_list = [ Card("H","6"), Card("C","6"), Card("H","8"), Card("H","9"), Card("H","10"), ] hand = Hand(card_list) self.assertEqual(hand.result(), "One Pair")
def test_pair_de_7_na_second_and_fourth_position(self): card_list = [ Card("H","9"), Card("H","7"), Card("H","8"), Card("C","7"), Card("H","10"), ] hand = Hand(card_list) self.assertEqual(hand.result(), "One Pair")
def test_high_card_hand(self): card_list = [ Card("H","6"), Card("C","5"), Card("H","8"), Card("D","2"), Card("H","10"), ] hand = Hand(card_list) self.assertEqual(hand.result(), "High Card")
def test_four_of_8_second_to_last_pos(self): card_list = [ Card("C","9"), Card("C","8"), Card("H","8"), Card("D","8"), Card("S","8"), ] hand = Hand(card_list) self.assertEqual(hand.result(), "Four of a kind")
def test_full_house_of_8_and_9(self): card_list = [ Card("C","9"), Card("H","9"), Card("H","8"), Card("D","8"), Card("S","8"), ] hand = Hand(card_list) self.assertEqual(hand.result(), "Full house")
def test_figures_out_straight_flush_is_best_card(self): cards = [ Card("6", "Diamonds"), Card("7", "Diamonds"), Card("8", "Diamonds"), Card("9", "Diamonds"), Card("10", "Diamonds") ] hand = Hand() hand.add_cards(cards) self.assertEqual(hand.best_rank(), "Straight Flush")
def p54(data_file_path: str) -> int: player_1_wins = 0 with open(data_file_path) as file: for line in file: cards = line.split() player_1_hand = Hand(list(map(Card, cards[0:5]))) player_2_hand = Hand(list(map(Card, cards[5:10]))) if player_1_hand > player_2_hand: player_1_wins += 1 return player_1_wins
def test_comparing_royal_flush(self): hands = [ Hand(list(map(Card, ['AH', 'KH', 'QH', 'JH', 'TH']))), Hand(list(map(Card, ['TS', 'QS', 'KS', 'JS', 'AS']))), Hand(list(map(Card, ['AD', 'KD', 'QD', 'JD', 'TD']))), Hand(list(map(Card, ['AC', 'KC', 'QC', 'JC', 'TC']))), ] for left_index in range(0, len(hands)): for right_index in range(left_index + 1, len(hands)): self.assertEqual(hands[left_index], hands[right_index])
def test_figures_out_four_of_a_kind_is_best_card(self): cards = [ Card("Ace", "Diamonds"), Card("Ace", "Hearts"), Card("Ace", "Clubs"), Card("Ace", "Spades"), Card("King", "Diamonds") ] hand = Hand() hand.add_cards(cards) self.assertEqual(hand.best_rank(), "Four of a Kind")
def test_figures_out_full_house_is_best_card(self): cards = [ Card("Ace", "Diamonds"), Card("Ace", "Hearts"), Card("Ace", "Clubs"), Card("King", "Spades"), Card("King", "Diamonds") ] hand = Hand() hand.add_cards(cards) self.assertEqual(hand.best_rank(), "Full House")
def test_figures_out_royal_flush_is_best_card(self): cards = [ Card("10", "Diamonds"), Card("Jack", "Diamonds"), Card("Queen", "Diamonds"), Card("King", "Diamonds"), Card("Ace", "Diamonds") ] hand = Hand() hand.add_cards(cards) self.assertEqual(hand.best_rank(), "Royal Flush")
def test_comparing_full_house(self): hands = [ Hand(list(map(Card, ['3H', '3C', '3D', '2S', '2H']))), Hand(list(map(Card, ['3H', '3C', '3D', '4S', '4H']))), Hand(list(map(Card, ['4H', '4C', '4D', '2S', '2H']))), Hand(list(map(Card, ['4H', '4C', '4D', '3S', '3H']))), ] for left_index in range(0, len(hands)): for right_index in range(left_index + 1, len(hands)): self.assertTrue(hands[right_index] > hands[left_index])
def test_figures_out_two_pairs_is_best_card(self): cards = [ Card("Ace", "Diamonds"), Card("Ace", "Clubs"), Card("King", "Diamonds"), Card("King", "Clubs"), Card("5", "Clubs"), ] hand = Hand() hand.add_cards(cards) self.assertEqual(hand.best_rank(), "Two Pair")
def test_figures_out_three_of_the_kind_is_best_card(self): cards = [ Card("Ace", "Diamonds"), Card("Ace", "Clubs"), Card("Ace", "Diamonds"), Card("King", "Clubs"), Card("5", "Clubs"), ] hand = Hand() hand.add_cards(cards) self.assertEqual(hand.best_rank(), "Three of a Kind")
def test_suited_hand_to_combos(): assert Hand("76s").to_combos() == ( Combo("7c6c"), Combo("7d6d"), Combo("7h6h"), Combo("7s6s"), )
def testhands(): testinput = [ 'TH TS TC KH KC', # full house K 'QH QS QC 2H 2C', # full house Q 'TH TS TC KH AH', # 3ofakind T '3S 2H 4C 5H 6H', # straight 6 'AH 2S 3C 4H 5H', # straight 5 'AH 2H 3H 4H 5H', # str flush 5 'TH TS QH KH AH', # pair T '8H 8S 9H 9S AS', # twopair 9 'TH TS KH KS AS', # twopair K 'TH JH QH KH AH', # royal A ] testscores = [] for test in testinput: hand = Hand.fromString(test) testscores.append(hand) testscores.sort(key=lambda hand: hand.result().score(), reverse=True) for hand in testscores: print hand.result(), hand.result().score() pass
def test_comparing_different_hand_types(self): hands = [ Hand(list(map(Card, ['2D', '4C', '6H', '8S', 'TD']))), #HC Hand(list(map(Card, ['2D', '4C', '4D', '5H', '8D']))), #OP Hand(list(map(Card, ['2C', '3H', '3C', '6D', '6C']))), #TP Hand(list(map(Card, ['3D', '4H', '4D', '4S', 'TC']))), #TK Hand(list(map(Card, ['7S', '8C', '9H', 'TC', 'JD']))), #S Hand(list(map(Card, ['3H', '5H', 'JH', 'AH', '2H']))), #F Hand(list(map(Card, ['5D', 'TC', 'TH', '5S', '5H']))), #FH Hand(list(map(Card, ['7H', '7D', '7C', '7S', '8H']))), #FK Hand(list(map(Card, ['8D', '9D', 'TD', 'JD', 'QD']))), #SF Hand(list(map(Card, ['TC', 'JC', 'QC', 'KC', 'AC']))), #RF ] for left_index in range(0, len(hands)): for right_index in range(left_index + 1, len(hands)): self.assertTrue(hands[right_index] > hands[left_index])
def setUp(self): self.cards = { Card(5, Suits.Clubs), Card(5, Suits.Spades), Card(2, Suits.Hearts), Card(2, Suits.Diamonds), Card(2, Suits.Spades), } self.hand = Hand(self.cards)
def test_pair_hand_to_combos(): assert Hand("22").to_combos() == ( Combo("2c2d"), Combo("2c2h"), Combo("2c2s"), Combo("2d2h"), Combo("2d2s"), Combo("2h2s"), )
def test_comparing_three_of_a_kind(self): ordered_hands = [ Hand(list(map(Card, ['6H', '6D', '6C', '2S', '4H']))), Hand(list(map(Card, ['6H', '6D', '6C', '3S', '4H']))), Hand(list(map(Card, ['6H', '6D', '6C', '3S', '5H']))), Hand(list(map(Card, ['6H', '6D', '6C', '7S', '9H']))), Hand(list(map(Card, ['6H', '6D', '6C', '8S', '9H']))), Hand(list(map(Card, ['6H', '6D', '6C', '7S', 'TH']))), Hand(list(map(Card, ['7H', '7D', '7C', '2S', '4H']))), Hand(list(map(Card, ['7H', '7D', '7C', '8S', '9H']))), ] for left_index in range(0, len(ordered_hands)): for right_index in range(left_index + 1, len(ordered_hands)): self.assertTrue( ordered_hands[right_index] > ordered_hands[left_index])
def parse_player_hands_from_line(line): cards = line.split(' ') # For every chunk of 5 cards (a player's hand) chunk_size = 5 player_hands = [] player_number = 1 for i in range(0, len(cards), chunk_size): player_hand = Hand(cards[i:i + chunk_size]) print("Player {} Hand: {}".format(player_number, str(player_hand))) player_hands.append(player_hand) player_number += 1 # Determine the winner determine_winner(player_hands)
def test_offsuit_hand_to_combos(): assert Hand("76o").to_combos() == ( Combo("7c6d"), Combo("7c6h"), Combo("7c6s"), Combo("7d6c"), Combo("7d6h"), Combo("7d6s"), Combo("7h6c"), Combo("7h6d"), Combo("7h6s"), Combo("7s6c"), Combo("7s6d"), Combo("7s6h"), )
def test_full_house(self): # Given hand = Hand([ Card(CardFace.Two, CardSuit.Clubs), Card(CardFace.Five, CardSuit.Spades), Card(CardFace.Five, CardSuit.Clubs), Card(CardFace.Five, CardSuit.Hearts), Card(CardFace.Two, CardSuit.Hearts), ]) # When hand_rank = hand.rank expected = Trick.FullHouse # Then expect(hand_rank).to(equal(expected))
def test_three_of_a_kind(self): # Given hand = Hand([ Card(CardFace.Two, CardSuit.Clubs), Card(CardFace.Five, CardSuit.Spades), Card(CardFace.Five, CardSuit.Clubs), Card(CardFace.Five, CardSuit.Hearts), Card(CardFace.Six, CardSuit.Hearts), ]) # When hand_rank = hand.rank expected = Trick.ThreeOfAKind # Then expect(hand_rank).to(equal(expected))
def test_two_pair(self): # Given hand = Hand([ Card(CardFace.Two, CardSuit.Clubs), Card(CardFace.Five, CardSuit.Spades), Card(CardFace.Five, CardSuit.Clubs), Card(CardFace.Six, CardSuit.Clubs), Card(CardFace.Six, CardSuit.Hearts), ]) # When hand_rank = hand.rank expected = Trick.TwoPair # Then expect(hand_rank).to(equal(expected))
def test_hi_card(self): # Given hand = Hand([ Card(CardFace.Two, CardSuit.Clubs), Card(CardFace.Four, CardSuit.Spades), Card(CardFace.Five, CardSuit.Clubs), Card(CardFace.Six, CardSuit.Clubs), Card(CardFace.Seven, CardSuit.Clubs), ]) # When hand_rank = hand.rank expected = Trick.HiCard # Then expect(hand_rank).to(equal(expected))
class FullHouseTest(TestCase): def setUp(self): self.cards = { Card(5, Suits.Clubs), Card(5, Suits.Spades), Card(2, Suits.Hearts), Card(2, Suits.Diamonds), Card(2, Suits.Spades), } self.hand = Hand(self.cards) def test_category(self): self.assertEqual(self.hand.best_category, Categories.FullHouse, self.hand.count_groups()) def test_kickers(self): self.assertEqual(self.hand.kickers, set()) def test_punchers(self): self.assertEqual(self.cards, self.hand.punchers)
def test_make_random(): hand = Hand.make_random() assert isinstance(hand, Hand) assert isinstance(hand.first, Rank) assert isinstance(hand.second, Rank)