Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Grid {
- private ICell[][] maze;
- private int rows;
- private int columns;
- private static int startX = 0;
- private static int startY = 0;
- private static bool goalFoundFlag = false;
- private static int goalX;
- private static int goalY;
- private static int iterations = 0;
- /*
- *
- */
- private static int MIN_ITERATIONS_GOAL = 10;
- //private static int goalX = rows - 1;
- //private static int goalY = columns - 1;
- /*
- * Initialize the grid.
- */
- public Grid(int rows, int columns) {
- this.rows = rows;
- this.columns = columns;
- /*
- * Initialize grid filled with cells.
- */
- this.maze = new ICell[rows][];
- for(int i = 0; i < rows; i++)
- {
- maze[i] = new ICell[columns];
- }
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- this.maze[i][j] = new Cell(false, i, j);
- }
- }
- /*
- * Set each cell's neighbors.
- */
- for (int x = 0; x < rows; x++) {
- for (int y = 0; y < columns; y++) {
- // east
- if (y + 1 < columns) {
- this.maze[x][y].setEast(this.maze[x][y + 1]);
- } else {
- this.maze[x][y].setEast(new Cell(true, -1, -1));
- }
- //west
- if (y - 1 >= 0) {
- this.maze[x][y].setWest(this.maze[x][y - 1]);
- } else {
- this.maze[x][y].setWest(new Cell(true, -1, -1));
- }
- //north
- if (x - 1 >= 0) {
- this.maze[x][y].setNorth(this.maze[x - 1][y]);
- } else {
- this.maze[x][y].setNorth(new Cell(true, -1, -1));
- }
- //south
- if (x + 1 < rows) {
- this.maze[x][y].setSouth(this.maze[x + 1][y]);
- } else {
- this.maze[x][y].setSouth(new Cell(true, -1, -1));
- }
- }
- }
- }
- /**
- * Generates the maze. This is the start of the algorithm.
- */
- public void generateMaze() {
- ICell exit = this.maze[startX][startY];
- //Start with a cell, name it the exit
- //mark it as visited
- exit.setVisited();
- this.recursion(exit);
- randomization ();
- }
- /**
- * Pokes some holes in the maze to make the path less obvious.
- */
- public void randomization()
- {
- for (int x = 2; x < rows-2; x ++) {
- for (int y = 2; y < columns-2; y ++) {
- ICell temp = maze [x] [y];
- int r = Random.Range (0, 100);
- if (r < 15) {
- r = Random.Range (0, 4);
- ICell neighbor;
- switch (r) {
- case 0:
- neighbor = temp.getNorth ();
- break;
- case 1:
- neighbor = temp.getSouth ();
- break;
- case 2:
- neighbor = temp.getEast ();
- break;
- case 3:
- neighbor = temp.getWest ();
- break;
- default:
- neighbor = temp.getSouth ();
- break;
- }
- this.removeWalls (temp, neighbor);
- }
- }
- }
- }
- /**
- * DFS maze generation algorithm.
- */
- public void recursion(ICell exit) {
- iterations ++;
- ArrayList queue = new ArrayList();
- this.addNeighbors(queue, exit);
- if (queue.Count == 0 && !goalFoundFlag && iterations > MIN_ITERATIONS_GOAL) {
- goalX = exit.getX ();
- goalY = exit.getY ();
- goalFoundFlag = true;
- }
- while (queue.Count > 0) {
- int r = Random.Range (0,queue.Count);
- object[] array = queue.ToArray();
- ICell current = (ICell)array[r];
- queue.RemoveAt(r);
- if (!current.isVisited()) {
- this.removeWalls(current, exit);
- current.setVisited();
- this.recursion(current);
- }
- }
- }
- /**
- * Removes the wall between the two cells.
- */
- public void removeWalls(ICell current, ICell exit) {
- /*
- * North wall.
- */
- if (exit.getNorth().equals(current)) {
- exit.setNorthWall(false);
- current.setSouthWall(false);
- }
- //East wall
- else if (exit.getEast().equals(current)) {
- exit.setEastWall(false);
- current.setWestWall(false);
- }
- //South Wall
- else if (exit.getSouth().equals(current)) {
- exit.setSouthWall(false);
- current.setNorthWall(false);
- }
- // West wall
- else if (exit.getWest().equals(current)) {
- exit.setWestWall(false);
- current.setEastWall(false);
- }
- }
- /**
- * Populates the list of this cell's neighbors.
- */
- public void addNeighbors(ArrayList queue, ICell exit) {
- if (!exit.getNorth().isEmptyValue() && !exit.getNorth().isVisited()) {
- queue.Add(exit.getNorth());
- }
- if (!exit.getEast().isEmptyValue() && !exit.getEast().isVisited()) {
- queue.Add(exit.getEast());
- }
- if (!exit.getSouth().isEmptyValue() && !exit.getSouth().isVisited()) {
- queue.Add(exit.getSouth());
- }
- if (!exit.getWest().isEmptyValue() && !exit.getWest().isVisited()) {
- queue.Add(exit.getWest());
- }
- }
- /**
- * Returns the cell at the given x/y location.
- */
- public ICell getCell(int x, int y)
- {
- return this.maze [x] [y];
- }
- /**
- * Calls to string on the given cell.
- * return - a string representation of the cell
- */
- public string printCell(int x, int y) {
- return this.maze [x] [y].ToString ();
- }
- /**
- * Returns the goal cell location.
- * return - an integer array; x and y value
- */
- public int[] goalCell()
- {
- int[] array = new int[] {goalX, goalY};
- return array;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement