Home > Software design >  How can I make the objects appear in the intended inventory?
How can I make the objects appear in the intended inventory?

Time:04-04

For this code, it has a number of rooms and in each room, there are certain items and grabbables that you can put into the person's inventory when you use the action look. However when I run my code and use the look action it doesn't place the object into the inventory, it just leaves it blank. The other functions such as go east, or go west work. I tried checking my indentations, although the indents on here are a little weird, but I can't figure out what the problem is

Here's my code:

#the blueprint for a room

class Room:

def __init__(self, name):
    self.name = name
    self. exits = []
    self.exitLocations = []
    self.items = []
    self.itemDescriptions = []
    self.grabbables = []

#getter and setters for the instance variables      

@property
def name(self):
    return self._name

@name.setter
def name(self, value):
    self._name = value

@property
def exits(self):
    return self._exits

@exits.setter
def exits(self, value):
    self._exits = value

@property
def exitLocations(self):
    return self._exitLocations

@exitLocations.setter
def exitLocations(self, value):
    self._exitLocations = value

@property
def items(self):
    return self._items

@items.setter
def items(self, value):
    self._items = value

@property
def itemDescriptions(self):
    return self._itemDescriptions

@itemDescriptions.setter
def itemDescriptions(self, value):
    self._itemDescriptions = value

@property
def grabbables(self):
    return self._grabbables

@grabbables.setter
def grabbables(self, value):
    self._grabbables = value
    
#adds an exit to the room
#the exit is a string (e.g., north)
#the room is an instance of a room

def addExit(self, exit, room):
    self._exits.append(exit)
    self._exitLocations.append(room)
    
#adds an item to the room

#the item is a string (e.g., table)

#the desc is a string that describes the item

def addItem(self, item, desc):
    self._items.append(item)
    self._itemDescriptions.append(desc)

#adds a grabbable item to the room

#the item is a string (e.g., key)

def addGrabbable(self, item):
    self._grabbables.append(item)

#removes a grabbable item from the room

def delGrabbable(self, item):
    self._grabbables.remove(item)
    
def __str__(self):
    s = "You are in {}.\n".format(self.name)
    s  = "You see: "
    for item in self.items:
        s  = item   " "
    s  = "\n"
    s  = "Exits: "
    for exit in self.exits:
        s  = exit   " "
    return s

#creates the rooms

def createRooms():
global currentRoom

#create the rooms and give them meaningful names

r1 = Room("Room 1")
r2 = Room("Room 2")
r3 = Room("Room 3")
r4 = Room("Room 4")
r5 = Room("Room 5")
r6 = Room("Room 6")

#add exits to room 1

r1.addExit("east", r2) 
r1.addExit("south", r3)

#add grabbables to room 1

r1.addGrabbable("key")

#add items to room 1

r1.addItem("chair", "It is made of wicker and no one is sitting on it.")
r1.addItem("table", "It is made of oak. A golden key rests on it.")

#add exits to room 2

r2.addExit("west", r1)
r2.addExit("south", r4)

#add items to room 2

r2.addItem("rug", "It is nice and Indian. It also needs to be vacuumed.")
r2.addItem("fireplace", "It is full of ashes.")

#add exits to room 3

r3.addExit("north", r1)
r3.addExit("east", r4)

#add grabbables to room 3

r3.addGrabbable("book")

#add items to room 3

r3.addItem("bookshelves", "They are empty. Go figure.")
r3.addItem("statue", "There is nothing special about it.")
r3.addItem("desk", "The statue is resting on it. So is a book.")

#add exits to room 4

r4.addExit("north", r2)
r4.addExit("west", r3)
r4.addExit("south", None) # DEATH!

#add grabbables to room 4

r4.addGrabbable("6-pack")

#add items to room 4

r4.addItem("brew_rig", "Gourd is brewing some sort of oatmeal stout on the brew rig. A 6-pack is resting beside it.")

#add exits to room 5

r5.addExit("north", r3)
r5.addExit("south", r6)

