Maze generation problem

So here’s what I already have: https://pastebin.com/BpUshpKY (this is the relevant function)

The maze is divided into invidiual blocks, the maze this code will generate is 9x9 “blocks”.

My maze is defined using an X and Y system, and each X and Y also having 4 values for left, up, right, down.
These extra values define whether or not there is a wall in this place.

I then have the draw() function read the gameGrid[][][] array, and draw out the lines in each block, so for example:

gameGrid[0][0][0] = 0;
gameGrid[0][0][1] = 0;
gameGrid[0][0][2] = 0;
gameGrid[0][0][3] = 1;

will be the top-left-most block, and it will have a wall on the left side, the up side, the right side, and it will have no wall on the bottom side.

the actual random generation is probably very needlessly complicated and even though I made it, I don’t fully comprehend how it works… because it doesn’t fully work as intended. the aim of that part of the function is to make sure that there are always enough “openings” or “sides with no walls”, so that the whole thing is connected…
it kind of works. but it’s very, very slightly inconsistent. like 1 out of 100 attempts will block off one part of the maze.

the reason i went through all this trouble instead of looking for a random maze generation algorithm is because I need to know the location of every wall.
This is because I aim to put tanks inside this maze, and their projectiles will bounce off the walls, so i can create a tank vs tank game, in 2d top-down view, and the main focus of the game is going to be the fact that you can shoot someone from behind a wall if you can aim your projectiles to bounce off the walls in the right way.

tl;dr: somewhat working random maze generator that allows me to keep track of where every wall is, and isnt, to create a random map for a tank game.

so the actual problem i have is: does anyone know of a simpler method to achieve this?
more specifically, I want to achieve random maze generation, but one where I can keep track of the walls so I can create collisions between them and the tanks/tank projectiles.

edit: here is my entire code, which is compilable, and should sort of showcase what I want, minus the tanks: https://pastebin.com/bKcDhPeP

1 Like

Check out Prim’s algorithm:

It sounds complicated, but it’s explained more simply here:

  1. Start with a grid full of walls.

  2. Pick a cell, mark it as part of the maze. Add the walls of the cell to the wall list.

  3. While there are walls in the list:
    a. Pick a random wall from the list. If only one of the two cells that the wall divides is visited, then:

    1. Make the wall a passage and mark the unvisited cell as part of the maze.
    2. Add the neighboring walls of the cell to the wall list.

    b. Remove the wall from the list.

When in doubt, googling “generate random maze algorithm” will give you a ton of results.

1 Like

Thank you for the great suggestion!

Here’s what I’m wondering about this: This algorithm creates an actual maze, which means that there is only one path towards the end. What if I want there to be multiple paths?

1 Like

You can modify the algorithm, and note that there are other algorithms listed in the second link.

Basically you could modify step 3.a and remove walls that are between two cells already in the maze. Or you could generate a maze with the algorithm, and then remove some walls after it’s done.

1 Like

This sounds doable for a beginner like myself. :slight_smile: I’ll properly look into the algorithms. Once again, thank you very much!

1 Like