私は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!")
「stand!= True」は「stand is not true」または「stand is False」である必要があります。平等をTrueまたはFalseにチェックしないでください。 "is True"または "is False"を使用してください –
私は質問を理解していません。 Total> 21の場合、条件式がTotal <21およびのwhileループには入れません。あなたの最大の問題は組織です。あなたは純粋に手続き型のシステム(ループ、条件付き、入力、出力としてすべてのステップを記述することにより、ステップバイステップでこれをしようとしています)。ブラックジャックの「パーツ」を機能に分割して手続き的にそれらの機能を呼び出すと、各ステップで何が起こっているのかがより簡単にわかります。 –