Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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))