2017-06-15 37 views
0

私は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にチェックしないでください。 "is True"または "is False"を使用してください –

+0

私は質問を理解していません。 Total> 21の場合、条件式がTotal <21およびのwhileループには入れません。あなたの最大の問題は組織です。あなたは純粋に手続き型のシステム(ループ、条件付き、入力、出力としてすべてのステップを記述することにより、ステップバイステップでこれをしようとしています)。ブラックジャックの「パーツ」を機能に分割して手続き的にそれらの機能を呼び出すと、各ステップで何が起こっているのかがより簡単にわかります。 –

答えて

1

以下は、作業コードの一種です。それはまだブラックジャックの奇妙な変種を実装しています(1から15までのランク、1から11までカウントするエースのようなものはありません。打撃後にスプリットが許されます)。分割は一般的にここでうまく処理されません...私はあなたが分割することができると思う/ etcをヒットしますか?しかし、打撃はスプリットハンドのどちらも更新しないし、スプリットすることは何もしない。私はそれらの論理的な誤りを整理するためにそれをあなたに任せます。

あなたが描いている問題は@Martinの答えによって最もよく説明されていると思います。私はちょうどelseでこのロジックを簡略化し、非バストケースを処理しました。ちなみに、実際に欲しいものがbreakの場合は、standまたはdoneのようなフラグを使用してループを終了する必要はありません。

また、不要なコンバージョンを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!") 
0

ユーザーの胸像と新しいゲームを開始することを選択した場合、あなたのコードは、それだけで今後続け、while not doneループの先頭から開始されません。したがって、あなたがwhile (total < 21) and (stand != True)行に到着したとき、合計はまだユーザーをバストに導く合計です。

0

{中止中:} プレイヤーが「n」と入力すると壊れます。 新しいゲームを開始する機能は後でコード内にあります私が信じている変数の割り当ては決して再開されません。 また、小さな関数やメソッドを分割すると、コーディングに役立ちます。

+0

私はあなたが言っていることを理解していると思います。しかし、単純に変数= 0に設定し、ループの最上部にある私のブール値をリセットすることでうまくいくと思いました。 – JKooper

関連する問題