2015-12-26 32 views
0

我目前正在使用python進行二十一點玩家模擬器工作,用戶可以通過遊戲中的步驟進行操作。我已經在程序結構方面取得了一些成功,但是在嘗試通過應用程序觸發時會觸發我的邏輯。例如,通常在抽取王牌時,「新總計」文本重複一次。該計劃的另一個問題是,當抽籤一張A時,所用的兩個總數不會像我所希望的那樣進入下一張抽牌/總數。作爲編程新手,我提前抱歉,由於我在識別深度錯誤時遇到了問題,因此我的描述聽起來很模糊。如何解決我的「二十一點播放器模擬器」的邏輯?

所有幫助表示讚賞。

import random 

def play_hand_dealer(): 
    total = 0 
    player_is_bust = False 
    ace_was_thrown = False 
    another_card = True 
    ace_total = 0 
    high_ace = 10 

    while player_is_bust == False and another_card != "n": 
     card = random.choice([1,2,3,4,5,6,7,8,9,10,10,10,10]) 
     total = total + int(card) 

     if card == 1: 
      ace_was_thrown = True 
      ace_total = high_ace + int(total) 
      print("Ace  New Total is", total, "or", ace_total, end = " ") 

     if ace_total < 21 and ace_was_thrown: 
      ace_total = total + 10 
      print("New Total is ", total, "or", ace_total, end = " ") 

     else: 
      print(card, "New Total is ", total, end = " ") 

     if total > 21: 
       player_is_bust = True 

     if total < 21: 
      another_card = input("Another Card? ") 

    if ace_was_thrown: 
     if total + 10 >= 17 and total + 10 <= 21: 
      total = total + 10 

    if player_is_bust: 
     print("Bust") 

    else: 
     print("  Final Total:",total) 


choice = "" 
while(choice != "q"): 
    play_hand_dealer() 
    print() 
    choice = input("Enter to play again , or 'q' to quit: ") 
    print() 
+0

也許這個問題,它更適合Code Review.SE。嘗試將你的問題分成更具體的編程問題。 – iled

+4

@iled只要有「邏輯錯誤」,它根本不屬於Code Review。請閱讀[Stack Overflow用戶代碼評論指南](http://meta.codereview.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users) –

+0

@ SimonForsbergMcFeely謝謝,這很有幫助。我已經看到其他類似的問題被建議去CR,但是這個話題更加清晰。謝謝。無論如何,我仍然認爲OP應該用更小更具體的問題來解決這個問題。 – iled

回答

0

我不知道二十一點規則,所以我只能評論代碼本身,而不是遊戲邏輯。

total = total + int(card) 
ace_total = high_ace + int(total) 

你不需要int(),因爲這些已經是一個數字。


if card == 1: 
     ace_was_thrown = True 
     ace_total = high_ace + int(total) 
     print("Ace  New Total is", total, "or", ace_total, end = " ") 

    if ace_total < 21 and ace_was_thrown: 
     ace_total = total + 10 
     print("New Total is ", total, "or", ace_total, end = " ") 

    else: 
     print(card, "New Total is ", total, end = " ") 

你或許應該與如果,ELIF更換,否則。這就是爲什麼你兩次獲得「新合計」的原因。或者你可以將所有的印刷品移到下面,如果其他的話,這取決於你需要做什麼。


if total > 21: 
      player_is_bust = True 

    if total < 21: 
     another_card = input("Another Card? ") 

這個地方不知道怎麼做,如果總== 21你或許應該改成這樣:

if total > 21: 
     player_is_bust = True 

elif total < 21: 
    another_card = input("Another Card? ") 

else: 
    break 

有可能獲得超過一個王牌在一場比賽中,我希望你的程序正確處理。

請解釋這個遊戲的規則,如果你需要更好的幫助。

+0

謝謝!你不知道這對我有多大幫助。 – Semicircle