Hacker News new | ask | show | jobs
Maze Generator and Solver (golubitsky.github.io)
53 points by lovegraphs 4047 days ago
13 comments

For a great collection of maze generation algorithms, see the blog of Jamis Buck. On http://weblog.jamisbuck.org/2011/2/7/maze-generation-algorit... he recaps the algorithms discussed in earlier posts, and he's publishing a book on the topic: Maze for Programmers. It's currently in beta on the Pragmatic Bookshelf: https://pragprog.com/book/jbmaze/mazes-for-programmers. Can't wait for the hardcover book, currently planned for September 2015.
Anyone that hand draws mazes as a hobby knows these auto-generated mazes leave something to be desired. There's an art to designing a maze and I've yet to see an auto-generator mimic that art. First step, curved and multi-length branches. Next step, add complex and very long teaser dead-ends. Block-style mazes are way too simple.
GitHub Pages supports HTTPS, so please include your JS/CSS schema-relative (//) or directly with HTTPS. Otherwise people who force HTTPS on that domain get mixed content warnings and nothing loads.
Thanks! Pull request merged.
I mostly wrote this: https://github.com/LanceH/Maze while on conference calls before I started using that time to learn to draw.

I only ever ran it from within eclipse, I can't remember if it would run from the command line. It's a depth first search with solution also printed.

The only thing I added that I hadn't seen elsewhere is the "twisty" variable, which weights the random direction to either going straight or turning. I liked straight hallways on large mazes because it makes things blur together.

I would like to see a code, which solves a maze by calculating the Poisson equation for pressure. As boundary condition set high pressure at the start of the maze and low pressure at the end and follow the steepest decent.
this kind of idea has been explored under the name "collaborative diffusion".

e.g. http://sgd.cs.colorado.edu/wiki/Collaborative_Diffusion

Thanks. Indeed, the idea looks similar.
I assume Neumann 0 boundary condition at the walls. Would it always solve the maze though? It seems intuitively true but hard to prove.
Hacked something together. Seems to work.

http://simulationcorner.net/maze/

Yes, open boundary conditions at the walls.

I am not sure what happens if you have two possible paths to the exit but one of them is wider, but longer. It might not always find the shortest path.

I like this main idea, because it is independent on the dimension and structure of the maze.

I really like this idea!
If you want to see a faster maze solver, here is one that I submitted some time back:

https://github.com/mikolalysenko/l1-path-finder

In this demo page you can click on the title and the little dots will chase your mouse around:

http://mikolalysenko.github.io/l1-path-finder/www/

Whatever it was built for, it doesn't run in Safari. And “but it runs in browser X” is almost as acceptable a retort as “it would run with square wheels”, in 2015, on the front page of HN.
Tricycle with square wheels: https://www.youtube.com/watch?v=FayemJb2-w4
Which goes to show that with a floor in the right shape, almost anything is possible :)
By the way, thank you for your colorful simile--it really spurred me to fix this issue this morning, and I've definitely learned to take cross-browser compatibility more seriously. Cheers.
Glad you took it this way, I'll have another look :)
I'm not sure if it's the only problem, but main.css contains a C-style "//" single-line comment. This is not valid CSS syntax.
Thanks! Removed.
Tough to solve on iOS http://i.imgur.com/zzb3nMJ.jpg
Lamentable. Must be some missing cross-browser CSS.. I'll look into it tomorrow! In the meanwhile try it in Chrome if you have a chance :)
FYI, it has the exact same issue on Safari on OS X.
Safari is fixed! I need to get a Macbook so testing is faster than waiting for a screenshot online...
Now I get this error in the console as soon as the page loads in each browser I try:

    [Error] ReferenceError: Can't find variable: css
            generateMaze (mazeEventHandling.js, line 30)
Thank you, sorry about that. I changed some references around and caught that error only after having deployed.
Thanks! It now works on iOS. Still working on Safari...
I'm interested see how the maze is generated so that there Is a solution to it (ie, no exit point without an open way)
The particular generation algorithm that you see is called "Randomized Prim's algorithm" on Wikipedia. It starts at a random cell in a grid, marks it as discovered, and adds all of that cell's adjacent walls to a queue. It then selects a random wall from the queue and, if the cell on the other side of the wall has not yet been discovered, opens up that wall. It adds all of the adjacent cell's walls to the queue, marks the adjacent cell as discovered, and selects the next random wall in the queue until all the cells have been discovered. It's kind of a modified BFS. This algorithm guarantees that all cells are connected.
Most maze generators build a giant tree, starting from an arbitrary point and sending out branching corridors until the area is full. By its nature everything's connected.
Not OP but you can always generate a fully random maze and then merge the unconnected blocks by simply removing a wall that can connect them
As a novice in this whole arena, are there any decent algorithms for determining longest path between two adjacent cells?
Sounds a lot like the longest path problem [1], which is NP-hard.

[1] http://en.wikipedia.org/wiki/Longest_path_problem

Cool! How does it instantly recognize when a path would lead into a trap?
It probably calculates the path in advance (note the "please wait" indicator appears just after you hit 'Solve') then it follows the path with an animation that gives you exactly the impression that it never falls into a trap as it goes.
That's right, it does a breadth-first-search from the end-point immediately upon clicking the end-point; it then traces the path from the start-point, following the parent-pointers set during the BFS, painting the DOM in the process.
Not using A* ?
Great work, Mike! :)
Hey Vishal, thanks! Small world!
great work.
Thanks!