#add grabbables to room 5

r5.addGrabbable("note")
r5.addGrabbable("old photo")

#add items to room 5

r5.addItem("dresser", "It is mahogany. A note resides in one of the drawers.")
r5.addItem("bed", "It is covered by a white bed canopy. On the bed is an old photo of a couple.")

#add exits to room 6

r6.addExit("east", r2)
r6.addExit("west", r3)
r6.addExit("north", r5)

#add grabbables to room 6

r6.addGrabbable("candle")
       
#add items to room 6  

r6.addItem("nightstand", "It is covered in dust. On top of it is a candle.")
r6.addItem("picture_frame", "A giant picture covers the left side of the wall. Its a picture of a family.")

#set room 1 as the current room at the beginning of the game
currentRoom = r1

#####################################################
#START THE GAME!!!!

inventory = []
createRooms()

#play forever (well, at least until the player dies or asks to quit)

while (True):
status = "{}\nYou are carrying: {}\n".format(currentRoom, inventory)
if (currentRoom == None):
    status = "You are dead."
print("========================================================")
print(status)
if (currentRoom == None):
    death()
    break
action = input("What to do? ")
action = action.lower()
if (action == "quit" or action == "exit" or action == "bye"):
    break

#set a default response

response = "I don't understand. Try verb noun. Valid verbs are go, look, and take"

#split the user input into words (words are separated by spaces)

words = action.split()

#the game only understands two word inputs

if (len(words) == 2):
    verb = words[0]
    noun = words[1]
    if (verb == "go"):
        response = "Invalid exit."

        for i in range(len(currentRoom.exits)):
            if (noun == currentRoom.exits[i]):
                currentRoom = currentRoom.exitLocations[i]
                response = "Room changed."
                break
            
#the verb is: look

    elif (verb == "look"):
        response = "I don't see that item."
        for i in range(len(currentRoom.items)):
            if (noun == currentRoom.items[i]):
                response = currentRoom.itemDescriptions[i]
                break   
#the verb is: take

    elif (verb == "take"):
        response = "I don't see that item."
        for grabbable in currentRoom.grabbables:
            if (noun == grabbable):
                inventory.append(grabbable)
                currentRoom.delGrabbable(grabbable)
                response = "Item grabbed."
                break
    
#display the response

print("\n{}".format(response))

CodePudding user response:

I've tried to run it and in this version it works (at least for me :) )

class Room:


    def __init__(self, name):
        self.name = name
        self.exits = []
        self.exitLocations = []
        self.items = []
        self.itemDescriptions = []
        self.grabbables = []


    # getter and setters for the instance variables

    @property
    def name(self):
        return self._name


    @name.setter
    def name(self, value):
        self._name = value


    @property
    def exits(self):
        return self._exits


    @exits.setter
    def exits(self, value):
        self._exits = value


    @property
    def exitLocations(self):
        return self._exitLocations


    @exitLocations.setter
    def exitLocations(self, value):
        self._exitLocations = value


    @property
    def items(self):
        return self._items


    @items.setter
    def items(self, value):
        self._items = value


    @property
    def itemDescriptions(self):
        return self._itemDescriptions


    @itemDescriptions.setter
    def itemDescriptions(self, value):
        self._itemDescriptions = value


    @property
    def grabbables(self):
        return self._grabbables


    @grabbables.setter
    def grabbables(self, value):
        self._grabbables = value


    # adds an exit to the room
    # the exit is a string (e.g., north)
    # the room is an instance of a room

    def addExit(self, exit, room):
        self._exits.append(exit)
        self._exitLocations.append(room)


    # adds an item to the room

    # the item is a string (e.g., table)

    # the desc is a string that describes the item

    def addItem(self, item, desc):
        self._items.append(item)
        self._itemDescriptions.append(desc)


    # adds a grabbable item to the room

    # the item is a string (e.g., key)

    def addGrabbable(self, item):
        self._grabbables.append(item)


    # removes a grabbable item from the room

    def delGrabbable(self, item):
        self._grabbables.remove(item)


    def __str__(self):
        s = "You are in {}.\n".format(self.name)
        s  = "You see: "
        for item in self.items:
            s  = item   " "
        s  = "\n"
        s  = "Exits: "
        for exit in self.exits:
            s  = exit   " "
        return s


