@@ -19,18 +19,18 @@ def __init__(self, x, y, reachable):
19
19
20
20
class AStar (object ):
21
21
def __init__ (self ):
22
- self .op = []
23
- heapq .heapify (self .op )
24
- self .cl = set ()
22
+ self .opened = []
23
+ heapq .heapify (self .opened )
24
+ self .closed = set ()
25
25
self .cells = []
26
- self .gridHeight = 6
27
- self .gridWidth = 6
26
+ self .grid_height = 6
27
+ self .grid_width = 6
28
28
29
29
def init_grid (self ):
30
30
walls = ((0 , 5 ), (1 , 0 ), (1 , 1 ), (1 , 5 ), (2 , 3 ),
31
31
(3 , 1 ), (3 , 2 ), (3 , 5 ), (4 , 1 ), (4 , 4 ), (5 , 1 ))
32
- for x in range (self .gridWidth ):
33
- for y in range (self .gridHeight ):
32
+ for x in range (self .grid_width ):
33
+ for y in range (self .grid_height ):
34
34
if (x , y ) in walls :
35
35
reachable = False
36
36
else :
@@ -57,7 +57,7 @@ def get_cell(self, x, y):
57
57
@param y cell y coordinate
58
58
@returns cell
59
59
"""
60
- return self .cells [x * self .gridHeight + y ]
60
+ return self .cells [x * self .grid_height + y ]
61
61
62
62
def get_adjacent_cells (self , cell ):
63
63
"""
@@ -68,13 +68,13 @@ def get_adjacent_cells(self, cell):
68
68
@returns adjacent cells list
69
69
"""
70
70
cells = []
71
- if cell .x < self .gridWidth - 1 :
71
+ if cell .x < self .grid_width - 1 :
72
72
cells .append (self .get_cell (cell .x + 1 , cell .y ))
73
73
if cell .y > 0 :
74
74
cells .append (self .get_cell (cell .x , cell .y - 1 ))
75
75
if cell .x > 0 :
76
76
cells .append (self .get_cell (cell .x - 1 , cell .y ))
77
- if cell .y < self .gridHeight - 1 :
77
+ if cell .y < self .grid_height - 1 :
78
78
cells .append (self .get_cell (cell .x , cell .y + 1 ))
79
79
return cells
80
80
@@ -112,30 +112,30 @@ def update_cell(self, adj, cell):
112
112
113
113
def process (self ):
114
114
# add starting cell to open heap queue
115
- heapq .heappush (self .op , (self .start .f , self .start ))
116
- while len (self .op ):
115
+ heapq .heappush (self .opened , (self .start .f , self .start ))
116
+ while len (self .opened ):
117
117
# pop cell from heap queue
118
- f , cell = heapq .heappop (self .op )
118
+ f , cell = heapq .heappop (self .opened )
119
119
# add cell to closed list so we don't process it twice
120
- self .cl .add (cell )
120
+ self .closed .add (cell )
121
121
# if ending cell, display found path
122
122
if cell is self .end :
123
123
self .display_path ()
124
124
break
125
125
# get adjacent cells for cell
126
126
adj_cells = self .get_adjacent_cells (cell )
127
- for c in adj_cells :
128
- if c .reachable and c not in self .cl :
129
- if (c .f , c ) in self .op :
127
+ for adj_cell in adj_cells :
128
+ if adj_cell .reachable and adj_cell not in self .closed :
129
+ if (adj_cell .f , adj_cell ) in self .opened :
130
130
# if adj cell in open list, check if current path is
131
131
# better than the one previously found
132
132
# for this adj cell.
133
- if c .g > cell .g + 10 :
134
- self .update_cell (c , cell )
133
+ if adj_cell .g > cell .g + 10 :
134
+ self .update_cell (adj_cell , cell )
135
135
else :
136
- self .update_cell (c , cell )
136
+ self .update_cell (adj_cell , cell )
137
137
# add adj cell to open list
138
- heapq .heappush (self .op , (c .f , c ))
138
+ heapq .heappush (self .opened , (adj_cell .f , adj_cell ))
139
139
140
140
a = AStar ()
141
141
a .init_grid ()
0 commit comments