Posted by Epitaph64 on Mon 25 May 17:27
report abuse | download | new post
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package mazetime;
- /**
- *
- * @author Epitaph64
- */
- public class MazeGenerator {
- private boolean working;
- public Cell[][] maze;
- public int width, height;
- public class Cell
- {
- boolean flooded = false;
- public boolean[] connections = new boolean[4];
- public boolean hasConnections()
- {
- boolean r = false;
- for (int i = 0; i < 4; i++)
- {
- if (connections[i])
- {
- r = true;
- }
- }
- return r;
- }
- public int hasNumberConnections()
- {
- int x = 0;
- for (int i = 0; i < 4; i++)
- {
- if (connections[i])
- {
- x ++;
- }
- }
- return x;
- }
- }
- public Cell[][] generateMazeRecursive(int width, int height)
- {
- this.width = width;
- this.height = height;
- maze = new Cell[width][height];
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- maze[x][y] = new Cell();
- }
- }
- branchOff(cursorX, cursorY);
- return maze;
- }
- public void branchOff(int x, int y)
- {
- if (maze[x][y].hasNumberConnections() <= 1)
- {
- {
- if (y - 1 >= 0)
- {
- if (! maze[x][y-1].hasConnections())
- {
- maze[x][y].connections[0] = true;
- maze[x][y-1].connections[2] = true;
- branchOff(x, y-1);
- }
- }
- }
- else
- {
- if (y + 1 < height)
- {
- if (! maze[x][y+1].hasConnections())
- {
- maze[x][y].connections[2] = true;
- maze[x][y+1].connections[0] = true;
- branchOff(x, y+1);
- }
- }
- }
- if (x + 1 < width)
- {
- if (! maze[x+1][y].hasConnections())
- {
- maze[x][y].connections[1] = true;
- maze[x+1][y].connections[3] = true;
- branchOff(x+1, y);
- }
- }
- if (x - 1 >= 0)
- {
- if (! maze[x-1][y].hasConnections())
- {
- maze[x][y].connections[3] = true;
- maze[x-1][y].connections[1] = true;
- branchOff(x-1, y);
- }
- }
- }
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.