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_lt_eq_card(): card1 = cardlib.NumberedCard(5, cardlib.Suits.Hearts) card2 = cardlib.NumberedCard(5, cardlib.Suits.Hearts) card3 = cardlib.AceCard(cardlib.Suits.Spades) assert card1 == card2 assert card1 < card3
def test_equal_between_cardsuits(): ''' Test the __eq__ method to compare two playingcards ''' suit1 = cardlib.Suit.SPADES card1 = cardlib.NumberedCard(9, suit1) for suit2 in cardlib.Suit: card2 = cardlib.NumberedCard(9, suit2) card1 == card2
def test_flush(): # Checking the flush function hand = cl.Hand() for i in range(5): hand.add_card(cl.NumberedCard(cl.Rank(2), cl.Suit(0))) k = cl.check_flush(hand.cards) assert k[0] == cl.NumberedCard(cl.Rank(2), cl.Suit(0)).give_value().value assert k[1] == hand.cards hand.drop_cards(0) assert cl.check_flush(hand.cards) is None
def test_cards(): # Testing so that the card functions give the correct values assert cl.NumberedCard(cl.Rank(2), cl.Suit(0)).card[0].value == 2 assert cl.NumberedCard(cl.Rank(2), cl.Suit(0)).card[0].name == "Two" assert cl.NumberedCard(cl.Rank(2), cl.Suit(0)).card[1].value == 0 assert cl.NumberedCard(cl.Rank(2), cl.Suit(0)).card[1].name == "Clubs" assert cl.JackCard(cl.Suit(0)).give_value().value == 11 assert cl.QueenCard(cl.Suit(0)).give_value().value == 12 assert cl.KingCard(cl.Suit(0)).give_value().value == 13 assert cl.AceCard(cl.Suit(0)).give_value().value == 14
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_numberedcard_out_of_bounds(): ''' Test if we can call numberedcard with a too high value ''' value = 10 card1 = cardlib.NumberedCard(value, cardlib.Suit.SPADES) assert card1.get_value() == value with pytest.raises(cardlib.NumberedCardOutOfRangeError): value2 = 14 cardlib.NumberedCard(value2, cardlib.Suit.SPADES)
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_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_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
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_numberedcard_creates_playingcard(): ''' Test that numberedcard creates a card with the correct value ''' for value in range(2, 11): for suit in cardlib.Suit: card = cardlib.NumberedCard(value, suit) assert type(card.get_value()) == int assert card.get_value() == value
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_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_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_less_and_greater_than_of_playingcards(): ''' Test the __lt__ and __gt__ methods between two playing cards (less than & greater than) ''' suit = cardlib.Suit.SPADES ace = cardlib.AceCard(suit) king = cardlib.KingCard(suit) queen = cardlib.QueenCard(suit) jack = cardlib.JackCard(suit) for value in range(2, 11): numberedcard = cardlib.NumberedCard(value, suit) assert ace > numberedcard assert king > numberedcard assert queen > numberedcard assert jack > numberedcard assert numberedcard < ace assert numberedcard < king assert numberedcard < queen assert numberedcard < jack assert ace > king assert ace > queen assert ace > jack assert king < ace assert king > queen assert king > jack assert queen < ace assert queen < king assert queen > jack assert jack < ace assert jack < king assert jack < queen
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