Ghytro

dandy_bot.py

Feb 13th, 2022
1,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. from collections import deque
  2.  
  3. def shortest_path_to_gold(source_point, check) -> "list[str]":
  4.     q = deque()
  5.     reached = set()
  6.     parents = {}
  7.     q.append(source_point)
  8.     while len(q):
  9.         current = q.popleft()
  10.         reached.add(current)
  11.         if check("gold", current[0], current[1]):
  12.             path_points = [current]
  13.  
  14.             while parents.get(path_points[-1], None) is not None:
  15.                 path_points.append(parents[path_points[-1]])
  16.             path_points.reverse()
  17.             path_commands = []
  18.  
  19.             for i, p in enumerate(path_points[1:]):
  20.                 if p[0] - path_points[i][0] == 1:
  21.                     path_commands.append("right")
  22.                 elif p[0] - path_points[i][0] == -1:
  23.                     path_commands.append("left")
  24.                 elif p[1] - path_points[i][1] == 1:
  25.                     path_commands.append("down")
  26.                 elif p[1] - path_points[i][1] == -1:
  27.                     path_commands.append("up")
  28.                 else:
  29.                     raise Exception("don't know where to go") # debug
  30.             return path_commands
  31.  
  32.         adjacent_cells = [
  33.             (current[0] + 1, current[1]),
  34.             (current[0], current[1] + 1),
  35.             (current[0] - 1, current[1]),
  36.             (current[0], current[1] - 1)
  37.         ]
  38.  
  39.         for p in adjacent_cells:
  40.             if not check("wall", p[0], p[1]) and p not in reached:
  41.                 q.append(p)
  42.                 reached.add(p)
  43.                 parents[p] = current
  44.     return []
  45.  
  46. def script(check, x, y):
  47.     if check("gold", x, y) and check("player", x, y):
  48.         return "take"
  49.     if check("player", x, y):
  50.         return shortest_path_to_gold((x, y), check)[0]
  51.     return "pass"
  52.  
Advertisement
Add Comment
Please, Sign In to add comment