-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameSetup.py
224 lines (192 loc) · 6.46 KB
/
gameSetup.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
218
219
220
221
222
223
224
from gameEngine import Player, Room, Item, Door, Shelf, Trap
from testGame import TestGame
import pickle
import json
import os
MAX_MOVES = 30
class GameComponents:
def __init__(self):
self.basePath = "./InitGame/"
self.items = self.loadItems()
self.traps = self.loadTraps()
self.rooms = self.loadRooms()
self.player = Player(self.getRoom("helicopterPad"), MAX_MOVES)
self.getDoorDestinations()
self.getItemDestinations()
self.getTrapDestinations()
# @param: directory - the name of the directory to get all files in
#
# returns: list of files in parsed directory. Will return empty list if error occurs
def getFilesInDir(self, directory):
path = self.basePath + "{directory}/".format(directory=directory)
if os.path.exists(path):
files = []
try:
files = os.listdir(path)
except:
print("Error: Could not read files from path: {path}.".format(path=path))
return files
else:
print("Error: No such path: {path}.".format(path=path))
exit()
# @param: directory - directory file is in
# @param: filename - the name of the file to retreive object from
#
# returns: object loaded from file or 'None' if error occurs
def getFileObj(self, directory, filename):
path = self.basePath + "{directory}/{fname}".format(directory=directory, fname=filename)
if os.path.exists(path):
try:
with open(path, 'r') as data_file:
return json.load(data_file)
except:
print("Error: Could not load from {path}.".format(path=path))
else:
print("Error: No such path: {path}.".format(path=path))
return None
# @param: None
#
# returns: dict of instantiated Room objects for every room file stored in the rooms directory
def loadRooms(self):
roomDict = {}
room_files = self.getFilesInDir("Rooms")
for filename in room_files:
room = self.getFileObj("Rooms", filename)
if room:
key = room["key"]
name = room["name"]
desc = room["longDesc"]
shortDesc = room["shortDesc"]
roomDict[key] = Room(name, desc, shortDesc)
for item in room["items"]["floor"]:
if self.items[item]:
itemObj = self.items[item]
roomDict[key].add_item(itemObj)
else:
print("Error: {item} does not exist.".format(item=item))
for shelf in room["items"]["shelves"]:
sName = shelf["name"]
sDesc = shelf["desc"]
sLocked = True if shelf["locked"] == "True" else False
sLockVal = shelf["lock_val"] if shelf["lock_val"] != "" else None
contents = shelf["contents"]
shelfObj = Shelf(sName, sDesc, sLocked, sLockVal)
for item in contents:
if self.items[item]:
itemObj = self.items[item]
shelfObj.add_item(itemObj)
else:
print("Error: {item} does not exist.".format(item=item))
roomDict[key].add_shelf(shelfObj)
for door in room["doors"]:
dName = door["name"]
dDesc = door["desc"]
unlock_desc = door["unlock_desc"] if door["unlock_desc"] != "" else None
direction = door["direction"]
lock_val = door["lock_val"] if door["lock_val"] != "" else None
destination = door["neighbor"]
locked = True if door["locked"] == "True" else False
doorObj = Door(dName, dDesc, direction, destination, locked, lock_val, unlock_desc)
roomDict[key].add_door(doorObj)
for trap in room["traps"]:
if self.traps[trap]:
trapObj = self.traps[trap]
roomDict[key].add_trap(trapObj)
else:
print("Error: {trap} does not exist.".format(trap=trap))
return roomDict
# @param: None
#
# returns: dict of instantiated Item objects for every item file stored in the items directory
def loadItems(self):
itemDict = {}
item_files = self.getFilesInDir("Items")
for filename in item_files:
item = self.getFileObj("Items", filename)
if item:
name = item["name"]
desc = item["desc"]
key = item["key_val"]
lock = item["lock_val"] if item["lock_val"] != "" else None
trans_id = item["trans_id"] if item["trans_id"] != "" else None
trap_desc = item["trap_desc"] if item["trap_desc"] != "" else None
destination = item["destination"] if item["destination"] != "" else None
itemDict[key] = Item(name, desc, key, lock, trans_id, trap_desc, destination)
for prop, item in itemDict.items():
if item._TransID:
item_key = item._TransID
item._TransID = itemDict[item_key]
return itemDict
# @param: None
#
# returns: dict of instantiated Trap objects for every trap file stored in the traps directory
def loadTraps(self):
trapDict = {}
trap_files = self.getFilesInDir("Traps")
for filename in trap_files:
trap = self.getFileObj("Traps", filename)
if trap:
name = trap["name"]
desc = trap["desc"]
s_desc = trap["s_desc"]
lock_val = trap["lock_val"] if trap["lock_val"] != "" else None
d_desc = trap["d_desc"]
destination = trap["destination"] if trap['destination'] != "" else None
trapDict[name] = Trap(name, desc, s_desc, lock_val, d_desc, destination)
return trapDict
# @param: room_name - name of room to return
#
# returns: room if it exists, else 'None'
def getRoom(self, room_key):
try:
return self.rooms[room_key]
except:
return None
def getItem(self, item_key):
try:
return self.items[item_key]
except:
return None
def getTrap(self, trap_key):
try:
return self.traps[trap_key]
except:
return None
def getDoorDestinations(self):
for room in list(self.rooms):
roomObj = self.rooms[room]
for door in list(roomObj.Doors):
neighbor_key = door.Destination
neighbor = self.getRoom(neighbor_key)
door.Destination = neighbor
def getItemDestinations(self):
for item in list(self.items):
itemObj = self.items[item]
if itemObj.Destination:
room = itemObj.Destination
itemObj.Destination = self.rooms[room]
def getTrapDestinations(self):
for trap in list(self.traps):
trapObj = self.traps[trap]
if trapObj.Destination:
room = trapObj.Destination
trapObj.Destination = self.traps[trap]
def saveGame(self, game):
directory = "./SavedGame"
path = "{directory}/Game.save".format(directory=directory)
if not os.path.exists(directory):
os.makedirs(directory)
pickle.dump(game, open(path, 'wb'))
cont_game = 1
while cont_game:
gameState = input("Enter 'loadgame' or 'savegame': ")
directory = "./SavedGame"
path = "{directory}/Game.save".format(directory=directory)
if gameState == "loadgame":
game = pickle.load(open(path, 'rb'))
else:
game = GameComponents()
if gameState == "savegame":
game.saveGame(game)
tester = TestGame(game)
cont_game = tester.main()