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_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 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_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_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_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_score_meld_15(self): # Given a meld that adds up to 15 gs = GameState() meld = (Card.from_string("5♦"), Card.from_string("J♣")) # When the meld is scored score = gs.score_meld(meld) # The returned score is 2 self.assertEqual(score, 2)
def test_score_meld_nothing(self): # Given a meld that has no value gs = GameState() meld = (Card.from_string("4♦"), Card.from_string("J♣")) # When the meld is scored score = gs.score_meld(meld) # The returned score is 0 self.assertEqual(score, 0)
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 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_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_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 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_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_score_meld_straight(self): # Given a meld that has a straight of three cards of different suits gs = GameState() meld = (Card.from_string("3♦"), Card.from_string("2♣"), Card.from_string("4♦")) # When the meld is scored score = gs.score_meld(meld) # The returned score is 3 self.assertEqual(score, 3)
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_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_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_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_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_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_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_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_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_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_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_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_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 get_score(self): score = 0 for card in self._hand: best_score = None for i in xrange(card.variations): card_score = Card.process_card(card, i) if not best_score or (best_score < score + card_score <= 21): best_score = score + card_score score = best_score return score
def test_is_playable_negative_denomination_tests(self): last_card_played = Card(100, Power.Rescue, None) next_card = Card(10, Power.Go, None) self.assertFalse(next_card.is_playable(last_card_played)) last_card_played = Card(10000, Power.Clone, None) next_card = Card(10000, Power.Rescue, None) self.assertFalse(next_card.is_playable(last_card_played)) last_card_played = Card(100000, Power.Clone, None) next_card = Card(100000, Power.Rescue, None) self.assertFalse(next_card.is_playable(last_card_played)) last_card_played = Card(1, Power.Discard, None) next_card = Card(1, Power.Go, None) self.assertFalse(next_card.is_playable(last_card_played))
def test_is_playable_negative_clone_tests(self): last_card_played = Card(100000, Power.Discard, None) next_card = Card(10, Power.Clone, None) self.assertFalse(next_card.is_playable(last_card_played)) last_card_played = Card(100000, Power.Discard, None) next_card = Card(100, Power.Clone, None) self.assertFalse(next_card.is_playable(last_card_played)) last_card_played = Card(100000, Power.Discard, None) next_card = Card(1000, Power.Clone, None) self.assertFalse(next_card.is_playable(last_card_played)) last_card_played = Card(100000, Power.Discard, None) next_card = Card(10000, Power.Clone, None) self.assertFalse(next_card.is_playable(last_card_played))
def test_is_actionable_card(self): card = Card(100, Power.Go, None) self.assertTrue(card.is_actionable_power()) card = Card(1000, Power.Rescue, None) self.assertTrue(card.is_actionable_power())
def test_card_copy(self): c = Card(1, Power.Poison, "John") ccopy = c.get_copy() self.assertEqual(ccopy.denomination, 1) self.assertEqual(ccopy.power, Power.Poison) self.assertEqual(ccopy.owner, "John")
def test_is_playable_next_denom_again(self): last_card_played = Card(100, Power.Rescue, None) next_card = Card(1000, Power.Rescue, None) self.assertTrue(next_card.is_playable(last_card_played))
def test_is_playable_one_clone_after_big_money(self): last_card_played = Card(100000, Power.Discard, None) next_card = Card(1, Power.Clone, None) self.assertTrue(next_card.is_playable(last_card_played))
def test_negative_is_actionable_card(self): card = Card(1, Power.Clone, None) self.assertFalse(card.is_actionable_power()) card = Card(1000, Power.Skip, None) self.assertFalse(card.is_actionable_power())