0

I need to create maze,

first, I tried randomly insert 'O' or 'B' into a 2-D array then randomly put the exit and start. but the problem is that 9 of the 10 maze I created doesn't have a path(start doesn't lead to exit, 'O' is the open path);

so what I'm trying to do is to create a path that will have at least 1 path from the start to the exit.

start point cannot be the border and exit point can only be the border.

B B B B B B
B B B B B B
B B B B S B
B B B B B B
B B X B B B

Basically I want to create a path to the exit and some path tat doesn't lead to the exit.

Example:

B B B B B B
B B O O O B
B B B B S B
B B O O O B (doesn't have to be this shape, this is just an example what I want to create)
B B X B B B

here is my code:

public static void randomly(int sx, int sy){ //sx and sy is the location of the startpoint
int x[] ={-1,0,0,1};
int y[] ={0,-1,1,0};
int count;
boolean check = false;
int afterX, afterY;
for(int i = 0 ; i < 4; i++){
  afterX = sx + x[i];
  afterY = sy + y[i];
  if(maze[afterX][afterY] == 'X'){
    return;
  }
}
do{
  count = 0;
  int number = (int)(Math.random()* 4);
  afterX = sx + x[number];
  afterY = sy + y[number];
  if(afterX > 0 && afterY > 0 && afterX < row-1 && afterY<column-1){
    maze[afterX][afterY] = 'O';
  }

  }
}while((afterX == 0 || afterY == 0 || afterX == row-1 || afterY == column-1) && check);

randomly(afterX,afterY);
}

The problem is that I get this:

B B B B B B
B B O O B B
B O O O S B
B O O O O B
B B X B B B

I don't always get this, but most of the time

The problem is that the 'O' usually stick together, but what I want is the example I gave above

Secret.BoBo
  • 25
  • 1
  • 6
  • http://stackoverflow.com/questions/38502/whats-a-good-algorithm-to-generate-a-maze – Iłya Bursov Apr 25 '17 at 20:58
  • There are already a ton of well known algorithms for creating a maze, you should not have to re do that all from scratch. – takendarkk Apr 25 '17 at 20:58
  • Possible duplicate of [What's a good algorithm to generate a maze?](http://stackoverflow.com/questions/38502/whats-a-good-algorithm-to-generate-a-maze) – Michael Apr 25 '17 at 21:00
  • Can anyone tell me how to I code recursive backtracker to create a maze?? i read it but the code is not java, and the different is that that they have a wall and I have B as a wall. – Secret.BoBo Apr 25 '17 at 21:15
  • I know whe had to make such an algorythm in a school assignment. We created a nice one wich combined routes, where you can't combine a route with itself. At start you have height x width different routes, all different coörds, no route yet combined. Randomly choose a route, and find another route next to it and combine them. At the end you have 1 route where eventually there is only 1 way from any point to any other point on the map. A perfect maze :) – Jeroen van Dijk-Jun Apr 25 '17 at 21:34
  • Did you follow the link to "The Greatest Maze Presentation Ever" at the "duplicate" questions linked above? There's some really good info in that presentation. http://www.jamisbuck.org/presentations/rubyconf2011/index.html. It's unclear what you mean by "...and I have B as a wall". Your B's are like table cells and maze walls are like table borders between cells. In a maze most cells don't have all 4 borders/walls. – geneSummons Apr 25 '17 at 21:59
  • Basically the wall that the presentation talks about is the B in my code? right? – Secret.BoBo Apr 25 '17 at 22:05
  • This answer has a list of algorithms you could use: http://stackoverflow.com/a/23681987/481248 – Jason Apr 25 '17 at 22:15
  • The problem with the link is that he is using a grid where the line is a wall, but in my case, I don't have a line as a wall, and I don't think they are using a 2-D array. In my case, i don't have a wall, and i have a 2-D array and each cell have a character and if it is B it is a wall, O mean opening path., If i have any misunderstood, please let me know and exlapin – Secret.BoBo Apr 25 '17 at 22:36

0 Answers0