コード例 #1
0
ファイル: BlackJack.py プロジェクト: aantoine97/BlackJack
def play():
    play_again = True
    player = Human()
    computer = Computer()

    while play_again:
        player.empty_hand()
        computer.empty_hand()

        deck = Deck()
        player.pick_up_card(deck.give_card())
        player.pick_up_card(deck.give_card())

        computer.pick_up_card(deck.give_card())
        computer.pick_up_card(deck.give_card())

        print("\nComputer hand: ")
        computer.display_hand()

        print("\nYour hand: ")
        player.display_hand()

        player.get_points()
        print("Your current chip balance is: {}".format(player.chips))

        player_bet = get_bet(player)

        keep_drawing = True

        while keep_drawing:
            user_input = input(
                "\nHit or stay? (type h for hit or s for stay): ")
            user_input = Utilities.get_valid_response(user_input.lower(),
                                                      ["h", "s"])

            if user_input == "s":
                keep_drawing = False
            else:
                player.pick_up_card(deck.give_card())
                player.display_hand()
                player.get_points()
                if player.bust():
                    print(
                        "\nYou went bust! Unfortunately you lost your bet and your chip balance is {}"
                        .format(player.chips))
                    play_again = ask_to_play_again()
                    break

        if not player.bust():
            print(
                "\nThe computer will now draw until it either beats your score or busts..."
            )
            computer.collect_cards(deck, player)

            if computer.bust():
                player.collect_winnings(player_bet * 2)
                print(
                    "\nThe computer went bust so you win! Your total score was {} and the computer's was {}. Your chip balance is {}"
                    .format(player.get_total_points(),
                            computer.get_total_points(), player.chips))
                play_again = ask_to_play_again()
            elif computer.get_total_points() == player.get_total_points():
                player.collect_winnings(player_bet)
                print(
                    "\nLooks like there's been a tie! You get to keep your bet so your chip balance is {}"
                    .format(player.chips))
                play_again = ask_to_play_again()
            else:
                print(
                    "\nThe computer beat your score :(... Your total score was {} and the computer's was {}. Your chip balance is {}"
                    .format(player.get_total_points(),
                            computer.get_total_points(), player.chips))
                play_again = ask_to_play_again()