I'm trying to implement my own A* pathfinder, but the voices in my head scream of better ways to do this. Probably because I'm exceeding Python's default maximum recursion depth of 1000.
class PathFinder():
## grid is a list of 8 sub-lists, each containing 8 nodes.
## start and goal are tuples representing the grid indexes of my start and goal.
def __init__(self, start, goal, grid):
## I don't do anything with these yet,
## just thought they would be good to have.
self.goal = goal
self.start = start
self.grid = grid
def find_path(self, start, goal, path=[]):
## My seemingly hackish method of looping through
## the 8 cells surrounding the one I'm on.
for row in (-1,0,1):
for col in (-1,0,1):
if row == 0 and col == 0:
continue
cell = [start[0]+row,start[1]+col]
## Catching the inevitable exception when I try to examine
## the surroundings of cells that are already on the edge.
try:
## Skip if the cell is blocked ("X").
if self.grid[cell[0]][cell[1]] == "X":
continue
except IndexError:
continue
path.append(cell)
if cell == goal:
return path
self.find_path(cell, goal, path)
def main():
import Gridder2
grid = Gridder2.board()
start = (0,0)
goal = (0,3)
pt = PathFinder(start, goal, grid)
print(pt.find_path(start, goal))
return 0
if __name__ == '__main__':
main()
My find_path
function particularly seems like it needs work. Seems like I should be able to calculate a path through an 8x8 grid with a recursion depth of less than 1000.
How can I improve this?