def test_poker_hand(): # Creates a poker table with 5 cards card1 = cardlib.NumberedCard(5, cardlib.Suits.Hearts) card2 = cardlib.NumberedCard(5, cardlib.Suits.Hearts) card3 = cardlib.AceCard(cardlib.Suits.Spades) card4 = cardlib.KingCard(cardlib.Suits.Clubs) card5 = cardlib.QueenCard(cardlib.Suits.Diamonds) poker_table = cardlib.Hand() poker_table.cards = [card1, card2, card3, card4, card5] # Creates two hands with two cards hand1 = cardlib.Hand() hand2 = cardlib.Hand() hand1.cards = [cardlib.NumberedCard(10, cardlib.Suits.Diamonds), cardlib.JackCard(cardlib.Suits.Hearts)] hand2.cards = [cardlib.NumberedCard(5, cardlib.Suits.Clubs), cardlib.NumberedCard(4, cardlib.Suits.Diamonds)] # Create two pokerhands using the hands cards and the poker_table cards pokerhand1 = hand1.best_poker_hand(poker_table.cards) # Should return poker hand 'Straight' pokerhand2 = hand2.best_poker_hand(poker_table.cards) # Should return poker hand 'Three of a kind' assert pokerhand1.poker_hand.name == 'Straight' assert pokerhand2.poker_hand.name == 'Threeofakind' # Testing < operand for the pokerhand comparison assert pokerhand2 < pokerhand1
def test_same_poker_hand_diff_values(): ''' #Check to see that the equals and lessthan methods work as intended. ''' player1hand = cardlib.Hand() player1hand.add_new_card(cardlib.NumberedCard(8, cardlib.Suit.HEARTS)) player1hand.add_new_card(cardlib.NumberedCard(8, cardlib.Suit.SPADES)) player1hand.add_new_card(cardlib.NumberedCard(8, cardlib.Suit.CLUBS)) player1hand.add_new_card(cardlib.NumberedCard(5, cardlib.Suit.SPADES)) player1hand.add_new_card(cardlib.NumberedCard(5, cardlib.Suit.HEARTS)) player2hand = cardlib.Hand() player2hand.add_new_card(cardlib.NumberedCard(8, cardlib.Suit.HEARTS)) player2hand.add_new_card(cardlib.NumberedCard(8, cardlib.Suit.SPADES)) player2hand.add_new_card(cardlib.NumberedCard(8, cardlib.Suit.CLUBS)) player2hand.add_new_card(cardlib.NumberedCard(10, cardlib.Suit.SPADES)) player2hand.add_new_card(cardlib.NumberedCard(10, cardlib.Suit.HEARTS)) player3hand = cardlib.Hand() player3hand.add_new_card(cardlib.NumberedCard(10, cardlib.Suit.HEARTS)) player3hand.add_new_card(cardlib.NumberedCard(10, cardlib.Suit.SPADES)) player3hand.add_new_card(cardlib.NumberedCard(10, cardlib.Suit.CLUBS)) player3hand.add_new_card(cardlib.NumberedCard(10, cardlib.Suit.DIAMONDS)) player3hand.add_new_card(cardlib.NumberedCard(4, cardlib.Suit.HEARTS)) pokerhand1 = cardlib.Hand.best_poker_hand(player1hand) pokerhand2 = cardlib.Hand.best_poker_hand(player2hand) pokerhand3 = cardlib.Hand.best_poker_hand(player3hand) assert pokerhand2 > pokerhand1 assert pokerhand1 == pokerhand1 assert pokerhand3 > pokerhand2
def test_three_kind(): # Checking the three kind function hand = cl.Hand() for i in range(3): hand.add_card(cl.NumberedCard(cl.Rank(3), cl.Suit(0))) k = cl.check_three_kind(hand.cards) assert k[0] == cl.NumberedCard(cl.Rank(3), cl.Suit(0)).give_value().value assert k[1] == hand.cards hand.drop_cards(0) hand2 = cl.Hand() hand2.cards = hand.cards assert cl.check_three_kind(hand2.cards) is None
def test_pokerhand(): # Creating the pokerhand objects and then testing them against other pokerhands. deck = cl.Deck() deck.create_deck() hand = cl.Hand() hand2 = cl.Hand() for i in range(5): hand.add_card(deck.draw()) for i in range(5): hand2.add_card(deck.draw()) bph = hand.best_poker_hand() bph2 = hand2.best_poker_hand() assert bph > bph2 assert bph == bph assert bph2 < bph
def test_add_new_card_to_hand(): empty_hand = [] card = cardlib.NumberedCard(8, cardlib.Suit.DIAMONDS) hand = cardlib.Hand() hand.add_new_card(card) assert hand.cards > empty_hand assert len(hand.cards) == 1
def test_four_kind(): # Checking the four kind function hand = cl.Hand() for i in range(4): hand.add_card(cl.NumberedCard(cl.Rank(2), cl.Suit(0))) k = cl.check_four_kind(hand.cards) assert k[0] == hand.cards[0].give_value().value assert k[1] == hand.cards hand.drop_cards(0) assert cl.check_four_kind(hand.cards) is None
def test_drop_cards_drop_correct_card_from_hand(): hand = cardlib.Hand() hand.add_new_card(cardlib.QueenCard(cardlib.Suit.DIAMONDS)) hand.add_new_card(cardlib.KingCard(cardlib.Suit.SPADES)) hand.add_new_card(cardlib.JackCard(cardlib.Suit.HEARTS)) dropped_cards = [hand.cards[0], hand.cards[1]] # copy topcard of deck hand.drop_cards([0, 1]) assert (dropped_cards[0] and dropped_cards[1]) not in hand.cards assert hand.cards[0] == cardlib.JackCard(cardlib.Suit.HEARTS)
def test_straight(): # Checking the straight function hand = cl.Hand() for i in range(2, 11): hand.add_card(cl.NumberedCard(cl.Rank(i), cl.Suit(0))) k = cl.check_straight(hand.cards) assert k[0] == cl.NumberedCard(cl.Rank(10), cl.Suit(0)).give_value().value hand.sort_cards() assert k[1] == hand.cards[:5] hand.cards = hand.cards[:4] assert cl.check_straight(hand.cards) is None
def test_add_new_card_adds_correct_card_from_source(): deck = cardlib.StandardDeck() hand = cardlib.Hand() deck_top_card = deck.cards[0] # copy topcard of deck hand.add_new_card(deck.deal_top_card()) # deal top card to hand assert hand.cards[0] == deck_top_card # Check that the previous topcard is discarded assert deck.cards[0] != deck_top_card
def test_high_card(): # Checking the highest card function hand = cl.Hand() hand.add_card(cl.AceCard(cl.Suit(2))) hand.add_card(cl.AceCard(cl.Suit(3))) hand.add_card(cl.NumberedCard(cl.Rank(2), cl.Suit(0))) k = cl.check_high_card(hand.cards) assert k[1] == hand.cards assert k[0] == cl.AceCard(cl.Suit(3)).give_value().value assert k[1][0].give_suit().value == cl.AceCard( cl.Suit(3)).give_suit().value
def test_straight_flush(): # Checking the Straight Flush function deck = cl.Deck() deck.create_deck() hand = cl.Hand() for i in range(5): hand.add_card(deck.draw()) k = cl.check_straight_flush(hand.cards) assert k[0] == 14 assert k[1] == cl.AceCard(cl.Suit(3)) hand.drop_cards(0) assert cl.check_straight_flush(hand.cards) is None
def test_pair(): # Checking the pair function hand = cl.Hand() hand.add_card(cl.NumberedCard(cl.Rank(2), cl.Suit(0))) hand.add_card(cl.NumberedCard(cl.Rank(2), cl.Suit(0))) hand.add_card(cl.NumberedCard(cl.Rank(4), cl.Suit(0))) hand.add_card(cl.NumberedCard(cl.Rank(4), cl.Suit(0))) k = cl.check_pair(hand.cards) hand.sort_cards() hand.drop_cards([2, 3]) assert k[0] == cl.NumberedCard(cl.Rank(4), cl.Suit(0)).give_value().value assert k[1] == hand.cards
def test_straight_flush(): card_1 = num_card(6, suit.spades) card_2 = num_card(7, suit.spades) player_1_cards = cardlib.Hand() player_1_cards.add_card(card_1) player_1_cards.add_card(card_2) community_cards = [(num_card(1, suit.spades)), (num_card(2, suit.spades)), (num_card(3, suit.spades)), (num_card(4, suit.spades)), (num_card(5, suit.spades))] poker_hand = player_1_cards.best_poker_hand(community_cards)
def test_two_pair(): # Checking the two pair function hand = cl.Hand() for i in range(3, 5): hand.add_card(cl.NumberedCard(cl.Rank(i), cl.Suit(0))) hand.add_card(cl.NumberedCard(cl.Rank(i), cl.Suit(0))) k = cl.check_two_pair(hand.cards) assert k[0][0] == cl.NumberedCard(cl.Rank(4), cl.Suit(0)).give_value().value assert k[0][1] == cl.NumberedCard(cl.Rank(3), cl.Suit(0)).give_value().value assert k[1][0][0] == cl.NumberedCard(cl.Rank(4), cl.Suit(0)) assert k[1][1][0] == cl.NumberedCard(cl.Rank(3), cl.Suit(0))
def test_high_card(): card_1 = num_card(9, suit.hearts) card_2 = num_card(7, suit.spades) player_1_cards = cardlib.Hand() player_1_cards.add_card(card_1) player_1_cards.add_card(card_2) community_cards = [(num_card(2, suit.hearts)), (num_card(3, suit.clubs)), (num_card(4, suit.spades)), (num_card(4, suit.diamonds)), (num_card(5, suit.diamonds))] check = cardlib.PokerHand.check_high_card(player_1_cards, community_cards) assert check == [num_card(9, suit.hearts)]
def test_hand_and_deck(): # Creating the cards and then testing them towards the values they should have. Checking if the functions in the # hand and deck classes are working as well as the values from the poker_hand class. deck = cl.Deck() deck.create_deck() # Testing create_deck func hand = cl.Hand() hand2 = cl.Hand() n = 5 k = [0, 2, 4] for i in range(n): hand.add_card(deck.draw()) # Testing add_card function hand.drop_cards(k) # Testing drop_cards funtion hand2.add_card(cl.KingCard(cl.Suit(0))) hand2.add_card(cl.JackCard(cl.Suit(0))) assert len(hand.cards) == n - len(k) assert hand.cards == hand2.cards hand2.add_card(cl.AceCard(cl.Suit(0))) hand2.sort_cards() # Test of sort function assert hand2.cards[0] == cl.AceCard(cl.Suit(0)) poker_hand = hand2.best_poker_hand() assert poker_hand.type == cl.HandType(1) # Test of best_poker_hand assert poker_hand.value == cl.AceCard(cl.Suit(0)).card[0] assert poker_hand.cards == hand2.cards
def test_straight_flush(): card_1 = num_card(6, suit.spades) card_2 = num_card(7, suit.spades) player_1_cards = cardlib.Hand() player_1_cards.add_card(card_1) player_1_cards.add_card(card_2) community_cards = [(num_card(1, suit.spades)), (num_card(2, suit.spades)), (num_card(3, suit.spades)), (num_card(4, suit.spades)), (num_card(5, suit.spades))] check = cardlib.PokerHand.check_straight_flush(player_1_cards, community_cards) assert check == [num_card(7, suit.spades)]
def test_add_drop_cards(): deck = cardlib.StandardDeck() test_deck1 = [card.get_value() for card in deck.cards] deck.shuffledeck() test_deck2 = [card.get_value() for card in deck.cards] assert deck.cards != [] assert test_deck1 != test_deck2 # Tests shuffle function test_hand = cardlib.Hand() test_hand.add(deck.drawfromdeck()) test_hand.add(deck.drawfromdeck()) assert len(test_hand.cards) == 2 test_hand.drop([0]) assert len(test_hand.cards) == 1
def test_sort_cards_is_sorting(): my_hand = cardlib.Hand() my_hand.add_new_card(cardlib.AceCard(cardlib.Suit.CLUBS)) my_hand.add_new_card(cardlib.NumberedCard(9, cardlib.Suit.SPADES)) my_hand.add_new_card(cardlib.NumberedCard(2, cardlib.Suit.DIAMONDS)) my_hand.add_new_card(cardlib.NumberedCard(8, cardlib.Suit.HEARTS)) my_hand.add_new_card(cardlib.NumberedCard(5, cardlib.Suit.DIAMONDS)) my_hand.sort_cards() # sort sorts card in order: 1 - suit 2-value assert my_hand.cards[0] == cardlib.NumberedCard(9, cardlib.Suit.SPADES) assert my_hand.cards[1] == cardlib.NumberedCard(8, cardlib.Suit.HEARTS) assert my_hand.cards[2] == cardlib.AceCard(cardlib.Suit.CLUBS) assert my_hand.cards[3] == cardlib.NumberedCard(2, cardlib.Suit.DIAMONDS) assert my_hand.cards[4] == cardlib.NumberedCard(5, cardlib.Suit.DIAMONDS)
def test_flush(): card_1 = num_card(12, suit.hearts) card_2 = num_card(7, suit.spades) player_1_cards = cardlib.Hand() player_1_cards.add_card(card_1) player_1_cards.add_card(card_2) # Create a large list of community cards resulting in multiple flushes, with # 12 of hearts being the highest-ranking card and thus the best flush. community_cards = [(num_card(2, suit.hearts)), (num_card(3, suit.hearts)), (num_card(4, suit.hearts)), (num_card(6, suit.hearts)), (num_card(5, suit.hearts)), (num_card(2, suit.spades)), (num_card(3, suit.spades)), (num_card(4, suit.spades)), (num_card(6, suit.spades)), (num_card(5, suit.spades))] check = cardlib.PokerHand.check_flush(player_1_cards, community_cards) assert check[0] == card_1
def test_full_house(): card_1 = num_card(7, suit.hearts) card_2 = num_card(9, suit.spades) player_1_cards = cardlib.Hand() player_1_cards.add_card(card_1) player_1_cards.add_card(card_2) community_cards = [(num_card(9, suit.hearts)), (num_card(10, suit.spades)), (num_card(7, suit.clubs)), (num_card(10, suit.diamonds)), (num_card(1, suit.spades)), num_card(9, suit.clubs)] check = cardlib.PokerHand.check_full_house(player_1_cards, community_cards) assert check == [(num_card(9, suit.hearts)), (num_card(9, suit.spades)), (num_card(9, suit.clubs)), (num_card(10, suit.spades)), (num_card(10, suit.diamonds))]
def test_full_house(): # Checking the full house function hand = cl.Hand() for i in range(3): hand.add_card(cl.NumberedCard(cl.Rank(2), cl.Suit(0))) for i in range(2): hand.add_card(cl.NumberedCard(cl.Rank(3), cl.Suit(0))) k = cl.check_full_house(hand.cards) hand.sort_cards() assert k[0][0] == cl.NumberedCard(cl.Rank(2), cl.Suit(0)).give_value().value assert k[0][1] == cl.NumberedCard(cl.Rank(3), cl.Suit(0)).give_value().value assert k[1][0][0].give_value() == hand.cards[4].give_value() assert k[1][1][0].give_value() == hand.cards[0].give_value() hand.drop_cards(0) assert cl.check_full_house(hand.cards) is None
import cardlib num_card = cardlib.NumberedCard jack_card = cardlib.JackCard queen_card = cardlib.QueenCard king_card = cardlib.KingCard ace_card = cardlib.AceCard hand = cardlib.Hand() suit = cardlib.Suit def test_cards(): card_1 = num_card(2, cardlib.Suit.clubs) assert card_1.get_value() == 2 assert card_1.get_suit() == 0 card_2 = cardlib.JackCard(cardlib.Suit.diamonds) assert card_2.get_value() == 11 assert card_2.get_suit() == 1 card_3 = cardlib.QueenCard(cardlib.Suit.spades) assert card_3.get_value() == 12 assert card_3.get_suit() == 2 card_4 = cardlib.KingCard(cardlib.Suit.diamonds) assert card_4.get_value() == 13 assert card_4.get_suit() == 1 card_5 = cardlib.AceCard(cardlib.Suit.diamonds)
def test_best_poker_hand(): deck_1 = cardlib.StandardDeck() deck_1.create_deck() card_1 = num_card(9, suit.spades) card_2 = ace_card(suit.hearts) player_1_cards = cardlib.Hand() player_1_cards.add_card(card_1) player_1_cards.add_card(card_2) # High card community_cards = [(num_card(1, suit.diamonds)), (num_card(8, suit.spades)), (num_card(3, suit.spades)), (num_card(4, suit.clubs)), (num_card(5, suit.spades))] check_high_card = player_1_cards.best_poker_hand(community_cards) assert check_high_card == [card_2] # One pair community_cards = [(num_card(3, suit.diamonds)), (num_card(8, suit.spades)), (num_card(3, suit.spades)), (num_card(4, suit.clubs)), (num_card(5, suit.spades))] check_one_pair = player_1_cards.best_poker_hand(community_cards) assert check_one_pair == [ num_card(3, suit.spades), num_card(3, suit.diamonds) ] # Two pairs community_cards = [(num_card(3, suit.diamonds)), (num_card(8, suit.spades)), (num_card(3, suit.spades)), (num_card(4, suit.clubs)), (ace_card(suit.spades))] check_two_pairs = player_1_cards.best_poker_hand(community_cards) assert check_two_pairs == [ ace_card(suit.hearts), ace_card(suit.spades), num_card(3, suit.spades), num_card(3, suit.diamonds) ] # Three of a kind community_cards = [(num_card(3, suit.diamonds)), (num_card(3, suit.clubs)), (num_card(3, suit.spades)), (num_card(4, suit.clubs)), (num_card(2, suit.spades))] check_three_of_a_kind = player_1_cards.best_poker_hand(community_cards) assert check_three_of_a_kind == [(num_card(3, suit.spades)), (num_card(3, suit.diamonds)), (num_card(3, suit.clubs))] # Straight community_cards = [(num_card(7, suit.diamonds)), (num_card(5, suit.clubs)), (num_card(8, suit.spades)), (num_card(6, suit.clubs)), (num_card(2, suit.spades))] check_straight = player_1_cards.best_poker_hand(community_cards) assert check_straight == [(num_card(9, suit.spades))] # Flush community_cards = [(num_card(3, suit.hearts)), (num_card(3, suit.hearts)), (num_card(3, suit.hearts)), (num_card(4, suit.hearts)), (num_card(2, suit.spades))] check_flush = player_1_cards.best_poker_hand(community_cards) assert check_flush == [(ace_card(suit.hearts)), (num_card(4, suit.hearts)), (num_card(3, suit.hearts)), (num_card(3, suit.hearts)), (num_card(3, suit.hearts))] # Full house community_cards = [(ace_card(suit.diamonds)), (num_card(3, suit.clubs)), (num_card(3, suit.spades)), (num_card(3, suit.clubs)), (num_card(2, suit.spades))] check_full_house = player_1_cards.best_poker_hand(community_cards) assert check_full_house == [(num_card(3, suit.spades)), (num_card(3, suit.clubs)), (num_card(3, suit.clubs)), (ace_card(suit.hearts)), (ace_card(suit.diamonds))] # Four of a kind community_cards = [(num_card(3, suit.diamonds)), (num_card(3, suit.clubs)), (num_card(3, suit.spades)), (num_card(3, suit.hearts)), (num_card(2, suit.spades))] check_four_of_a_kind = player_1_cards.best_poker_hand(community_cards) assert check_four_of_a_kind == [(num_card(3, suit.hearts)), (num_card(3, suit.spades)), (num_card(3, suit.diamonds)), (num_card(3, suit.clubs))] # Straight flush community_cards = [(king_card(suit.hearts)), (jack_card(suit.hearts)), (queen_card(suit.hearts)), (num_card(10, suit.hearts)), (num_card(2, suit.spades))] check_straight_flush = player_1_cards.best_poker_hand(community_cards) assert check_straight_flush == [(ace_card(suit.hearts))]