The ghost is supposed to use recursion to find pacman, However my recursion goes into an infinite loop.
here is the code
public boolean traverse(int row, int col)
{
boolean done = false;
if( first == true) /* first is set to true at the start because the transverse maze solver starts from the ghost's location*/
{
row = getLocation().getRow();
col = getLocation().getCol();
first = false;
}
if (getGrid().isValid(new Location(row,col)) && !(getGrid().get(new Location(row,col)) instanceof Board))
{
tried[row][col] = 3;
if (row == player.getLocation().getRow() && col == player.getLocation().getCol())
done = true; //the maze is solved
else
{
done = traverse(row+1, col); //down
if (!done)
done = traverse(row, col+1); //right
if (!done)
done = traverse(row-1, col); //up
if (!done)
done = traverse(row, col-1); //left
}
if (done) //this location is part of the final path
tried[row][col] = 2;
}
return done;
}
after the transverse method finishes. it adds all the locations row and col that are equal to 3 in my 2d int array. 3 = path in that array. then the ghost chooses the location in the array list of paths with the shortest distance from him , so he can travel the path in correct order.
^ that is my plan.. and i have coded it out but by transverse method, that will find the route to PacMan and create a path does not work?

New Topic/Question
Reply


MultiQuote

|