-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
78 lines (61 loc) · 1.9 KB
/
util.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
from random import choice
class Queue:
def __init__(self):
self.storage = []
self.size = 0
def enqueue(self, val):
self.size += 1
self.storage.append(val)
def dequeue(self):
if self.size:
self.size -= 1
return self.storage.pop(0)
else:
return None
class Stack:
def __init__(self):
self.storage = []
self.size = 0
def push(self, val):
self.size += 1
self.storage.append(val)
def pop(self):
if self.size:
self.size -= 1
return self.storage.pop()
else:
return None
reverse_dirs = {
'n': 's',
's': 'n',
'e': 'w',
'w': 'e'
}
class Graph:
"""Represent the world as a dictionary of rooms mapping (room, dir) as edge."""
def __init__(self):
self.rooms = {}
def add_vertex(self, room):
"""
Add a vertex to the graph.
"""
if room not in self.rooms:
self.rooms[room] = {d: '?' for d in room.get_exits()}
def go_in_direction_until_dead_end(self, room, path=None):
path = path if path else []
next_dirs = self.get_unexplored_dir(room)
if len(next_dirs):
direction = choice(next_dirs)
path.append(direction)
self.explore(room, direction)
next_room = room.get_room_in_direction(direction)
return self.go_in_direction_until_dead_end(next_room, path)
else:
return path
def get_unexplored_dir(self, room):
return [direction for direction, value in self.rooms[room].items() if value == '?']
def explore(self, room, direction):
if direction in room.get_exits():
next_room = room.get_room_in_direction(direction)
self.rooms[room][direction] = next_room
self.rooms[next_room][reverse_dirs[direction]] = room