Estou tentando fazer um labirinto numa matriz… sendo que está começando a dar este erro com estouro de Array :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at br.com.Lab.Maze.nextPosition(Maze.java:72)
at br.com.Lab.Maze.walk(Maze.java:49)
at br.com.Lab.Maze.walk(Maze.java:43)
at br.com.Lab.Maze.walk(Maze.java:43)
at br.com.Lab.Maze.walk(Maze.java:46)
at br.com.Lab.Maze.walk(Maze.java:52)
at br.com.Lab.Maze.walk(Maze.java:52)
at br.com.Lab.Maze.main(Maze.java:117)
Vou postar o código abaixo para que possam me dar algumas idéias :
package br.com.Lab;
public class Maze {
private static String maze[][] = {
{ "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1" },
{ "1", "0", "1", "0", "0", "0", "1", "1", "0", "0", "0", "1", "1",
"1", "1", "1", "1" },
{ "1", "1", "0", "0", "0", "1", "1", "0", "1", "1", "1", "0", "0",
"1", "1", "1", "1" },
{ "1", "0", "1", "1", "0", "0", "0", "0", "1", "1", "1", "1", "0",
"0", "1", "1", "1" },
{ "1", "1", "1", "0", "1", "1", "1", "1", "0", "1", "1", "0", "1",
"1", "0", "0", "1" },
{ "1", "1", "1", "0", "1", "0", "0", "1", "0", "1", "1", "1", "1",
"1", "1", "1", "1" },
{ "1", "0", "0", "1", "1", "0", "1", "1", "0", "1", "1", "1", "1",
"1", "0", "1", "1" },
{ "1", "0", "1", "1", "1", "1", "0", "0", "1", "1", "1", "1", "1",
"1", "1", "1", "1" },
{ "1", "0", "0", "1", "1", "0", "1", "1", "0", "1", "1", "1", "1",
"1", "0", "1", "1" },
{ "1", "1", "1", "0", "0", "0", "1", "1", "0", "1", "1", "0", "0",
"0", "0", "0", "1" },
{ "1", "0", "0", "1", "1", "1", "1", "1", "0", "0", "0", "1", "1",
"1", "1", "0", "1" },
{ "1", "0", "1", "0", "0", "1", "1", "1", "1", "1", "0", "1", "1",
"1", "1", "0", "1" },
{ "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1" } };
private static String mark[][];
private static int wall = 1;
private static int walking;
public static int walk(int x, int y, String direc){
maze[x][y] = "1";
if(direc.equals("N"))
return walk(x - 1, y, nextPosition(x,y));
else if(direc.equals("NE"))
return walk(x - 1, y + 1, nextPosition(x,y));
else if(direc.equals("E"))
return walk(x , y + 1, nextPosition(x,y));
else if(direc.equals("SE"))
return walk(x + 1, y + 1, nextPosition(x,y));
else if(direc.equals("S"))
return walk(x + 1, y, nextPosition(x,y));
else if(direc.equals("SW"))
return walk(x + 1, y - 1, nextPosition(x,y));
else if(direc.equals("W"))
return walk(x, y - 1, nextPosition(x,y));
else if(direc.equals("NW"))
return walk(x - 1, y - 1, nextPosition(x,y));
else
return wall;
}
public static String nextPosition(int x, int y){
if(maze[x - 1][y].equals("0"))
return "N";
else if(maze[x - 1][y + 1].equals("0"))
return "NE";
else if(maze[x][y + 1].equals("0"))
return "E";
else if(maze[x + 1][y + 1].equals("0"))
return "SE";
else if(maze[x + 1][y].equals("0"))
return "S";
else if(maze[x + 1][y - 1].equals("0"))
return "SW";
else if(maze[x][y -1].equals("0"))
return "W";
else if(maze[x - 1][y - 1].equals("0"))
return "NW";
else
return "X";
}
private static void exibeMatriz(String[][] matriz){
for(int i = 0; i < matriz.length; i++){
for(int j = 0; j < matriz[i].length; j++){
System.out.print("" + matriz[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
exibeMatriz(maze);
while(walking != 1){
for(int i = 1; i < maze.length; i++){
for(int j = 1; j < maze[i].length; j++){
walking = walk(i, j, nextPosition(i, j));
mark[i][j] = maze[i][j];
}
System.out.println("");
}
}
System.out.println("");
exibeMatriz(mark);
}
}
Obs.: É um trabalho sobre recursividade no qual meu professor passou, não quero respostas ou algo do tipo, quero apenas uma ajuda com esse erro…
