2017-06-15 249 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檢查平等。使用「是真」或「是假」 –

+0

我不明白這個問題。如果Total> 21,那麼它不應該輸入一個while循環,其條件是Total <21和。這裏最大的問題是組織。你試圖把它作爲一個純粹的程序系統(逐步地將每一步寫成循環,條件,輸入,輸出)。如果要將二十一點的「部分」分解爲函數,然後按程序調用這些函數,則可以更輕鬆地看到每一步發生了什麼。 –

回答

1

下面是(在某種程度上)工作的代碼。它仍然實現了一個奇怪的二十一點變體(排名從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!") 
0

如果用戶蕭條,並選擇開始新遊戲,你的代碼不會從while not done循環的起點開始,它只是不斷向前發展。所以當你到達while (total < 21) and (stand != True)這條線時,總數仍然是導致用戶崩潰的總數。

0

看起來你正在玩{{while not done:} 裏面的遊戲如果玩家輸入「n」 但它只是突破它,但是開始新遊戲的函數在代碼後面比我認爲,變量賦值永遠不會重新啓動。 此外,如果您打破較小的功能或方法,它將有助於您的編碼。

+0

我想我明白你在說什麼。但我想通過簡單地設置變量= 0,並重置我的布爾在循環的頂部將工作。 – JKooper