1

Ok, so i want t generate a random maze for my maze game. I have hardcoded the maze like this, and have several different versions that i would like to be able to have spawned at random.

public Maze() {
        this.mazeMap1 = new BlockType[][] {
                {H, H, H, H, H, H, H, H, H, H, H, H, H, H, H},
            {H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
            {H, E, E, E, H, E, E, H, E, E, H, E, E, E, H},
            {H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
            {H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
            {H, E, E, E, H, E, E, H, E, E, H, E, E, E, H},
            {H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
            {H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
            {H, E, E, E, H, E, E, H, E, E, H, E, E, E, H},
            {H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
            {H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
            {H, E, E, E, H, E, E, H, E, E, H, E, E, E, H},
            {H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
            {H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
            {H, H, H, H, H, H, H, H, H, H, H, H, H, H, H}
            };
}

then i create a getter and return the maze

public BlockType[][] getMazeMap() {
return mazeMap2;
}

Then i have a class 'Board' where i make the maze

 private void makeBoard() {
        blocks = new Maze().getMazeMap();
    }

If i would have, say 10 different hardcoded mazes, how would i generate one at random?

  • Put your 10 different mazes in an array and select one of the array elements at random. (http://stackoverflow.com/questions/8065532/how-to-randomly-pick-an-element-from-an-array) – OH GOD SPIDERS Mar 24 '17 at 10:32
  • it would be much more interesting to actually generate the maze at random, rather than pick a pre-existing one – Sean Patrick Floyd Mar 24 '17 at 10:40
  • Suggestion (not on the question): instead of hardcoding the mazes, write a simple utility that takes a binary (black and white) image as input and converts each pixel in a `BlockType` (black -> 'H', white -> 'E'). In this way you can create new mazes with any image editor – Oneiros Mar 24 '17 at 11:03

2 Answers2

3

You need a collection of your maze maps. Having mazemap1, mazemap2, etc. doesn't (easily) allow you to pick one.

ArrayList<Block[][]> mazemaps = new ArrayList<>();

mazemaps.add( new BlockType[][] { ... } ); // with all your data
mazemaps.add( new BlockType[][] { ... } ); // second map data

Then you can pick one:

int maze = new Random().nextInt(mazemaps.size());
return mazemaps.get(maze);

(There are plenty of other things you can maybe do better, but this is a start)

Andrew McGuinness
  • 2,092
  • 13
  • 18
1

Here is my take:

You first need to generate the exit way. By exit way, I mean the way that goes from start to finish.

Then:

  1. Generate a false way that leads to nowhere
  2. Generate the wall surrounding the false way
  3. Go back to step #1

After you generate every single cell as either wall or passable terrain, run Djikstra/A* algorithm and prove that the maze is actually solvable.

ardilgulez
  • 1,856
  • 18
  • 19