-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpathfinder.py
58 lines (45 loc) · 1.48 KB
/
pathfinder.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
__author__ = 'donatm'
# graph is in adjacent list representation
def highlightPath(mapModel, fromRoom, toRoom):
print(toRoom)
oldPath = mapModel.getPath()
for roomId in oldPath:
room = mapModel.rooms()[roomId]
room.setHighlight(False)
room.getView().update()
if fromRoom is None:
return
if toRoom not in mapModel.rooms():
return
path = findPath(mapModel, fromRoom, mapModel.rooms()[toRoom])
mapModel.setPath(path)
for roomId in path:
room = mapModel.rooms()[roomId]
room.setHighlight(True)
room.getView().update()
print(path)
def findPath(mapModel, fromRoom, toRoom):
log = []
queue = []
queue.append([fromRoom.getId()])
while queue:
# get the first path from the queue
path = queue.pop(0)
# get the last node from the path
node = path[-1]
# path found
log.append(node)
if node == toRoom.getId():
return path
# enumerate all adjacent nodes, construct a new path and push it into the queue
for adjacent in mapModel.rooms()[node].getLinks().values():
destination = adjacent.getDestinationFor(mapModel.rooms()[node])
if destination.getId() in log:
continue
new_path = list(path)
new_path.append(destination.getId())
queue.append(new_path)
log.append(destination.getId())
return []
def d_highlightPath():
pass