简体   繁体   English

二十一点 python 游戏

[英]Blackjack python game

I have to create a blackjack game in python in which the user inputs the number of decks that are being used and the amount of money the user wants to bet.我必须在 python 中创建一个二十一点游戏,用户在其中输入正在使用的牌组数量和用户想要下注的金额。 The rules are: o The player places his bet (should be read from the keyboard).规则是: o 玩家下注(应从键盘上读取)。 o The dealer and player are dealt two cards (one card of the dealer should be hidden). o 发给庄家和玩家两张牌(应该隐藏庄家的一张牌)。 o If the player has 21 he wins his bet; o 如果玩家有 21,他就赢了他的赌注; else if the dealer has 21 then the dealer wins and the player loses his bet.否则,如果庄家有 21,则庄家获胜,玩家输掉赌注。 o The player can only select to draw a new card (hit) or pass. o 玩家只能通过 select 抽一张新牌(命中)或放弃。 Repeat until the player passes or busts.重复直到玩家通过或破产。 If the player goes above 21 then he busts.如果玩家的点数超过 21,那么他就会爆牌。 o Then the dealer draws cards (automatically) until he either busts or reaches 17 or higher. o 然后庄家(自动)抓牌,直到他爆牌或达到 17 点或更高点数。 o Values of cards: A= 1 or 11 (the highest that does not bust you) and J=Q=K=10 o Note: in blackjack there are more actions: insurance, double, split; o 牌面值:A= 1 或 11(不会让你爆牌的最高点数)和 J=Q=K=10 o 注意:在 21 点中有更多操作:保险、加倍、拆分; those are not required to be implemented as they would make the code you have to produce much, much longer and harder不需要实施这些,因为它们会使您必须生成的代码变得更长、更长、更难

I will show you my code below when I tried to run it, it said that the name "total" is not defined on the function game, but earlier on I have already defined it.当我尝试运行它时,我会在下面向您展示我的代码,它说名称“total”没有在 function 游戏中定义,但早些时候我已经定义了它。 I don't know how can i fix it.我不知道该如何解决。

import random

N=int(input("Please enter number of decks of cards used: "))

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

wins=0
losses=0
chip_pool = 100
bet = 1

def deal(deck):
    hand = []
    for i in range(2):
        random.shuffle(deck)
        card=deck.pop()
        if card == 11:
            card = "J"
        if card == 12:
            card = "Q"
        if card == 13:
            card = "K"
        if card == 14:
            card = "A"
        hand.append(card)
    return hand

def play_again():
    again = input("Do you want to play again? (Y/N) : ").lower()
    if again == "y":
        dealer_hand = []
        player_hand = []
        deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*N
        game()
    else:
        print("Thank you for playing.")
        exit()

def total_hand():
    total = 0
    for card in hand:
        if card == "J" or card == "Q" or card == "K":
            total = total + 10
        elif card == "A":
            if total >=11:
                total = total + 1
            else:
                total = total + 11
        else:
            total = total + card 
    return total

def hit(hand):
    card = deck.pop()
    if card == 11:
        card = "J"
    if card == 12:
        card = "Q"
    if card == 13:
        card = "K"
    if card == 14:
        card = "A"
    hand.append(card)
    return hand

def print_results(dealer_hand, player_hand):
    clear()
    
    print("\n  WELCOME TO BLACKJACK!\n")
    print("The dealer has a " + str(dealer_hand) + "for a total of" + str(total(dealer_hand)))
    print("You have a" + str(player_hand) + "for a total of" + str(total(player_hand)))

def blackjack(dealer_hand, player_hand):
    global wins
    global losses
    if total(player_hand) == 21:
        print_results(player_hand,dealer_hand)
        print("Congratulations! You got a blackjack.\n")
        wins = wins + 1
        play_again()
    elif total(dealer_hand) == 21:
        print_results(dealer_hand,player_hand)
        print("Sorry you lose. The dealer got a blackjack.\n")
        chip_pool -= bet
        loses = loses + 1
        play_again()

