Maze Generator with A* Path Finding

Published: Monday July 28, 2008 at 12:00 AM

View or Add Comments

ActionScript path finding

I recently put together a small maze generator in ActionScript using a depth-first approach. I did it partially because I found what looked like a good implementation of the A path finding algorithm by B. Korsmit that I wanted to try out. The result is nothing spectacular but interesting nonetheless. The A implementation seems decent and fast. There are a few things about its interface that I would change but nothing major - it does the trick quite well. Click on the maze to generate a new one (map size is random and large maps can take a a few seconds to solve on some machines).

While generating the maze, using depth-first can be troublesome since you basically have to recurse through all the grid spaces of the maze touching each grid space at least once. This can cause stack overflows in Flash quite easily. Luckily, Flash throws an error when a stack overflow happens so we can use this to our advantage. What I did was keep a reference to the last grid space used while creating the maze and catch overflows on the initial call to the generate method. When an overflow is caught Flash dumps the stack anyway so we can resume right from where we left off.

Example:

private function generate_path():void {
  _last_step = new Gridspace(0,0);
  resume_path();
}

private function resume_path():void {
  try { step_path(_last_step) }
  catch (e:Error) {
    // We've caught an overflow so resume from where it was last
    resume_path();
  }
}

private function step_path(current_grid:Gridspace):void {
  _last_step = current_grid;
  // Called recursively to generate the path
}

Comments

 
  • From Pete Graham
  • Date Thursday October 23, 2008 at 4:38 PM

This is really impressive, are you planning on releasing the source code for this? I'd love to see it.

  • From Paul
  • Date Thursday October 23, 2008 at 10:47 PM

Thanks! I hadn't planned on releasing the source but I don't see why I couldn't. I'll take a look over it in the next little while and see if it needs any cleanup.

  • From duzid
  • Date Monday December 1, 2008 at 12:20 AM

Hey Paul,

whatever happened to your plan to put the source up? I could really need it well right now. Thanks! d.

  • From Paul
  • Date Monday December 1, 2008 at 4:44 PM

I'll release it eventually but lately I have been totally swamped and I'd like to clean up and generalize the code more before making it public. I'll have some free time near the end of December when I hope to get to this.

 

Add a Comment

*email addresses are not made public

*uses a small subset of markdown for formatting and links