Depth-first-search algorithm for maze generation

The algorithm in real time:

End result:
RobloxStudioBeta_eHNiP5t2oY

This algorithm is flexible, too! If you want long corridors, then you can add a horizontal bias to the random cell picker.

RobloxStudioBeta_T7QRWEnPPu

This was probably the most enjoyable thing to work on for me. If you want more info on the algorithm, since my explanation will be brief, you can read on it here for the algorithm itself, and here for how it’s used to generate mazes.

Using depth-first-search to create a maze is pretty easy, actually. How it works is basically it keeps a notebook on all the cells, and every cell starts unchecked. It then begins by picking a random cell around the starting cell, and once it picks a cell, it marks it as picked. It then repeats this process. If it hits a dead end, as in all the cells around it are picked, then it will backtrack on the exact path it took to get there until it finds a cell that hasn’t been picked. This can ensure every cell has a path to it. if it backtracks all the way to the beginning, then we can reliably know that the maze is finished. In my case, there may be some cells that are totally boxed, but this is rare.

9 Likes

Really neat, I’d hate to have to go through that in first person :stuck_out_tongue:

How do the completely boxed in cells occur, the way you’ve explained the algorithm’s process leaves me to believe that it should have none.

1 Like

That is awesome! Now I want a video of you completing the maze. :+1: :rofl: :sweat_smile:

3 Likes

I can fix it, but basically, its a result of 3 am code. Basically, when boxes are being checked in my code, there’s a table of the 4 surrounding boxes. Whenever a box in the table is picked, it removes the box from the table and sets it to nil. This apparently doesn’t remove the entry from the table, so the #table still returns 4, so whenever i get a random entry using

table[random(1,#table)] 

it may return nil and cause an error. To combat this, I have it so it picks 1,4 in the table until it gets an entry. If no entry is acquired in 20 tries, then it assumes the table is empty. Since it’s completely random, there’s a very small chance that in all 20 of those tries, it missed an entry it the table. This creates the boxes. Of course, to prevent this, you can increase the iteration to something like 100 before it assumes the table is empty. This runs poorly however, so I had it at 20.

table.remove(Table, table.find(Table, Box))
2 Likes

Yeah, I know that now, but 3 am me didn’t. :sweat_smile:

1 Like

Damn, this is neat! Just image being in this, like a life or death situation where you have to make it out to stay alive.

That’s actually the point of this, I have plans for procedurally generated “monsters”, so you never know what’s going to be chasing you.

1 Like

Seems like a very similar game to my Beast project back in 2019. I used a recursive backtracking algorithm to generate a maze, and then had a number of monsters attack the player while they attempted to find a key and escape through the door.

2 Likes

It’s also similar to a game I LOVED back in the day called Identity Fraud. I find these types of games very simple and fun to make.

1 Like

Nice! will it do that when a new server is created? (what I mean is will the thing be randomized.) also, that hurt my brain. lol

2 Likes

Yes, a different experience every server.

1 Like