def score(dealer_hand,player_hand):
    if total(player_hand) == 21:
        print_results(dealer_hand, player_hand)
        print("Congratulations! You got a blackjack1\n")
    elif total(dealer_hand) == 21:
        print_results(dealer_hand, player_hand)
        print("Sorry you lose. The dealer go a blackjack\n")
    elif total(player_hand) > 21:
        print_results(dealer_hand, player_hand)
        print("Sorry, you busted. You lose.\n")
    elif total(dealer_hand) > 21:
        print_results(dealer_hand, player_hand)
        print("Dealer busts. You win!\n")
        chip_pool += bet
    elif total(player_hand) < total(dealer_hand):
        print_results(dealer_hand, player_hand)
        print("Sorry, the score is not higher than the dealer. You lose.\n")
        chip_pool -= bet 
    elif total(player_hand) > total(dealer_hand):
        print_results(dealer_hand, player_hand)
        print("Congratulations. Your score is higher than the dealer. You win.\n")
        chip_pool += bet
    elif total(player_hand)  == total(dealer_hand):
        print_results(playe_hand, dealer_hand)
        print("There is a tie. In a tie dealer always wins!\n")
        chip_pool -= bet


def make_bet():
    
    global bet
    bet = 0
    print("What amount of chips would you like to bet? ")
    while bet == 0:
        bet_comp = input()
        bet_comp = int(bet_comp)
        
        if bet_comp >= 1 and bet_comp <= chip_pool:
            bet = bet_comp
        else:
            print("Invalid bet, you only have" + str(chip_pool) + "remaining")

def game():
    choice = 0
    print("Welcome to Blackjack!\n")
    dealer_hand = deal(deck)
    player_hand = deal(deck)
    print("The dealer is showing a " +str(dealer_hand[0]))
    make_bet()
    print("You have a " + str(player_hand))
    blackjack(dealer_hand, player_hand)
    quit = False
    while not quit:
        choice = input("Do you want to [H]it, [S]tand, or [Q]uit: ").lower()
        if choice == 'h':
            hit(player_hand)
            print(player_hand)
            if total(player_hand) > 21:
                print("You busted")
                chip_pool -= bet
                play_again()
            elif choice == "s":
                while total(dealer_hand) < 17:
                    hit(dealer_hand)
                    print(dealer_hand)
                if total(dealer_hand) > 21:
                    print("Dealer busts. You win!")
                    chip_pool += bet
                    play_again()
                score(dealer_hand, playe_hand)
                play_again()
            elif choice == "q" :
                print("Thank you for playing. Hope you enjoyed!")
                quit = True 
                exit()

if __name__ == "__main__":
   game()

You have not defined the function total that you call.您尚未定义您调用的 function total Try adding this to your code尝试将此添加到您的代码中

def total(array):
    total = 0
    for card in array:
        if card == "J" or card == "Q" or card == "K":
            total = total + 10
        elif card == "A":
            if total >=11:
                total = total + 1
            else:
                total = total + 11
        else:
            total = total + card 
    return total

However I see some more issues in your code that will need fixing later on, Stand or Quit do nothing currently, and on busting there is an exception thrown since chip_pool never had a value assigned to it但是,我在您的代码中发现了一些需要稍后修复的问题,Stand 或 Quit 当前什么都不做,并且在破坏时抛出异常,因为chip_pool从未分配过值

EDIT 1: You defined a function编辑 1:您定义了一个 function

def total_hand():
    total = 0
    for card in hand:
        if card == "J" or card == "Q" or card == "K":
            total = total + 10
        elif card == "A":
            if total >=11:
                total = total + 1
            else:
                total = total + 11
        else:
            total = total + card 
    return total

Similar to what I suggested.类似于我的建议。 Maybe it was just a typo but you need to do the following也许这只是一个错字,但您需要执行以下操作

  1. Rename the function from total_hand to total OR call total_hand将 function 从total_hand重命名为total或调用total_hand
  2. Change the definition from total_hand() to total_hand(hand)将定义从total_hand()更改为total_hand(hand)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM