我是一个Python新手,但我试过写21点脚本。在调试并纠正了所有明显的错误之后,我遇到了一个我无法理解的奇怪事件。你知道吗
当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!")
看起来您正在{未完成时:}内玩游戏 只有当玩家键入“n”时,它才会中断 但是启动新游戏的函数在代码中比变量赋值要晚,我相信,变量赋值永远不会重新启动。 另外,如果你把它分解成更小的函数或方法,它也会帮助你编写代码。你知道吗
下面是(某种)工作代码。它仍然实现了21点的一个奇数变体(等级从1到15,没有1或11的王牌,击球后可以分开)。一般来说,分裂在这里处理得不好。。。我想你可以分开,然后仍然打/等等。?但是击打不会更新任何一只分开的手,再次分开也不会起任何作用。我把那些逻辑错误留给你去解决。你知道吗
我认为你所描述的问题最好用@Martin的回答来解释。最后我用一个
else
来简化这个逻辑,以处理非半身像的情况。顺便说一句,如果您真正想要的只是break
,则不需要使用stand
或done
这样的标志来退出循环。你知道吗我还清理了一些杂物。。。删除了一些对
str
不必要的转换,清理了检测玩家半身像、检测和打印推送等逻辑。请参阅下面的完整代码并注意区别。你知道吗如果用户失败并选择开始一个新游戏,那么您的代码不会从
while not done
循环开始,而是继续向前。因此,当您到达while (total < 21) and (stand != True)
行时,total仍然是导致用户崩溃的total。你知道吗相关问题 更多 >
编程相关推荐