Advertisement
Guest User

Maze Generator

a guest
Apr 28th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class MazeGenerator {
  6.     int[,] maze;
  7.     int width;
  8.     int height;
  9.  
  10.     public int[,] generateMaze(int height, int width) {
  11.         maze = new int[height, width];
  12.         width = width;
  13.         height = height;
  14.         // Initialize
  15.         for (int i = 0; i < height; i++)
  16.             for (int j = 0; j < width; j++)
  17.                 maze[i, j] = 1;
  18.  
  19.      Random rand = new Random();
  20.      // r for row、c for column
  21.      // Generate random r
  22.      int r = rand.Next(height);
  23.      while (r % 2 == 0) {
  24.          r = rand.Next(height);
  25.      }
  26.      // Generate random c
  27.      int c = rand.Next(width);
  28.      while (c % 2 == 0) {
  29.          c = rand.Next(width);
  30.      }
  31.      // Starting cell
  32.      maze[r, c] = 0;
  33.  
  34.      // Allocate the maze with recursive method
  35.      recursion(r, c);
  36.  
  37.      return maze;
  38.  }
  39.  
  40.  public void recursion(int r, int c) {
  41.      // 4 random directions
  42.      int[] randDirs = generateRandomDirections();
  43.      // Examine each direction
  44.      for (int i = 0; i < randDirs.Count(); i++) {
  45.  
  46.          switch(randDirs[i]){
  47.          case 1: // Up
  48.              // Whether 2 cells up is out or not
  49.              if (r - 2 <= 0)
  50.                  continue;
  51.              if (maze[r - 2, c] != 0) {
  52.                  maze[r-2, c] = 0;
  53.                  maze[r-1, c] = 0;
  54.                  recursion(r - 2, c);
  55.              }
  56.              break;
  57.          case 2: // Right
  58.              // Whether 2 cells to the right is out or not
  59.              if (c + 2 >= width - 1)
  60.                  continue;
  61.              if (maze[r, c + 2] != 0) {
  62.                  maze[r, c + 2] = 0;
  63.                  maze[r, c + 1] = 0;
  64.                  recursion(r, c + 2);
  65.              }
  66.              break;
  67.          case 3: // Down
  68.              // Whether 2 cells down is out or not
  69.              if (r + 2 >= height - 1)
  70.                  continue;
  71.              if (maze[r + 2,c] != 0) {
  72.                  maze[r+2,c] = 0;
  73.                  maze[r+1,c] = 0;
  74.                  recursion(r + 2, c);
  75.              }
  76.              break;
  77.          case 4: // Left
  78.              // Whether 2 cells to the left is out or not
  79.              if (c - 2 <= 0)
  80.                  continue;
  81.              if (maze[r,c - 2] != 0) {
  82.                  maze[r,c - 2] = 0;
  83.                  maze[r,c - 1] = 0;
  84.                  recursion(r, c - 2);
  85.              }
  86.              break;
  87.          }
  88.      }
  89.  
  90.  }
  91.  
  92.  /**
  93.  * Generate an array with random directions 1-4
  94.  * @return Array containing 4 directions in random order
  95.  */
  96.  public int[] generateRandomDirections() {
  97.       List<int> randoms = new List<int>();
  98.       for (int i = 0; i < 4; i++)
  99.            randoms.Add(i + 1);
  100.      Random rnd = new Random();
  101.       randoms.OrderBy(x => rnd.Next());
  102.  
  103.      return randoms.ToArray();
  104.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement