我正在用Java制作一个Recursive backtracker 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);
}
}
}
答案 0 :(得分:0)
你的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] ))) {