Python迷宫寻路

时间:2017-08-30 21:05:15

标签: python python-2.7 maze

我正在使用Python进行塔防游戏,其中用户的塔可以修改敌人必须采取的路径。为了计算路径,我认为Lee算法的实现可能是最好和最简单的,特别是如果网格变大。我试图在此基础上松散地建立算法。

然而,我的代码并不起作用。我无法理解为什么。

def getRoute(self):
  self.routes = [[(0,0)]]

  def _getRoutes():
    new_routes = []
    for route in self.routes:
      for i in [(1,0),(-1,0),(0,1),(0,-1)]:
        new_loc = [x + y for x,y in zip(route[-1],i)]
        for index in new_loc:
          if index < 0:
            print('< 0')
            continue ## if the index causes a skip across the side of the array, ignore
        try:
          if self.level.map[new_loc[0]][new_loc[1]].travellable: ## if the tile is able to be travelled on by enemies
            route.append(new_loc) ## add the tile to the 'r' array
            new_routes.append(route) ## add the new route to the new_routes array
        except IndexError: ## if the tile is off the map, ignore
          print('index error')
    self.routes = new_routes

  def _checkRoutesValidity():
    for route in self.routes:
      if self.level.map[route[-1][0]][route[-1][1]].access == 5:
        return route
        break
    else:
      return None

  while not _checkRoutesValidity():
    _getRoutes()

  self.route = _checkRoutesValidity()
  for i,j in self.route:
    self.level.map[i][j].overlay_col = [0,1,0,1]

路线是应该包含敌人可以采取的所有可能路线的变量,并且最终是正确的路线。目前,该算法应以最快的绿色阴影路线。 Level是一个关卡对象,level.map是一个Grid对象的2D数组,每个对象都是一个单元格。如果cell.access为5,则表示它是敌人的出口点。

实际发生的一切,是它创建了一个非常长的元组列表(0,1)和(1,0)。除了1或0之外,不会产生任何负数。

有人可以指出我正确的方向是创建一个合适的Lee算法还是修复我当前的代码?

1 个答案:

答案 0 :(得分:1)

据我所知,你永远不会检查你是否已经去过广场。此外,您还要使用xy来查找不是x,y坐标的值,并检查索引的一端并捕获另一端的异常。这非常复杂。我的建议是实现实际的算法:

def gen_lee(start, size, travelable):
    neighbor_offsets = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    score = 0
    path_map = [[None for _ in xrange(size)] for _ in xrange(size)]
    node_list = [start]
    path_map[start[0]][start[1]] = 0
    for node in node_list:
        score = path_map[node[0]][node[1]]
        for neighbor_offset in neighbor_offsets:
            neighbor_x = node[0] + neighbor_offset[0]
            neighbor_y = node[1] + neighbor_offset[1]
            if neighbor_x < 0 or \
               neighbor_y < 0 or \
               neighbor_x >= size or \
               neighbor_y >= size:
                continue  # Skip out of map neighbors
            if not travelable[neighbor_x][neighbor_y]:
                continue  # Skip untravelable neighbors
            if path_map[neighbor_x][neighbor_y] is None:
                node_list.append((neighbor_x, neighbor_y))
                path_map[neighbor_x][neighbor_y] = score + 1
    return path_map

有了这个,我们可以生成路由网格:

path_map = gen_lee((1, 1), 5, [[1]* 5] * 5)

没有障碍物,我们得到:

for row in path_map:
    print row

[2, 1, 2, 3, 4]
[1, 0, 1, 2, 3]
[2, 1, 2, 3, 4]
[3, 2, 3, 4, 5]
[4, 3, 4, 5, 6]

有障碍物:

travelable = [[1, 1, 1, 0, 1], 
              [1, 1, 1, 0, 1], 
              [1, 1, 1, 0, 1], 
              [1, 1, 1, 0, 1], 
              [1, 1, 1, 1, 1]]

path_map = gen_lee((1, 1), 5, travelable)

我们得到:

[2, 1, 2, None, 10]
[1, 0, 1, None, 9]
[2, 1, 2, None, 8]
[3, 2, 3, None, 7]
[4, 3, 4,    5, 6]

然后,你开始目标并继续前进,找到比你当前得分低1的邻居。这将产生最佳路径。 (注意,可能有多个满足此要求:您可以选择其中任何一个并找到等效路径)

如果在找到路径步骤的任何时候,您找不到较低的邻居(并且您没有达到目标),那么路径就会被阻止。

相关问题