I'm trying to create a PacMan A.I. The one last thing I am desperately trying to get done is to give Pacman an actual path from his current position to any other position. The difficulty for me with this one is that there can be, as you all know, walls in his way.
Here is an example (E=empty field, D=dot, P=Pacman, W=wall, X=path):
I've got this Pacman Map as an array:
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
And I want to have this Output:
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
So I need an array that shows a way around the walls.
With the folowing code I was able to get the Manhattan distance:
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();
}
}
In a similar kind of way I want to find the actual path.
Does anyone have any idea? I hope you people can help me out :D