我正在研究A星算法,我需要一些有关代理移动的帮助。我的A星函数为我提供了嵌套列表中x,y坐标到最接近目标的路径。我需要让我的经纪人走这条路才能达到最接近的目标。
我尝试在range(len(path) -1)
循环中使用for
,但是它只能吐出一招,而游戏必须运行100次迭代。我正在使用函数def gather_closest_goal
。嵌套列表的第一个元素是我的特工的位置,列表的最后一个元素是最近的目标。
def gather_closest_goal(self,field,figure,path):
figure_position = self.get_player_position(figure,field)
for i in range(len(path)-1):
if (path[i][0] == path[i+1][0] -1) and (path[i][1] == path[i[1]):
return MOVE_RIGHT
for i in range(len(path) - 1):
if (path[i][0] == path[i+1][0]) and (path[i][1] == path[i+1][1]+1):
return MOVE_UP
if (path[i][0] == path[i+1][0]) and (path[i][1] == path[i+1][1]-1):
return MOVE_UP
if (path[i][0] == path[i+1][0]) and (path[i][1] == path[i-1][1] +1):
return MOVE_DOWN
else:
return MOVE_LEFT
def move_sheep(self,player_number,field):
if player_number == 1:
figure = CELL_SHEEP_1
sheep_position = tuple(self.get_player_position(CELL_SHEEP_1, field))
else:
figure = CELL_SHEEP_2
sheep_position = tuple(self.get_player_position(CELL_SHEEP_2, field))
if self.wolf_close(player_number,field):
#print('wolf close move')
return self.run_from_wolf(player_number,field)
if self.food_present(field):
#print('gather food move')
# print(astar(field,sheep_position,(2,5)))
return self.gather_closest_goal(field,figure,astar(field,sheep_position,self.closest_goal(player_number,field)))
else:
return MOVE_NONE