Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6801223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:04:25+00:00 2026-05-26T19:04:25+00:00

I’ve been trying to implement a recursive backtracking maze generation algorithm in javascript. These

  • 0

I’ve been trying to implement a recursive backtracking maze generation algorithm in javascript. These were done after reading a great series of posts on the topic here

While the recursive version of the algorithm was a no brainer, the iterative equivalent has got me stumped.

I thought I understood the concept, but my implementation clearly produces incorrect results. I’ve been trying to pin down a bug that might be causing it, but I am beginning to believe that my problems are being caused by a failure in logic, but of course I am not seeing where.

My understanding of the iterative algorithm is as follows:

  • A stack is created holding representations of cell states.

  • Each representation holds the coordinates of that particular cell, and a list of directions to access adjacent cells.

  • While the stack isn’t empty iterate through the directions on the top of the stack, testing adjacent cells.

  • If a valid cell is found place it at the top of the stack and continue with that cell.

Here is my recursive implementation ( note: keydown to step forward ): http://jsbin.com/urilan/14

And here is my iterative implementation ( once again, keydown to step forward ): http://jsbin.com/eyosij/2

Thanks for the help.

edit: I apologize if my question wasn’t clear. I will try to further explain my problem.

When running the iterative solution various unexpected behaviors occur. First and foremost, the algorithm doesn’t exhaust all available options before backtracking. Rather, it appears to be selecting cells at a random when there is one valid cell left. Overall however, the movement doesn’t appear to be random.

var dirs = [ 'N', 'W', 'E', 'S' ];
var XD = { 'N': 0, 'S':0, 'E':1, 'W':-1 };
var YD = { 'N':-1, 'S':1, 'E':0, 'W': 0 };


function genMaze(){

var dirtemp = dirs.slice().slice();    //copies 'dirs' so its not overwritten or altered
var path = [];                         // stores path traveled.

var stack = [[0,0, shuffle(dirtemp)]]; //Stack of instances. Each subarray in 'stacks' represents a cell
                                       //and its current state. That is, its coordinates, and which adjacent cells have been
                                       //checked. Each time it checks an adjacent cell a direction value is popped from 
                                       //from the list

while ( stack.length > 0 ) {

  var current = stack[stack.length-1]; // With each iteration focus is to be placed on the newest cell.

  var x = current[0], y = current[1], d = current[2];
  var sLen = stack.length;             // For testing whether there is a newer cell in the stack than the current.
  path.push([x,y]);                    // Store current coordinates in the path

  while ( d.length > 0 ) {
    if( stack.length != sLen ){ break;}// If there is a newer cell in stack, break and then continue with that cell

    else {
      var cd = d.pop();
      var nx = x + XD[ cd ];
      var ny = y + YD[ cd ];

      if ( nx >= 0 && ny >= 0  && nx < w && ny < h && !cells[nx][ny] ){

        dtemp = dirs.slice().slice();
        cells[nx][ny] = 1;
        stack.push( [ nx, ny, shuffle(dtemp) ] ); //add new cell to the stack with new list of directions.
                                                  // from here the code should break from the loop and start again with this latest addition being considered.


      }
    }

  }

  if (current[2].length === 0){stack.pop(); } //if all available directions have been tested, remove from stack


}
return path;
}

I hope that helps clear up the question for you. If it is still missing any substance please let me know.

Thanks again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:04:26+00:00Added an answer on May 26, 2026 at 7:04 pm

    I’m not very good in javascript, but I try to implement your recursive code to iterative. You need to store For index on stack also. So code look like:

    function genMaze(cx,cy) {
    
        var dirtemp = dirs;    //copies 'dirs' so its not overwritten
        var path = [];                         // stores path traveled.    
        var stack = [[cx, cy, shuffle(dirtemp), 0]];  // we also need to store `for` indexer
    
        while (stack.length > 0) {
    
            var current = stack[stack.length - 1]; // With each iteration focus is to be placed on the newest cell.
    
            var x = current[0], y = current[1], d = current[2], i = current[3];
            if (i > d.length) {
                stack.pop();
                continue;
            }
            stack[stack.length - 1][3] = i + 1; // for next iteration
    
            path.push([x, y]);    // Store current coordinates in the path
            cells[x][y] = 1;
    
            var cd = d[i];
            var nx = x + XD[cd];
            var ny = y + YD[cd];
    
            if (nx >= 0 && ny >= 0 && nx < w && ny < h && !cells[nx][ny]) {
    
                dtemp = dirs;
                stack.push([nx, ny, shuffle(dtemp), 0]);
            }
        }
        return path;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.