酒杯python脚本,重新启动游戏

酒杯python脚本,重新启动游戏

问题描述:

当流量错误我是一个Python的新手,但我想我的手在写一个脚本酒杯。在调试和纠正所有明显的错误之后,我遇到了一个我无法弄清楚的奇怪事件。酒杯python脚本,重新启动游戏

total> 21时,它似乎跳过了while (total < 21) and (stand != True):代码块,即使在游戏循环开始时我将所有相关变量归零。

我已经花了太多时间试图弄清楚这一点,我不禁想,这有一个明显的解决方案。

我不明白为什么while (total < 21) and (stand != True):似乎被忽略,即使它应该是在每场比赛开始时的真实的陈述。

这里是下面的完整代码。请随时测试一下,看看我的意思。

import pygame 
import random 

print("Welcome to PyBlackjack V1.0") 
done = False 

while not done: 

    # --- Reset our Hands --- 

    dealerhand = 0 
    split = False 
    stand = False 
    total = 0 

    # --- Dealing the Player's hand. --- 

    print("Dealer is dealing hand.") 
    firstcard = random.randrange(1, 15) 
    print("First card:",str(firstcard),) 
    secondcard = random.randrange(1, 15) 
    print("Second card:",str(secondcard),) 

    total = (firstcard + secondcard) 

    print("Hand:",str(total),) 

     # --- Bust Check --- 

    if total > 21: 
     print("Bust! Game Over.") 
     newgame = input("Play again? Y/N: ") 
     if str(newgame) == "n": 
      done = True 
      break 
     else: 
      print("Starting new game! Good Luck!") 

    dealerfirstcard = random.randrange(1, 15) 
    dealerholecard = random.randrange(1, 15) 

    dealerhand = (dealerfirstcard + dealerholecard) 

    print("Dealer's Hand:",str(dealerfirstcard)) 

    # --- Player decides what to do --- 

    while (total < 21) and (stand != True): 

     if split != True: 
      print("Hand:",str(total)) 
     elif split == True: 
      print("Left hand:",str(lefthand),"| Right hand:",str(righthand)) 

     playerchoice = input("Hit (1), Double Down(2), Split(3), Stand(4)?") 

     if int(playerchoice) == 1: 
      total += random.randrange(1, 15) 
     elif int(playerchoice) == 2: 
      #Reserved 
      break 
     elif int(playerchoice) == 3: 
      if ((firstcard + secondcard)/2) == firstcard and split != True: 
       lefthand = (firstcard + random.randrange(1, 15)) 
       righthand = (secondcard + random.randrange(1, 15)) 
       split = True 
      else: 
       print("You cannot split this hand!") 
     elif int(playerchoice) == 4: 
      print("You stand.") 
      stand = True 
     else: 
      print("Invalid Choice!") 

    print("Hand:",total,) 

    if total > 21: 
     print("Bust! Game Over.") 

    newgame = input("Play again? Y/N: ") 
    if str(newgame) == "n": 
     done = True 
     break 
    else: 
     print("Starting new game! Good Luck!") 

    print("Dealer reveals hole card...") 
    print("Dealer Hand:",str(dealerhand),) 

    # --- Dealer hits until >= 17 --- 

    while dealerhand < 17: 
     print("Dealer hits...") 
     dealerhand = (dealerhand + random.randrange(1, 15)) 
     print("Dealer hand:",dealerhand,) 

    # --- Deciding who wins --- 

    if dealerhand > 21: 
     print("Dealer busts! You win!") 
    elif dealerhand >= 17: 
     print("Your hand:",total,"| Dealer hand:",dealerhand,) 
     if split != True: 
      if dealerhand >= total: 
       print("You lose!") 
      elif dealerhand < total: 
       print("You win!") 
     elif split == True: 
      if lefthand > dealerhand: 
       print("Left hand wins!") 
      elif lefthand < dealerhand: 
       print("Left hand loses!") 
      else: 
       print("An error occured. Ending program.") 
       done = True 
       break 

      if righthand > dealerhand: 
       print("Right hand wins!") 
      elif righthand < dealerhand: 
       print("Right hand loses!") 
      else: 
       print("An error occured. Ending program.") 
       done = True 
       break 

    # --- To loop or not to loop --- 

    newgame = input("Play again? Y/N: ") 
    if str(newgame) == "n": 
     done = True 
     break 
    else: 
     print("Starting new game! Good Luck!") 
+0

甚至在测试之前:“stand!= True”应该是“stand is not True”或“stand is False”。不要用True或False检查平等。使用“是真”或“是假” –

+0

