Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Maze.findSolution(Maze.java:41)
at Maze.solution(Maze.java:64)
at Main3.main(Main3.java:21)
public class Maze {
// Class Variables
final static char BLANK = ' ';
final static char WALL = 'X';
final static char BACK = 'B';
final static char PATH = '-';
static char[][] map=null;
Maze(String [] n)
{
map= new char[n.length][];
for(int i =0;i<n.length;i++){
map[i] = n[i].toCharArray();
}
}
public boolean findSolution(int row, int col)
{
//Determines if out of bounds
if(row<0 || col<0 || row>=map.length || col>=map.length)
{
return false;
}
if(col==map.length)
{
return true;
}
if (map[row][col]==WALL)
{
return false;
}
map[row][col]=PATH;
if(map[row][col+1]==BLANK)
{
findSolution(row,col+1);
return true;
}
if(map[row-1][col]==BLANK)
{
findSolution(row-1,col);
return true;
}
if(map[row][col-1]==BLANK)
{
findSolution(row,col-1);
return true;
}
if(map[row+1][col]==BLANK)
{
findSolution(row+1,col);
return true;
}
else{
map[row][col] = BACK;
return false;}
}
public void solution()
{
findSolution(0,5);
}
public void displayMaze()
{
final char[ ][ ] maze = map;
for(int i = 0; i < maze.length; i++){
for(int j = 0; j < maze.length; j++)
System.out.print("|"+maze[i][j]);
System.out.println("|");
}
}
}
public class Main3 {
/**
* @param args
*/
public static void main(String[] args) {
Maze maze1 = new Maze( new String[]
{"xxxxxxxxxxxx",
"x xxxxxxxxxx",
"x xxxxxx ",
"xxx x xx x",
" x x xx x",
"x xxx xx x",
"x xxx xx xx",
"xx x xx",
"xx xxxxx xxx",
"xx x xxx",
"xxxx xxxxx",
"xxxxxxxxxxxx"});
maze1.solution();
maze1.displayMaze();
}
}
any help would be greatly appericated

New Topic/Question
Reply



MultiQuote





|