|
| 1 | +public class MazeRecursiveGenerator |
| 2 | +{ |
| 3 | + public enum MazeMode { |
| 4 | + OnePath, |
| 5 | + FilledDeadEnds, |
| 6 | + Loops |
| 7 | + }; |
| 8 | + |
| 9 | + private static readonly (int, int)[] Directions = { (0, -1), (1, 0), (0, 1), (-1, 0) }; // Up, Right, Down, Left |
| 10 | + private static Random random = new Random(); |
| 11 | + |
| 12 | + public static bool[,] GenerateMaze(int width, int height, MazeMode mazeMode = MazeMode.OnePath) |
| 13 | + { |
| 14 | + if (width % 2 == 0 || height % 2 == 0) |
| 15 | + throw new ArgumentException("Width and height must be odd numbers for a proper maze."); |
| 16 | + |
| 17 | + bool[,] maze = new bool[width, height]; // by default, everything is a wall (cell value == false) |
| 18 | + |
| 19 | + // Start the maze generation |
| 20 | + GenerateMazeRecursive(maze, 1, 1); |
| 21 | + |
| 22 | + // Make sure the entrance and exit are open |
| 23 | + maze[0, 1] = true; // Entrance |
| 24 | + maze[width - 1, height - 2] = true; // Exit |
| 25 | + |
| 26 | + if(mazeMode == MazeMode.FilledDeadEnds) |
| 27 | + FillDeadEnds(maze); |
| 28 | + |
| 29 | + else if(mazeMode == MazeMode.Loops) |
| 30 | + RemoveDeadEnds(maze); |
| 31 | + |
| 32 | + return maze; |
| 33 | + } |
| 34 | + |
| 35 | + private static void GenerateMazeRecursive(bool[,] maze, int x, int y) |
| 36 | + { |
| 37 | + maze[x, y] = true; |
| 38 | + |
| 39 | + // Shuffle directions |
| 40 | + var shuffledDirections = ShuffleDirections(); |
| 41 | + |
| 42 | + foreach (var (dx, dy) in shuffledDirections) |
| 43 | + { |
| 44 | + int nx = x + dx * 2; |
| 45 | + int ny = y + dy * 2; |
| 46 | + |
| 47 | + // Check if the new position is within bounds and not visited |
| 48 | + if (IsInBounds(maze, nx, ny) && !maze[nx, ny]) |
| 49 | + { |
| 50 | + // Carve a path |
| 51 | + maze[x + dx, y + dy] = true; |
| 52 | + GenerateMazeRecursive(maze, nx, ny); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static List<(int, int)> ShuffleDirections() |
| 58 | + { |
| 59 | + var directions = new List<(int, int)>(Directions); |
| 60 | + for (int i = directions.Count - 1; i > 0; i--) |
| 61 | + { |
| 62 | + int j = random.Next(i + 1); |
| 63 | + (directions[i], directions[j]) = (directions[j], directions[i]); |
| 64 | + } |
| 65 | + return directions; |
| 66 | + } |
| 67 | + |
| 68 | + private static bool IsInBounds(bool[,] maze, int x, int y) |
| 69 | + { |
| 70 | + return x > 0 && y > 0 && x < maze.GetLength(0) - 1 && y < maze.GetLength(1) - 1; |
| 71 | + } |
| 72 | + |
| 73 | + private static void FillDeadEnds(bool[,] maze) |
| 74 | + { |
| 75 | + bool removed; |
| 76 | + do |
| 77 | + { |
| 78 | + removed = false; |
| 79 | + for (int x = 1; x < maze.GetLength(0) - 1; x++) |
| 80 | + { |
| 81 | + for (int y = 1; y < maze.GetLength(1) - 1; y++) |
| 82 | + { |
| 83 | + if (maze[x, y]) // If it's a path |
| 84 | + { |
| 85 | + int neighbors = 0; |
| 86 | + foreach (var (dx, dy) in Directions) |
| 87 | + { |
| 88 | + if (maze[x + dx, y + dy]) |
| 89 | + neighbors++; |
| 90 | + } |
| 91 | + if (neighbors <= 1) // If it's a dead end |
| 92 | + { |
| 93 | + maze[x, y] = false; |
| 94 | + removed = true; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } while (removed); |
| 100 | + } |
| 101 | + |
| 102 | + private static void RemoveDeadEnds(bool[,] maze) |
| 103 | + { |
| 104 | + bool removed; |
| 105 | + do |
| 106 | + { |
| 107 | + removed = false; |
| 108 | + for (int x = 1; x < maze.GetLength(0) - 1; x++) |
| 109 | + { |
| 110 | + for (int y = 1; y < maze.GetLength(1) - 1; y++) |
| 111 | + { |
| 112 | + if (maze[x, y]) // If it's a path |
| 113 | + { |
| 114 | + int neighbors = 0; |
| 115 | + foreach (var (dx, dy) in Directions) |
| 116 | + { |
| 117 | + if (maze[x + dx, y + dy]) |
| 118 | + neighbors++; |
| 119 | + } |
| 120 | + if (neighbors <= 1) // If it's a dead end |
| 121 | + { |
| 122 | + // Pick a random neighbor to keep open |
| 123 | + var shuffledDirections = ShuffleDirections(); |
| 124 | + foreach(var (dx, dy) in shuffledDirections) |
| 125 | + { |
| 126 | + if(IsInBounds(maze, x + dx, y + dy) && !maze[x + dx, y + dy]) |
| 127 | + { |
| 128 | + maze[x + dx, y + dy] = true; |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + removed = true; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } while (removed); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments