forked from noops-challenge/mazebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.py
33 lines (27 loc) · 1.1 KB
/
Node.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
class Node:
def __init__(self, parent=None, position=None):
self.parent = parent
self.position = position
self.g = 0 # distance between the current and start node
self.h = 0 # estimated distance from current to end node
self.f = 0 # total cost of the node
def __eq__(self, compare):
return self.position == compare.position
def __str__(self):
return f"pos={self.position}, g={self.g}, h={self.h}, f={self.f}"
# Returns list of four nearest neighbour Nodes
def generateChildren(self, maze, maxX, maxY):
neighbourNodes = []
x = self.position[0]
y = self.position[1]
adj = [[x,y+1],[x,y-1],[x+1,y],[x-1,y]]
# Remove any out-of-bounds and walls
for a in adj:
x = a[0]
y = a[1]
if x >= 0 and y >= 0 and x < maxX and y < maxY:
# Maze from api is a 2D array
# Meaning maze[0] is an array of x's at y=0
if maze[y][x] != 'X':
neighbourNodes.append(Node(self, a))
return neighbourNodes