Loading...
Logo
Processing Forum

Maze making

in Programming Questions  •  2 years ago  
Today I made a sketch that makes mazes... A lot of debugging but i sorted it out. The problem is it takes a long time to do it. Whenever the point is stuck it goes only on white squares(at random) until it finds somewhere thats good to go...


int x=100;
int y=100;
int syze=10;
int px, py;
boolean rightstuck=false;
boolean leftstuck=false;
boolean downstuck=false;
boolean upstuck=false;
int stuckdir=0;
int stuckcount=0;
int ssolvecount1=0;
int ssolvecount2=0;
int ssolvecount3=0;
int ssolvecount4=0;



void setup() {
  size(500, 500);
  background(0);
  smooth();
  frameRate(100);
}
/* rm numbers
 1 is right
 2 is down
 3 is left
 4 is up
 */



void draw() {
  px=x;
  py=y;


  //moving machine 
  rightstuck=true;
  leftstuck=true;
  downstuck=true;
  upstuck=true;
  float rm=random(1, 5);
  //------------------------------------------------------------------------------------------------ 

    if (int(rm)==1) {
      if (get(x+syze, y)==-16777216) {//the spot where its going
        if (get(x+syze, y-syze)==-16777216) {//the spot below where its going
          if (get(x+syze, y+syze)==-16777216) {//the spot above where its going
            if (get(x+syze*2, y)==-16777216) {//the spot 2 spots away from where its going
              if (get(x+syze*2, y+syze)==-16777216) {//the spot above 2 spots away from where its going
                if (get(x+syze*2, y-syze)==-16777216) {//the spot below 2 spots away from where its going
                  if (x+syze<width) {
                    x=x+syze;
                    rightstuck=false;
                    //        println("1 is bad");
                  }
                }
              }
            }
          }
        }
      }
    }
    //------------------------------------------------------------------------------------------------ 
    if (int(rm)==2) {
      if (get(x, y+syze)==-16777216) {
        if (get(x-syze, y+syze)==-16777216) {
          if (get(x+syze, y+syze)==-16777216) {
            if (get(x, y+syze*2)==-16777216) {
              if (get(x+syze, y+syze*2)==-16777216) {
                if (get(x-syze, y+syze*2)==-16777216) {
                  if (y+syze<width) {
                    y=y+syze;
                    downstuck=false;
                    //        println("2 is bad");
                  }
                }
              }
            }
          }
        }
      }
    }
    //------------------------------------------------------------------------------------------------ 
    if (int(rm)==3) {
      if (get(x-syze, y)==-16777216) {
        if (get(x-syze, y-syze)==-16777216) {
          if (get(x-syze, y+syze)==-16777216) {
            if (get(x-syze*2, y)==-16777216) {
              if (get(x-syze*2, y+syze)==-16777216) {
                if (get(x-syze*2, y-syze)==-16777216) {
                  if (x-syze>0) {
                    x=x-syze;
                    leftstuck=false;
                    //        println("3 is bad");
                  }
                }
              }
            }
          }
        }
      }
    }
    //------------------------------------------------------------------------------------------------ 
    if (int(rm)==4) {
      if (get(x, y-syze)==-16777216) {
        if (get(x-syze, y-syze)==-16777216) {
          if (get(x+syze, y-syze)==-16777216) {
            if (get(x, y-syze*2)==-16777216) {
              if (get(x+syze, y-syze*2)==-16777216) {
                if (get(x-syze, y-syze*2)==-16777216) {
                  if (y-syze>0) {
                    y=y-syze;
                    upstuck=false;
                    //        println("4 is bad");
                  }
                }
              }
            }
          }
        }
      }
    }
  
  //------------------------------------------------------------------------------------------------ 


  //stuck machine
  if (rightstuck==true && leftstuck==true && upstuck==true && downstuck==true) {
    stuckcount++;
  }
  else {
    stuckcount=0;
  }

  if (stuckcount>20) {
    println("stuck");


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++NEW MACHINE+++++++++++++++++++++NEW MACHINE++++++++++++++++++++++++++++++++++++++++++++++++++++++++NEW MACHINE
/*
1 is right
2 is left
3 is down
4 is up
*/

float stuckdir=random(1,5);
if(int(stuckdir)==1){
  if(get(x+syze,y)==-1){
    x=x+syze;
  }
}
if(int(stuckdir)==2){
  if(get(x-syze,y)==-1){
    x=x-syze;
  }
}
if(int(stuckdir)==3){
  if(get(x,y+syze)==-1){
    y=y+syze;
  }
}
if(int(stuckdir)==4){
  if(get(x,y-syze)==-1){
    y=y-syze;
  }
}
  }


  noStroke();
  fill(255);
  rect(px, py, syze, syze);

  fill(255, 0, 0);
  rect(x, y, syze, syze);


  println(stuckcount);
  }



That's it... I'm asking how I could make it faster, because I really can't use this when it's this slow... I've seen mazes that generate instantly, but i have no idea of how that works. Thanks

Replies(8)

Re: Maze making

2 years ago
I haven't had the time to look at the code in depth, but wow. I've never seen so many nested conditions!

Try at least using some AND (&&) and OR (||) conjunctions to make things a little more readable:
Copy code
  1. if (int(rm)==4 
  2.     && get(x, y-syze)==-16777216 
  3.     && get(x-syze, y-syze)==-16777216
  4.     && get(x+syze, y-syze)==-16777216
  5.     && get(x, y-syze*2)==-16777216
  6.     && get(x+syze, y-syze*2)==-16777216 
  7.     && get(x-syze, y-syze*2)==-16777216
  8.     && y-syze>0) {
  9.       
  10.   y=y-syze;
  11.   upstuck=false;
  12.   
  13. }

Haven't tried it, but it should work. And I'll stress that this might not be the best way to go about it. From the little I've read about programming I've gleaned that if you find yourself waist deep in conditions and loops there's probably something wrong with the way you're tackling the problem.

Re: Maze making

2 years ago
Haha it was my first time through so I let it get sloppy... And i agree I am probably missing some vital things to make this easier... I found it easiest to break it up into 2 different parts, which i called machines. One for moving when it's not stuck, and another for moving when it is stuck... My machine for moving when the point isn't stuck is good, and I think I inadvertently ended up with Prim's Algorithm.. but the stuck engine takes forever because it just moves randomly... and I have no idea how to make a more efficient one

Re: Re: Maze making

2 years ago
Did you look into maze generation before you started? I know it's a basic question, but it might save loads of time if you adapt something that already exists rather than trying to reinvent the wheel.

Re: Re: Maze making

2 years ago
bjzaba, dillard5 gave a link to the Wikipedia page on maze generation... I also recommend the Think Labyrinth site!

Re: Maze making

2 years ago
Thanks for the link... Interesting stuff... I also was looking at krazydad's page. I guess I could just try another way to find unused space whenever it gets stuck... But It'll probably take a long time to implement with the tools I have

Re: Maze making

2 years ago
Also avoid "magic numbers" like 16777216. Make it a constant, like COLOR_TO_DETECT or something, and use the Processing color facilities (#FF0000 or color(255, 0, 0) or other). It is more readable and easier to change later.

Re: Maze making

1 years ago
So I did make it faster... I added a for loop over my whole draw(){. Presumably it makes mazes 1000 times faster now if my for loop draws when it reaches 1000, which was a pleasant surprise...

Re: Maze making

1 year ago
Kudos to you man, great work on the maze generation algorithm