2017-08-29 11 views
0

私が修正しようとしているコードはPythonで作成されたzorkプログラムですが、別のコードにループバックしようとすると、デフォルトのアイドル。私のコードを試してみてください。私は本当にこの問題の助けが必要です。これらのコマンドを実行すると起こります。 は が上がるダウン 行く 東 移動敷物行く北 開いているウィンドウ 行くウィンドウ を入力し、北に行きます。Pythonは以前のループ(Zork)に戻りません

作業していないコードは、地下入り口のセクションにあります。

#This sets the commands to be used 
inv_command = ["inv", "inventory", "i"] 
west = ["west", "w", "go west", "walk west"] 
east = ["east", "e", "go east", "walk east"] 
north = ["north", "n", "go north", "walk north"] 
south = ["south", "s", "go south", "walk south"] 
northeast = ["northeast", "ne", "go northeast", "walk northeast"] 
northwest = ["northwest", "nw", "go northwest", "walk northwest"] 
southeast = ["southeast", "se", "go southeast", "walk southeast"] 
southwest = ["southwest", "sw", "go southwest", "walk southwest"] 
up = ["up", "u", "go up", "go up stairs", "go up ladder", "up stairs", "up ladder"] 
down = ["down", "d", "go down", "go down stairs", "go down ladder", "down stairs", "down ladder"] 
take = ["take", "t"] 

#This sets the objects 
mailbox = 1 
house_window = 0 
house_bag = 0 
knife = 0 
lamp = 0 
carpet = 0 
rug = 0 
light = 0 
#This sets the inventory 
class Item(object): 
    def __init__(self, name, attack, armour): 
     self.name = name 
     self.attack = attack 
     self.armour = armour 

class Inventory(object):  
    def __init__(self): 
     self.items = {} 

    def add_item(self, item): 
     self.items[item.name] = item 

    def contain_item(self, item): 
     self.items["Leaflet"] = item 

    def print_items(self): 
     print('\t'.join(['Name', 'Atk', 'Arm'])) 
     for item in self.items.values(): 
      print('\t'.join([str(x) for x in [item.name, item.attack, item.armour]])) 
inventory = Inventory() 

