Generates a maze on the fly after entering the desired height of the maze. This compiled fine back in 1988 when I submitted it to the IOCCC (having rediscovered Eller's algorithm). Modern C compilers don't allow constant strings to be overwritten, which can be avoided by changing the first line to
Sadly neither version works here with an older clang on OS X. Both variants build fine with 9 warnings each. But the old variant dies with a "Bus Error: 10", and the new variant with "Segmentation fault: 11". Same with gcc (albeit only 8 warnings.)
/edit
OK, just wrong user input. You gotta feed it a number, and not a "foobar" or another random string.
For anyone interested in this, Jamis Buck's book 'Mazes for Programmers' is a masterpiece of the genre.
My personal favorite distinction is between the Recursive Backtracker (which creates long, winding corridors with few dead ends which is great for tower defense games) vs. Prim's Algorithm (which creates lots of short cul-de-sacs which is better for roguelikes). The bias of the algorithm dictates the feel of the game more than the graphics do.
The main difference between the above tool and most custom shaped maze generators out there is breaking the assumption that the maze's outer shape must be defined by adding or removing regularly-shaped cells along the edge.
To have mazes look more human drawn, cells need to be irregular and the inner walls need naturally follow the contours of the outer shape.
Have you considered finding a conformal transformation† that maps a square to any other possible shape, as long as the shape doesn't have any holes? Such a transformation always exists by the Riemann Mapping Theorem, and is unique as long as you specify in addition (1.) which point the square's centre maps to, and (2.) the angle of rotation around that point. Not sure if anyone's ever tried that.
If you actually want more aesthetic freedom, you can compose with an arbitrary diffeomorphism of the square to itself. But I think that might usually look worse.
† - That is, preserving all angles, including right angles. The terminology stems from the output angles conforming (???) to the input angles.
My favorite maze algorithm is a variant of the growing tree algorithm - each time you carve a cell, add it to a random one of N lists. When choosing a cell to visit, pop the last cell off the first non-empty list. It's considerably faster than the standard tree algorithm, but more importantly, changing N has a dramatic impact on the texture of the maze (compare 1 2 4 8 etc on a decently large maze).
I still remember the days when I started out programming.. I used simply copy and write code from TheCodingTrain's challenge videos. And the recursive backtracking algorithm was the first project I took on by myself coding it in java while only looking at how it works from his explanation.
Awesome resource. I recently (in the past week) created a maze game. I used Claude (sonnet 4.5) for the most part, but some things, like images, were created with ChatGPT. I may do a blog about it if anyone is interested in the inner-workings and my thought process from concept to vibecoded. I am by no means a game dev, was just curious about what it would take to create unique single solution mazes with some game-like components thrown in, and trying it with the assistance of AI. It turned out somewhat retro. Now go get lost!
Hey thanks for sharing. I gave it a try but ran into some issues.
Is this intended to be able to be used on a phone? I saw the instructions about guiding a yellow ball but what I experienced was about 10 seconds of chiptune music with a maze that had some icons I couldn't interact with.
After the 10 seconds a series of green spheres and vertical lines washed across the screen but I wasn't able to control them.
I'm using the duck duck go browser on Android if that helps.
Hi there; NOT compatible or designed with mobile in mind unfortunately. Built more for desktop browser and keyboard (WASD-style movement controls) or trackpad. That, and the maze image generator in its current form requires static dimensions so the pieces can be placed within their chosen coordinates, meaning it displays better on full-sized browser :(
This makes me nostalgic, when i started to programming i tried to create ultimate generating maze algorithm and use it in some game. But my solutions was really naive.
That's not a good link for a list of past threads since the idea for the latter is to include only the ones with interesting comments.
However, it looks like a good article that could use a repost! Just not soon, since we want to give enough time for the hivemind caches to clear :) - if you want to repost it in (say) a month or two, email us at hn@ycombinator.com and we'll put it in the SCP (https://news.ycombinator.com/item?id=26998308).
I've always known about algorithms that solve mazes, but never about actually making them. It's interesting seeing all these algorithms and how the mazes they generate look different.
The main insight in many of these algorithms is that if you have a tree, there's exactly one path between any two leaves (assuming you don't pass over a branch twice). So if you just draw a random tree-like structure and make the entry and exit points of the maze leaves in the tree, then you have a valid maze with exactly one solution.
Thanks! Yeah, you're right, the point is to get to the center, and it's a lot easier to know where that is on my original "Polar" maze https://xn--sberg-lra.net/maze/polar?size=5&entryCount=1 I should update them to have a goal icon like you say, maybe antoher time.
Here's a Google Sheets Maze Generator that's pure formula and no script. This post has got me thinking how I might format the formula to be shaped like a maze.
https://tinyurl.com/SheetsMazeGenerator
Is it known which algorithms produce 'difficult' mazes? I'm imagining you could run all the maze solving algorithms against all the maze generating algorithms many times, and then calculate what the Nash equilibrium would be if the solver is trying to minimise expected time and the generator is trying to maximise it.
There is another old site ("since September 23, 1996"), my second favorite maze site, that has some articles about things like that. Like on the page below ("Tips on how to create difficult and fun Mazes, and how to solve and analyze them").
I think there is a difference if you want to make it only expensive to solve using popular maze solver algorithms, vs to make it difficult for a human to solve. Many of the recommendations on that page are for how to do things that can make a maze more difficult for humans to solve, but will not always matter to an algorithm that just mechanically tries solutions in some order.
seconding the jamis buck book, its one of the few programming books i actually finished. the way he explains each algorithm with visualizations makes it stick
It feels like many of the more complicated algorithms produce worse mazes (long horizontal/vertical walls, many 1-2 square dead ends next to another) than basic recursive backtracking.
reply