def score(self) -> int: """ Get a score of hand. :return: total_score """ aces = [ Card("ACE", "SPADES", "AS"), Card("ACE", "HEARTS", "AH"), Card("ACE", "CLUBS", "AC"), Card("ACE", "DIAMONDS", "AD") ] total_score = 0 # Go through all cards that are not aces, add them together. for card in self.cards: if card not in aces: total_score += card.get_card_value # Go through all aces, and if the ace value makes the total over 21, the value is 1. for card in self.cards: if card in aces: if total_score + 11 > 21: total_score += 1 else: # There is an ace that has a value of 11 because it didn't make the total go over 21. self.is_soft = True total_score += 11 return total_score
def test_ace_ranks_1_when_hand_getting_bust(self): hand = Hand("") hand.add(Card("diamond", 5)) hand.add(Card("diamond", 1)) hand.add(Card("clubs", 1)) hand.add(Card("hearts", 1)) self.assertEqual(hand.sum(), 21)
def test_draws_the_top_card(self): cards = [Card("diamond", 2), Card("diamond", 3)] deck = Deck(cards) card = deck.draw() self.assertEqual(card.rank, 3) self.assertEqual(deck.count(), 1)
def test_init(self): c = Card("♥", 9) assert c.suit == deck.Suit.Hearts assert c.value == 9 c = Card("spades", "ace") assert c.suit == deck.Suit.Spades assert c.value == 1
def test_eq(self): c1 = Card("Diamonds", "King") c2 = Card("♦", 13) assert c1 == c2 c1 = Card(joker=True) c2.joker = True assert c1 == c2
def test_failed_init(self): '''Init fails is value or suit are invalid''' with self.assertRaises(ValueError): Card('11', 'Clubs') with self.assertRaises(ValueError): Card('4', 'Trees')
def test_illegal(self): """ Make illegal cards """ with self.assertRaises(AssertionError): Card(0, Suites.clubs) with self.assertRaises(AssertionError): Card(14, Suites.diamonds) with self.assertRaises(AssertionError): Card(1, {'FakeSuite', '!', 'orange'})
def isMarriage(self, card, player): if card.rank == deck.RANK_OBER and Card( card.suit, deck.RANK_KING) in player.hand.cards: return True if card.rank == deck.RANK_KING and Card( card.suit, deck.RANK_OBER) in player.hand.cards: return True return False
def test_card_gt(self): """Cards with higher rank are greater than cards with lower rank.""" rank_list = list(Rank) for i in range(1, len(rank_list)): with self.subTest("Rank", i=i): self.assertGreater(Card(rank_list[i], Suit(1)), Card(rank_list[i - 1], Suit(1)))
def test_card(self): """An instance of card is correctly created.""" c1 = Card('6', 'Hearts') self.assertEqual(c1.value, '6') self.assertEqual(c1.suit, 'Hearts') c2 = Card('Q', 'Diamonds') self.assertEqual(str(c2), 'Q of Diamonds')
def test_TableGotOneCardFromPlayer(self): self.game.table = [] self.game.player1.stack_of_cards = [Card(9, "swords")] self.game.playCard() self.assertEqual(self.game.table[0], Card(9, "swords")) self.assertEqual(len(self.game.player1.stack_of_cards), 0)
def test_eq(self): c1 = Card("Diamonds", "King") c2 = Card("♦", 13) assert c1 == c2 c1 = Card(joker=True) c2.joker = True assert c1 == c2 assert c1.value == deck.Value.Joker
def test_all_piles_same_row_not_game_over(self): state = GameState() card_piles = [[[Card("A", "s")], [Card("2", "s")], [Card("3", "s")]], [[], [], []], [[], [], []]] state.start_new_game(lucky_card=Card("4", "s"), card_piles=card_piles) state.discards_remaining = 1 assert len(state.actions()) == 3 assert not state.is_game_over()
def test_remove_deck(self): test_collection = Collection(TEST_COLLECTION_NAME) self.assertEqual(test_collection.size(), 0) main_deck = [] sideboard = [] CARDS_IN_MAIN_DECK = 100 CARDS_IN_SIDEBOARD = 50 ARBITRARY_NUMBER = 1000 deck_size = 0 collection_size = 0 for x in range(0, CARDS_IN_MAIN_DECK): quantity = random.randint(1, 2) card = Card("card" + str(x), quantity) main_deck.append(card) deck_size += quantity for x in range(CARDS_IN_MAIN_DECK // 2, CARDS_IN_SIDEBOARD + CARDS_IN_MAIN_DECK // 2): quantity = random.randint(1, 2) card = Card("card" + str(x), quantity) sideboard.append(card) deck_size += quantity test_deck = Deck(main_deck, sideboard) #assert cant remove from empty collection self.assertEqual(test_collection.remove_deck(test_deck), None) for x in range(0, CARDS_IN_MAIN_DECK // 2 + CARDS_IN_SIDEBOARD): quantity = random.randint(ARBITRARY_NUMBER, ARBITRARY_NUMBER) card = Card("card" + str(x), quantity) test_collection.add_card(card) collection_size += quantity self.assertEqual(test_collection.size(), collection_size) test_collection.remove_deck(test_deck) self.assertEqual(test_collection.size(), collection_size - deck_size) collection_size = test_collection.size() flag_removed_success = True max_loops = ARBITRARY_NUMBER // 2 #to break loop incase future implementation breaks something loop_num = 0 while (flag_removed_success and loop_num < max_loops): loop_num += 1 removed_or_not = test_collection.remove_deck(test_deck) if removed_or_not is None: flag_removed_success = False if flag_removed_success: collection_size -= deck_size self.assertEqual(test_collection.size(), collection_size) else: self.assertEqual(test_collection.size(), collection_size)
def test_high_card_ace(self): cards = [ Card("Spades", 2), Card("Clubs", 4), Card("Hearts", 5), Card("Diamonds", 7), Card("Spades", "Ace"), ] hand = deck.get_poker_hand(cards) assert hand == (deck.PokerHand.HighCard, 14, 7, 5, 4, 2)
def test_pair(self): cards = [ Card("Spades", 2), Card("Clubs", 4), Card("Hearts", 5), Card("Diamonds", 9), Card("Spades", 9), ] hand = deck.get_poker_hand(cards) assert hand == (deck.PokerHand.Pair, 9, 5)
def test_playCardTwice_TableGotTwoCardsFromDifferentPlayers(self): self.game.table = [] self.game.player1.stack_of_cards = [Card(9, "swords")] self.game.player2.stack_of_cards = [Card(9, "clubs")] self.game.playCard() self.game.playCard() self.assertEqual(self.game.table[0], Card(9, "swords")) self.assertEqual(self.game.table[1], Card(9, "clubs"))
def test_straight_flush_with_low_ace(self): cards = [ Card("Clubs", 1), Card("Clubs", 2), Card("Clubs", 3), Card("Clubs", 4), Card("Clubs", 5), ] hand = deck.get_poker_hand(cards) assert hand == (deck.PokerHand.StraightFlush, 5)
def test_fours(self): cards = [ Card("Spades", 2), Card("Clubs", 9), Card("Hearts", 9), Card("Diamonds", 9), Card("Spades", 9), ] hand = deck.get_poker_hand(cards) assert hand == (deck.PokerHand.FourOfAKind, 9)
def test_straight_flush(self): cards = [ Card("Clubs", 5), Card("Clubs", 6), Card("Clubs", 7), Card("Clubs", 8), Card("Clubs", 9), ] hand = deck.get_poker_hand(cards) assert hand == (deck.PokerHand.StraightFlush, 9)
def test_flush(self): cards = [ Card("Clubs", 2), Card("Clubs", 4), Card("Clubs", 5), Card("Clubs", 7), Card("Clubs", 9), ] hand = deck.get_poker_hand(cards) assert hand == (deck.PokerHand.Flush, 9)
def test_full_house(self): cards = [ Card("Spades", 4), Card("Clubs", 4), Card("Hearts", 9), Card("Diamonds", 9), Card("Spades", 9), ] hand = deck.get_poker_hand(cards) assert hand == (deck.PokerHand.FullHouse, 9)
def test_straight_with_low_ace(self): cards = [ Card("Spades", 1), Card("Spades", 2), Card("Clubs", 3), Card("Hearts", 4), Card("Diamonds", 5), ] hand = deck.get_poker_hand(cards) assert hand == (deck.PokerHand.Straight, 5)
def test_triples(self): cards = [ Card("Spades", 2), Card("Clubs", 4), Card("Hearts", 9), Card("Diamonds", 9), Card("Spades", 9), ] hand = deck.get_poker_hand(cards) assert hand == (deck.PokerHand.ThreeOfAKind, 9, 4)
def test_quad_hands(self): state = GameState() card_piles = [[[Card("K", "h")], [Card("K", "d")], [Card("K", "s")]], [[Card("3", "c")], [Card("A", "s")], [Card("A", "d")]], [[Card("3", "s")], [Card("A", "c")], [Card("K", "c")]]] state.start_new_game(lucky_card=Card("7", "h"), card_piles=card_piles) quad_hands = state._get_quad_hands() assert len(quad_hands) == 1 assert set([(0, 0), (0, 1), (0, 2), (2, 2)]) in quad_hands
def test_gratest_on_table(self): seven_diamonds = Card('Diamonds', '7', 7) ten_diamonds = Card('Diamonds', '10', 10) two_spades = Card('Spades', '2', 2) jack_hearts = Card('Hearts', 'J', 12) cards_on_table = [ seven_diamonds, ten_diamonds, two_spades, jack_hearts ] trump = 'Diamonds' greatest = self.game.greatest_on_table(cards_on_table, trump) self.assertEqual(greatest, ten_diamonds)
def test_computer_always_tries_to_get_higher_total_than_player(self): player_hand = Hand("") player_hand.add(Card("spades", 13)) player_hand.add(Card("clubs", 7)) cards = [Card("hearts", 3), Card("hearts", 6), Card("hearts", 13)] deck = Deck(cards) computer_hand = Hand("") Computer().play(computer_hand, deck, player_hand) self.assertTrue(computer_hand.sum() > 21)
def test_computer_stops_at_21(self): player_hand = Hand("") player_hand.add(Card("spades", 13)) player_hand.add(Card("clubs", 8)) cards = [Card("hearts", 1), Card("hearts", 7), Card("hearts", 13)] pile = Deck(cards) computer_hand = Hand("") Computer().play(computer_hand, pile, player_hand) self.assertEqual(computer_hand.sum(), 21)
def test_card_init(self): for value in range(14): for suit in ['spade', 'club', 'heart', 'diamond']: card = Card(value, suit, True) card.flip() card = Card(value, suit, False) card.flip() card.show() card = Card(14, 'none', True) card.show() card = Card(15, 'none', True) card.show()
def test_intersect_exact(self): hand1, hand2 = self.HAND1, self.HAND2 # Self-intersect should be the complete set assert set(hand1.intersect(hand1, deck.HandComparison.Exact)) == set(hand1) assert set(hand2.intersect(hand2, deck.HandComparison.Exact)) == set(hand2) # Only exact matches should remain after an intersect assert set(hand1.intersect(hand2, deck.HandComparison.Exact)) == { Card(joker=True), Card("♠", 11), }