public
anonymous / 2D Maze.cs
Created

  • Download Gist
2D Maze.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
public void GenerateMaze()
{
int counter = 0;
 
List<bool> cellWall = new List<bool>();
 
for (int x = 0; x < MAZEWIDTH; x++)
{
for (int y = 0; y < MAZEHEIGHT; y++)
{
counter++;
 
mazeCells[x, y].Wall = true;
mazeCells[x, y].Visited = false;
cellWall.Add(mazeCells[x, y].Wall);
}
}
 
int BeginningSquareSize = 4;
for (int x = 1; x <= BeginningSquareSize; x++)
{
for (int y = 1; y <= BeginningSquareSize; y++)
{
mazeCells[x, y].Visited = true;
mazeCells[x, y].Wall = false;
}
}
 
EvaluateCell(new Vector2(BeginningSquareSize, BeginningSquareSize));
 
}
 
private void EvaluateCell(Vector2 cell)
{
List<int> neighborCells = new List<int>();
neighborCells.Add(0);
neighborCells.Add(1);
neighborCells.Add(2);
neighborCells.Add(3);
 
while (neighborCells.Count > 0)
{
int pick = rand.Next(0, neighborCells.Count);
int selectedNeighbor = neighborCells[pick];
neighborCells.RemoveAt(pick);
 
Vector2 neighbor = cell;
 
switch (selectedNeighbor)
{
case 0:
neighbor += new Vector2(0, -1);
break;
case 1:
neighbor += new Vector2(1, 0);
break;
case 2:
neighbor += new Vector2(0, 1);
break;
case 3:
neighbor += new Vector2(-1, 0);
break;
}
 
if ((neighbor.X > 0) && (neighbor.X < MAZEWIDTH - 1) &&
(neighbor.Y > 0) && (neighbor.Y < MAZEHEIGHT - 1))
{
if (!mazeCells[(int)neighbor.X, (int)neighbor.Y].Visited)
{
if (!mazeCells[(int)neighbor.X, (int)neighbor.Y].HasWallBorder(neighbor, cell, mazeCells))
{
mazeCells[(int)neighbor.X, (int)neighbor.Y].Visited = true;
mazeCells[(int)cell.X, (int)cell.Y].Wall = false;
mazeCells[(int)neighbor.X, (int)neighbor.Y].Wall = false;
 
EvaluateCell(neighbor);
}
}
}
}
}
3D Maze.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#region Maze Generation
public void GenerateMaze()
{
 
for (int x = 0; x < MAZEWIDTH; x++)
{
for (int z = 0; z < MAZEHEIGHT; z++)
{
MazeCells[x, z].Walls[0] = true;
MazeCells[x, z].Walls[1] = true;
MazeCells[x, z].Walls[2] = true;
MazeCells[x, z].Walls[3] = true;
MazeCells[x, z].Visited = false;
}
}
 
MazeCells[0, 0].Visited = true;
{
MazeCells[0, 0].Walls[1] = false;
MazeCells[0, 0].Walls[2] = false;
}
MazeCells[0, 1].Visited = true;
{
MazeCells[0, 1].Walls[0] = false;
MazeCells[0, 1].Walls[1] = false;
}
MazeCells[1, 0].Visited = true;
{
MazeCells[1, 0].Walls[2] = false;
MazeCells[1, 0].Walls[3] = false;
}
MazeCells[1, 1].Visited = true;
{
MazeCells[1, 1].Walls[0] = false;
MazeCells[1, 1].Walls[3] = false;
}
EvaluateCell(new Vector2(1, 1));
}
 
private void EvaluateCell(Vector2 cell)
{
List<int> neighborCells = new List<int>();
neighborCells.Add(0);
neighborCells.Add(1);
neighborCells.Add(2);
neighborCells.Add(3);
 
while (neighborCells.Count > 0)
{
int pick = rand.Next(0, neighborCells.Count);
int selectedNeighbor = neighborCells[pick];
neighborCells.RemoveAt(pick);
 
Vector2 neighbor = cell;
 
switch (selectedNeighbor)
{
case 0: neighbor += new Vector2(0, -1);
break;
case 1: neighbor += new Vector2(1, 0);
break;
case 2: neighbor += new Vector2(0, 1);
break;
case 3: neighbor += new Vector2(-1, 0);
break;
}
 
if ((neighbor.X >= 0) && (neighbor.X < MAZEWIDTH) &&
(neighbor.Y >= 0) && (neighbor.Y < MAZEHEIGHT))
{
if (!MazeCells[(int)neighbor.X, (int)neighbor.Y].Visited)
{
MazeCells[(int)neighbor.X, (int)neighbor.Y].Visited = true;
MazeCells[(int)cell.X, (int)cell.Y].Walls[selectedNeighbor] = false;
MazeCells[(int)neighbor.X, (int)neighbor.Y].Walls[(selectedNeighbor + 2) % 4] = false;
EvaluateCell(neighbor);
}
}
}
}
#endregion

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.