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)
class Player(object): def __init__(self, starting_chips=100): self.chips = starting_chips self.hand = Stack() def get_chips(self): return self.chips def get_hand(self): return self.hand def empty_hand(self): """Empties the contents of the player's hand""" self.hand.empty() def add_cards(self, cards): """Add the list of cards to the player's hand""" self.hand += cards def add_chips(self, num_chips): """Add chips to player's chip total""" self.chips += num_chips def take_chips(self, num_chips): """Subtract chips from player's chip total""" assert (num_chips <= self.chips) self.chips -= num_chips
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_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_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_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_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 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_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_go_down_auto_select(self): self.game.hand.add(self.threes_and_fours_hand) VictoryConditions.two_three_of_a_kind(self.game.hand.cards, self.game.victory_cards) self.game.go_down() self.assertEqual(Stack(), self.game.hand) self.assertEqual(self.threes_and_fours_hand, self.game.down_cards)
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 test_invalid(self): cards = Stack(cards=[Card("3", SUITS[0]), Card("8", SUITS[1])]) self.assertEqual(get_cards_type(cards), -1) cards = Stack(cards=[Card("3", SUITS[0]), Card("4", SUITS[0]), Card("8", SUITS[1])]) self.assertEqual(get_cards_type(cards), -1) cards = Stack(cards=[Card("3", SUITS[0]), Card("4", SUITS[0]), Card("5", SUITS[0]), Card("8", SUITS[1])]) self.assertEqual(get_cards_type(cards), -1) cards = Stack(cards=[Card("3", SUITS[0]), Card("4", SUITS[0]), Card("5", SUITS[0]), Card("6", SUITS[0]), Card("8", SUITS[1])]) self.assertEqual(get_cards_type(cards), -1) cards = Stack(cards=[Card("3", SUITS[0]), Card("4", SUITS[0]), Card("5", SUITS[0]), Card("6", SUITS[0]), Card("7", SUITS[0]), Card("8", SUITS[0])]) self.assertEqual(get_cards_type(cards), -1)
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 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_two_three_of_a_kind_two_wild(self): """Test two wild three of a kinds with deuces.""" self.game.hand.add(wild_three_of_a_kind(3)) self.game.hand.add(wild_three_of_a_kind(4)) self.assertTrue( VictoryConditions.two_three_of_a_kind(self.game.hand.cards, self.game.victory_cards)) self.game.go_down() self.assertEqual(Stack(), self.game.hand) self.assertEqual( Stack(cards=deque([ Card(value='2', suit='Clubs'), Card(value='3', suit='Diamonds'), Card(value='3', suit='Hearts'), Card(value='2', suit='Clubs'), Card(value='4', suit='Diamonds'), Card(value='4', suit='Hearts') ])), self.game.down_cards)
def is_flush(self, sorted_cards): splits = {s: Stack() for s in self.suits} for i, card in enumerate(sorted_cards): splits[card.suit].add(card) for suit in splits: if len(splits[suit]) >= 5: sorted_flush = self.sort_descending(splits[suit]) return True, sorted_flush[0:5] return False, None
def is_straight_flush(self, sorted_cards): splits = {s: Stack() for s in self.suits} for i, card in enumerate(sorted_cards): splits[card.suit].add(card) for suit in splits: if len(splits[suit]) >= 5: sorted_flush = self.sort_descending(splits[suit]) is_straight, straight_cards = self.is_straight(sorted_flush) return is_straight, straight_cards return False, None
def test_four_of_a_kind(self): cards = Stack(cards=[ Card("3", "Diamonds"), Card("3", "Clubs"), Card("3", "Hearts"), Card("3", "Spades"), Card("4", "Diamonds") ]) self.assertEqual(get_money_lost(cards, 5, 20), 50) cards = Stack(cards=[ Card("3", "Diamonds"), Card("3", "Clubs"), Card("3", "Hearts"), Card("3", "Spades"), Card("4", "Diamonds"), Card("5", "Spades") ]) self.assertEqual(get_money_lost(cards, 5, 20), 60)
def test_straight_flush(self): cards = Stack(cards=[ Card("3", "Diamonds"), Card("4", "Diamonds"), Card("5", "Diamonds"), Card("6", "Diamonds"), Card("7", "Diamonds") ]) self.assertEqual(get_money_lost(cards, 5, 20), 50) cards = Stack(cards=[ Card("3", "Diamonds"), Card("4", "Diamonds"), Card("5", "Diamonds"), Card("6", "Diamonds"), Card("7", "Diamonds"), Card("7", "Clubs") ]) self.assertEqual(get_money_lost(cards, 5, 20), 60)
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') ]))
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_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 test_check_for_blackjack(self): card_1 = Card('Ace', 'Hearts') card_2 = Card('King', 'Spades') temp_hand = [card_1, card_2] test_hand_1 = Stack(cards=temp_hand) card_3 = Card('Ace', 'Hearts') card_4 = Card('Ace', 'Spades') temp_hand = [card_3, card_4] test_hand_2 = Stack(cards=temp_hand) card_5 = Card('10', 'Diamonds') card_6 = Card('Ace', 'Clubs') temp_hand = [card_5, card_6] test_hand_3 = Stack(cards=temp_hand) card_7 = Card('6', 'Hearts') card_8 = Card('5', 'Spades') temp_hand = [card_7, card_8] test_hand_4 = Stack(cards=temp_hand) self.assertTrue(check_for_blackjack(test_hand_1)) self.assertFalse(check_for_blackjack(test_hand_2)) self.assertTrue(check_for_blackjack(test_hand_3)) self.assertFalse(check_for_blackjack(test_hand_4))
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_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))
class Dealer(object): def __init__(self): self.deck = Deck() self.hand = Stack() self.deck.build() self.deck.shuffle() self.cut_num = random.randint(12, 42) def get_deck(self): return self.deck def get_hand(self): return self.hand def initial_deal(self, player): """Deal two cards to the player and the dealer.""" cards = self.deck.deal(2) player.add_cards(cards) self.hand += self.deck.deal(2) def deal(self): """Get one card from the deck""" return self.deck.deal(1) def deal_dealer(self): """Deal one card to the dealer's hand""" self.hand += self.deck.deal(1) def empty_hand(self): """Remove all cards from the dealer's hand""" self.hand.empty() def reshuffle(self): """Reset the decks so that there is one deck of 104 shuffled cards""" new_deck = Deck() new_deck.build() new_deck.shuffle() self.deck = new_deck self.cut_num = random.randint(12, 42)
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_auto_select_down_cards_two_threes(self): # Round 1: 2 x 3 of a kind threes = [ Card('3', 'clubs'), Card('3', 'diamonds'), Card('3', 'hearts') ] self.game.hand.add(threes) fours = [ Card('4', 'clubs'), Card('4', 'diamonds'), Card('4', 'hearts') ] self.game.hand.add(fours) self.assertEqual(Stack(), self.game.down_cards) self.assertEqual(self.threes_and_fours_hand, self.game.hand) VictoryConditions.two_three_of_a_kind(self.game.hand.cards, self.game.victory_cards) auto_select_down_cards(self.game.hand, self.game.victory_cards, self.game.down_cards) self.assertEqual(self.threes_and_fours_hand, self.game.down_cards) self.assertEqual(Stack(), self.game.hand)