我正在尝试创建一个PacMan A.I. 我拼命想完成的最后一件事是给吃豆子一条从他当前位置到任何其他位置的实际路径。 我很难做到的是,众所周知,他的方式可能有围墙。
这里是一个例子(E =空字段,D =点,P =吃豆子,W =墙,X =路径):
我将这个Pacman Map作为数组:
W-W-W-W-W-W-W
W-E-E-E-E-E-W
W-E-P-E-E-E-W
W-E-E-E-E-E-W
W-W-W-W-E-E-W
W-E-D-E-E-E-W
W-W-W-W-W-W-W
我想要这个输出:
W-W-W-W-W-W-W
W-E-E-E-E-E-W
W-E-P-E-E-E-W
W-E-X-X-X-E-W
W-W-W-W-X-E-W
W-E-D-X-X-E-W
W-W-W-W-W-W-W
所以我需要一个数组来显示墙壁周围的路径。
使用以下代码,我能够获得曼哈顿距离:
public static void ManhattanDist(int currentX, int currentY) {
int[][] ratings = new int[5][5];
int value = 0;
for (int i = 0; i < ratings.length; i++) {
value = Math.abs(i - currentX);
for (int j = 0; j < ratings[i].length; j++) {
ratings[i][j] = value + Math.abs(j - currentY);
System.out.print("-" + ratings[i][j]);
}
System.out.println();
}
}
我想以类似的方式找到实际路径。
有人知道吗?希望大家能帮助我:D