-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.py
94 lines (69 loc) · 2.55 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# import pygame module
import pygame
# import constants
import constants
# Node object
class Node:
# constructor
def __init__(self, row, col, width, total_rows):
self.row = row
self.col = col
self.x = row * width
self.y = col * width
self.color = constants.UNVISITED
self.neighbors = []
self.width = width
self.total_rows = total_rows
# get position of node
def get_pos(self):
return self.row, self.col
# getter functions
def is_closed(self):
return self.color == constants.CLOSED
def is_open(self):
return self.color == constants.OPEN
def is_barrier(self):
return self.color == constants.BARRIER
def is_start(self):
return self.color == constants.START
def is_end(self):
return self.color == constants.END
# setter functions
def reset(self):
self.color = constants.UNVISITED
def make_closed(self):
self.color = constants.CLOSED
def make_open(self):
self.color = constants.OPEN
def make_barrier(self):
self.color = constants.BARRIER
def make_start(self):
self.color = constants.START
def make_end(self):
self.color = constants.END
def make_path(self):
self.color = constants.PATH
def draw(self, win):
pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.width))
# update neighbors of the node
def update_neighbors(self, grid):
# empty neighbor array
self.neighbors = []
# neighbor below
if self.row < self.total_rows - 1 and not grid[self.row + 1][self.col].is_barrier():
# if not boundary node AND neighbor isn't barrier, add it
self.neighbors.append(grid[self.row + 1][self.col])
# neighbor above
if self.row > 0 and not grid[self.row - 1][self.col].is_barrier():
# if not boundary node AND neighbor isn't barrier, add it
self.neighbors.append(grid[self.row - 1][self.col])
# neighbor at left
if self.col < self.total_rows - 1 and not grid[self.row][self.col + 1].is_barrier():
# if not boundary node AND neighbor isn't barrier, add it
self.neighbors.append(grid[self.row][self.col + 1])
# neighbor at right
if self.col > 0 and not grid[self.row][self.col - 1].is_barrier():
# if not boundary node AND neighbor isn't barrier, add it
self.neighbors.append(grid[self.row][self.col - 1])
def __lt__(self, other):
return False