获得二十一点的正确值

时间:2018-11-10 06:05:21

标签: python

我对发牌总数有疑问。运行代码时,它不能给您正确的两张卡的总和,即卡[2,8]等于24 ..当它只有10时,怎么回事?任何帮助是极大的赞赏!

还有其他似乎不对的地方吗?如果我的任何意见有误,请随时纠正我!

import os

import random

deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] * 4


def deal(deck):  # deck function
    hand = []


    for i in range(2):
        random.shuffle(deck)  # shuffles deck
        card = deck.pop()  # removes and returns the value back to card
        if card == 11: card = "J"  # sets 11 to jack
        if card == 12: card = "Q"  # sets 12 to queen
        if card == 13: card = "K"  # sets 13 to king
        if card == 14: card = "A"  # sets 14 to ace
        hand.append(card)
    return hand


def play_again():  # ask payer if wants to play again function
    again = raw_input("Do you want to play again? (Y/N) : ").lower()


    if again == "y":  # if answers yes, will give the player more cards
        dealer_hand = []
        player_hand = []
        deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] * 4  # sets deck to values of cards
        game()
    else:
        print ("Bye!")  # exits program
        exit()


def total(hand):
    total = 0  # start off with total of zero


    for card in hand:  # for the cards you dealt
        if card == "J" or card == "Q" or card == "K":  # if the card is j,q,k it equals 10
            total += 10
        elif card == "A":
            if total >= 11: total += 1  # if the card is an A, if the total is more than 11, it makes the vaue 1
        else:
            total += 11  # makes the value of the ace 1 if the total is greater than 11
    else:
        total += card  # adds up your cards
    return total


def hit(hand):
    card = deck.pop()  # removes and returns the value back to card


    if card == 11: card = "J"  # setting the card number to the face value
    if card == 12: card = "Q"
    if card == 13: card = "K"
    if card == 14: card = "A"
    hand.append(card)
    return hand


def clear():
    if os.name == 'nt':
        os.system('CLS')


if os.name == 'posix':
    os.system('clear')


def print_results(dealer_hand, player_hand):  # funtcion for results of hand
    clear()


    print ("The dealer has a " + str(dealer_hand) + " for a total of " + str(total(dealer_hand)))  # gives you dealers hand
    print ("You have a " + str(player_hand) + " for a total of " + str(total(player_hand)))  # gives you your hand


def blackjack(dealer_hand, player_hand):
    if total(player_hand) == 21:
        print_results(dealer_hand, player_hand)  # prints dealers and your hand
        print ("Congratulations! You got a Blackjack!\n")
        play_again()
    elif total(dealer_hand) == 21:  # if hand is more than 21,
        print_results(dealer_hand, player_hand)  # prints out dealer and your hand
        print ("Sorry, you lose. The dealer got a blackjack.\n")
        play_again()


def score(dealer_hand, player_hand):
    if total(player_hand) == 21:  # if your hand = 21
        print_results(dealer_hand, player_hand)  # shows dealers/ your hand
        print ("Congratulations! You got a Blackjack!\n")
    elif total(dealer_hand) == 21:  # if dealer hand = 21
        print_results(dealer_hand, player_hand)  # shows dealers/ your hand
        print ("Sorry, you lose. The dealer got a blackjack.\n")
    elif total(player_hand) > 21:  # if your hand is greater than
        print_results(dealer_hand, player_hand)  # shows dealers/ your hand
        print ("Sorry. You busted. You lose.\n")
    elif total(dealer_hand) > 21:  # if dealer hand is greater than 21
        print_results(dealer_hand, player_hand)  # shows dealers/ your hand
        print ("Dealer busts. You win!\n")
    elif total(player_hand) < total(dealer_hand):  # if your hand is less than the dealers
        print_results(dealer_hand, player_hand)  # shows dealers/ your hand
        print ("Sorry. Your score isn't higher than the dealer. You lose.\n")
    elif total(player_hand) > total(dealer_hand):  # if your hand is higher than the dealers
        print_results(dealer_hand, player_hand)  # shows dealers/ your hand
        print ("Congratulations. Your score is higher than the dealer. You win\n")


def game():
    choice = 0


    clear()
    print ("WELCOME TO BLACKJACK!\n")  # welcomes player to game
    dealer_hand = deal(deck)  # deals to dealers
    player_hand = deal(deck)  # deals to player
    while choice != "q":
        print ("The dealer is showing a " + str(dealer_hand[0]))  # shows what one of the dealers card is
        print ("You have a " + str(player_hand) + " for a total of " + str(total(player_hand)))  # shows you your hand
        blackjack(dealer_hand, player_hand)  # send inormation to blackjack funciton
        choice = raw_input(
            "Do you want to [H]it, [S]tand, or [Q]uit: ").lower()  # gets user input on if they want to hit, stand, or quit
        clear()
        if choice == "h":
            hit(player_hand)  # adds a new card value to your exisiting han d
            while total(dealer_hand) < 17:  # dealer has to hit is value of cards below 17
                hit(dealer_hand)
            score(dealer_hand, player_hand)
            play_again()
        elif choice == "s":
            while total(dealer_hand) < 17:
                hit(dealer_hand)
            score(dealer_hand, player_hand)
            play_again()
        elif choice == "q":
            print("Bye!")
            exit()

if __name__ == "__main__":
    game()

1 个答案:

答案 0 :(得分:0)

您粘贴的代码存在一些缩进问题。我修复了该问题并运行了代码,并获得了正确的结果:

