二十一点游戏不起作用

时间:2018-04-29 14:01:13

标签: python-3.x blackjack

import random
deckOfCards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
playerHand = []
computerHand = []


def testWin():
    if sum(playerHand) == sum(computerHand):
        print("Draw")
    elif sum(playerHand) == 21:
        print("Blackjack! You win")
    elif sum(computerHand) == 21:
        print("Computer has blackjack you lose")

    if sum(playerHand) > 21:
        if sum(computerHand) < 21:
            print("You lost")
        elif sum(computerHand) > 21:
            print("Draw")
    elif sum(computerHand) > 21:
        if sum(playerHand) < 21:
            print("You win")
        elif sum(playerHand) > 21:
            print("Draw")
    elif sum(playerHand) < 21:
        if sum(computerHand) > 21:
            print("You win!")
        elif sum(computerHand) < 21 and sum(computerHand) < sum(playerHand):
            print("You win")
        elif sum(computerHand) < 21 and sum(computerHand) > sum(computerHand):
            print("You lose")


def drawPlayerCard():
        playerHand.append(deckOfCards[random.randint(0, 9)])
        print("Your Cards are:", playerHand)
        print("total:", sum(playerHand), "\n")
        if len(playerHand) < 2:
            drawPlayerCard()
        drawComputerHand()


def drawComputerHand():
    if sum(computerHand) <= 17:
        computerHand.append(deckOfCards[random.randint(0, 9)])
        print("the computer has:", computerHand)
        print("total:", sum(computerHand), "\n")
        if len(computerHand) < 2:
            drawComputerHand()
        hitStand()
    else:
        print("the computer stands with a total of:", sum(computerHand))
        hitStand()


def hitStand():
        option = input("do you want to hit or stand? [h/s]")
        if option.lower() == "h":
            drawPlayerCard()
        elif option.lower() == "s":
            testWin()
        else:
            print("please say if you want to hit or stand!")
            hitStand()


def start():
    startGaming = input("Do you want to play Blackjack? [y/n]")
    if startGaming == "y":
        drawPlayerCard()
    elif startGaming == "n":
        pass
    else:
        print("please state if you want to start the game")
        start()


start()
嘿,我是pyhton的新手,我试图制作一个简单的二十一点游戏。它没有完全按预期工作。当我站起来时,我会得到一个永无止境的循环:“你想要击中或站立吗?[h / s]”或类似的东西

do you want to hit or stand? [h/s]h
Your Cards are: [10, 2, 4]
total: 16 

the computer has: [9, 6, 4]
total: 19 

do you want to hit or stand? [h/s]s
Your Cards are: [10, 2, 4, 3]
total: 19 

Draw
do you want to hit or stand? [h/s]s
Your Cards are: [10, 2, 4, 3, 4]
total: 23 

You lost
the computer stands with a total of: 19
do you want to hit or stand? [h/s]s
Your Cards are: [10, 2, 4, 3, 4, 2]
total: 25 

You lost
在代码停止之前

我无法弄清楚它为什么会这样做。它给你一个你想要击中或站立的循环,直到它突然停止并突然添加另一张卡而没有我击中

1 个答案:

答案 0 :(得分:0)

问题在于&#34; drawPlayerCard()&#34;调用&#34; drawComputerHand()&#34;和&#34; drawComputerHand()&#34; call&#34; hitStand()&#34;。

说明:

&#34; drawPlayerHand()&#34;中的第一个问题:

    if len(playerHand) < 2:
        drawPlayerCard()
    drawComputerHand()

当你的if语句为真时,&#34; drawPlayerHand()&#34;第二次执行。这意味着&#34; drawComputerHand()&#34;也被执行了2次。

你在&#34; drawComputerHand()&#34;:

中遇到同样的问题
    if len(computerHand) < 2:
        drawComputerHand()
    hitStand()

尝试自己解决。如果您无法修复它,我可以发布更多代码。