This repository was archived by the owner on Apr 26, 2020. It is now read-only.
forked from nickclason/Python-Final-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathfinding.py
More file actions
63 lines (53 loc) · 2.24 KB
/
pathfinding.py
File metadata and controls
63 lines (53 loc) · 2.24 KB
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
class Node():
def __init__(self, parent=None, position=None):
self.parent = parent
self.position = position
def __eq__(self, other):
return self.position == other.position
def breadthFirst(maze, start, end):
"""
Breadth-first algorithm for pathfinding
Start and end coordinates are [row, col]
"""
start_node = Node(None, start)
end_node = Node(None, end) # Used to check if done
nodeList = []
visitedNodes = []
nodeList.append(start_node) # Start at first node
while len(nodeList) > 0:
neighbors = [] # Nearby valid nodes
for node in nodeList:
nodeList.remove(node)
visitedNodes.append(node)
if node == end_node: # Reached end, return path
path = []
current = node
while current is not None:
path.append(current.position)
current = current.parent
return path[::-1] # Return reversed path
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0)]: # Neighbor positions
neighborNode = (node.position[0] + new_position[0], node.position[1] + new_position[1])
#Neighbor is in bounds
if neighborNode[0] > (len(maze)-1) or neighborNode[0] < 0 or neighborNode[1] > (len(maze[0])-1) or neighborNode[1] < 0:
continue
#Neighbor is not a wall
if maze[neighborNode[0]][neighborNode[1]] == '1':
continue
# Make sure we haven't touched this node ever yet
add = True
newNode = Node(node, neighborNode)
for tNode in nodeList:
if tNode == newNode:
add = False
for tNode in visitedNodes:
if tNode == newNode:
add = False
for tNode in neighbors:
if tNode == newNode:
add = False
if add:
neighbors.append(newNode)
for node in neighbors:
nodeList.append(node)
return None # End is not reachable