def test_init(verbose): """Test initializer of blackjack objects. Extra info printed if `verbose` is True. """ tester_name = inspect.stack()[0][3] print("Running " + tester_name + "()") # Small test c1 = c11.Card(0, 12) c2 = c11.Card(1, 10) c3 = c11.Card(2, 9) c4 = c11.Card(0, 1) start_deck = [c1, c2, c3, c4] if verbose: print("testing start deck of " + c11.cardlist_str(start_deck)) game = blackjack.Blackjack(start_deck) ca.assert_equals(set([c1, c2]), set(game.playerHand)) # Sets ignore order ca.assert_equals([c3], game.dealerHand) ca.assert_equals([c4], start_deck) # Check that cards were removed ca.assert_equals(start_deck, game.deck) # Bigger test. start_deck = c11.full_deck() if verbose: print("testing start deck of " + c11.cardlist_str(start_deck)) game = blackjack.Blackjack(start_deck) ca.assert_true( set([c11.Card(0, 1), c11.Card(0, 2)]) == set(game.playerHand)) ca.assert_equals([c11.Card(0, 3)], game.dealerHand) # Check card removal ca.assert_equals(c11.full_deck()[3:], start_deck) ca.assert_equals(start_deck, game.deck) print_done_message(verbose, tester_name)
def play_a_game(): """Create and play a single new blackjack game. This function provides a text-based interface for blackjack. It will continue to run until the end of the game.""" # Create a new shuffled full deck deck = card_lab11.full_deck() random.shuffle(deck) # Start a new game. game = Blackjack(deck) # Tell player the scoring rules print('Welcome to CS 1110 Blackjack.') print('Rules: Face cards are 10 points. Aces are 11 points.') print(' All other cards have face value.') print() # Show initial deal print('Your hand: ' + card_lab11.cardlist_str(game.playerHand)) print('Dealer\'s hand: ' + card_lab11.cardlist_str(game.dealerHand)) print() # While player has not busted out, ask if player wants to draw player_halted = False # True if player asked to halt, False otherwise while not game.playerBust() and not player_halted: # ri: input received from player ri = _prompt_player('Type h for new card, s to stop: ', ['h', 's']) player_halted = (ri == 's') if (not player_halted): game.playerHand.append(game.deck.pop(0)) print("You drew the " + str(game.playerHand[-1])) print() if game.playerBust(): print("You went bust, dealer wins.") else: print() _dealer_turn(game) print() print_winner_after_dealer(game) print("The final scores were " + str(game))
def test_str(verbose): """Test __str__ function for Blackjack objects. Extra info printed if `verbose` is True.""" tester_name = inspect.stack()[0][3] print("Running " + tester_name + "()") # Test cases: # Keys are tuples of the form (playerhand, dealerhand) # Values are the desired output of __str__ test_cases = { ((c11.Card(alt='QH'), c11.Card(alt='TD')), (c11.Card(alt='9H'), )): 'player: 20; dealer: 9', ((), (c11.Card(alt='9H'), )): 'player: 0; dealer: 9', ((), (c11.Card(alt='9H'), c11.Card(alt="AH"))): 'player: 0; dealer: 20', ((c11.Card(alt='9H'), c11.Card(alt="TH")), (c11.Card(alt='3S'), c11.Card(alt='KH'), c11.Card(alt='9C'))): 'player: 19; dealer: 22' } game = make_dummy_game() for tc in test_cases: phand = list(tc[0]) dhand = list(tc[1]) if verbose: print('\tTesting ' + c11.cardlist_str(phand) + ' and ' + c11.cardlist_str(dhand)) answer = test_cases[tc] game.playerHand = phand game.dealerHand = dhand output = str(game) ca.assert_equals(answer, output) print_done_message(verbose, tester_name)
def test_score(verbose): """Test _score function. Extra info printed if `verbose` is True.""" tester_name = inspect.stack()[0][3] print("Running " + tester_name + "()") for tc in test_cases_in_common: if verbose: print('\tTesting ' + c11.cardlist_str(tc)) answer = test_cases_in_common[tc] output = blackjack._score(list(tc)) # Convert tuple to list ca.assert_equals(answer, output) print_done_message(verbose, tester_name)
def test_playerBust(verbose): """Test playerBust function. Extra info printed if `verbose` is True.""" tester_name = inspect.stack()[0][3] print("Running " + tester_name + "()") game = make_dummy_game() for tc in test_cases_bust: if verbose: print('\tTesting ' + c11.cardlist_str(tc)) answer = test_cases_bust[tc] game.playerHand = list(tc) output = game.playerBust() ca.assert_equals(answer, output) print_done_message(verbose, tester_name)
def test_dealerScore(verbose): """Test dealerScore function. Extra info printed if `verbose` is True.""" tester_name = inspect.stack()[0][3] print("Running " + tester_name + "()") game = make_dummy_game() for tc in test_cases_in_common: if verbose: print('\tTesting ' + c11.cardlist_str(tc)) answer = test_cases_in_common[tc] game.dealerHand = list(tc) # Set the dealerHand to be a test case. output = game.dealerScore() ca.assert_equals(answer, output) print_done_message(verbose, tester_name)