# creates the rooms

def createRooms():
    global currentRoom


# create the rooms and give them meaningful names

r1 = Room("Room 1")
r2 = Room("Room 2")
r3 = Room("Room 3")
r4 = Room("Room 4")
r5 = Room("Room 5")
r6 = Room("Room 6")

# add exits to room 1

r1.addExit("east", r2)
r1.addExit("south", r3)

# add grabbables to room 1

r1.addGrabbable("key")

# add items to room 1

r1.addItem("chair", "It is made of wicker and no one is sitting on it.")
r1.addItem("table", "It is made of oak. A golden key rests on it.")

# add exits to room 2

r2.addExit("west", r1)
r2.addExit("south", r4)

# add items to room 2

r2.addItem("rug", "It is nice and Indian. It also needs to be vacuumed.")
r2.addItem("fireplace", "It is full of ashes.")

# add exits to room 3

r3.addExit("north", r1)
r3.addExit("east", r4)

# add grabbables to room 3

r3.addGrabbable("book")

# add items to room 3

r3.addItem("bookshelves", "They are empty. Go figure.")
r3.addItem("statue", "There is nothing special about it.")
r3.addItem("desk", "The statue is resting on it. So is a book.")

# add exits to room 4

r4.addExit("north", r2)
r4.addExit("west", r3)
r4.addExit("south", None)  # DEATH!

# add grabbables to room 4

r4.addGrabbable("6-pack")

# add items to room 4

r4.addItem("brew_rig", "Gourd is brewing some sort of oatmeal stout on the brew rig. A 6-pack is resting beside it.")

# add exits to room 5

r5.addExit("north", r3)
r5.addExit("south", r6)

# add grabbables to room 5

r5.addGrabbable("note")
r5.addGrabbable("old photo")

# add items to room 5

r5.addItem("dresser", "It is mahogany. A note resides in one of the drawers.")
r5.addItem("bed", "It is covered by a white bed canopy. On the bed is an old photo of a couple.")

# add exits to room 6

r6.addExit("east", r2)
r6.addExit("west", r3)
r6.addExit("north", r5)

# add grabbables to room 6

r6.addGrabbable("candle")

# add items to room 6

r6.addItem("nightstand", "It is covered in dust. On top of it is a candle.")
r6.addItem("picture_frame", "A giant picture covers the left side of the wall. Its a picture of a family.")

# set room 1 as the current room at the beginning of the game
currentRoom = r1


# death function

def death():
    print(" " * 17   "u" * 7)