#This is the spawn area 
import string 
loop = 1 
while loop == 1: 
    inp = input("You wake up in a field surrounded by trees. The only pathway is going north \n>") 
    if inp in north: 
     loop = 2 
    elif inp in inv_command: 
     inventory.print_items() 
    else: 
     print("<You can't do that>") 

    #This is the front of the house 
    while loop == 2: 
     inp = input("You face the front of a house. The windows are boarded up and the door is closed. In front there is a mailbox, and pathways leading west and further north\n>") 
     if inp in south: 
      loop = 1 
     elif inp in inv_command: 
      inventory.print_items() 
     elif inp in north: 
      loop = 3 
     elif inp in west: 
      loop = 4 
     elif inp in "open door": 
      print("<The door is locked>") 
     elif inp in "open window": 
      print("<The windows can't be opened. They seem to be nailed shut>") 
     elif inp in "read leaflet": 
      if inventory.contain_item: 
       print("<Welcome to the text based adventure!>") 
       print("<When typing commands, please don't use capital letters.>") 
       print("<have fun!>") 
      else: 
       print("<You don't have a leaflet>") 
     elif inp in "open mailbox": 
      if mailbox == 1: 
       print("<The mailbox contains a leaflet>") 
      else: 
       print("<The mailbox is empty>") 
     elif inp in "take leaflet": 
      if mailbox == 1: 
       inventory.add_item(Item('Leaflet', 0, 0)) 
       mailbox = 0 
       print("<You have taken the leaflet>") 
      else: 
       print("<There's no leaflet in the mailbox>") 
     else: 
      print("<You can't do that>") 

     #This is the back of the house 
     while loop == 3: 
      inp = input("You face the back of the house, which has nothing except one ajar window. There is another pathway heading east \n>") 
      if inp in inv_command: 
       inventory.print_items() 
      if inp in south: 
       loop = 2 
      if inp in "open window": 
       house_window = 1 
       print("<You opened the window enough for you to enter through>") 
      if inp in "enter window": 
       if house_window == 1: 
        loop = 5 
       else: 
        print("<The window isn't opened enough to fit through>") 
      else: 
       print("<You can't do that>") 

      #This is the kitchen 
      while loop == 5: 
       inp = input("You are inside the kitchen of the house. On the table there is a bag, there is a staircase leading up stairs and to the east there is a living room \n>") 
       if inp in inv_command: 
        inventory.print_items() 
       elif inp in "read manual": 
        if inventory.contain_item: 
         print("<Commands - inventory, take, open, attack>") 
         print("<Moving commands - go north, go south, go northeast, go northwest, go southeast, go southwest, up, down.>") 
         print("<Good luck!>") 
       elif inp in up: 
        loop = 9 
       elif inp in east: 
        loop = 8 
       elif inp in "exit window": 
        loop = 3 
       elif inp == "open bag": 
        print("<The bag contains a manual>") 
       elif inp == "take bag": 
        print("<You would much rather take what's inside the bag then the bag itself>") 
       elif inp == "take manual": 
        if house_bag == 0: 
         print("<You took the manual>") 
         house_bag = 1 
         inventory.add_item(Item('manual', 0, 0)) 
        else: 
         print("<You have already taken the manual>") 
       else: 
        print("<You can't do that>") 
       #This is the living room 
       while loop == 8: 
        if rug == 0: 
         inp = input("You are in the living room and you see a rug on the floor.\n>") 
        if rug == 1: 
         inp = input("You are in the living room and you can see a trap door.\n>") 
        if inp == "move rug": 
         rug = 1 
         print("<You have found a trap door to the basement>") 
        if inp in down: 
         loop = 11 
        if inp in inv_command: 
         inventory.print_items() 
       else: 
        print("<You can't do that>") 

       #This is the Attic of the house 
       while loop == 9: 
        if knife == 0: 
         if lamp == 0: 
          inp = input("You are in the attic, you can see a lamp and a knife. You see blood stains and stairs downwards \n>") 
         else: 
          inp = input("You are in the attic, you can see a knife. You see blood stains and stairs downwards \n>") 
        elif knife == 1: 
         if lamp == 0: 
          inp = input("You are in the attic, you can see a lamp. You see blood stains and stairs downwards \n>") 
         else: 
          inp = input("You are in the attic. You see blood stains and stairs downwards \n>") 
        if inp in inv_command: 
         inventory.print_items() 
        elif inp in down: 
         loop = 5 

        elif inp in "take knife": 
         if knife == 0: 
          print("You pulled the knife out from the wall and put it in your pocket") 
          print("<You have obtain a knife>") 
          knife = 1 
          inventory.add_item(Item('knife', 3, 0)) 
         else: 
          print("<You have already taken the knife>") 
        elif inp in "take lamp": 
         if lamp == 0: 
          print("<You have taken the lamp>") 
          lamp = 1 
          inventory.add_item(Item('lamp', 0,0)) 
         else: 
          print("<You have already taken the lamp>") 
        else: 
         print("<You can't do that>") 
       #This is the underground entrance 
       while loop == 11: 
        if lamp == 1: 
         if light == 0: 
          inp = input("It is dark, you can't see anything.\n>") 
          if inp in up: 
           loop = 8 
          if inp in inv_command: 
           inventory.print_items() 
          if inp == "turn on lamp" or "turn lamp on": 
           print("<You turned on the lamp>") 
           light = 1 
         if light == 1: 
          inp = input("You are below the living room, where you can see a entrance to a underground tunnel north of you.\n>") 
          if inp in up: 
           loop = 8 
          if inp in inv_command: 
           inventory.print_items() 
          if inp in north: 
           loop = 12 
        if lamp == 0: 
         inp = input("It is dark, you can't see anything you need to find a lamp.\n>") 
         if inp in up: 
          loop = 8 
         if inp in inv_command: 
          inventory.print_items() 
        else: 
         print("<You can't do that>") 



     #This is the west forest entrance 
     while loop == 4: 
      inp = input("You are now in a dense forest. There is a fork in the road going NorthWest and SouthWest \n>") 
      if inp in inv_command: 
       inventory.print_items() 
      elif inp in east: 
       loop = 2 
      elif inp in northwest: 
       loop = 6 
      elif inp in southwest: 
       loop = 7 
      else: 
       print("<You can't do that>") 
