-
Notifications
You must be signed in to change notification settings - Fork 1
/
RecursiveBacktracker.cs
137 lines (126 loc) · 3.69 KB
/
RecursiveBacktracker.cs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class RecursiveBacktracker : MonoBehaviour, RandomMaze
{
private Grid grid;
private int cellCount = 1;
private Tools.dimensions d;
public RecursiveBacktracker(int rows, int cols)
{
this.grid = new Grid2D(rows, cols);
this.d = Tools.dimensions.TwoD;
genMaze();
}
public RecursiveBacktracker(int rows, int cols, int levels)
{
this.grid = new Grid3D(rows, cols, levels);
this.d = Tools.dimensions.ThreeD;
genMaze();
}
public RecursiveBacktracker(Grid2D grid)
{
this.grid = grid;
this.d = grid.dimensions();
genMaze();
}
private void genMaze()
{
this.grid.genActiveCells();
IEnumerable cells = this.grid.getCells();
Stack<Cell> stack = new Stack<Cell>();
Cell firstCell = new Cell2D(0, 0);
foreach (Cell cell in cells)
{
if (cell.IsActive())
{
firstCell = cell;
break;
}
}
//go through left to right from 00 and then up
stack.Push(firstCell);
genMazeH(cells, stack);
}
private void genMazeH(IEnumerable cells, Stack<Cell> stack)
{
//print("starting genMazeH");
if (!(cellCount == this.grid.getActiveCells().Count))
{
Cell currentCell = stack.Peek();
List<Cell> neighbors = new List<Cell>();
IEnumerable allNeighbors = new ArrayList();
try
{
Cell2D cell = currentCell as Cell2D;
allNeighbors = cell.getNeighbors().Values;
}
catch (NullReferenceException)
{
//print("not 2D");
}
try
{
Cell3D cell = currentCell as Cell3D;
allNeighbors = cell.GetNeighbors().Values;
}
catch (NullReferenceException) { print("not 3D"); }
foreach (Cell neighbor in allNeighbors)
{
//print("deciding whether to link");
if (neighbor.GetLinks().Count == 0)
neighbors.Add(neighbor);
}
if (neighbors.Count == 0)
{
//print("no neighbors");
stack.Pop();
genMazeH(cells, stack);
}
else
{
Cell nextCell = neighbors[UnityEngine.Random.Range(0, neighbors.Count)];
if (d == Tools.dimensions.TwoD)
{
Cell2D toLink = nextCell as Cell2D;
Cell2D me = currentCell as Cell2D;
me.Link(toLink);
}
else
{
Cell3D toLink = nextCell as Cell3D;
Cell3D me = currentCell as Cell3D;
me.Link(toLink);
}
stack.Push(nextCell);
cellCount++;
genMazeH(cells, stack);
}
}
}
public Grid getGrid() => this.grid;
public Tools.dimensions getDimensions() => d;
public RenderMaze getRenderer()
{
if (d == Tools.dimensions.TwoD)
{
return new RenderMaze2D(grid as Grid2D);
}
else
{
return new RenderMaze3D(grid as Grid3D);
}
}
public RenderMaze getRenderer(Vector3 pos)
{
if (d == Tools.dimensions.TwoD)
{
return new RenderMaze2D(grid as Grid2D, pos);
}
else
{
return new RenderMaze3D(grid as Grid3D, pos);
}
}
}