Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int sizex = 100;
- int sizey = 100;
- MazeGenKruskal gen;
- void setup()
- {
- size(700, 700, P2D);
- noLoop();
- gen = new MazeGenKruskal();
- }
- void draw()
- {
- drawMaze(gen.createMaze(sizex, sizey));
- }
- void keyPressed()
- {
- redraw();
- }
- void drawMaze(Cell[][] maze)
- {
- background(0xFFAAAAAA);
- float cellwidth = (float) width / sizex;
- float cellheight = (float) height / sizey;
- for (int x = 0; x < sizex; x++)
- {
- for (int y = 0; y < sizey; y++)
- {
- pushMatrix();
- translate(cellwidth * x, cellheight * y);
- maze[x][y].draw(cellwidth, cellheight);
- popMatrix();
- }
- }
- }
- class Cell
- {
- Wall[] walls;
- Cell next;
- MazeGenKruskal.EquivalenceClass equivalenceClass;
- boolean solution;
- int type;
- Cell()
- {
- walls = new Wall[4];
- solution = true;
- type = 0;
- next = null;
- }
- void draw(float width, float height)
- {
- noStroke();
- fill(type == 1 ? 0xFF00FF00 : type == 2 ? 0xFFFF0000 : solution ? 0xFF007777 : 0xFFAAAAAA);
- rect(0, 0, width, height);
- stroke(0);
- strokeWeight(1);
- if (walls[0].standing)
- {
- line(0, 0, width, 0);
- }
- if (walls[1].standing)
- {
- line(width, 0, width, height);
- }
- if (walls[2].standing)
- {
- line(0, height, width, height);
- }
- if (walls[3].standing)
- {
- line(0, 0, 0, height);
- }
- point(0,0);
- point(0,width);
- point(width,height);
- point(0,height);
- }
- }
- class Wall
- {
- boolean standing;
- Cell c1, c2;
- Wall(Cell c1, Cell c2)
- {
- standing = true;
- this.c1 = c1;
- this.c2 = c2;
- }
- }
- class MazeGenKruskal
- {
- Cell[][] maze = null;
- int sizex = 0, sizey = 0;
- Wall[] walls;
- int index;
- int count;
- Cell[][] createMaze(int x, int y)
- {
- maze = new Cell[x][y];
- sizex = x;
- sizey = y;
- walls = new Wall[((sizex - 1) * sizey) + ((sizey - 1) * sizex)];
- index = 0;
- count = sizex * sizey;
- createEmptyMaze();
- createEquivalenceClasses();
- process();
- solveMaze();
- return maze;
- }
- void solveMaze()
- {
- for (int x = 0; x < sizex; x++)
- {
- for (int y = 0; y < sizey; y++)
- {
- if (maze[x][y].solution)
- {
- Cell c = maze[x][y];
- int exits;
- do
- {
- if (c.type > 0)
- {
- break;
- }
- exits = 0;
- Cell co = null;
- for (int i = 0; i < 4; i++)
- {
- Wall w = c.walls[i];
- if (w.standing)
- {
- continue;
- }
- Cell tempc = (c == w.c1) ? w.c2 : w.c1;
- if (tempc.solution)
- {
- exits++;
- co = tempc;
- }
- }
- if (exits == 1)
- {
- c.solution = false;
- c = co;
- }
- }
- while (exits == 1);
- }
- }
- }
- }
- void process()
- {
- while (count > 1)
- {
- Wall w = walls[index];
- index++;
- if (w.c1.equivalenceClass != w.c2.equivalenceClass)
- {
- w.standing = false;
- union(w.c1.equivalenceClass, w.c2.equivalenceClass);
- }
- }
- }
- void createEquivalenceClasses()
- {
- for (int x = 0; x < sizex; x++)
- {
- for (int y = 0; y < sizey; y++)
- {
- new EquivalenceClass(maze[x][y]);
- }
- }
- }
- void createEmptyMaze()
- {
- for (int x = 0; x < sizex; x++)
- {
- for (int y = 0; y < sizey; y++)
- {
- maze[x][y] = new Cell();
- }
- }
- for (int y = 0; y < sizey; y++)
- {
- maze[0][y].walls[3] = new Wall(null, null);
- maze[sizex - 1][y].walls[1] = new Wall(null, null);
- }
- for (int x = 0; x < sizex; x++)
- {
- maze[x][0].walls[0] = new Wall(null, null);
- maze[x][sizey - 1].walls[2] = new Wall(null, null);
- }
- for (int x = 0; x < sizex - 1; x++)
- {
- for (int y = 0; y < sizey; y++)
- {
- Wall w = new Wall(maze[x][y], maze[x + 1][y]);
- maze[x][y].walls[1] = w;
- maze[x + 1][y].walls[3] = w;
- walls[index++] = w;
- }
- }
- for (int y = 0; y < sizey - 1; y++)
- {
- for (int x = 0; x < sizex; x++)
- {
- Wall w = new Wall(maze[x][y], maze[x][y + 1]);
- maze[x][y].walls[2] = w;
- maze[x][y + 1].walls[0] = w;
- walls[index++] = w;
- }
- }
- index = 0;
- maze[0][0].type = 1;
- maze[sizex - 1][sizey - 1].type = 2;
- Random r = new Random();
- for (int i = 0; i < walls.length; i++)
- {
- int pos = r.nextInt(walls.length - i) + i;
- Wall temp = walls[i];
- walls[i] = walls[pos];
- walls[pos] = temp;
- }
- }
- void union(EquivalenceClass l1, EquivalenceClass l2)
- {
- if (l1.count < l2.count)
- l2.joinClass(l1);
- else
- l1.joinClass(l2);
- count--;
- }
- class EquivalenceClass
- {
- int count;
- Cell first = null;
- Cell last = null;
- EquivalenceClass(Cell c)
- {
- first = c;
- last = c;
- count = 1;
- c.equivalenceClass = this;
- }
- void joinClass(EquivalenceClass l)
- {
- Cell c = l.first;
- do
- {
- c.equivalenceClass = this;
- }
- while ((c = c.next) != null);
- l.last.next = first;
- first = l.first;
- count += l.count;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement