1

そのため、このコードの問題を解決するためにしばらく取り組んできましたが、解決できないようです。どの方向に移動しようとしても、キッチンで立ち往生していると表示されます。現在の部屋の印刷行を追加しました。これは、プログラムがそれを他の部屋に移動していることを示していますが、正しい説明が印刷されていません。

どんな助けでも素敵です!

事前に感謝します。

class Room():
        room = []
        def __init__ ( self, desc, north , east , west , south, northwest, southwest, northeast, southeast):
            self.room.append(desc)
            self.room.append(north)
            self.room.append(east)
            self.room.append(west)
            self.room.append(south)
            self.room.append(northwest)
            self.room.append(southwest)
            self.room.append(northeast)
            self.room.append(southeast)
        def get_Desc(self):
            return self.room[0]
        def get_North(self):
            return self.room[1]
        def get_East(self):
            return self.room[2]
        def get_West(self):
            return self.room[3]
        def get_South(self):
            return self.room[4]
        def get_Northwest(self):
            return self.room[5]
        def get_Southwest(self):
            return self.room[6]
        def get_Northeast(self):
            return self.room[7]
        def get_Southeast(self):
            return self.room[8]



#Object Class
class Object():
    name = ""
    description = ""
    room = None
    def __init__ (self, newname, desc, newroom):
        self.name = newname
        self.description = desc
        self.room = newroom
    def get_Name():
        return name
    def set_Name(self , name):
        self.name = name
    def get_Desc(self):
        return description
    def set_Desc(self , description):
        self.description = description
    def get_Room(self):
        return self.description
    def set_Room(self, room):
        self.room = room




#This is a list of Objects in game
p_items = []
puppy = Object("Puppy", "OMG ROFL, LMAO, ITS CUTE AS ALL BALLS, GRAB IT AND SQUEEZE IT!!!", 2)
p_items.append(puppy)
Inventory = []
#This is a list of Rooms
room_list = []
room_1 = Room("This is the kitchen - 1. There is a passage to the North, East, and North East.", 3, 2, None, None , None, None, 7, None)
room_list.append (room_1)
room_2 = Room("This is the dining room - 2. There is a passage to the North, North East, and North West", 7, None, 1, None, 3, None, 6, None)
room_list.append(room_2)
room_3 = Room("This is the West hallway -3. There is a passage to the North, East, West, North East, North West, South East, and South West.", 4, 7, 1, None, 4, 1, 5, 2)
room_list.append(room_3)
room_4 = Room("This is the bedroom - 4. There is a passage to the East, West, and Southeast.", None, 5, 3, None, None, None, None, 7)
room_list.append(room_4)
room_5 = Room("This is the office -5. There is a passage to the South, West, Southwest, Northwest, and Northeast .", None, None, 7, 4 , 3, None, None, 6)
room_list.append(room_5)
room_6 = Room("This is the balcony. There is a passage to the West, North West, and South West.", None, None, None, 7, 5, 2, None, None )
room_list.append(room_6)
room_7 = Room("This is the East hallway. There is a passage to the North, South, and West, North East, North West, South East, and South West.", 5, None, 2, 3, 4, 1, 5, 2)
room_list.append(room_7)

current_room = 3
done = False
while not done:
    print (current_room)
    print ((room_list[current_room]).get_Desc()) 
    for object in p_items:
        if object.get_Room() == current_room:
            print("There is an Object in this room : " + object.get_Name() + " - " + object.get_Desc())
    answer = input("Which direction would you like to go? \n N, E, S, W, NE, SE, NW, SW, O to open Inventory, P to pick up an item, or Q to quit ").lower()
    if answer == ("q"):
        done = True
    elif answer == ("n") and room_list[current_room].get_North() != None:
        current_room = room_list [current_room].get_North() 
    elif answer == ("e") and room_list[current_room].get_East() != None:
        current_room = room_list[current_room].get_East() 
    elif answer == ("w") and room_list[current_room].get_West() !=None:
        current_room = room_list[current_room].get_West() 
    elif answer == ("s") and room_list[current_room].get_South() !=None:
        curent_room = room_list[current_room].get_South() 
    elif answer == ("nw") and room_list[current_room].get_Northwest() !=None:
        current_room = room_list[current_room].get_Northwest() 
    elif answer == ("sw") and room_list[current_room].get_Southwest() !=None:
        curent_room = room_list[current_room].get_Southwest() 
    elif answer == ("ne") and room_list[current_room].get_Northeast() !=None:
        current_room = room_list[current_room].get_Northeast() 
    elif answer == ("se") and room_list[current_room].get_Southeast() !=None:
        curent_room = room_list[current_room].get_Souteasth() 
    elif answer == ("o"):
        print ("\nYou have the following objects in your Inventory:")
        for object in inventory:
            print ( "-" + object.get_Name() + " ; " + object.get_Desc());
    elif answer == ("p"):
        print ("\nThere are these items in this room:")
        for object in inventory:
            if object.get_Room() == current_room:
                print ( "-" + object.get_Name() + " ; " + object.get_Desc());                
                pu = input("Would you like to pick it up? (y/n): ").lower()
                if  pu == "y":
                    object.set_Room(666)
                    inventory.append(object)
                elif pu == "n":
                    print ( "Dont be dumb, pick it up!")
                else:
                    print ("That is not a valid option")  

                    print ("That is not a valid option")

    else:
        print ("\nThat is not a valid option!\n") 
    print (answer)
    print (current_room)


 #No quiting allowed everrrrr
4

1 に答える 1

3

問題の原因はRoomクラスです。

class Room:
    room = []

この行は、room をクラス レベルのリスト (静的変数) にします。したがって、Room の新しいインスタンスを作成するときはいつでも、このクラス レベルのリストにデータを追加しますが、最初のインデックスはゲーム ロジックによってアクセスされます。

def get_Desc(self):
    return self.room[0]

etc...

常に同じ部屋の値になります。

メソッド内で作成すると、リストはクラスレベルではなくなり、問題は修正されます。

class Room():
    def __init__(self, desc, north, east, west, south, northwest, southwest, northeast, southeast):
        self.room = []
于 2015-12-09T10:28:08.217 に答える