+0

inp == "ランプを点灯"またはinp == "ランプを点灯"すれば大丈夫だと思います:正しい方法にしてください –

+0

甘いクリスマス...リファクタリングについて読んだら、たくさんの良い本があります。後で感謝します。がんばろう! – alfasin

答えて

0

あなたがここに実行している問題は、データ構造(位置が互いに接続されている方法を表現しようとしているということです)をプログラム構造体(whileループの束)として使用すると、うまくいきません。

あり、このプログラムに存在しなければならない一つのループが正確だし、それは非常にシンプルなものだ - 私は擬似コードでそれを表現ます:ゲーム状態

while the game is still running: 
    get a command 
    perform that command 

プロパティ - 含むをプレーヤーが置かれている - 変数として格納する必要があります。これらの場所に関する情報はオブジェクトとして保存する必要があります。 (例:Roomオブジェクトを持ち、それぞれの場所に1つのインスタンスがあり、その説明と内容のような標準的なプロパティを持つことを考慮してください)。これにより、プログラムをはるかに賢明で再現性の低い方法で構造化できます。

+0

私はそれを修正しようとしますが、いくつかのコードをどのようにbw構造化すべきかを示すために変換することができますありがとう –

0

あなたが持っている問題は、リビングルームでのダウンコマンドがあるときの位置を追跡し、あなたのloop変数は、サポートされていない値(10)に設定されていることです。

#This is the living room 
while loop == 8: 
    if rug == 0: 
     inp = input("You are in the living room and you see a rug on the floor.\n>") 
    if rug == 1: 
     inp = input("You are in the living room and you can see a trap door.\n>") 
    if inp in down: 
     loop = 10 

あなたのコードをナビゲートするのは困難ではありませんが、我々は部屋が処理されるかを確認するために、文字列while loop ==を検索することができます。

Line 50: while loop == 1: 
Line 60:  while loop == 2: 
Line 97:   while loop == 3: 
Line 115:    while loop == 5: 
Line 144:     while loop == 8: 
Line 162:     while loop == 9: 
Line 196:     while loop == 11: 
Line 227:   while loop == 4: 

のでloopが10に等しい場合、これらのwhileのどれも文が真となるので、コードは実行を終了します。ゲームの状態を印刷するプログラムの最後に、これらの機会のための行を置くことができます。

これは地下入り口の正しい場所であるので、直下の問題を解決するはずのライン196をwhile loop == 10:に変更すると、196以降のコードが表示されます。

ただし、コードを再構成してリファクタリングすると、長期的に役立ちます。または、おそらくInformまたはTADSのようなより適切なプラットフォームを考えてみてください。

編集:上記の問題を修正するための変更に引き続き、地下室から北へ行くのに問題があります。 loop12に設定されていますが、そのような部屋は定義されていません。あなたがloop =を検索することができ、これらの部屋の遷移やイベントのすべてを確認するには:

Line 49: loop = 1 
Line 50: while loop == 1: 
Line 53:   loop = 2 
Line 60:  while loop == 2: 
Line 63:    loop = 1 
Line 67:    loop = 3 
Line 69:    loop = 4 
Line 97:   while loop == 3: 
Line 102:     loop = 2 
Line 108:      loop = 5 
Line 115:    while loop == 5: 
Line 125:      loop = 9 
Line 127:      loop = 8 
Line 129:      loop = 3 
Line 144:     while loop == 8: 
Line 153:       loop = 11 
Line 160:     while loop == 9: 
Line 174:       loop = 5 
Line 194:     while loop == 11: 
Line 199:         loop = 8 
Line 208:         loop = 8 
Line 212:         loop = 12 
Line 216:        loop = 8 
Line 225:   while loop == 4: 
Line 230:     loop = 2 
Line 232:     loop = 6 
Line 234:     loop = 7 
+0

リビングルームに戻ってループが戻ってこないということを除けば、私はあなたに感謝したいと思います –

+0

地下のトンネルにいて、ブレーキをかけて、しかし、私は助けが必要なこと、私は私のコードでこの間違いを見なかったので、それに感謝します。 –

関連する問題