我已经在Java中实现了迷宫作为坐标[x,y]的2D数组,并且我有使用A *算法(类型)导航迷宫的敌人以便捕捉玩家(移动) NES和W - 不允许对角线移动)。它有效,但它有点瑕疵,因为:
玩家可以不断移动,从而可能导致之前的任何路线计算无关紧要
迷宫是一个难以驾驭的环境(每次随机生成它)
迷宫可以这样想:
0 0 0 0 0 0 0
0 1 0 1 0 1 0
0 1 1 1 1 1 0
0 1 0 0 1 0 0
0 0 1 1 1 0 0
0 1 1 0 1 1 0
0 0 0 0 0 0 0
其中1是路径,0是墙(它的实际实现略有不同,但功能与上面相同)。
我已经找到了像移动目标D *搜索这样的替代方案,但我想尝试让我的A *实现更有效地工作。
基本上,它的工作原理如下:
Every time I move:
Get the current cell info
if I've been here before:
Increment the cell cost
Increment the no. of times this cell has been visited
if I can move North:
Calculate the cartesian distance between that cell and the player's coords, add to "dist" variable
if I've seen that cell before:
Increment the "dist" variable
Store "dist" in an array of up to 4 costs (corresponding to North cost)
else (if I can't move North because of a wall):
Assign infinite cost to that direction
(Repeat for E S and W)
Find the lowest cost of the 4 directions and move there
所以最终它的工作基于距离和我们看过那个细胞的次数。
我尝试添加其他东西,例如他们可以从该单元格中有多少潜在的位置,但是敌人似乎总是把时间停留在某些区域,或者来回走动,直到这样做的成本超过了移动到其他地方的成本。他们倾向于最终解决问题,但我知道我的算法远非最佳。敌人可以使用玩家的坐标,因此他们知道目标在哪里。
我的问题是:我该如何改进?
编辑:我只知道问题出在哪里 - 在我的"打破"逻辑(例如,当2个或更多个方向具有相同的成本时),我总是在比较集合中采用最新的方法,例如:
least cost = first entry in costs array
for each entry in costs array
if costs entry < least cost
least cost becomes that entry
这可能导致它来回一段时间(UP是最便宜的,现在DOWN是最便宜的,现在UP最便宜......)。我认为这是一个插入启发式的好地方,或者只是改变它的工作方式(也许是为了让这种情况永远不会出现)。它还告诉我,我的初始启发式计算(从开始+ dist到目标的距离)可能会被错误地实现。