我创建一个Java的迷宫游戏中,想要添加一个智者鬼(像吃豆子),其走向用户的位置追上他。对于智者鬼我选择了A *寻路算法,发现下面的链接实现该算法:
HTTPS :?//$c$c.google.com/p/a-star/source/browse/trunk/java/PathFinder.java R = 8
HTTPS :?//$c$c.google.com/p/a-star/source/browse/trunk/java/AStar.java R = 8
不过,code未完全正常工作,我的意思是code似乎只找到路径,从左上角去右下角,而不是例如左 - 右 - 左 - 左下来。
有关例如:
如果
源=(0,0)
目标=(8,8)//完美的作品..
如果
源=(8,8)
目标=(0,0)//没有工作:(
请帮我解决这个code或提供有用的链接,实现这一点。
解决方案我认为这个问题是在这部分的code:
保护名单,其中,节点> generateSuccessors(节点节点){
名单<节点> RET =新的LinkedList<节点>();
INT X = node.x;
INT Y = node.y;
如果(Y< map.length - 1安培;&放大器;图[Y + 1] [X] == 1)
ret.add(新节点(X,Y + 1));
如果(X LT;地图[0] .length - 1安培;&安培;地图[Y] [X + 1] == 1)
ret.add(新节点(X + 1,Y));
返回RET;
}
这code为回归邻居当前节点列表,但它只是试图向下和向右的方向。尝试添加相应的$ C $下要遵循,如任何其他方向
如果(Y> 0安培;&放大器;图[Y-1] [X] == 1)
ret.add(新节点(X,Y-1));
如果(X大于0&安培;&安培;映射[Y] [X-1] == 1)
ret.add(新节点(X-1,y))为;
I am creating a Maze game in java and wants to add a smarty ghost (like Pacman) that move towards user location to catch him. For smarty ghost I chose A* PathFinding Algorithm and found below links for implementing this algorithm :
https://code.google.com/p/a-star/source/browse/trunk/java/PathFinder.java?r=8
https://code.google.com/p/a-star/source/browse/trunk/java/AStar.java?r=8
But the code is not completely working properly , I mean the code only seems to find paths going from top left to bottom right, not for example left - right - left - left down .
for e.g :
if
source = (0,0)
Destination = (8,8) // works perfectly..
if
source = (8,8)
Destination = (0,0) // doesn't work :(
Please help me to correct this code or give useful link for implementing this.
解决方案I think the issue is in this part of the code:
protected List<Node> generateSuccessors(Node node){
List<Node> ret = new LinkedList<Node>();
int x = node.x;
int y = node.y;
if(y < map.length - 1 && map[y+1][x] == 1)
ret.add(new Node(x, y+1));
if(x < map[0].length - 1 && map[y][x+1] == 1)
ret.add(new Node(x+1, y));
return ret;
}
This code is returning a list of neighbours for the current node, however it only tries down and right directions. Try adding corresponding code for any other directions you wish to follow, e.g.
if(y > 0 && map[y-1][x] == 1)
ret.add(new Node(x, y-1));
if(x > 0 && map[y][x-1] == 1)
ret.add(new Node(x-1, y));