# init the start and end node start_node = node(None,start) end_node = node(None,end)
# init the open and closed lists open_list = [] close_list = []
# add the start node to the open list open_list.append(start_node)
# loop util find the end_node whilelen(open_list)>0: # make the best node as current_node # init 1 current_node = open_list[0] current_index = 0 for index, nod inenumerate(open_list): if nod.f<current_node.f: current_node = nod current_index = index
# pop the curr node off open list, add to close list open_list.pop(current_index) close_list.append(current_node)
# terminal conditions (reach the end node ) if current_node == end_node: path = [] while(current_node isnotNone): path.append(current_node.position) current_node = current_node.parent
# return the path we find. return path[::-1]
# the body of the loop: update the nodes # find the available children nodes children = []
# define the adjacent squares positions = [(-1,0),(1,0),(0,-1),(0,1)] for pos in positions: # get childrens positions node_pos = (current_node.position[0]+pos[0], current_node.position[1]+pos[1])
# make sure within range if node_pos[0] < 0or node_pos[0] >= map.shape[0] or node_pos[1] < 0or node_pos[1] >= map.shape[1]: continue
# if the child is already in the open list, compare it if child in open_list: open_index = open_list.index(child) open_node = open_list[open_index] if child.g > open_node.g: continue open_list.pop(open_index) # if it is not in the open/closelist or better than that in open list, we add it. open_list.append(child)