-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
54 lines (47 loc) · 1.35 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# TODO: f(x) = g(x) + h(x)
class Node:
"""
Node values:
1: start
2: end
3: wall
4: visited
5: path
0: empty
"""
def __init__(self, x, y, val):
self.x = x
self.y = y
self.val = val
self.f = 0
self.g = 0
self.h = 0
self.neighbors = []
self.came_from = None
self.is_wall = False
def __str__(self):
neighbors = [n.val for n in self.neighbors]
return "%s %s %s %s" % (self.x, self.y, self.val, neighbors)
def get_val(self):
return self.val, self.x, self.y
def get_coords(self):
return self.x, self.y
def add_neighbors(self, grid, cols, rows):
x = self.x
y = self.y
if x < cols - 1:
self.neighbors.append(grid[x + 1][y])
if y < rows - 1:
self.neighbors.append(grid[x + 1][y + 1])
if y > 0:
self.neighbors.append(grid[x + 1][y - 1])
if x > 0:
self.neighbors.append(grid[x - 1][y])
if y < rows - 1:
self.neighbors.append(grid[x - 1][y + 1])
if y > 0:
self.neighbors.append(grid[x - 1][y - 1])
if y < rows - 1:
self.neighbors.append(grid[x][y + 1])
if y > 0:
self.neighbors.append(grid[x][y - 1])