-
Notifications
You must be signed in to change notification settings - Fork 0
/
rooms.py
217 lines (173 loc) · 5.86 KB
/
rooms.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import colors
class Room:
"""
Attributes
----------
name: string
name of the room
description: string
description of the room
doors: string list
list of directions (n,s,e,w)
"""
def __init__(self, name, desc):
"""
Make a room
"""
self.name = name
self.description = desc
self.items = {}
self.doors = {} # direction: new room name
self.hidden_doors = {} # direction: new room name
self.locked = {} # direction: name of item that unlocks it
self.message = {} # direction : locked message
self.people = []
def __str__(self):
return "{}\n=====\n{}\nItems: {}\nDoors: {}\n" \
"Hidden Doors: {}\nLockedDoors: {}\nPeople: {}\n" \
"".format(self.name, self.description, self.items, self.doors,
self.hidden_doors, self.locked, self.people)
def add_neighbor(self, neighbor_name, direction, locked, unlock, hidden, message):
if hidden:
self.hidden_doors[direction] = neighbor_name
else:
self.doors[direction] = neighbor_name
if locked:
self.locked[direction] = unlock
self.message[direction] = message
def delete_neighbor(self, direction):
if direction in self.doors:
self.doors.pop(direction)
if direction in self.locked:
self.locked.pop(direction)
if direction in self.message:
self.message.pop(direction)
# def add_hidden_neighbor(self, neighbor_name, direction):
# self.hidden_doors[direction] = neighbor_name
def add_item(self, name, desc=""):
new_item = Item(name, desc)
self.items[name] = new_item
def add_gold(self, amt):
gold = Gold(amt)
self.items["gold coin"] = gold
# TODO: Add find doors
class Map:
def __init__(self, rooms=None):
"""
Make a map
"""
if not rooms:
rooms = {} # name : room object
self.rooms = rooms
def add_room(self, name, desc=""):
new_room = Room(name, desc)
self.rooms[name] = new_room
def get_room(self, name):
if name in self.rooms.keys():
return self.rooms[name]
def add_trap_door(self, fr, to, direction, locked=False, unlock=None, hidden=False, message="The door is locked."):
"""
A directed edge between rooms
"""
if self.get_room(fr) is None:
self.add_room(fr)
if self.get_room(to) is None:
self.add_room(to)
self.get_room(fr).add_neighbor(to, direction, locked, unlock, hidden, message)
def delete_trap_door(self, fr, to, direction):
"""
A directed edge between rooms
"""
# if self.get_room(fr) is None:
# self.add_room(fr)
# if self.get_room(to) is None:
# self.add_room(to)
self.get_room(fr).delete_neighbor(direction)
def add_hidden_trap_door(self, fr, to, direction):
"""
A directed edge between rooms
"""
if self.get_room(fr) is None:
self.add_room(fr)
if self.get_room(to) is None:
self.add_room(to)
self.get_room(fr).add_hidden_neighbor(to, direction)
def add_door(self, id1, id2, direction, hidden=False, locked=False, unlock="", message="The door is locked."):
mirror = {"e": "w", "n": "s", "u": "d"}
mirror.update(dict((v, k) for (k, v) in mirror.items()))
self.add_trap_door(id1, id2, direction, locked, unlock, hidden, message)
self.add_trap_door(id2, id1, mirror[direction], hidden)
def add_hidden_door(self, id1, id2, direction):
mirror = {"e": "w", "w": "e", "n": "s", "s": "n", "u": "d", "d": "u"}
self.add_hidden_trap_door(id1, id2, direction)
self.add_trap_door(id2, id1, mirror[direction])
def walk(self, fr, direction, i):
keycard = 0
for item in i:
if item in fr.locked.values():
keycard = 1
if direction in fr.locked.keys() and not keycard:
print(fr.message[direction])
elif direction in fr.doors.keys():
new_room = self.get_room(fr.doors[direction])
return new_room
elif direction in fr.hidden_doors.keys():
new_room = self.get_room(fr.hidden_doors[direction])
return new_room
else:
print("You can't go that way.")
class NPC:
"""
Attributes
----------
name: string
name of the person
"""
def __init__(self, name, desc):
"""
Make a room
"""
self.name = name
self.description = desc
self.questions = []
self.answers = []
def ask(self):
for q in self.questions:
print(q)
ans = input("What do you want to ask? ")
print("\n" + colors.pink + self.name + " replies: ")
print(self.answers[int(ans) - 1])
print("\n")
class Item:
"""
Attributes
----------
name: string
name of the person
"""
def __init__(self, name, desc):
"""
Make an item
"""
self.name = name
self.description = desc
def __str__(self):
return "{}\n=====\n{}\n".format(self.name, self.description)
class Gold(Item):
def __init__(self, amt):
self.amt = amt
super().__init__(name="Gold",
desc="A round coin with {} stamped on the front.".format(str(self.amt)))
class Player:
def __init__(self, items=None):
"""
Make an item
"""
self.inventory = {}
self.turns = 0
def get_item(self, name):
if name in self.inventory.keys():
return self.inventory[name]
def add_item(self, name):
new_item = self.get_item(name)
self.inventory[name] = new_item