

Maze Generator and Solver - lovegraphs
http://golubitsky.github.io/maze_solver/

======
jschulenklopper
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...](http://weblog.jamisbuck.org/2011/2/7/maze-generation-algorithm-
recap.html) 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](https://pragprog.com/book/jbmaze/mazes-for-programmers). Can't
wait for the hardcover book, currently planned for September 2015.

------
ChicagoDave
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.

------
stwe
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.

~~~
lovegraphs
Thanks! Pull request merged.

------
LanceH
I mostly wrote this:
[https://github.com/LanceH/Maze](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.

------
s-macke
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.

~~~
leni536
I assume Neumann 0 boundary condition at the walls. Would it always solve the
maze though? It seems intuitively true but hard to prove.

~~~
s-macke
Hacked something together. Seems to work.

[http://simulationcorner.net/maze/](http://simulationcorner.net/maze/)

------
33a
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](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/](http://mikolalysenko.github.io/l1-path-finder/www/)

------
tempodox
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.

~~~
ctdonath
Tricycle with square wheels:
[https://www.youtube.com/watch?v=FayemJb2-w4](https://www.youtube.com/watch?v=FayemJb2-w4)

~~~
tempodox
Which goes to show that with a floor in the right shape, almost anything is
possible :)

------
vermooten
Tough to solve on iOS
[http://i.imgur.com/zzb3nMJ.jpg](http://i.imgur.com/zzb3nMJ.jpg)

~~~
lovegraphs
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 :)

~~~
stevenh
FYI, it has the exact same issue on Safari on OS X.

~~~
lovegraphs
Safari is fixed! I need to get a Macbook so testing is faster than waiting for
a screenshot online...

~~~
stevenh
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)

~~~
lovegraphs
Thank you, sorry about that. I changed some references around and caught that
error only after having deployed.

------
harel
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)

~~~
lovegraphs
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.

------
zentiggr
As a novice in this whole arena, are there any decent algorithms for
determining longest path between two adjacent cells?

~~~
bananasbandanas
Sounds a lot like the longest path problem [1], which is NP-hard.

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

------
rosstex
Cool! How does it instantly recognize when a path would lead into a trap?

~~~
ahmetmsft
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.

~~~
lovegraphs
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.

~~~
je42
Not using A* ?

------
vishaldpatel
Great work, Mike! :)

~~~
lovegraphs
Hey Vishal, thanks! Small world!

------
arrc
great work.

~~~
lovegraphs
Thanks!

