forked from bloominstituteoftechnology/Intro-Python-II
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroom.py
More file actions
54 lines (45 loc) · 1.31 KB
/
Copy pathroom.py
File metadata and controls
54 lines (45 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from item import Item
# Implement a class to hold room information. This should have name and
# description attributes.
class Room:
def __init__(self, name, description, items=[]):
self.__name = name
self.__description = description
self.items = [Item(item) for item in items]
self.n = None
self.e = None
self.w = None
self.s = None
def __str__(self):
return f"{self.__name}"
def __repr__(self):
return f"Room({self.__name})"
def assign_room(self, direction, room):
opposite_direction = {
's': 'n',
'n': 's',
'w': 'e',
'e': 'w'
}
setattr(self, direction, room)
setattr(room, opposite_direction[direction], self)
def get_room(self, direction):
return self[direction]
def get_description(self):
return self.__description
def __contains__(self, item):
for i in self.items:
if str(i) == item:
return True
def remove_item(self, item):
# slice up to index of item name
# concat the index after item
item_location = 0
for index in range(len(self.items)):
if str(self.items[index]) == item:
item_location = index
break
self.items = self.items[0:item_location] + self.items[item_location+1:]
def add_item(self, item):
item = Item(item)
self.items.append(item)