Given any map such as the one below, 1 for path and 0 for walls, can you guys review my code and see what I am doing wrong. My code is supposed to return an arrayList of the positions that it passed by on the map. But my code is not returning any positions and I'm confused on how I can record the "WINNING" positions. Please help, I would truly appreciate it. How can I return an arraylist of winning positions, please help? Please consider that the Path is declared as an instance variable.
An example would be GetPath from point (0,0) to (0,5) and the array returned should contain (0, 5) (1, 5) (2, 5) (3, 5) (4, 5) (4, 4) (4, 3) (4, 2) (3, 2) (2, 2) (1, 2) (0, 2) (0, 1) (0, 0).
int[][] Map = new int[][]{
{ 1, 1, 1, 0, 0, 1 },
{ 0, 0, 1, 0, 0, 1 },
{ 0, 0, 1, 0, 0, 1 },
{ 0, 0, 1, 0, 0, 1 },
{ 0, 0, 1, 1, 1, 1 },
{ 0, 0, 1, 0, 0, 1 },
};
The recursive method is
private ArrayList<Point> GetPath(Point CurrentPosition, Point EndPosition, int[][] Map)
{
int x = (int) CurrentPosition.getX();
int y = (int) CurrentPosition.getY();
int x2 = (int) EndPosition.getX();
int y2 = (int) EndPosition.getY();
System.out.println("CurrentPosition: "+ x + "," + y);
Path.add(CurrentPosition);
if (x == x2 && y == y2) //base case
{
return Path;
}
Map[x][y] = 0; //Will keep track of positions that have been visited.
ArrayList<Point> recursion;
if (x + 1 < Map.length && Map[x+1][y] == 1) //checking down
{
recursion = GetPath(new Point(x + 1, y),EndPosition, Map);
if (recursion != null)
{
Path = recursion;
return Path;
}
}
if (x-1 >= 0 && Map[x-1][y] == 1) //checking up
{
recursion = GetPath(new Point(x-1, y),EndPosition, Map);
if (recursion != null)
{
Path = recursion;
return Path;
}
}
if (y+1< Map.length && Map[x][y+1] == 1) //checking right
{
recursion = GetPath(new Point(x, y+1), EndPosition, Map);
if (recursion != null)
{
Path = recursion;
return Path;
}
}
if (y -1 >= 0 && Map[x][y-1] == 1) checking left
{
recursion = GetPath(new Point(x, y-1),EndPosition, Map);
if (recursion != null)
{
Path = recursion;
return Path;
}
}
return null;
}