Code review
This commit is contained in:
parent
02071f919f
commit
25040f0ea0
26
UI/Apath.py
26
UI/Apath.py
@ -35,46 +35,46 @@ def APath(table, start, end):
|
|||||||
i=i+1
|
i=i+1
|
||||||
current_node = open_list[0]
|
current_node = open_list[0]
|
||||||
current_index = 0
|
current_index = 0
|
||||||
|
|
||||||
|
# Find current node
|
||||||
for index, item in enumerate(open_list):
|
for index, item in enumerate(open_list):
|
||||||
if(item.f < current_node.f):
|
if(item.f < current_node.f):
|
||||||
current_node = item
|
current_node = item
|
||||||
current_index = index
|
current_index = index
|
||||||
|
|
||||||
# Pop current off open list, add to closed list
|
# Pop current off open list and add to closed list
|
||||||
open_list.pop(current_index)
|
open_list.pop(current_index)
|
||||||
closed_list.append(current_node)
|
closed_list.append(current_node)
|
||||||
|
|
||||||
# Found the goal
|
# Found end node
|
||||||
if current_node == end_node:
|
if current_node == end_node:
|
||||||
path = []
|
path = []
|
||||||
current = current_node
|
current = current_node
|
||||||
while current is not None:
|
while current is not None:
|
||||||
path.append(current.position)
|
path.append(current.position)
|
||||||
current = current.parent
|
current = current.parent
|
||||||
return path[::-1] # Return reversed path
|
return path[::-1]
|
||||||
|
|
||||||
# Generate children
|
# Generate children
|
||||||
children = []
|
children = []
|
||||||
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]: # Adjacent squares
|
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]:
|
||||||
|
|
||||||
# Get node position
|
|
||||||
node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])
|
node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])
|
||||||
|
|
||||||
# Make sure within range
|
# If in maze
|
||||||
if node_position[0] > (len(table) - 1) or node_position[0] < 0 or node_position[1] > (len(table[len(table)-1]) -1) or node_position[1] < 0:
|
if node_position[0] > (len(table) - 1) or node_position[0] < 0 or node_position[1] > (len(table[len(table)-1]) -1) or node_position[1] < 0:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Make sure walkable terrain
|
# If not obstacle
|
||||||
if table[node_position[0]][node_position[1]].field_type == 3:
|
if table[node_position[0]][node_position[1]].field_type == 3:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Create new node
|
|
||||||
new_node = AStarNode(current_node, node_position)
|
new_node = AStarNode(current_node, node_position)
|
||||||
|
|
||||||
# Append
|
|
||||||
children.append(new_node)
|
children.append(new_node)
|
||||||
|
|
||||||
# Loop through children
|
|
||||||
for child in children:
|
for child in children:
|
||||||
|
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ def APath(table, start, end):
|
|||||||
|
|
||||||
def InOpenlist(child: AStarNode):
|
def InOpenlist(child: AStarNode):
|
||||||
for open_node in open_list:
|
for open_node in open_list:
|
||||||
if child == open_node: # and child.g > open_node.g:
|
if child == open_node:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -95,7 +95,6 @@ def APath(table, start, end):
|
|||||||
if InClosedlist(child)==True:
|
if InClosedlist(child)==True:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Create the f, g, and h values
|
|
||||||
child.g = current_node.g + 1
|
child.g = current_node.g + 1
|
||||||
child.h = max(abs(child.position[0] - end_node.position[0]), abs(child.position[1] - end_node.position[1]))
|
child.h = max(abs(child.position[0] - end_node.position[0]), abs(child.position[1] - end_node.position[1]))
|
||||||
child.f = child.g + child.h
|
child.f = child.g + child.h
|
||||||
@ -104,7 +103,6 @@ def APath(table, start, end):
|
|||||||
if InOpenlist(child)==True:
|
if InOpenlist(child)==True:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Add the child to the open list
|
|
||||||
open_list.append(child)
|
open_list.append(child)
|
||||||
|
|
||||||
|
|
||||||
|
2
main.py
2
main.py
@ -8,7 +8,7 @@ def main():
|
|||||||
# initialize window (grid, start,end,mode)
|
# initialize window (grid, start,end,mode)
|
||||||
#mode 1 - random obstacles
|
#mode 1 - random obstacles
|
||||||
#mode 2 - board obstacles
|
#mode 2 - board obstacles
|
||||||
Window(grid,(0,0),(19,19),2)
|
Window(grid,(0,0),(19,19),1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user