print(" " * 13   "u" * 2   "$" * 11   "u" * 2)
print(" " * 10   "u" * 2   "$" * 17   "u" * 2)
print(" " * 9   "u"   "$" * 21   "u")
print(" " * 8   "u"   "$" * 23   "u")
print(" " * 7   "u"   "$" * 25   "u")
print(" " * 7   "u"   "$" * 25   "u")
print(" " * 7   "u"   "$" * 6   "\""   " " * 3   "\""   "$" * 3   "\""   " " * 3   "\""   "$" * 6   "u")
print(" " * 7   "\""   "$" * 4   "\""   " " * 6   "u$u"   " " * 7   "$" * 4   "\"")
print(" " * 8   "$" * 3   "u"   " " * 7   "u$u"   " " * 7   "u"   "$" * 3)
print(" " * 8   "$" * 3   "u"   " " * 6   "u"   "$" * 3   "u"   " " * 6   "u"   "$" * 3)
print(" " * 9   "\""   "$" * 4   "u" * 2   "$" * 3   " " * 3   "$" * 3   "u" * 2   "$" * 4   "\"")
print(" " * 10   "\""   "$" * 7   "\""   " " * 3   "\""   "$" * 7   "\"")
print(" " * 12   "u"   "$" * 7   "u"   "$" * 7   "u")
print(" " * 13   "u$\"$\"$\"$\"$\"$\"$u")
print(" " * 2   "u" * 3   " " * 8   "$" * 2   "u$ $ $ $ $u"   "$" * 2   " " * 7   "u" * 3)
print(" u"   "$" * 4   " " * 8   "$" * 5   "u$u$u"   "$" * 3   " " * 7   "u"   "$" * 4)
print(" " * 2   "$" * 5   "u" * 2   " " * 6   "\""   "$" * 9   "\""   " " * 5   "u" * 2   "$" * 6)
print("u"   "$" * 11   "u" * 2   " " * 4   "\"" * 5   " " * 4   "u" * 4   "$" * 10)
print("$" * 4   "\"" * 3   "$" * 10   "u" * 3   " " * 3   "u" * 2   "$" * 9   "\"" * 3   "$" * 3   "\"")
print(" "   "\"" * 3   " " * 6   "\"" * 2   "$" * 11   "u" * 2   " "   "\"" * 2   "$"   "\"" * 3)
print(" " * 11   "u" * 4   " \"\""   "$" * 10   "u" * 3)
print(" " * 2   "u"   "$" * 3   "u" * 3   "$" * 9   "u" * 2   " \"\""   "$" * 11   "u" * 3   "$" * 3)
print(" " * 2   "$" * 10   "\"" * 4   " " * 11   "\"\""   "$" * 11   "\"")
print(" " * 3   "\""   "$" * 5   "\""   " " * 22   "\"\""   "$" * 4   "\"\"")
print(" " * 5   "$" * 3   "\""   " " * 25   "$" * 4   "\"")

#####################################################
# START THE GAME!!!!

def start():
    global currentRoom
    inventory = []
    createRooms()

    # play forever (well, at least until the player dies or asks to quit)

    while True:
        status = "{}\nYou are carrying: {}\n".format(currentRoom, inventory)
        if currentRoom == None:
            status = "You are dead."
        print("========================================================")
        print(status)
        if currentRoom == None:
            death()
            break
        action = input("What to do? ")
        action = action.lower()
        if action == "quit" or action == "exit" or action == "bye":
            break

        # set a default response

        response = "I don't understand. Try verb noun. Valid verbs are go, look, and take"

        # split the user input into words (words are separated by spaces)

        words = action.split()

        # the game only understands two word inputs

        if (len(words) == 2):
            verb = words[0]
            noun = words[1]
            if (verb == "go"):
                response = "Invalid exit."

                for i in range(len(currentRoom.exits)):
                    if (noun == currentRoom.exits[i]):
                        currentRoom = currentRoom.exitLocations[i]
                        response = "Room changed."
                        break

            # the verb is: look

            elif (verb == "look"):
                response = "I don't see that item."
                for i in range(len(currentRoom.items)):
                    if (noun == currentRoom.items[i]):
                        response = currentRoom.itemDescriptions[i]
                        break
                    # the verb is: take

            elif (verb == "take"):
                response = "I don't see that item."
                for grabbable in currentRoom.grabbables:
                    if (noun == grabbable):
                        inventory.append(grabbable)
                        currentRoom.delGrabbable(grabbable)
                        response = "Item grabbed."
                        break

        # display the response

        print("\n{}".format(response))

        
if __name__ == "__main__":
    start()

I followed to the room where 'bookshelves' and 'desk' are located and performed 'take book' - it was added to my inventory as expected.

You are in Room 3. You see: bookshelves statue desk Exits: north east You are carrying: []

What to do? look desk

The statue is resting on it. So is a book. ======================================================== You are in Room 3. You see: bookshelves statue desk Exits: north east You are carrying: []

What to do? take book

Item grabbed. ======================================================== You are in Room 3. You see: bookshelves statue desk Exits: north east You are carrying: ['book']

What to do?

  • Related