def test_normal_hand(self): """Test generate summary for normal hand.""" hand = Hand() hand.cards = [Card('Queen', 'Spades'), Card('7', 'Clubs')] summary = hand.generate_hand_summary() self.assertEqual(summary, '[Queen of Spades, 7 of Clubs] for a total of 17')
def test_addAce(self): """A hand should the highest score when an ace is added""" h = Hand() h.add(Card(CardValues.QUEEN, Suits.DIAMONDS)) assert(h.score == 10) h.add(Card(CardValues.ACE, Suits.SPADES)) assert(h.score == 21)
def test_one_big_ace_hand(self): """Test generate summary for hand with one ace.""" hand = Hand() hand.cards = [Card('Ace', 'Spades'), Card('7', 'Clubs')] summary = hand.generate_hand_summary() self.assertEqual(summary, '[Ace of Spades, 7 of Clubs] for a total of 18 (8)')
def test_no_ace_alt_score(self): """Test generate summary for hand with one ace.""" hand = Hand() hand.cards = [Card('Ace', 'Spades'), Card('7', 'Clubs')] summary = hand.generate_hand_summary(show_ace_alt_score=False) self.assertEqual(summary, '[Ace of Spades, 7 of Clubs] for a total of 18')
def test_determine_winners(self): """ Test that the dealer hits at the appropriate times and that winning hands are appropriated updated as such. """ # setup deck deck = Deck() deck.create() deck.shuffle() # setup gambler, dealer & hands dealer = Dealer() dealer_hand = Hand() dealer.hands.append(dealer_hand) gambler = Gambler("Test") gambler_hand = Hand() gambler.hands.append(gambler_hand) players = [gambler, dealer] # must hit a soft 17 (A, 6) gambler_hand.final_value = 20 dealer_hand.cards.append(Card('Hearts', 'Ace', 1)) dealer_hand.cards.append(Card('Hearts', 'Six', 6)) bj.determine_winners(players, deck) self.assertTrue(len(dealer_hand.cards) > 2) # must hit on 16 or less (K, 6) self.reset_hand_attrs(dealer_hand) dealer_hand.cards.append(Card('Hearts', 'King', 10)) dealer_hand.cards.append(Card('Hearts', 'Six', 6)) bj.determine_winners(players, deck) self.assertTrue(len(dealer_hand.cards) > 2) # check dealer bust (K, 6, J) dealer_hand.cards.pop() dealer_hand.cards.append(Card('Hearts', 'Jack', 10)) bj.determine_winners(players, deck) self.assertTrue(dealer_hand.busted) self.assertTrue(gambler_hand.win) # check dealer stands on 17 (K, 7) self.reset_hand_attrs(dealer_hand) dealer_hand.cards.append(Card('Hearts', 'King', 10)) dealer_hand.cards.append(Card('Hearts', 'Seven', 7)) bj.determine_winners(players, deck) self.assertTrue(len(dealer_hand.cards) == 2) # gambler wins with 20 to dealer's 17 self.assertTrue(gambler_hand.win) # check dealer stands on soft 18 (Ace, 7) self.reset_hand_attrs(dealer_hand) dealer_hand.cards.append(Card('Hearts', 'Ace', 1)) dealer_hand.cards.append(Card('Hearts', 'Seven', 7)) bj.determine_winners(players, deck) self.assertTrue(len(dealer_hand.cards) == 2)
class hand_strTest(unittest.TestCase): def setUp(self): """Call before every test case.""" self.hand = Hand() self.hand.add_card(Card("C","A")) self.hand.add_card(Card("S","2")) def testStr(self): assert self.hand.__str__() == str(['CA', 'S2']), 'Hand.__str__() does not provide the right return value'
def test_hand_value(): deck = Deck() deck.shuffle() hand = Hand(deck) assert isinstance(hand.cards[0], Card) assert isinstance(hand.cards[1], Card) hand.value = 20 hand.deck.deal_card = MagicMock(return_value=Card('spade', 'A')) hand.get_card() assert hand.value == 21
def deal_initial_hands(deck): """Add two cards to each hand and return the hand objects.""" dealer_hand = Hand() hit(deck, dealer_hand) hit(deck, dealer_hand) player_hand = Hand() hit(deck, player_hand) hit(deck, player_hand) return dealer_hand, player_hand
def test_deal_card(self): """ Test that dealing a card removes 1 card from the deck and adds 1 card to the hand. """ self.deck = Deck(num_decks=1) self.deck.create() self.deck.shuffle() self.hand = Hand() self.hand.deal_card(self.deck) self.assertEqual(len(self.deck.cards), 51) self.assertEqual(len(self.hand.cards), 1)
def deal(): global player_balance global player_hand global dealer_hand player_hand = Hand() dealer_hand = Hand() current_bet = 10 player_hand.bet(current_bet) player_balance -= current_bet player_hand.cards.append(deck.cards.pop()) dealer_hand.cards.append(deck.cards.pop()) player_hand.cards.append(deck.cards.pop()) dealer_hand.cards.append(deck.cards.pop())
class TestHand(unittest.TestCase): def test_deal_card(self): """ Test that dealing a card removes 1 card from the deck and adds 1 card to the hand. """ self.deck = Deck(num_decks=1) self.deck.create() self.deck.shuffle() self.hand = Hand() self.hand.deal_card(self.deck) self.assertEqual(len(self.deck.cards), 51) self.assertEqual(len(self.hand.cards), 1) def test_check_busted(self): """ Check to see if a hand is a bust (> 21) or not. """ # hand is busted self.hand = Hand() self.hand.cards.append(Card('Hearts', 'King', 10)) self.hand.cards.append(Card('Hearts', 'Three', 3)) self.hand.cards.append(Card('Hearts', 'Jack', 10)) self.hand.check_busted() self.assertTrue(self.hand.busted) # hand is not busted self.hand = Hand() self.hand.cards.append(Card('Hearts', 'King', 10)) self.hand.cards.append(Card('Hearts', 'Three', 3)) self.hand.cards.append(Card('Hearts', 'Six', 6)) self.hand.check_busted() self.assertFalse(self.hand.busted) def test_get_hand_value(self): """ Make sure get_hand_value() returns accurate hand values. """ # test hand values with Aces self.hand = Hand() self.hand.cards.append(Card('Hearts', 'Ace', 1)) self.hand.cards.append(Card('Spades', 'Ace', 1)) self.hand.cards.append(Card('Hearts', 'Six', 6)) hand_values = self.hand.get_hand_value() self.assertEqual(hand_values, [8, 18]) # test hand values with hidden card (Dealer) self.hand.cards[2].hidden = True hand_values = self.hand.get_hand_value() self.assertEqual(hand_values, [2, 12]) # test hand values with hidden card included (Dealer) hand_values = self.hand.get_hand_value(include_hidden=True) self.assertEqual(hand_values, [8, 18])
class hand_addCardTest(unittest.TestCase): def setUp(self): """Call before every test case.""" self.hand = Hand() def testAddCard(self): assert self.hand.add_card(Card("C","A")) == dummy(self), 'Hand.add_card() does not provide the right return value'
class hand_getValueTest(unittest.TestCase): def setUp(self): """Call before every test case.""" self.hand = Hand() def testAddCard(self): assert self.hand.get_value() == dummy(self), 'Hand.get_value() does not provide the right return value'
def test_downgrade_ace_on_bust(self): hand = Hand() hand.cards = [Card('Ace', 'Spades'), Card('5', 'Hearts')] hand.hit(Card('Jack', 'Diamonds')) is_hand_busted = hand.check_for_bust() self.assertFalse(is_hand_busted) self.assertEqual(hand.calculate_total(), 16)
def test_dealer_final_hands(): deck = Deck() deck.set_cards( [ Cards.SIX, Cards.FOUR, Cards.ACE, Cards.THREE, Cards.TEN, Cards.TWO, Cards.TWO, Cards.FOUR, Cards.TWO, Cards.TWO, Cards.THREE, Cards.TEN, Cards.FIVE, Cards.TEN, ] ) assert resolve_dealer_action(Hand([Cards.ACE, Cards.JACK]), deck) == ( DealerResultTypes.BLACKJACK, 21, ) assert resolve_dealer_action(Hand([Cards.ACE, Cards.SIX]), deck) == ( DealerResultTypes.LIVE, 17, ) assert resolve_dealer_action(Hand([Cards.TEN, Cards.SIX]), deck) == ( DealerResultTypes.LIVE, 21, ) assert resolve_dealer_action(Hand([Cards.TEN, Cards.TWO]), deck) == ( DealerResultTypes.BUST, 22, ) assert resolve_dealer_action(Hand([Cards.FIVE, Cards.THREE]), deck) == ( DealerResultTypes.LIVE, 19, ) assert resolve_dealer_action(Hand([Cards.ACE, Cards.ACE]), deck) == ( DealerResultTypes.LIVE, 19, ) assert resolve_dealer_action(Hand([Cards.TWO, Cards.FOUR]), deck) == ( DealerResultTypes.LIVE, 21, ) assert resolve_dealer_action(Hand([Cards.TEN, Cards.SIX]), deck) == ( DealerResultTypes.BUST, 22, )
def test_addAce2(self): """A hand should pick the highest score when 2 aces are added unless bust""" h = Hand() h.add(Card(CardValues.QUEEN, Suits.DIAMONDS)) assert(h.score == 10) h.add(Card(CardValues.ACE, Suits.SPADES)) assert(h.score == 21) h.add(Card(CardValues.ACE, Suits.SPADES)) assert(h.score == 12)
def test_addAce2_1(self): """A hand should the highest score when 2 aces are added""" h = Hand() h.add(Card(CardValues.TWO, Suits.DIAMONDS)) assert(h.score == 2) h.add(Card(CardValues.ACE, Suits.SPADES)) assert(h.score == 13) h.add(Card(CardValues.ACE, Suits.SPADES)) assert(h.score == 14)
def test_check_busted(self): """ Check to see if a hand is a bust (> 21) or not. """ # hand is busted self.hand = Hand() self.hand.cards.append(Card('Hearts', 'King', 10)) self.hand.cards.append(Card('Hearts', 'Three', 3)) self.hand.cards.append(Card('Hearts', 'Jack', 10)) self.hand.check_busted() self.assertTrue(self.hand.busted) # hand is not busted self.hand = Hand() self.hand.cards.append(Card('Hearts', 'King', 10)) self.hand.cards.append(Card('Hearts', 'Three', 3)) self.hand.cards.append(Card('Hearts', 'Six', 6)) self.hand.check_busted() self.assertFalse(self.hand.busted)
def test_get_hand_value(self): """ Make sure get_hand_value() returns accurate hand values. """ # test hand values with Aces self.hand = Hand() self.hand.cards.append(Card('Hearts', 'Ace', 1)) self.hand.cards.append(Card('Spades', 'Ace', 1)) self.hand.cards.append(Card('Hearts', 'Six', 6)) hand_values = self.hand.get_hand_value() self.assertEqual(hand_values, [8, 18]) # test hand values with hidden card (Dealer) self.hand.cards[2].hidden = True hand_values = self.hand.get_hand_value() self.assertEqual(hand_values, [2, 12]) # test hand values with hidden card included (Dealer) hand_values = self.hand.get_hand_value(include_hidden=True) self.assertEqual(hand_values, [8, 18])
def test_empty(self): """A hand should reset after using the empty command""" h = Hand() h.add(Card(CardValues.QUEEN, Suits.DIAMONDS)) assert(h.score == 10) h.add(Card(CardValues.ACE, Suits.SPADES)) assert(h.score == 21) h.empty() assert(h.score == 0) assert(len(h.cards) == 0) assert(h.containsAce == False)
class hand_drawTest(unittest.TestCase): def setUp(self): """Call before every test case.""" self.hand = Hand() def tearDown(self): """Call after every test case.""" def testDrawInit(self): testCanvas = Canvas() p = [1,1] assert self.hand.draw(testCanvas, p) == dummy(self), 'Hand.draw() does not provide the right return value'
def test_two_ace_hand(self): """Test generate summary for hand with one ace.""" hand = Hand() hand.cards = [Card('Ace', 'Spades'), Card('Ace', 'Clubs')] hand.check_for_bust() # to downgrade one Ace to value=1 summary = hand.generate_hand_summary() self.assertEqual( summary, '[Ace of Spades, Ace of Clubs] for a total of 12 (2)')
def command_line_interface(): """Defines the CLI (Command Line Interface) of the Game""" deck = Deck() playerHand = Hand() dealerHand = Hand(dealer=True) playing = True num_cards_to_draw = 2 try: while playing: for _ in range(num_cards_to_draw): playerHand.addCard(deck.deal()) dealerHand.addCard(deck.deal()) num_cards_to_draw = 1 # after 1st iter, draw single card each time "stick" print( f"Player Cards [{playerHand.getValue}] :\n{playerHand.showHand()}" ) print( f"Dealer Cards [{dealerHand.getValue}] :\n{dealerHand.showHand()}" ) # check if black jack obtained for p, msg in zip([playerHand, dealerHand], ["Player", "Dealer"]): if p.is_blackjack: print(f"{msg} WINS!") playing = False raise GameComplete wager = input( "Choose [Hit/Stick] : ").lower() # should draw more cards? while wager not in ["h", "s", "hit", "stick"]: wager = input( "Please Enter 'Hit' or 'Stick' (or H/S) : ").lower() if wager not in ["h", "hit"]: playing = False # find the winning player print(get_winning_player(playerHand.getValue, dealerHand.getValue)) except GameComplete: pass
def setUp(self): """Call before every test case.""" self.hand = Hand() self.hand.add_card(Card("C","A")) self.hand.add_card(Card("S","2"))
def test_addAce4(self): """A hand should the highest score when 4 aces are added""" h = Hand() h.add(Card(CardValues.NINE, Suits.SPADES)) assert(h.score == 9) h.add(Card(CardValues.EIGHT, Suits.HEARTS)) assert(h.score == 17) h.add(Card(CardValues.ACE, Suits.SPADES)) assert(h.score == 18) h.add(Card(CardValues.ACE, Suits.CLUBS)) assert(h.score == 19) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 20) h.add(Card(CardValues.ACE, Suits.HEARTS)) assert(h.score == 21)
def create_hand_test(self): set1 = Hand() set1.add_card(Card("ace", "spades")) set1.add_card(Card("king", "spades")) self.assertEqual([("ace", "spades"), ("king", "spades")], set1.cards)
def test_addAce22(self): """A hand should bust when 22 aces are added""" h = Hand() h.add(Card(CardValues.ACE, Suits.SPADES)) assert(h.score == 11) h.add(Card(CardValues.ACE, Suits.CLUBS)) assert(h.score == 12) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 13) h.add(Card(CardValues.ACE, Suits.HEARTS)) assert(h.score == 14) h.add(Card(CardValues.ACE, Suits.SPADES)) assert(h.score == 15) h.add(Card(CardValues.ACE, Suits.CLUBS)) assert(h.score == 16) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 17) h.add(Card(CardValues.ACE, Suits.HEARTS)) assert(h.score == 18) h.add(Card(CardValues.ACE, Suits.CLUBS)) assert(h.score == 19) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 20) h.add(Card(CardValues.ACE, Suits.HEARTS)) assert(h.score == 21) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 12) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 13) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 14) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 15) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 16) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 17) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 18) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 19) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 20) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == 21) h.add(Card(CardValues.ACE, Suits.DIAMONDS)) assert(h.score == -1)
def test_new_hand_has_no_cards(): hand = Hand() assert len(hand.cards) == 0
print("Dealer and player tie! PUSH!") ''' GAME LOOP ''' while True: print("WELLCOME TO BLACKJACK") #create and shuffle the deck, deal two cards to each player deck = Deck() deck.shuffle() player_hand = Hand() player_hand.add_card(deck.deal()) player_hand.add_card(deck.deal()) dealer_hand = Hand() dealer_hand.add_card(deck.deal()) dealer_hand.add_card(deck.deal()) # Set up plauer's chips player_chips = Chips() # prompt the player for their bet take_bet(player_chips) # show cards (but keep one dealer card hidden) show_some(player_hand, dealer_hand)
def test_player_final_hands(): # lots of splits, doesn't split more than 4 deck = Deck() player = Player( bankroll=np.inf, hard_policy=hard_policy, soft_policy=soft_policy, split_policy=split_policy, betting_policy=betting_policy, ) player.increment_num_hands() deck.set_cards( [ Cards.TEN, Cards.QUEEN, Cards.KING, Cards.NINE, Cards.EIGHT, Cards.ACE, Cards.TEN, Cards.FOUR, Cards.EIGHT, Cards.NINE, Cards.TWO, Cards.TWO, Cards.TWO, Cards.TWO, Cards.TWO, Cards.TWO, Cards.TWO, ] ) output = resolve_player_action( Hand([Cards.TWO, Cards.TWO]), Cards.SEVEN, player, deck ) assert output == [ (PlayerResultTypes.LIVE, 21), (PlayerResultTypes.DOUBLE, 14), (PlayerResultTypes.LIVE, 21), (PlayerResultTypes.DOUBLE, 21), ] player.next_round() player.increment_num_hands() deck.set_cards( [ Cards.FIVE, Cards.FIVE, Cards.TWO, Cards.KING, Cards.ACE, Cards.ACE, Cards.ACE, Cards.ACE, ] ) output = resolve_player_action( Hand([Cards.ACE, Cards.ACE]), Cards.SEVEN, player, deck ) assert output == [ (PlayerResultTypes.LIVE, 12), (PlayerResultTypes.LIVE, 12), (PlayerResultTypes.LIVE, 21), (PlayerResultTypes.LIVE, 13), ] player.next_round() player.increment_num_hands() output = resolve_player_action( Hand([Cards.ACE, Cards.JACK]), Cards.SEVEN, player, deck ) assert output == [(PlayerResultTypes.BLACKJACK, 21)] player.next_round() player.increment_num_hands() deck.set_cards( [ Cards.FIVE, Cards.FIVE, Cards.TWO, Cards.KING, Cards.ACE, Cards.ACE, Cards.ACE, Cards.ACE, ] ) output = resolve_player_action( Hand([Cards.ACE, Cards.SIX]), Cards.SIX, player, deck ) assert output == [(PlayerResultTypes.DOUBLE, 18)] player.next_round() player.increment_num_hands() output = resolve_player_action( Hand([Cards.JACK, Cards.SIX]), Cards.ACE, player, deck ) assert output == [(PlayerResultTypes.SURRENDER, 16)] player.next_round() player.increment_num_hands() output = resolve_player_action( Hand([Cards.JACK, Cards.SEVEN]), Cards.ACE, player, deck ) assert output == [(PlayerResultTypes.SURRENDER, 17)] player.next_round() player.increment_num_hands() deck.set_cards([Cards.TEN, Cards.THREE]) output = resolve_player_action( Hand([Cards.ACE, Cards.TWO]), Cards.TWO, player, deck ) assert output == [(PlayerResultTypes.LIVE, 16)] player.next_round() player.increment_num_hands() deck.set_cards([Cards.SIX, Cards.TEN, Cards.THREE]) output = resolve_player_action( Hand([Cards.ACE, Cards.TWO]), Cards.SEVEN, player, deck ) assert output == [(PlayerResultTypes.BUST, 22)] player.next_round() player.increment_num_hands() deck.set_cards([Cards.THREE]) output = resolve_player_action( Hand([Cards.ACE, Cards.SEVEN]), Cards.TWO, player, deck ) assert output == [(PlayerResultTypes.DOUBLE, 21)] player.next_round() player.increment_num_hands() deck.set_cards([Cards.FIVE]) output = resolve_player_action( Hand([Cards.ACE, Cards.SEVEN]), Cards.TWO, player, deck ) assert output == [(PlayerResultTypes.DOUBLE, 13)] player.next_round() player.increment_num_hands() deck.set_cards([Cards.FIVE]) output = resolve_player_action( Hand([Cards.ACE, Cards.EIGHT]), Cards.TWO, player, deck ) assert output == [(PlayerResultTypes.LIVE, 19)] player.next_round() player.increment_num_hands() deck.set_cards([Cards.FIVE]) output = resolve_player_action( Hand([Cards.NINE, Cards.NINE]), Cards.SEVEN, player, deck ) assert output == [(PlayerResultTypes.LIVE, 18)] player.next_round() player.increment_num_hands() deck.set_cards([Cards.FIVE]) output = resolve_player_action( Hand([Cards.EIGHT, Cards.EIGHT]), Cards.ACE, player, deck ) assert output == [(PlayerResultTypes.SURRENDER, 16)] player.next_round() player.increment_num_hands() deck.set_cards([Cards.FIVE]) output = resolve_player_action( Hand([Cards.TEN, Cards.TEN]), Cards.ACE, player, deck ) assert output == [(PlayerResultTypes.LIVE, 20)]
def test_hand_basic_functions(): deck = Deck(99) hand = Hand([Cards.ACE, Cards.KING]) hand_type, aces, lower_bound, upper_bound = hand.parse_hand() assert hand_type == HandTypes.SOFT assert lower_bound == 11 assert upper_bound == 21 hand.add_card(Cards.TWO) hand_type, aces, lower_bound, upper_bound = hand.parse_hand() assert hand_type == HandTypes.SOFT assert lower_bound == 13 assert upper_bound == 23 hand.delete_card(Cards.ACE) hand_type, aces, lower_bound, upper_bound = hand.parse_hand() assert hand_type == HandTypes.HARD assert lower_bound == 12 assert upper_bound == 12 hand = Hand([Cards.TWO, Cards.TWO]) hand_type, aces, lower_bound, upper_bound = hand.parse_hand() assert hand_type == HandTypes.DUPLICATE assert lower_bound == 4 assert upper_bound == 4 hand.add_card(Cards.TWO) hand_type, aces, lower_bound, upper_bound = hand.parse_hand() assert hand_type == HandTypes.HARD assert lower_bound == 6 assert upper_bound == 6 hand = Hand([Cards.ACE, Cards.ACE]) hand_type, aces, lower_bound, upper_bound = hand.parse_hand() assert hand_type == HandTypes.DUPLICATE assert lower_bound == 2 assert upper_bound == 12
def test_can_add_card_to_hand(): hand = Hand() hand.add(Card(9, "Clubs")) assert len(hand.cards) == 1 assert Card(9, "Clubs") in hand.cards
def game_play(): print('Welcome to Blackjack!') print() playing = True player_chips = Chips() while playing: player = Hand() computer = Hand() print(f'You have {player_chips.balance} chips') amount = bet(player_chips) if amount == 0: return game_deck = Deck() game_deck.shuffle() print() print('PLAYER CARDS:') player.play_one(game_deck.deal_one()) player.play_one(game_deck.deal_one()) print(f'Total hand value: {player.hand_value}') print() print('COMPUTER CARDS:') computer.play_one(game_deck.deal_one()) computer.play_one_hidden(game_deck.deal_one()) print('***Hidden card***') print('Total computer hand value: unknown') print() game_on = True player_turn = True while game_on: if win_check(game_on, player, computer, player_chips, amount) == False: break while player_turn: if win_check(player_turn, player, computer, player_chips, amount) == False: break choice = player_choice() print() if choice == 'hit': player.play_one(game_deck.deal_one()) print(f'Total hand value: {player.hand_value}') else: break game_on = False if player.hand_value < 21: comp_turn = True while comp_turn: if computer.hand_value < 16: print('COMPUTER TURN:') computer.play_one(game_deck.deal_one()) continue else: print(f'Total computer hand value: {computer.hand_value}') comp_win_check(comp_turn, player, computer, player_chips, amount) break if player_chips.balance == 0: playing = False print('Game Ended: You have run out of chips!') break elif play_again() == False: playing = False print('Game Ended: Thank you for playing!') break else: continue pass
def test_hand_to_str(): hand = Hand() hand.add(Card(9, "Clubs")) hand.add(Card(3, "Diamonds")) assert str(hand) == "9 of Clubs, 3 of Diamonds (12)"
def test_can_calculate_score(): hand = Hand() hand.add(Card(9, "Clubs")) hand.add(Card(3, 'Diamonds')) assert hand.get_score() == 12
def test_can_calculate_score_with_aces(): hand = Hand() hand.add(Card(9, "Clubs")) hand.add(Card("A", 'Diamonds')) assert hand.get_score() == 20
def setUp(self): """Call before every test case.""" self.hand = Hand()
def setUp(self): """Setup function.""" self.hand = Hand()
class TestHand(unittest.TestCase): """Test case for hand.""" def setUp(self): """Setup function.""" self.hand = Hand() def test_set_status(self): self.hand.set_status('hit') self.assertEqual(self.hand.status, 'hit') def test_invalid_status(self): with self.assertRaises(Exception) as context: self.hand.set_status('Its Complicated') def test_check_for_blackjack(self): self.hand.cards = [Card('Ace', 'Hearts'), Card('Jack', 'Clubs')] is_blackjack = self.hand.check_for_blackjack() self.assertTrue(is_blackjack) self.assertEqual(self.hand.status, 'blackjack') def test_not_a_blackjack(self): """Test check_for_blackjack() when not a blackjack.""" self.hand.cards = [Card('King', 'Hearts'), Card('Jack', 'Clubs')] is_blackjack = self.hand.check_for_blackjack() self.assertFalse(is_blackjack) self.assertEqual(self.hand.status, None) def test_false_blackjack(self): """Test check_for_black() returns None for a hand with more than 2 cards.""" self.hand.cards = [ Card('10', 'Hearts'), Card('9', 'Clubs'), Card('2', 'Spades') ] is_blackjack = self.hand.check_for_blackjack() self.assertFalse(is_blackjack) self.assertEqual(self.hand.status, None) def test_hit(self): """Test hit.""" self.hand.cards = [Card('5', 'Hearts'), Card('10', 'Clubs')] self.hand.hit(Card('4', 'Spades')) self.assertEqual(len(self.hand.cards), 3) self.assertEqual(self.hand.calculate_total(), 19) def test_calulate_total(self): """Test calculate total.""" self.hand.cards = [ Card('2', 'Hearts'), Card('3', 'Clubs'), Card('6', 'Diamonds'), Card('4', 'Spades'), Card('5', 'Hearts') ] self.assertEqual(self.hand.calculate_total(), 20) def test_check_for_bust_positive(self): """Test check for bust.""" self.hand.cards = [ Card('10', 'Hearts'), Card('4', 'Clubs'), Card('King', 'Spades') ] is_bust = self.hand.check_for_bust() self.assertTrue(is_bust) self.assertEqual(self.hand.status, 'bust') def test_check_for_bust_negative(self): """Test check for bust.""" self.hand.cards = [ Card('10', 'Hearts'), Card('4', 'Clubs'), Card('6', 'Spades') ] is_bust = self.hand.check_for_bust() self.assertFalse(is_bust) self.assertEqual(self.hand.status, None) def test_downgrade_ace_on_bust(self): hand = Hand() hand.cards = [Card('Ace', 'Spades'), Card('5', 'Hearts')] hand.hit(Card('Jack', 'Diamonds')) is_hand_busted = hand.check_for_bust() self.assertFalse(is_hand_busted) self.assertEqual(hand.calculate_total(), 16) def test_set_bet_amt(self): """Test set bet amt.""" self.hand.set_bet_amt(10) self.assertEqual(self.hand.bet_amt, 10)
def test_hand(self): hand = Hand() hand.draw(Card.N_10) self.assertEqual(hand.points, 10) self.assertEqual(hand.has_usabe_ace, False) hand.draw(Card.JACK) self.assertEqual(hand.points, 20) self.assertEqual(hand.has_usabe_ace, False) hand.draw(Card.N_2) self.assertEqual(hand.points, 22) self.assertEqual(hand.has_usabe_ace, False) hand = Hand() hand.draw(Card.QUEEN) hand.draw(Card.KING) hand.draw(Card.ACE) self.assertEqual(hand.points, 21) self.assertEqual(hand.has_usabe_ace, False) hand = Hand() hand.draw(Card.N_2) hand.draw(Card.ACE) self.assertEqual(hand.points, 13) self.assertEqual(hand.has_usabe_ace, True) hand = Hand() hand.draw(Card.N_10) hand.draw(Card.N_2) hand.draw(Card.ACE) self.assertEqual(hand.points, 13) self.assertEqual(hand.has_usabe_ace, False)
def test_add(self): """A hand should calculate the score correctly with every card added""" h = Hand() h.add(Card(CardValues.JACK, Suits.CLUBS)) assert(h.score == 10) h.add(Card(CardValues.THREE, Suits.DIAMONDS)) assert(h.score == 13) h.add(Card(CardValues.TWO, Suits.SPADES)) assert(h.score == 15) h.add(Card(CardValues.SIX, Suits.HEARTS)) assert(h.score == 21) h.add(Card(CardValues.SEVEN, Suits.HEARTS)) assert(h.score == -1)
def test_settle_up(self): """ Test that settle_up() function accurately updates each players .money attribute for various outcomes. """ # setup players dealer = Dealer() player = Gambler('Test') players = [player, dealer] # setup hands dealer_hand = Hand() dealer.hands.append(dealer_hand) hand = Hand() player.hands.append(hand) # check loss player.money = 500 hand.wager = 100 bj.settle_up(players) self.assertEqual(player.money, 400) # check win hand.win = True bj.settle_up(players) self.assertEqual(player.money, 500) # check push hand.win = False hand.push = True bj.settle_up(players) self.assertEqual(player.money, 500) # check insurance w/ dealer blackjack dealer_hand.blackjack = True hand.insurance = True bj.settle_up(players) self.assertEqual(player.money, 550) hand.insurance = False bj.settle_up(players) self.assertEqual(player.money, 550) # check insurance w/o dealer blackjack dealer_hand.blackjack = False hand.insurance = True bj.settle_up(players) self.assertEqual(player.money, 500) hand.insurance = False bj.settle_up(players) self.assertEqual(player.money, 500) # check Blackjack hand.push = False hand.blackjack = True bj.settle_up(players) self.assertEqual(player.money, 650) # check blackjack with fractional dollars hand.wager = 5 bj.settle_up(players) self.assertEqual(player.money, 657.5) # check dealer blackjack dealer_hand.blackjack = True hand.blackjack = False bj.settle_up(players) self.assertEqual(player.money, 652.5)