okay, if any of you have played Desktop Tower Defense you may be able to understand this easier.Code:public int findShortestPath (int fromx, int fromy, int tox, int toy)
{
squares [fromx] [fromy].check = false;
if (isTouching (fromx, fromy, tox, toy))
{
System.out.println ("");
return 1;
}
else
{
int[] paths = new int [4];
if (fromx < 11 && squares [fromx + 1] [fromy].check)
paths [0] = findShortestPath (fromx + 1, fromy, tox, toy);
else
paths [0] = 10000;
if (fromx > 0 && squares [fromx - 1] [fromy].check)
paths [1] = findShortestPath (fromx - 1, fromy, tox, toy);
else
paths [1] = 10000;
if (fromy < 13 && squares [fromx] [fromy + 1].check)
paths [2] = findShortestPath (fromx, fromy + 1, tox, toy);
else
paths [2] = 10000;
if (fromy > 0 && squares [fromx] [fromy - 1].check)
paths [3] = findShortestPath (fromx, fromy - 1, tox, toy);
else
paths [3] = 10000;
System.out.println ("" + (smallest (paths) + 1));
return smallest (paths) + 1;
}
}
Basically it's a game and like any other tower defense the creeps move through the maze, however the maze isn't predifined, it needs to find it's way to the end.
what this method is called it first checks if it's touching the destination square, if it is then return 1, because distance is 1. otherwise it calls this method again for every square around it that isn't outside of the grid, and hasn't been called already, or has a blocking object in that square.
if i set all squares.check to false before hand, since my grid is 12x14, the shortest path should be 26, but it's returning 156(starting at 0,0)
Recursive methods are hard to troubleshoot, any help?

