# Maze Generator: Recursive VEX code

## Recommended Posts

Hi, im build a Maze generator with vex. But i went into an issue by setting up the right iterations. I developed the code in a solver sop because it was easy for me to understand whats happening. But now i want the code iterating till the maze is finished. So I want the iterations to be driven by a condition, for example (and not by frames). Does anybody know how to make a recursive version of the code, so i will be able to setup a condition?

```function int[] nunvisited(int inpoint){
int pushed[] = array();
foreach(int n; neighbours(0,inpoint)){
if(inpointgroup(0,"visited",n)!=1){
push(pushed,n);
}
}
return pushed;
}

function void movefor(int cpoint){
int neighbours[] = nunvisited(cpoint);
int randbour = int(rand(cpoint+25)*len(neighbours));
int npoint = neighbours[randbour];
int visitedlist[] = detail(0,"visitedlist");
push(visitedlist, npoint);
setdetailattrib(0,"visitedlist",visitedlist,"set");
//enable visited group
setpointgroup(0,"visited",npoint,1,"set");
//disable current group
setpointgroup(0,"current",cpoint,0,"set");
//enable current group
setpointgroup(0,"current",npoint,1,"set");
//color
setpointattrib(0,"Cd",npoint, {0,0,1}, "set");
setprimgroup(0,"line",l,1,"set");
}

function void moveback(int cpoint){
int list[] = detail(0,"visitedlist");
int lastindex = len(list)-2;
int ppoint = list[lastindex];
//delete point from visitedlist array
int visitedlist[] = detail(0,"visitedlist");
removeindex(visitedlist,len(list)-1);
setdetailattrib(0,"visitedlist",visitedlist,"set");
//disable group
setpointgroup(0,"current",cpoint,0,"set");
//enable group
setpointgroup(0,"current",ppoint,1,"set");
setpointattrib(0,"Cd",ppoint, {1,0,0}, "set");
}

if(len(nunvisited(@ptnum))<1){
moveback(@ptnum);
}else{
movefor(@ptnum);
}```

##### Share on other sites

If you could provide your scene so we know the setup that needs to go before the solver, then we could help better.
I assume it's just a group where you set the "current" group to a single point, but I don't know for sure.

Anyways, I would use a For Each Number loop, set to Feedback Each Iteration and Fetch Feedback.
Iterations would be the max iteration amount. Basically, how long you are willing to wait for.

There is a Stop Condition. That would be an expression to fetch a detail attribute, set with the wrangle.
I'd make a "stop" detail attribute, and set it to 1 when you want to stop the loop.
Then, just reference it directly with the detail() expression.

What you can then do is just put a big number in Iterations (but not too large that if there's an issue, it doesn't take forever to stop), and check at each iteration if the maze is done.
In the example, I just check visitedlist's length, and if it's bigger than 100, I stop.

```// Arbitrary stop condition, for testing
int list[] = detail(0,"visitedlist");
if(len(list) > 100)
setdetailattrib(0, "stop", 1);```

You can see that in this case, the loop ran 158 times before it stopped.

The downside with this compared to a Solver is that you cannot see the path it takes through each iterations.
To solve that, you could store the iteration number on the "current" point, so that it gives you the path to visualize after.
Then, you could make a visualizer wrangle that sets the colors based on the "iteration" value on each point, based on the current frame.

Edited by Alain2131
##### Share on other sites

Thank you. I didnt know about that Stop condition in the sop loop. You assumed correctly, that the "current" point group is assigned to a single point. But here is the full setup for those who are interested. (without the stop condition. Im working on that)

maze_generator.hipnc

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×