Created
February 12, 2013 03:27
-
-
Save anonymous/4760028 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment