Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. my path finding function for kirk:
  2. def build_path(
  3.         curr_pos,
  4.         tar_pos,
  5.         values,
  6.         shortest_path_discovered=None,
  7.         curr_sides=None,
  8.         curr_path=None,
  9.         first_call=True,
  10.     ):
  11.     curr_path = curr_path or []
  12.     curr_sides = curr_sides or (UP, DOWN)
  13.     shortest_path_discovered = shortest_path_discovered or [UP] * MAX_MOVES
  14.     values = values.copy()
  15.  
  16.     if curr_pos == tar_pos:
  17.         return curr_path  # We're here!
  18.     if values[tar_pos] == '?':
  19.         if dist(curr_pos, tar_pos) == 1:  # We got as close as possible to unknown
  20.             return curr_path
  21.     if len(curr_path) > len(shortest_path_discovered):
  22.         return False  # A shorter path was found before
  23.  
  24.     safe_positions = build_line(curr_pos, values, curr_sides)
  25.  
  26. #    debug_path_finding(dist=dist(curr_pos, tar_pos), **locals())
  27.  
  28.     for pos in safe_positions:
  29.         values[pos] = 'W'
  30.  
  31.     next_sides = (UP, DOWN) if curr_sides[0] == RIGHT else (RIGHT, LEFT)
  32.     for pos in safe_positions:
  33.         if pos == curr_pos and not first_call:
  34.             continue
  35.         path = build_path(
  36.             pos,
  37.             tar_pos,
  38.             values,
  39.             shortest_path_discovered,
  40.             next_sides,
  41.             curr_path + [pos],
  42.             first_call=False,
  43.         )
  44.         if path and len(path) < len(shortest_path_discovered):
  45.             shortest_path_discovered = path
  46.             if dist(curr_pos, tar_pos) >= len(shortest_path_discovered):
  47.                 return shortest_path_discovered  # won't get quicker
  48.     if len(shortest_path_discovered) < MAX_MOVES:
  49.         return shortest_path_discovered  # we found a way
  50.     return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement