2016-03-16 5 views
-2

Я делаю рекурсивный backgracker mazegenerator в Java. но он продолжает переходить в ArrayIndexOutOfBoundsException и не очень рекурсивный, если кто-то посмотрит мой код и скажет мне, что я делаю неправильно. заранее спасибо.Рекурсивный backtracker mazegenerator не очень рекурсивный

public class RecursiveBacktrackerMazeGenerator { 
boolean[][] labMap = new boolean[16][24]; 
char [][] mazeMap = new char [16][24]; 
Random random = new Random(); 
int minRandomInt1 = random.nextInt(23); 
int minRandomInt2 = random.nextInt(15); 

int height; 
int width; 

public RecursiveBacktrackerMazeGenerator() { 

    for (int i = 0; i <= 15; i++) { 

     Arrays.fill(labMap[i],true); 
    } 
    width = minRandomInt1; 
    height = minRandomInt2; 

    Maze(); 

     for (int i = 0; i < 16; i++) { 
     for (int j = 0; j < 24; j++) { 
      if (labMap[i][j] == false){ 
       mazeMap[i][j] = ' '; 
      } 
      else if (labMap[i][j] == true){ 
       mazeMap[i][j] = 'X'; 
      } 
      System.out.print(mazeMap[i][j]); 
     } 
     System.out.println(); 
    } 
} 

private void Maze() { 

    labMap[height][width] = true; 

    try { 
     while ((height>=2&& height<=13 && width>=2 && width<=21) && (labMap[height - 1][width] == true && labMap[height - 2][width] == true) || (labMap[height + 1][width] == true && labMap[height + 2][width] == true) || (labMap[height][width - 1] == true && labMap[height][width - 2] == true) || (labMap[height][width + 1] == true && labMap[height][width + 2] == true)) { 
      int minRandomInt = random.nextInt(37); 
      //width = minRandomInt1; 
      //height = minRandomInt2; 

    if (minRandomInt >= 0 && minRandomInt <= 9) { 
     if (height>=2) 
     if (labMap[height - 1][width] == true && labMap[height - 2][width] == true){ 
       if (width != 23 && width != 0) { 
       labMap[height - 1][width] = false; 
       labMap[height - 2][width] = false; 
       height = height - 2; 
      } 
     } 
    } 
     else if (minRandomInt > 9 && minRandomInt <= 19) { 
     if (height <= 13) { 
       if (labMap[height + 1][width] == true && labMap[height + 2][width] == true) { 
        if (width != 23 && width != 0) { 
         labMap[height + 1][width] = false; 
         labMap[height + 2][width] = false; 
         height = height + 2; 
        } 
       } 
     } 
    } else if (minRandomInt > 19 && minRandomInt <= 28) { 
     if (width <= 21) { 
       if (labMap[height][width + 1] == true && labMap[height][width + 2] == true) { 
        if (height != 0 && height != 15) { 
         labMap[height][width + 1] = false; 
         labMap[height][width + 2] = false; 
         width = width + 2; 
        } 
       } 
     } 
    } else if (minRandomInt > 28 && minRandomInt <= 37) { 
     if (width >= 2) { 
       if (labMap[height][width - 1] == true && labMap[height][width - 2] == true) { 
        if (height != 0 && height != 15) { 
         labMap[height][width - 1] = false; 
         labMap[height][width - 2] = false; 
         width = width - 2; 
        } 
       } 
     } 
    } 
} 

if (height <= 14) { 
    if (labMap[height + 1][width] == false) { 
     height++; 
     Maze(); 
    } 
} 
else if (height >=1){ 
    if(labMap[height -1][width] == false) { 
     height--; 
     Maze(); 
    } 
} 
else if (width>=1){ 
     if (labMap[height][width-1] == false) { 
      width--; 
      Maze(); 
     } 
} 
else if (width <= 22) { 
    if (labMap[height][width + 1] == false) { 
     width++; 
     Maze(); 
    } 
} 


} 
catch (ArrayIndexOutOfBoundsException e) { 
System.out.println(e); 
System.out.println("width = "+ width); 
System.out.println("height = " + height); 
} 
} 
} 
+0

вы обращаетесь к позиции массива, не существует! попробуйте использовать некоторые отладки! –

+0

Чтобы быть более рекурсивным, вы должны передавать параметры вашему методу Maze(), возможно, свою высоту и ширину вашей следующей позиции (которые не должны быть переменными-членами). Таким образом, когда вы возвращаетесь из рекурсии, значения высоты и ширины такие же, как и перед вызовом. –

ответ

0

скобки Вашего времени цикла является своим родом перепутался, я изменил его на это, и она работает, и более читаемый:

while ( ((height>=2) && (height<=13) && (width>=2) && (width<=21)) 
     && ( (labMap[height - 1][width] && labMap[height - 2][width]) 
      || (labMap[height + 1][width] && labMap[height + 2][width]) 
      || (labMap[height][width - 1] && labMap[height][width - 2]) 
      || (labMap[height][width + 1] && labMap[height][width + 2]))) { 
Смежные вопросы