Topic:   Just released: HTML5/Javascript Maze Generator   (Read 27467 times)


0 Members and 1 Guest are viewing this topic.

Charlo


  • GMG-er

  • **


  • Posts: 451
Just released: HTML5/Javascript Maze Generator
« on: December 06, 2011, 07:37:56 AM »
I don't know if you could call it a "game", but I just released a maze generator written in Javascript.  It's on my site at http://charlo.gamemakersgarage.com/games/mazegen.php.

Despite the prevalence of maze generators using the same algorithm that I used, I didn't look at anybody else's code while writing this, so it's probably way bulkier than it needs to be.  It took me about a week on-and-off to get a version that did anything other than 1.) fail to do anything when hitting the "generate" button or 2.) get stuck in an infinite loop and crash the browser.  It will still crash your browser if you try to generate past 26000 cells or so.

The interface should be straightforward enough, just enter your parameters and hit the generate button to create a maze.  "Solve" will mark the solution path in red.  Left and right clicks will move the start and end points around.  I did add one game-like feature; you can use the arrow keys to move a dot around the maze. 

Check it out and tell me what you think!


Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Just released: HTML5/Javascript Maze Generator
« Reply #1 on: December 06, 2011, 12:49:07 PM »
Wow that's impressive! I really like your site Charlo. Plus that maze generator is made of epic sauce. I'm sure the solve feature uses a flood path algorithm but what'd you use to generate the maze?

Circuit


  • GMG-er

  • **


  • Posts: 299

  • blast from the past
Re: Just released: HTML5/Javascript Maze Generator
« Reply #2 on: December 06, 2011, 02:09:47 PM »
That's awesome.  I have no idea how to code any of that.   :o

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: Just released: HTML5/Javascript Maze Generator
« Reply #3 on: December 06, 2011, 03:50:36 PM »
Amazing! I don't even know what PHP stands for  :P
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Charlo


  • GMG-er

  • **


  • Posts: 451
Re: Just released: HTML5/Javascript Maze Generator
« Reply #4 on: December 06, 2011, 05:45:56 PM »
Quote
Wow that's impressive! I really like your site Charlo. Plus that maze generator is made of epic sauce. I'm sure the solve feature uses a flood path algorithm but what'd you use to generate the maze?

Thanks!  The algorithm is depth-first search.  Read about it on wikipedia: http://en.wikipedia.org/wiki/Maze_generation_algorithm

Quote
That's awesome.  I have no idea how to code any of that.   :o
Yeah, it is pretty awesome.  If I hadn't smartened up on Wikipedia beforehand, I doubt I would have been able to code it either.  But now it seems fairly straightforward.

Quote
Amazing! I don't even know what PHP stands for  :P
It stands for PHP: Hypertext Preprocessor, but it's just the language my site was coded in; the maze generator is Javascript.

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: Just released: HTML5/Javascript Maze Generator
« Reply #5 on: December 06, 2011, 06:19:13 PM »
I read the wikipedia on it. so cool :D
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Just released: HTML5/Javascript Maze Generator
« Reply #6 on: December 06, 2011, 07:44:26 PM »
Yeah it's cool. Code is hidden from the user, it's so flexible, data can be saved and did I mention it's so cool?
« Last Edit: December 06, 2011, 07:45:34 PM by Gandolf »

Charlo


  • GMG-er

  • **


  • Posts: 451
Re: Just released: HTML5/Javascript Maze Generator
« Reply #7 on: December 12, 2011, 04:48:26 PM »
UPDATE!  You can now generate "curvy" or "straight" mazes, along with the normal maze type.  Curvy mazes aren't literally curvy, they just have very twisty passageways.  Mazes not based on a rectangular grid are not in the cards at this point.  :D
« Last Edit: December 12, 2011, 04:48:34 PM by Charlo »

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: Just released: HTML5/Javascript Maze Generator
« Reply #8 on: December 13, 2011, 09:06:13 AM »
Oh i didn't notice you can click and move the starting point. And it still finds the shortest path!
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Just released: HTML5/Javascript Maze Generator
« Reply #9 on: December 13, 2011, 09:44:58 AM »
There's a way to generation huge maps without JS timeout. Add a count variable and a maxcount variable. Make the maxcount equal something like 100000. Now up the count variable inside your recursive loops. When count is equal to maxcount stop the recursive loop but save the loop's position. If the recursive loop is unfinished, call a timer on the same method. The timer can be 0.1 sec. That'll allow the method to continuously run until it finishes the looping. Then when the recursion has finished, call the finish code.

An alternative way would be to get the time at the beginning of the method and get the time in the loop and compare the two. If the time is greater than 2 sec, exit the loop and restart the method after 0.1 sec.

P.S These methods mimic a background thread. The timeout for 0.1 sec allows the gui to update and stay responsive.
« Last Edit: December 13, 2011, 09:46:49 AM by Gandolf »

Charlo


  • GMG-er

  • **


  • Posts: 451
Re: Just released: HTML5/Javascript Maze Generator
« Reply #10 on: December 15, 2011, 04:51:35 PM »
I sorta took Gan's advice and made the generation and solving loops non-recursive.  Now I'm basically doing this for the generation:
[code javascript]
while (theMaze.generatedCellCount < theMaze.cellCount - 1) {
    doGeneration();
}[/code]
And
[code javascript]
while (currentCell.isEnd !== true) {
    doSolve();
}[/code]
for solving the maze.  The variables to keep track of the state of each loop (generatedCellCount and currentCell) are declared in a broader scope to make this possible.

Now you can go to 1000x1000 and it only takes a minute to generate!  However, Chrome (which seems to have a faster JS engine than Firefox) choked on 2000x2000 and didn't display anything.  I don't know what happens in any other browsers.
« Last Edit: December 15, 2011, 04:52:46 PM by Charlo »

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Just released: HTML5/Javascript Maze Generator
« Reply #11 on: December 15, 2011, 05:13:30 PM »
When I make a maze around 250x250 cells, the web page's scroll bars get quite longer but the maze doesn't display. My guess is that it hides under the background of the site.

I'm on Safari.

Charlo


  • GMG-er

  • **


  • Posts: 451
Re: Just released: HTML5/Javascript Maze Generator
« Reply #12 on: December 15, 2011, 05:32:40 PM »
Quote
When I make a maze around 250x250 cells, the web page's scroll bars get quite longer but the maze doesn't display. My guess is that it hides under the background of the site.

I'm on Safari.
Try clearing your cache.  Safari might be saving an old version of the page.  Firefox, Chrome, and my old version of Safari don't have a problem with extending way past the boundaries of the page.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Just released: HTML5/Javascript Maze Generator
« Reply #13 on: December 15, 2011, 08:18:55 PM »
Cleared my cache. At size of 250, it doesn't display.
It worked perfectly for Chrome though.

Charlo


  • GMG-er

  • **


  • Posts: 451
Re: Just released: HTML5/Javascript Maze Generator
« Reply #14 on: December 15, 2011, 09:02:38 PM »
Quote
Cleared my cache. At size of 250, it doesn't display.
It worked perfectly for Chrome though.
Weird.  I just upgraded Safari to version 5.0.6, tested it again, and didn't have any problems.

The drawing of the maze to the canvas takes up the most processing power by far, so Safari could be running into some kind of memory limit after it generates the maze.  I saw similar behavior (scroll bars expanding but nothing drawn) in Chrome when I tried a 2000x2000 maze, and have no idea what causes it.  It shouldn't be disappearing behind anything because there's nothing there for it to disappear behind, just a background color.