我不明白这个问题。如果Total> 21,那么它不应该输入一个while循环,其条件是Total 。这里最大的问题是组织。你试图把它作为一个纯粹的程序系统(逐步地将每一步写成循环,条件,输入,输出)。如果要将二十一点的“部分”分解为函数,然后按程序调用这些函数,则可以更轻松地看到每一步发生了什么。 –

下面是(在某种程度上)工作的代码。它仍然实现了一个奇怪的二十一点变体(排名从1到15,没有这样的东西,可以算作1或11,打后允许分裂)。一般的分裂在这里处理不好......我认为你可以分裂然后仍然击中/等。但是击球并不会更新任何一方的分手,而再次分球也不会做任何事情。我会给你解决这些逻辑错误。

我想您所描述的问题是由@马丁的回答最好解释。我结束了简化这个逻辑与else来处理非胸部的情况。顺便说一句,如果你真正想要的只是break,就不需要使用像standdone这样的标志来退出循环。

我还清理了一些杂项东西......将一些不必要的转换删除了str,清理了检测玩家胸围,检测和打印按钮等的逻辑。请参阅下面的完整代码并注意不同之处。

import random 

print("Welcome to PyBlackjack V1.0") 

while True: 
    # --- Reset our Hands --- 
    dealerhand = 0 
    split = False 
    total = 0 

    # --- Dealing the Player's hand. --- 
    print("Dealer is dealing hand.") 
    firstcard = random.randrange(1, 15) 
    print("First card:", firstcard) 
    secondcard = random.randrange(1, 15) 
    print("Second card:", secondcard) 

    total = firstcard + secondcard 

    print("Hand:", total) 

    dealerfirstcard = random.randrange(1, 15) 
    dealerholecard = random.randrange(1, 15) 

    dealerhand = dealerfirstcard + dealerholecard 

    print("Dealer's hole card:", dealerfirstcard) 

    # --- Player decides what to do --- 
    while total < 21: 
     if not split: 
      print("Hand:", total) 
     else: 
      print("Left hand:", lefthand, "| Right hand:", righthand) 

     playerchoice = int(input("Hit (1), Double Down(2), Split(3), Stand(4)? ")) 

     if playerchoice == 1: 
      total += random.randrange(1, 15) 
     elif playerchoice == 2: 
      #Reserved 
      break 
     elif playerchoice == 3: 
      # NOTE: This will allow splitting even after hitting 
      if firstcard == secondcard and not split: 
       lefthand = firstcard + random.randrange(1, 15) 
       righthand = secondcard + random.randrange(1, 15) 
       split = True 
      else: 
       print("You cannot split this hand!") 
     elif playerchoice == 4: 
      print("You stand.") 
      break 
     else: 
      print("Invalid Choice!") 

    print("Hand:", total) 

    if total > 21: 
     print("Bust! Game Over.") 
    else: 
     print("Dealer reveals hole card...") 
     print("Dealer hand:", dealerhand) 

     # --- Dealer hits until >= 17 --- 
     while dealerhand < 17: 
      print("Dealer hits...") 
      dealerhand += random.randrange(1, 15) 
      print("Dealer hand:", dealerhand) 

     # --- Deciding who wins --- 
     if dealerhand > 21: 
      print("Dealer busts! You win!") 
     else: 
      print("Your hand:", total, "| Dealer hand:", dealerhand) 
      if not split: 
       if dealerhand >= total: 
        print("You lose!") 
       elif dealerhand < total: 
        print("You win!") 
       else: 
        print("Push.") 
      else: 
       if lefthand > dealerhand: 
        print("Left hand wins!") 
       elif lefthand < dealerhand: 
        print("Left hand loses!") 
       else: 
        print("Push.") 

       if righthand > dealerhand: 
        print("Right hand wins!") 
       elif righthand < dealerhand: 
        print("Right hand loses!") 
       else: 
        print("Push.") 

    # --- To loop or not to loop --- 
    newgame = input("Play again? Y/N: ") 
    if str(newgame) == "n": 
     break 
    else: 
     print("Starting new game! Good Luck!") 

如果用户萧条,并选择开始新游戏,你的代码不会从while not done循环的起点开始,它只是不断向前发展。所以当你到达while (total < 21) and (stand != True)这条线时,总数仍然是导致用户崩溃的总数。

看起来你正在玩{{while not done:} 里面的游戏如果玩家输入“n” 但它只是突破它,但是开始新游戏的函数在代码后面比我认为,变量赋值永远不会重新启动。 此外,如果您打破较小的功能或方法,它将有助于您的编码。

+0

我想我明白你在说什么。但我想通过简单地设置变量= 0,并重置我的布尔在循环的顶部将工作。 – JKooper