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_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_get_points_of_hand_JQK(self): self.game.hand.add(Card("Jack", "Clubs")) self.assertEqual(10, get_points(self.game.hand)) self.game.hand.add(Card("Queen", "Clubs")) self.assertEqual(20, get_points(self.game.hand)) self.game.hand.add(Card("King", "Clubs")) self.assertEqual(30, get_points(self.game.hand))
def test_get_hand(self): card_1 = Card('Ace', 'Spades') card_2 = Card('2', 'Diamonds') temp_hand = [card_1, card_2] test_hand = Stack(cards=temp_hand) self.player.hand = test_hand self.assertEqual(self.player.get_hand(), test_hand)
def three_of_a_kind(value): """A natural three of a kind, comprised of clubs, diamonds and hearts.""" return [ Card(value, 'clubs'), Card(value, 'diamonds'), Card(value, 'hearts') ]
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_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_basic(self): cards = Stack(cards=[Card("3", "Diamonds"), Card("3", "Clubs")]) self.assertEqual(get_money_lost(cards, 5, 20), 10) cards = Stack(cards=[ Card("3", "Diamonds"), Card("3", "Clubs"), Card("3", "Hearts") ]) self.assertEqual(get_money_lost(cards, 5, 20), 15)
def test_prompt_to_meld_manual_meld(self, mock_input): self.game.hand.add(all_spades) self.game.down = True self.game.opponents[0].down_cards.add(three_of_a_kind(3)) self.game.opponents[0].down_cards.add(three_of_a_kind(4)) self.game.opponents[1].down_cards.add(three_of_a_kind(4)) self.game.opponents[1].down_cards.add(three_of_a_kind(5)) self.game.opponents[2].down_cards.add(three_of_a_kind(5)) self.game.opponents[2].down_cards.add(three_of_a_kind(6)) self.game.prompt_to_meld() self.assertEqual( deque([ Card(value='2', suit='Spades'), Card(value='7', suit='Spades'), Card(value='8', suit='Spades'), Card(value='9', suit='Spades'), Card(value='10', suit='Spades'), Card(value='Jack', suit='Spades'), Card(value='Queen', suit='Spades'), Card(value='King', suit='Spades'), Card(value='Ace', suit='Spades') ]), self.game.hand.cards)
def test_get_hand_total(self): card_1 = Card('Ace', 'Hearts') card_2 = Card('King', 'Spades') card_3 = Card('Ace', 'Hearts') card_4 = Card('Ace', 'Spades') card_5 = Card('10', 'Diamonds') card_6 = Card('Ace', 'Clubs') card_7 = Card('7', 'Hearts') card_8 = Card('9', 'Spades') card_9 = Card('Queen', 'Diamonds') temp_hand = [card_1, card_3, card_4] test_hand_1 = Stack(cards=temp_hand) temp_hand = [card_1, card_3, card_4, card_6] test_hand_2 = Stack(cards=temp_hand) temp_hand = [card_1, card_2, card_3, card_8] test_hand_3 = Stack(cards=temp_hand) temp_hand = [card_7, card_8, card_5] test_hand_4 = Stack(cards=temp_hand) temp_hand = [card_9, card_2] test_hand_5 = Stack(cards=temp_hand) self.assertEqual(get_hand_total(test_hand_1), 13) self.assertEqual(get_hand_total(test_hand_2), 14) self.assertEqual(get_hand_total(test_hand_3), 21) self.assertEqual(get_hand_total(test_hand_4), 26) self.assertEqual(get_hand_total(test_hand_5), 20)
def test_empty_hand(self): card_1 = Card('Ace', 'Spades') card_2 = Card('2', 'Diamonds') temp_hand = [card_1, card_2] test_hand = Stack(cards=temp_hand) self.player.hand = test_hand self.player.empty_hand() self.assertEqual(self.player.hand.size, 0) # Test that emptying an empty hand is ok self.player.empty_hand() self.assertEqual(self.player.hand.size, 0)
def test_two_three_of_a_kind_non_match(self): """Test hands that do not satisfy the victory condition of two three of a kinds.""" mix = [ Card('3', 'clubs'), Card('4', 'diamonds'), Card('5', 'hearts'), Card('6', 'spades') ] self.game.hand.add(mix) self.assertFalse( VictoryConditions.two_three_of_a_kind(self.game.hand.cards, self.game.victory_cards))
def test_go_down_manual_select_with_wild_cards(self, mock_input): self.game.hand.add(wild_three_of_a_kind(3)) self.game.hand.add(wild_three_of_a_kind(4)) self.game.hand.add(wild_three_of_a_kind(5)) VictoryConditions.two_three_of_a_kind(self.game.hand.cards, self.game.victory_cards) self.game.go_down() self.assertEqual( Stack(cards=deque([ Card(value='5', suit='Diamonds'), Card(value='5', suit='Hearts') ])), self.game.hand)
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_single(self): for i in range(num_tests): value = random.choice(VALUES[1:]) cards_a = Stack(cards=[Card(value, random.choice(SUITS))]) if value == "Ace": value = "2" else: value = random.choice(VALUES[(VALUES.index(value) + 1):]) cards_b = Stack(cards=[Card(value, random.choice(SUITS))]) self.assertTrue(are_cards_bigger(cards_a, cards_b))
def test_ten_cards(self): cards = Stack(cards=[ Card("3", "Diamonds"), Card("4", "Clubs"), Card("5", "Diamonds"), Card("6", "Clubs"), Card("7", "Diamonds"), Card("8", "Clubs"), Card("9", "Diamonds"), Card("10", "Clubs"), Card("Jack", "Diamonds"), Card("Queen", "Clubs") ]) self.assertEqual(get_money_lost(cards, 5, 20), 100)
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 addCards(self, cards_played, allowed, numCardsInBuffer): #dumbAI length = numCardsInBuffer / 2 MAX = Card(value='Jack', suit='Diamonds') add_cards = [] have_legal_cards = False listOfLegalValues = list(set(x.value for x in cards_played)) print("LEGAL CARDS FOR ADDING:", listOfLegalValues) logger.info("LEGAL CARDS FOR ADDING: " + str(listOfLegalValues)) for card in self.hand: if card.value in listOfLegalValues: if card.suit != kozer or (length > 2 and card.lt(MAX)): if len(add_cards) < allowed: add_cards.append(card) if (len(add_cards) > 0): self.removeCards(add_cards) logger.info("inside dumbAI::addCards (" + self.name + ") hand is now: " + str(prettyShort(self.hand))) return add_cards else: print( colored( "No Cards can be added by " + self.name + ", moving on...", 'yellow')) logger.info("No cards can be added by " + self.name + ", moving on...") return []
def test_discard(self): all_spades_minus_ace = [ spade for spade in all_spades if spade.value != 'Ace' ] self.game.hand.add(all_spades) self.assertEqual(Card('Ace', 'spades'), self.game.discard(12, self.game.hand)) self.assertEqual(all_spades_minus_ace, self.game.hand)
def test_add_cards(self): card_1 = Card('Jack', 'Clubs') card_2 = Card('King', 'Hearts') temp_hand = [card_1] test_hand = Stack(cards=temp_hand) self.player.add_cards(temp_hand) self.assertEqual(self.player.hand, test_hand) # Test adding the same card twice self.player.add_cards(temp_hand) temp_hand = [card_1, card_1] test_hand = Stack(cards=temp_hand) self.assertEqual(self.player.hand, test_hand) temp_hand = [card_2] self.player.add_cards(temp_hand) temp_hand = [card_1, card_1, card_2] test_hand = Stack(cards=temp_hand) self.assertEqual(self.player.hand, test_hand)
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_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_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 get_stack_for_deck(): stack: Stack = Stack(); cards: List[Card] = [] for suit in SUITS: for value in VALUES: if value not in ["2", "3", "4", "5", "6"]: cards.append(Card(value, suit)) stack.add(cards) return stack
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_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_check_for_bust(self): card_1 = Card('Ace', 'Hearts') card_2 = Card('2', 'Spades') card_3 = Card('Ace', 'Hearts') card_4 = Card('Jack', 'Spades') card_5 = Card('9', 'Diamonds') temp_hand = [card_1, card_3, card_4] test_hand_1 = Stack(cards=temp_hand) temp_hand = [card_1, card_3, card_4, card_5] test_hand_2 = Stack(cards=temp_hand) temp_hand = [card_1, card_2, card_3, card_5] test_hand_3 = Stack(cards=temp_hand) temp_hand = [card_2, card_3, card_4] test_hand_4 = Stack(cards=temp_hand) temp_hand = [card_2, card_3, card_4, card_5] test_hand_5 = Stack(cards=temp_hand) self.assertFalse(check_for_bust(test_hand_1)) self.assertFalse(check_for_bust(test_hand_2)) self.assertFalse(check_for_bust(test_hand_3)) self.assertFalse(check_for_bust(test_hand_4)) self.assertTrue(check_for_bust(test_hand_5))
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_is_high_card(): hands = init_hands() player_hand = stack_and_sort( [Card("Ace", "Spades"), Card("3", "Clubs"), Card("5", "Hearts"), Card("7", "Spades"), Card("9", "Spades"), Card("10", "Diamonds"), Card("King", "Hearts")]) is_high_card, high_cards = hands.is_high_card(player_hand) assert is_high_card assert high_cards[0].value == "Ace" assert high_cards[1].value == "King" assert high_cards[2].value == "10" assert high_cards[3].value == "9" assert high_cards[4].value == "7"
def setUp(self) -> None: self.game = Game() self.game.hand.empty() self.threes_and_fours_hand = Stack(cards=deque([ Card(value='3', suit='Clubs'), Card(value='3', suit='Diamonds'), Card(value='3', suit='Hearts'), Card(value='4', suit='Clubs'), Card(value='4', suit='Diamonds'), Card(value='4', suit='Hearts') ]))