2012-10-11 22 views
1

我想從LPTHW的練習36中得到一個遊戲來工作。爲什麼這個程序重複自己?

我可以讓遊戲的整個部分工作,除非它進入戰鬥狀態。這是我認爲問題所在的地方。

def combat_engine():  
    global wins, hit_points, current_hp 
    if monster_current_hp or current_hp > 0: 
     print "You can type 'a' for attack, or 'q' for quit, choose one." 
     answer = raw_input(":> ") 
     if 'a' in answer: 
      attack(monster) 
      attack('player') 
      combat_engine() 
     elif 'q' in answer: 
      print t.white_on_red("You give up and are devoured by the %s.") % monster 
      exit(0) 
     else: 
      print "I dont understand %s." % answer 
      next() 
      combat_engine() 
    elif monster_hp == 0: 
     print "You defeated the %s, congradulations %s!" % monster, name 
     wins = wins + 1 
     if wins == 5: 
      you_win() 
     elif victim == 'player': 
      hit_points = hit_points + d6() 
      current_hp = hit_points 
      print "You feel hardier." 
      next() 
      barracks() 
    elif current_hp == 0: 
     print "You have been deafeted by the %s, better luck next time." % monster 
     next() 
     exit(0) 

    else: 
     print "Bug Somewhere" 

我相信這個bug是在這個函數的某個地方。當我打印出每個角色的HP值時,在怪物降低到-2馬力後,戰鬥仍在繼續。也許這是布爾人的問題?

我想要做的是要麼做所有統計調整的勝利和退出遊戲,如果你輸了。我只是想要通過這個,所以我可以開始學習班級和口號,這應該讓我的生活更輕鬆。請讓我知道,如果我應該發佈更多信息,我是新來的,而不是在這裏張貼問題的最佳選擇。

在此先感謝!

+0

嘗試減少代碼示例 - 刪除無關緊要的代碼行,直到只有觸發錯誤的代碼行。這涉及擺脫實際的戰鬥 - 只要看看你是否可以硬編碼導致意外行爲的一系列事件。 – millimoose

+0

原因是您的代碼示例很難通過閱讀進行檢查,並且該過程可能會幫助您找到錯誤。 – millimoose

+0

雖然我認爲問題出現在第一個關於combat_engine功能的'if'聲明上,但我不太確定哪條線是誠實的。也許我誤解你了?吮吸是一個新生事物。 – lerugray

回答

1

在代碼的最後部分,您不稱爲函數d100(),但值爲d100。這當然不是你想要做的。

if d20() >= monster_to_hit and d100() <= monster_crit: 
     print "The %s scored a critical hit against you!" % monster 
     hit_points = hit_points - (monster_dmg * 3) 
     next() 
    elif d20 >= monster_to_hit and d100() > crit: 
     print "The %s strikes you!" 
     hit_points = hit_points - monster_dmg 
     next() 

在python中進行調試時,主要字是「print」。你應該儘可能地打印,以便了解發生了什麼。

例如:

# attack function 
def attack(victim): 
    dice20 = d20() 
    dice100 = d100() 

    print "victim == monster", victim == monster  

    print dice20 
    print dice100 
    print to_hit 
    print crit 
    print monster_current_hp 
    print mod_dmg 
    print mod_dmg * 3 
    print monster_to_hit 
    print monster_crit 
    print hit_points 
    print monster_dmg 
    print monster_dmg * 3 

    global monster_current_hp, current_hp, to_hit, crit, hit_points 
    if victim == monster: 
     if dice20 >= to_hit and dice100 <= crit: 
      print "You scored a critical hit against the %s!" % monster 
      monster_current_hp = monster_current_hp - (mod_dmg * 3) 
      next() 
     elif dice20 >= to_hit and dice100 > crit: 
      print "You strike the %s!" % monster 
      monster_current_hp = monster_current_hp - mod_dmg 
      next() 
     else: 
      print "The %s evades your attack!" % monster 
      next() 
    else: 
     if dice20 >= monster_to_hit and dice100 <= monster_crit: 
      print "The %s scored a critical hit against you!" % monster 
      hit_points = hit_points - (monster_dmg * 3) 
      next() 
     elif dice20 >= monster_to_hit and dice100 > crit: 
      print "The %s strikes you!" 
      hit_points = hit_points - monster_dmg 
      next() 

做同樣的戰鬥引擎,以便看到無限循環出現。然後刪除無用的打印文件,用無用的信息來擴充你的跟蹤,直到你清楚地看到它循環的原因,因爲有一些值,因爲布爾如果測試不符合你的預期......類似的東西。

+0

謝謝你注意到這一點。似乎沒有解決我的問題,但我完全錯過了。 – lerugray

+0

Python中debbuging的關鍵點是放置值的打印。在你們每個人的比較中,添加打印來理解背後的邏輯。我要編輯告訴你'調試'打印我會添加檢查痕跡。 –

+0

很酷,謝謝! – lerugray

2

if monster_current_hp or current_hp > 0:有兩種方式存在缺陷:第一,它只是測試monster_current_hp的計算結果是否爲true,而不是針對0進行測試;其次,即使這個問題得到解決,如果任何一個戰鬥機的HP值都是正值,那麼該聲明將會繼續執行該程序,而假設任何一個戰鬥機的HP值都是負值,則可能會停止。試試這個:if monster_current_hp > 0 and current_hp > 0:

一旦你這樣做,你會開始在大多數情況下到「錯誤的地方」行。這是因爲當惠普從正面走向負面時,elif monster_hp == 0:elif current_hp == 0:不會觸發。在這兩種情況下使用<=來捕捉HP爲負數的情況。