Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. def a_star(map, start_column, start_row, goal_column, goal_row):
  2. """
  3. Algorithm to make path from start to goal by counting the weight of each point
  4. and make path to be more directed to goal
  5. :param map: Map with the lava
  6. :param start_column: In which column start point is
  7. :param start_row: In which row start point is
  8. :param goal_column: In which column goal point is
  9. :param goal_row: In which row goal point is
  10. :return: The path from start to goal
  11. :return:
  12. """
  13. start = (start_column, start_row)
  14. goal = (goal_column, goal_row)
  15. frontier = PriorityQueue()
  16. frontier.put((0, start))
  17. came_from = {}
  18. cost_so_far = {}
  19. came_from[start] = None
  20. cost_so_far[start] = 0
  21. path = []
  22. curr = None
  23. while not frontier.empty():
  24. _, curr = frontier.get()
  25. if map[curr[1]][curr[0]] == "D":
  26. break
  27. for next in neighbours(curr, map):
  28. if next is not None:
  29. if map[next[1]][next[0]] != "*":
  30. new_cost = cost_so_far[curr] + 1
  31. if next not in cost_so_far or new_cost < cost_so_far[next]:
  32. cost_so_far[next] = new_cost
  33. priority = new_cost + heuristic(goal, next)
  34. frontier.put((priority, next))
  35. came_from[next] = curr
  36. while curr != start:
  37. path.append(curr)
  38. curr = came_from[curr]
  39. path.append(start)
  40. path.reverse()
  41. visualize(path, map)
  42. return path
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement