Игра в блэкджек не работает - PullRequest
0 голосов
/ 29 апреля 2018
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, и я попытался создать простую игру в блэкджек. это не совсем работает, как задумано. когда я встаю, я получаю бесконечный цикл «ты хочешь ударить или стоять? [ч / с]» или что-то в этом роде

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 голосов
/ 30 апреля 2018

Проблема в том, что "drawPlayerCard ()" вызывает "drawComputerHand ()" и "drawComputerHand ()", вызывает "hitStand ()".

Объяснение:

первая проблема в "drawPlayerHand ()":

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

Когда ваш оператор if истинен, "drawPlayerHand ()" выполняется второй раз. Это означает, что drawComputerHand () также выполняется 2 раза.

И у вас такая же проблема в "drawComputerHand ()":

    if len(computerHand) < 2:
        drawComputerHand()
    hitStand()

Попробуйте исправить это самостоятельно. Если вы не можете это исправить, я могу опубликовать еще немного кода.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...