我是一个Python新手,但我尝试写了一个二十一点脚本。在调试并纠正所有明显的错误之后,我遇到了一个我无法弄清楚的奇怪事件。
当total
>&gt; 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 :(得分:1)
下面是(某种)工作代码。它仍然实现了一个奇怪的二十一点变体(排名从1到15,没有像ace那样计算为1或11,击中后允许分割)。一般分裂在这里处理得不好...我认为你可以拆分然后仍然打/等。但是击中并不会更新任何一只分手,而且再次分裂并没有做任何事情。我会留给你解决那些逻辑错误。
我认为你所描述的问题最好由@Martin的答案解释。我最后用else
简化了这个逻辑来处理非破坏案例。顺便说一句,如果您真正想要的只是stand
,则无需使用done
或break
等标记来退出循环。
我还清理了一些杂项......将一些不必要的转换丢给了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!")
答案 1 :(得分:0)
如果用户破产并选择开始新游戏,则代码不会从while not done
循环的开头开始,而是继续前进。因此,当您到达while (total < 21) and (stand != True)
行时,总数仍然是导致用户破产的总数。
答案 2 :(得分:0)
看起来你正在玩游戏{未完成:} 如果玩家输入&#34; n&#34;它只会突破它。 但是开始一个新游戏的功能在代码中比在变量赋值中更晚,我相信永远不会重新启动。 此外,如果你打破它较小的功能或方法,它将有助于你的编码。