Advertisement
duck

Unity C# maze generation algorithm

Sep 16th, 2013
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1.     IEnumerator DesignMaze()
  2.     {
  3.         // assign tile instances
  4.         tiles = new Tile[sizeX,sizeZ];
  5.         for (int x=0; x<sizeX; ++x)
  6.         for (int z=0; z<sizeZ; ++z)
  7.         {
  8.             tiles[x,z] = new Tile(x,z);
  9.         }
  10.        
  11.         // set tile neighbours
  12.         for (int x=0; x<sizeX; ++x)
  13.         for (int z=0; z<sizeZ; ++z)
  14.         {
  15.             if (z<sizeZ-1) { tiles[x,z].neighbours[0] = tiles[x,z+1]; } // N 0
  16.             if (x<sizeX-1) { tiles[x,z].neighbours[1] = tiles[x+1,z]; } // E 1
  17.             if (z>0      ) { tiles[x,z].neighbours[2] = tiles[x,z-1]; } // S 2
  18.             if (x>0      ) { tiles[x,z].neighbours[3] = tiles[x-1,z]; } // W 3
  19.         }
  20.        
  21.        
  22.         int[] opposite = new int[] { 2,3,0,1 };
  23.  
  24.         Stack<Tile> stack = new Stack<Tile>();
  25.    
  26.         int unvisitedCells = sizeX*sizeZ;
  27.         Tile current = tiles[Random.Range(0,sizeX), Random.Range(0,sizeZ)];
  28.        
  29.         current.visited = true; --unvisitedCells;
  30.        
  31.         bool start = true;
  32.    
  33.         while (unvisitedCells > 0)
  34.         {
  35.            
  36.             int[] unvisitedNeighbours = current.UnvisitedNeighbours();
  37.  
  38.             if (unvisitedNeighbours.Length > 0)
  39.             {
  40.                 int pickNum = unvisitedNeighbours[Random.Range(0,unvisitedNeighbours.Length)];
  41.                
  42.                 if (start)
  43.                 {
  44.                     start = false;
  45.                     startPos = new Vector3(current.x*tileSize*2,0,current.z*tileSize*2);
  46.                     startRot = Quaternion.Euler( 0, pickNum*90, 0 );
  47.                 }
  48.                
  49.                 stack.Push(current);
  50.                
  51.                 // break wall:
  52.                 current.walls[pickNum] = false;
  53.                 current.neighbours[pickNum].walls[ opposite[pickNum] ] = false;
  54.                 current = current.neighbours[pickNum];
  55.                 current.visited = true; --unvisitedCells;
  56.                
  57.             } else {
  58.                
  59.                 current = stack.Pop();
  60.             }
  61.         }
  62.                
  63.         yield return null;
  64.        
  65.     }
  66.    
  67.    
  68.    
  69.    
  70.    
  71.    
  72.    
  73.    
  74.    
  75.    
  76.    
  77.    
  78.  
  79. class Tile {
  80.    
  81.     public bool visited = false;
  82.  
  83.     public bool[] walls;
  84.    
  85.     public Tile[] neighbours;
  86.     public int x;
  87.     public int z;
  88.    
  89.     public Tile(int x, int z)
  90.     {
  91.         this.x = x;
  92.         this.z = z;
  93.         walls = new bool[] { true, true, true, true };  // N E S W 
  94.         neighbours = new Tile[4];  // N E S W    
  95.        
  96.     }
  97.    
  98.     public int[] UnvisitedNeighbours ()
  99.     {
  100.         List<int> unvisited = new List<int>();
  101.         for (int n=0; n<4; ++n)
  102.         {
  103.             if (neighbours[n] != null && !neighbours[n].visited)
  104.             {
  105.                 unvisited.Add(n);
  106.             }
  107.         }
  108.         return unvisited.ToArray();
  109.     }
  110.    
  111.     public override string ToString ()
  112.     {
  113.         return string.Format ("[Tile "+x+","+z+"]");
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement