Advertisement
Guest User

Untitled

a guest
Sep 21st, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Grid {
  6.  
  7.     private ICell[][] maze;
  8.     private int rows;
  9.     private int columns;
  10.    
  11.     private static int startX = 0;
  12.     private static int startY = 0;
  13.     private static bool goalFoundFlag = false;
  14.     private static int goalX;
  15.     private static int goalY;
  16.     private static int iterations = 0;
  17.     /*
  18.      *
  19.      */
  20.     private static int MIN_ITERATIONS_GOAL = 10;
  21.     //private static int goalX = rows - 1;
  22.     //private static int goalY = columns - 1;
  23.    
  24.     /*
  25.      * Initialize the grid.
  26.      */
  27.     public Grid(int rows, int columns) {
  28.         this.rows = rows;
  29.         this.columns = columns;
  30.         /*
  31.          * Initialize grid filled with cells.
  32.          */
  33.         this.maze = new ICell[rows][];
  34.         for(int i = 0; i < rows; i++)
  35.         {
  36.             maze[i] = new ICell[columns];
  37.         }
  38.         for (int i = 0; i < rows; i++) {
  39.             for (int j = 0; j < columns; j++) {
  40.                 this.maze[i][j] = new Cell(false, i, j);
  41.             }
  42.         }
  43.         /*
  44.          * Set each cell's neighbors.
  45.          */
  46.         for (int x = 0; x < rows; x++) {
  47.             for (int y = 0; y < columns; y++) {
  48.                 // east
  49.                 if (y + 1 < columns) {
  50.                     this.maze[x][y].setEast(this.maze[x][y + 1]);
  51.                 } else {
  52.                     this.maze[x][y].setEast(new Cell(true, -1, -1));
  53.                 }
  54.                 //west
  55.                 if (y - 1 >= 0) {
  56.                     this.maze[x][y].setWest(this.maze[x][y - 1]);
  57.                 } else {
  58.                     this.maze[x][y].setWest(new Cell(true, -1, -1));
  59.                 }
  60.                 //north
  61.                 if (x - 1 >= 0) {
  62.                     this.maze[x][y].setNorth(this.maze[x - 1][y]);
  63.                 } else {
  64.                     this.maze[x][y].setNorth(new Cell(true, -1, -1));
  65.                 }
  66.                 //south
  67.                 if (x + 1 < rows) {
  68.                     this.maze[x][y].setSouth(this.maze[x + 1][y]);
  69.                 } else {
  70.                     this.maze[x][y].setSouth(new Cell(true, -1, -1));
  71.                 }
  72.                
  73.             }
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * Generates the maze. This is the start of the algorithm.
  79.      */
  80.     public void generateMaze() {
  81.         ICell exit = this.maze[startX][startY];
  82.         //Start with a cell, name it the exit
  83.         //mark it as visited
  84.         exit.setVisited();
  85.         this.recursion(exit);
  86.         randomization ();
  87.     }
  88.     /**
  89.      * Pokes some holes in the maze to make the path less obvious.
  90.      */
  91.     public void randomization()
  92.     {
  93.         for (int x = 2; x < rows-2; x ++) {
  94.             for (int y = 2; y < columns-2; y ++) {
  95.                 ICell temp = maze [x] [y];
  96.                 int r = Random.Range (0, 100);
  97.                 if (r < 15) {
  98.                     r = Random.Range (0, 4);
  99.                     ICell neighbor;
  100.                     switch (r) {
  101.                     case 0:
  102.                         neighbor = temp.getNorth ();
  103.                         break;
  104.                     case 1:
  105.                         neighbor = temp.getSouth ();
  106.                         break;
  107.                     case 2:
  108.                         neighbor = temp.getEast ();
  109.                         break;
  110.                     case 3:
  111.                         neighbor = temp.getWest ();
  112.                         break;
  113.                     default:
  114.                         neighbor = temp.getSouth ();
  115.                         break;
  116.                     }
  117.                     this.removeWalls (temp, neighbor);
  118.                 }
  119.             }
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * DFS maze generation algorithm.
  125.      */
  126.     public void recursion(ICell exit) {
  127.         iterations ++;
  128.         ArrayList queue = new ArrayList();
  129.         this.addNeighbors(queue, exit);
  130.         if (queue.Count == 0 && !goalFoundFlag && iterations > MIN_ITERATIONS_GOAL) {
  131.             goalX = exit.getX ();
  132.             goalY = exit.getY ();
  133.             goalFoundFlag = true;
  134.         }
  135.         while (queue.Count > 0) {
  136.             int r = Random.Range (0,queue.Count);
  137.             object[] array = queue.ToArray();
  138.             ICell current = (ICell)array[r];
  139.             queue.RemoveAt(r);
  140.             if (!current.isVisited()) {
  141.                 this.removeWalls(current, exit);
  142.                 current.setVisited();
  143.                 this.recursion(current);
  144.             }
  145.         }
  146.     }
  147.  
  148.     /**
  149.      * Removes the wall between the two cells.
  150.      */
  151.     public void removeWalls(ICell current, ICell exit) {
  152.         /*
  153.          * North wall.
  154.          */
  155.         if (exit.getNorth().equals(current)) {
  156.             exit.setNorthWall(false);
  157.             current.setSouthWall(false);
  158.         }
  159.         //East wall
  160.         else if (exit.getEast().equals(current)) {
  161.             exit.setEastWall(false);
  162.             current.setWestWall(false);
  163.         }
  164.         //South Wall
  165.         else if (exit.getSouth().equals(current)) {
  166.             exit.setSouthWall(false);
  167.             current.setNorthWall(false);
  168.         }
  169.         // West wall
  170.         else if (exit.getWest().equals(current)) {
  171.             exit.setWestWall(false);
  172.             current.setEastWall(false);
  173.         }
  174.     }
  175.  
  176.     /**
  177.      * Populates the list of this cell's neighbors.
  178.      */
  179.     public void addNeighbors(ArrayList queue, ICell exit) {
  180.         if (!exit.getNorth().isEmptyValue() && !exit.getNorth().isVisited()) {
  181.             queue.Add(exit.getNorth());
  182.         }
  183.         if (!exit.getEast().isEmptyValue() && !exit.getEast().isVisited()) {
  184.             queue.Add(exit.getEast());
  185.         }
  186.         if (!exit.getSouth().isEmptyValue() && !exit.getSouth().isVisited()) {
  187.             queue.Add(exit.getSouth());
  188.         }
  189.         if (!exit.getWest().isEmptyValue() && !exit.getWest().isVisited()) {
  190.             queue.Add(exit.getWest());
  191.         }
  192.     }
  193.  
  194.     /**
  195.      * Returns the cell at the given x/y location.
  196.      */
  197.     public ICell getCell(int x, int  y)
  198.     {
  199.         return this.maze [x] [y];
  200.     }
  201.  
  202.     /**
  203.      * Calls to string on the given cell.
  204.      * return - a string representation of the cell
  205.      */
  206.     public string printCell(int x, int y) {
  207.         return this.maze [x] [y].ToString ();
  208.     }
  209.  
  210.     /**
  211.      * Returns the goal cell location.
  212.      * return - an integer array; x and y value
  213.      */
  214.     public int[] goalCell()
  215.     {
  216.         int[] array = new int[] {goalX, goalY};
  217.         return array;
  218.     }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement