-
Notifications
You must be signed in to change notification settings - Fork 1
/
maze.py
397 lines (335 loc) · 10.7 KB
/
maze.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
try:
from collections import defaultdict
except:
class defaultdict(dict):
def __init__(self, default_factory=None, *a, **kw):
if (default_factory is not None and
not hasattr(default_factory, '__call__')):
raise TypeError('first argument must be callable')
dict.__init__(self, *a, **kw)
self.default_factory = default_factory
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
return self.__missing__(key)
def __missing__(self, key):
if self.default_factory is None:
raise KeyError(key)
self[key] = value = self.default_factory()
return value
def __reduce__(self):
if self.default_factory is None:
args = tuple()
else:
args = self.default_factory,
return type(self), args, None, None, self.items()
def copy(self):
return self.__copy__()
def __copy__(self):
return type(self)(self.default_factory, self)
def __deepcopy__(self, memo):
import copy
return type(self)(self.default_factory,
copy.deepcopy(self.items()))
def __repr__(self):
return 'defaultdict(%s, %s)' % (self.default_factory,
dict.__repr__(self))
RED, YELLOW, BLUE, NO_FLOOR = 0, 1, 2, 3
def enum_matrix(mat):
for y, row in enumerate(mat):
for x, val in enumerate(row):
yield x, y, val
class Location(object):
def __init__(self, x, y):
self.x = x
self.y = y
def same_location(a, b):
return a.x == b.x and a.y == b.y
#
# Maze stuff
#
class Maze(object):
@classmethod
def fromJSON(cls, json_maze):
maze = Maze()
maze.height = len(json_maze["floors"])
maze.width = len(json_maze["floors"][0])
maze.floors = json_maze["floors"]
maze.hwalls = json_maze["hwalls"]
maze.vwalls = json_maze["vwalls"]
maze.objects = []
maze.obj_count = defaultdict(int)
maze.costs = json_maze.get("costs", {"chip": 10, "step": 1})
for json_obj in json_maze["objects"]:
maze.add_object(MazeObject.fromJSON(json_obj))
return maze
# object management methods
def add_object(self, obj):
self.remove_object_at(obj)
self.objects.append(obj)
self.obj_count[obj.name] += 1
def remove_object_at(self, loc):
for i, obj in enumerate(self.objects):
if same_location(obj, loc):
del self.objects[i]
self.obj_count[obj.name] -= 1
def get_object_at(self, loc):
for obj in self.objects:
if same_location(obj, loc):
return obj
# Floor methods
def get_floor_at(self, loc):
return self.floors[loc.y][loc.x]
def set_floor_at(self, loc, val):
self.floors[loc.y][loc.x] = val
# Wall methods
def get_vwall_at(self, loc):
return self.vwalls[loc.y][loc.x]
def set_vwall_at(self, loc, val):
self.vwalls[loc.y][loc.x] = val
def get_hwall_at(self, loc):
return self.hwalls[loc.y][loc.x]
def set_hwall_at(self, loc, val):
self.hwalls[loc.y][loc.x] = val
def facing_wall(self, obj):
if obj.dir % 2:
return self.hwalls[obj.y + (obj.dir == 1)][obj.x]
else:
return self.vwalls[obj.y][obj.x + (obj.dir == 0)]
# Methods to run the maze
def resolve_conflicts(self, t):
obj_hash = {}
for obj in self.objects:
i = (obj.x + obj.vx*t), (obj.y + obj.vy*t)
obj1 = obj_hash.get(i)
if obj1:
if obj1.name > obj.name:
obj, obj1 = obj1, obj
if obj1.name == "flag" and obj.name == "robot":
obj1.kill = True
obj_hash[i] = obj
else:
raise UnresolvedConflict(obj.name, obj1.name)
else:
obj_hash[i] = obj
def kill_dead_objects(self):
new_objects = []
for obj in self.objects:
if obj.kill:
self.obj_count[obj.name] -= 1
else:
new_objects.append(obj)
self.objects = new_objects
def update_objects(self):
for obj in self.objects:
if obj.painting is not None:
self.set_floor_at(obj, obj.painting)
obj.x += obj.vx
obj.y += obj.vy
if (not(0 <= obj.x < self.width and 0 <= obj.y < self.height)
or self.get_floor_at(obj) == NO_FLOOR):
obj.kill = True
obj.dir = (obj.dir + obj.rot) % 4
obj.rest()
class MazeObject(object):
def __init__(self, name, x=0, y=0, dir=0, rot=0, vx=0, vy=0):
self.name = name
self.x = x
self.y = y
self.dir = dir
self.rot = rot
self.vx = vx
self.vy = vy
self.painting = None
self.kill = False
@classmethod
def fromJSON(cls, json_obj):
json_obj = dict((str(k), v) for k, v in json_obj.iteritems())
return cls(**json_obj)
def rest(self):
self.vx = 0
self.vy = 0
self.rot = 0
self.painting = None
def move_forward(self):
self.vx, self.vy = [(1, 0), (0, 1), (-1, 0), (0, -1)][self.dir]
def turn_left(self):
self.rot = -1
def turn_right(self):
self.rot = 1
def paint(self, col):
self.painting = col
#
# Circuit board stuff
#
class Chip(object):
def __init__(self, name, run, is_test=False, time=0):
self.name = name
self.run = run
self.is_test = is_test
self.time = time
def run_nochip(state):
return 0
def run_wallp(state):
return "NY"[state.maze.facing_wall(state.obj)]
def run_move(state):
if not state.maze.facing_wall(state.obj):
state.obj.move_forward()
return 0
def run_left(state):
state.obj.turn_left()
return 0
def run_right(state):
state.obj.turn_right()
return 0
def run_start(state):
return 0
def run_paintRed(state):
state.obj.paint(RED)
return 0
def run_paintYellow(state):
state.obj.paint(YELLOW)
return 0
def run_paintBlue(state):
state.obj.paint(BLUE)
return 0
def run_redp(state):
return "NY"[state.maze.get_floor_at(state.obj) == RED]
def run_yellowp(state):
return "NY"[state.maze.get_floor_at(state.obj) == YELLOW]
def run_bluep(state):
return "NY"[state.maze.get_floor_at(state.obj) == BLUE]
chip_data = (
("nochip", {}),
("start", {}),
("move", {"time": 1}),
("left", {"time": 1}),
("right", {"time": 1}),
("paintRed", {"time": 1}),
("paintYellow", {"time": 1}),
("paintBlue", {"time": 1}),
("wallp", {"is_test": True}),
("redp", {"is_test": True}),
("yellowp", {"is_test": True}),
("bluep", {"is_test": True})
)
chips = dict((name, Chip(name, globals()["run_"+name], **prop))
for name, prop in chip_data)
no_chip = chips["nochip"]
class TransObject(object):
def __init__(self, x, y):
self.start = Location(x, y)
self.end = Location(x, y)
def translate(t, x, y):
tr = TransObject(x, y)
if (t == "E"):
tr.end.x += 1
tr.orient = 0
elif t == "W":
tr.end.x -= 1
tr.orient = 2
elif t == "N":
tr.end.y -= 1
tr.orient = 3
elif t == "S":
tr.end.y += 1
tr.orient = 1
else:
return None
return tr
class CircuitBoard(object):
def __init__(self, width=9, height=9):
self.chips = [[no_chip]*width for _ in range(height)]
self.transitions = {
0: [[0]*width for _ in range(height)],
"Y": [[0]*width for _ in range(height)],
"N": [[0]*width for _ in range(height)]
}
@classmethod
def fromJSON(cls, json_board):
board = CircuitBoard(json_board["width"], json_board["height"])
for data in json_board["chips"]:
board.chips[data["y"]][data["x"]] = chips[data["chip"]]
if data["chip"] == "start":
board.start = Location(data["x"], data["y"])
for data in json_board["transitions"]:
board.transitions[data["type"]][data["y"]][data["x"]] = data["dir"]
return board
def cost(self, chip_prices=None, trans_prices=None):
cost = 0
if chip_prices:
default = chip_prices.get("chip", 0)
for x, y, chip in enum_matrix(self.chips):
if chip.name != "nochip":
cost += chip_prices.get(chip.name, default)
if trans_prices:
raise NotImplemented
return cost
def get_chip(self, loc):
return self.chips[loc.y][loc.x]
def get_transition(self, val, loc):
return translate(self.transitions[val][loc.y][loc.x], loc.x, loc.y)
#
# Running the maze with a circuit board
#
class Thread(object):
def __init__(self, board, maze, obj):
self.board = board
self.maze = maze
self.obj = obj
self.reset_loc()
def reset_loc(self):
self.loc = self.board.start
def step(self):
chiploc = self.loc
chip = self.board.get_chip(chiploc)
trtype = chip.run(self)
tr = self.board.get_transition(trtype, chiploc)
if tr:
self.loc = tr.end
else:
self.reset_loc()
return chip.time
class MazeRunError(Exception):
pass
class MazeRun(object):
def __init__(self, maze, board):
self.maze = maze
self.board = board
for i, obj in enumerate(maze.objects):
if obj.name == "robot":
robotindex = i
break
else:
raise NoRobot
self.thread = Thread(board, maze, obj)
def step(self):
maze = self.maze
if not maze.obj_count["robot"]:
raise MazeRunError("dead robot")
dt = self.thread.step()
if dt:
maze.resolve_conflicts(0.5)
maze.kill_dead_objects()
maze.update_objects()
maze.resolve_conflicts(0)
maze.kill_dead_objects()
return dt
def run_maze(maze, board):
run = MazeRun(maze, board)
t = 0
instant_steps = 0
while True:
if instant_steps > 100:
raise MazeRunError("busy loop")
dt = run.step()
if dt:
t += 1
instant_steps = 0
else:
instant_steps += 1
if not maze.obj_count["flag"]:
return t
if t > 1000:
raise MazeRunError("Too many steps")