Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class MazeGenerator {
- int[,] maze;
- int width;
- int height;
- public int[,] generateMaze(int height, int width) {
- maze = new int[height, width];
- width = width;
- height = height;
- // Initialize
- for (int i = 0; i < height; i++)
- for (int j = 0; j < width; j++)
- maze[i, j] = 1;
- Random rand = new Random();
- // r for row、c for column
- // Generate random r
- int r = rand.Next(height);
- while (r % 2 == 0) {
- r = rand.Next(height);
- }
- // Generate random c
- int c = rand.Next(width);
- while (c % 2 == 0) {
- c = rand.Next(width);
- }
- // Starting cell
- maze[r, c] = 0;
- // Allocate the maze with recursive method
- recursion(r, c);
- return maze;
- }
- public void recursion(int r, int c) {
- // 4 random directions
- int[] randDirs = generateRandomDirections();
- // Examine each direction
- for (int i = 0; i < randDirs.Count(); i++) {
- switch(randDirs[i]){
- case 1: // Up
- // Whether 2 cells up is out or not
- if (r - 2 <= 0)
- continue;
- if (maze[r - 2, c] != 0) {
- maze[r-2, c] = 0;
- maze[r-1, c] = 0;
- recursion(r - 2, c);
- }
- break;
- case 2: // Right
- // Whether 2 cells to the right is out or not
- if (c + 2 >= width - 1)
- continue;
- if (maze[r, c + 2] != 0) {
- maze[r, c + 2] = 0;
- maze[r, c + 1] = 0;
- recursion(r, c + 2);
- }
- break;
- case 3: // Down
- // Whether 2 cells down is out or not
- if (r + 2 >= height - 1)
- continue;
- if (maze[r + 2,c] != 0) {
- maze[r+2,c] = 0;
- maze[r+1,c] = 0;
- recursion(r + 2, c);
- }
- break;
- case 4: // Left
- // Whether 2 cells to the left is out or not
- if (c - 2 <= 0)
- continue;
- if (maze[r,c - 2] != 0) {
- maze[r,c - 2] = 0;
- maze[r,c - 1] = 0;
- recursion(r, c - 2);
- }
- break;
- }
- }
- }
- /**
- * Generate an array with random directions 1-4
- * @return Array containing 4 directions in random order
- */
- public int[] generateRandomDirections() {
- List<int> randoms = new List<int>();
- for (int i = 0; i < 4; i++)
- randoms.Add(i + 1);
- Random rnd = new Random();
- randoms.OrderBy(x => rnd.Next());
- return randoms.ToArray();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement