Java 递归回溯mazegenerator不是很递归

Java 递归回溯mazegenerator不是很递归,java,recursion,Java,Recursion,我正在用Java制作一个递归回溯mazegenerator。但它一直是ArrayIndexOutOfBoundsException,并且不是很递归的,如果有人看到我的代码并告诉我我做错了什么。提前谢谢 public class RecursiveBacktrackerMazeGenerator { boolean[][] labMap = new boolean[16][24]; char [][] mazeMap = new char [16][24]; Random random = new

我正在用Java制作一个递归回溯mazegenerator。但它一直是ArrayIndexOutOfBoundsException,并且不是很递归的,如果有人看到我的代码并告诉我我做错了什么。提前谢谢

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);
}
}
}

你的while循环的括号有点乱,我把它改成了这个,它可以工作了,而且可读性更好:

  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] ))) {

您正在访问一个不存在的数组位置!尝试使用一些调试!为了更加递归,您应该将参数传递给迷宫方法,可能是下一个位置的高度和宽度,它不应该是成员变量。这样,当您从递归返回时,您得到的高度和宽度值与调用之前的值相同。