Jump to content

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)){
return pushed;

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

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");
    //disable group
    //enable group
    setpointattrib(0,"Cd",ppoint, {1,0,0}, "set");



maze generator in solver.png

Link to comment
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
Link to comment
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

  • Create New...