WELCOME TO BLACKJACK!

The dealer is showing a 5
You have a ['A', 8] for a total of 19
Do you want to [H]it, [S]tand, or [Q]uit: S

The dealer has a [5, 4, 7] for a total of 16
You have a ['A', 8] for a total of 19
Congratulations. Your score is higher than the dealer. You win

Do you want to play again? (Y/N) : 

还要在此处粘贴缩进代码:

import os

import random


deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4


def deal(deck):  # deck function
    hand = []
    for i in range(2):
        random.shuffle(deck)  # shuffles deck
        card = deck.pop()  # removes and returns the value back to card
        if card == 11:
            card = "J"  # sets 11 to jack
        if card == 12:
            card = "Q"  # sets 12 to queen
        if card == 13:
            card = "K"  # sets 13 to king
        if card == 14:
            card = "A"  # sets 14 to ace
        hand.append(card)
    return hand


def play_again():  # ask payer if wants to play again function
    again = raw_input("Do you want to play again? (Y/N) : ").lower()
    if again == "y":  # if answers yes, will give the player more cards
        dealer_hand = []
        player_hand = []
        deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4  #sets deck to values of cards
        game()
    else:
        print ("Bye!")  # exits program
        exit()


def total(hand):
    total = 0  # start off with total of zero
    for card in hand:  # for the cards you dealt
        if card == "J" or card == "Q" or card == "K":  # if the card is j,q,k it equals 10
            total += 10
        elif card == "A":
            if total >= 11: 
                total+= 1  # if the card is an A, if the total is more than 11, it makes the vaue 1
            else: 
                total += 11 # makes the value of the ace 1 if the total is greater than 11
        else:
            total += card  # adds up your cards
    return total


def hit(hand):
    card = deck.pop() # removes and returns the value back to card
    if card == 11:
        card = "J" # setting the card number to the face value
    if card == 12:
        card = "Q"
    if card == 13:
        card = "K"
    if card == 14:
        card = "A"
    hand.append(card)
    return hand


def clear():
    if os.name == 'nt':
        os.system('CLS')
    if os.name == 'posix':
        os.system('clear')


def print_results(dealer_hand, player_hand): # funtcion for results of hand
    clear()
    print ("The dealer has a " + str(dealer_hand) + " for a total of " + str(total(dealer_hand))) # gives you dealers hand
    print ("You have a " + str(player_hand) + " for a total of " + str(total(player_hand))) # gives you your hand


def blackjack(dealer_hand, player_hand):
    if total(player_hand) == 21:
        print_results(dealer_hand, player_hand)  # prints dealers and your hand
        print ("Congratulations! You got a Blackjack!\n")
        play_again()
    elif total(dealer_hand) == 21:  # if hand is more than 21,
        print_results(dealer_hand, player_hand)   # prints out dealer and your hand
        print ("Sorry, you lose. The dealer got a blackjack.\n")
        play_again()


def score(dealer_hand, player_hand):
    if total(player_hand) == 21: # if your hand = 21
        print_results(dealer_hand, player_hand) # shows dealers/ your hand
        print ("Congratulations! You got a Blackjack!\n")
    elif total(dealer_hand) == 21: # if dealer hand = 21
        print_results(dealer_hand, player_hand) # shows dealers/ your hand
        print ("Sorry, you lose. The dealer got a blackjack.\n")
    elif total(player_hand) > 21: # if your hand is greater than
        print_results(dealer_hand, player_hand) # shows dealers/ your hand
        print ("Sorry. You busted. You lose.\n")
    elif total(dealer_hand) > 21: # if dealer hand is greater than 21
        print_results(dealer_hand, player_hand) # shows dealers/ your hand
        print ("Dealer busts. You win!\n")
    elif total(player_hand) < total(dealer_hand): # if your hand is less than the dealers
        print_results(dealer_hand, player_hand) # shows dealers/ your hand
        print ("Sorry. Your score isn't higher than the dealer. You lose.\n")
    elif total(player_hand) > total(dealer_hand): # if your hand is higher than the dealers
        print_results(dealer_hand, player_hand) # shows dealers/ your hand
        print ("Congratulations. Your score is higher than the dealer. You win\n")


def game():
    choice = 0
    clear()
    print ("WELCOME TO BLACKJACK!\n") # welcomes player to game
    dealer_hand = deal(deck) # deals to dealers
    player_hand = deal(deck) # deals to player
    while choice != "q":
        print ("The dealer is showing a " + str(dealer_hand[0]))  # shows what one of the dealers card is
        print ("You have a " + str(player_hand) + " for a total of " + str(total(player_hand))) # shows you your hand
        blackjack(dealer_hand, player_hand)  # send inormation to blackjack funciton
        choice = raw_input("Do you want to [H]it, [S]tand, or [Q]uit: ").lower() # gets user input on if they want to hit, stand, or quit
        clear()
        if choice == "h":
            hit(player_hand) # adds a new card value to your exisiting han d
            while total(dealer_hand) < 17: # dealer has to hit is value of cards below 17
                hit(dealer_hand)
                score(dealer_hand, player_hand)
                play_again()
        elif choice == "s":
            while total(dealer_hand) < 17:
                hit(dealer_hand)
                score(dealer_hand, player_hand)
                play_again()
        elif choice == "q":
            print("Bye!")
            exit()


if __name__ == "__main__":
    game()

让我知道这是否有帮助。