Skip to content

Commit a84487b

Browse files
committed
PEP8 changes.
1 parent 7519268 commit a84487b

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

algorithms/a_star_path_finding.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ def __init__(self, x, y, reachable):
1919

2020
class AStar(object):
2121
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()
2525
self.cells = []
26-
self.gridHeight = 6
27-
self.gridWidth = 6
26+
self.grid_height = 6
27+
self.grid_width = 6
2828

2929
def init_grid(self):
3030
walls = ((0, 5), (1, 0), (1, 1), (1, 5), (2, 3),
3131
(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):
3434
if (x, y) in walls:
3535
reachable = False
3636
else:
@@ -57,7 +57,7 @@ def get_cell(self, x, y):
5757
@param y cell y coordinate
5858
@returns cell
5959
"""
60-
return self.cells[x * self.gridHeight + y]
60+
return self.cells[x * self.grid_height + y]
6161

6262
def get_adjacent_cells(self, cell):
6363
"""
@@ -68,13 +68,13 @@ def get_adjacent_cells(self, cell):
6868
@returns adjacent cells list
6969
"""
7070
cells = []
71-
if cell.x < self.gridWidth-1:
71+
if cell.x < self.grid_width-1:
7272
cells.append(self.get_cell(cell.x+1, cell.y))
7373
if cell.y > 0:
7474
cells.append(self.get_cell(cell.x, cell.y-1))
7575
if cell.x > 0:
7676
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:
7878
cells.append(self.get_cell(cell.x, cell.y+1))
7979
return cells
8080

@@ -112,30 +112,30 @@ def update_cell(self, adj, cell):
112112

113113
def process(self):
114114
# 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):
117117
# pop cell from heap queue
118-
f, cell = heapq.heappop(self.op)
118+
f, cell = heapq.heappop(self.opened)
119119
# add cell to closed list so we don't process it twice
120-
self.cl.add(cell)
120+
self.closed.add(cell)
121121
# if ending cell, display found path
122122
if cell is self.end:
123123
self.display_path()
124124
break
125125
# get adjacent cells for cell
126126
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:
130130
# if adj cell in open list, check if current path is
131131
# better than the one previously found
132132
# 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)
135135
else:
136-
self.update_cell(c, cell)
136+
self.update_cell(adj_cell, cell)
137137
# add adj cell to open list
138-
heapq.heappush(self.op, (c.f, c))
138+
heapq.heappush(self.opened, (adj_cell.f, adj_cell))
139139

140140
a = AStar()
141141
a.init_grid()

0 commit comments

Comments
 (0)