-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimsGrowingTreeGenerator.cs
105 lines (93 loc) · 4.37 KB
/
PrimsGrowingTreeGenerator.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BWolf.MazeGeneration.Generators
{
public class PrimsGrowingTreeGenerator : MazeGenerator
{
/// <summary>
/// Creates a maze by linking cells using the growing tree algorithm its prim version
/// </summary>
/// <param name="service"></param>
public override void CreateMaze(MazeGenerationService service)
{
//create data structures necessary for algorithm to work
List<MazeCell> bag = new List<MazeCell>();
List<MazeCell> visited = new List<MazeCell>();
//set starting point
MazeCell startPoint = service.RootCell;
startPoint.MarkAsVisited();
visited.Add(startPoint);
bag.Add(startPoint);
//loop until all cells have been visited
long totalCells = service.Cells.LongLength;
while (visited.Count != totalCells)
{
//pick a random cell from the bag and check if it has unvisited neighbours
MazeCell randomCell = bag[Random.Range(0, bag.Count)];
List<MazeCell> unvisitedNeighbours = service.GetUnVisitedNeighbours(randomCell);
if (unvisitedNeighbours.Count > 0)
{
//pick a random unvisited neighbour and mark it as visited
MazeCell randomUnvisitedNeighbour = unvisitedNeighbours[Random.Range(0, unvisitedNeighbours.Count)];
randomUnvisitedNeighbour.MarkAsVisited();
visited.Add(randomUnvisitedNeighbour);
//create passage between random cell and neighbour
randomCell.CreatePassage(randomUnvisitedNeighbour);
randomUnvisitedNeighbour.CreatePassage(randomCell);
//add the random unvisited neighbour to the bag
bag.Add(randomUnvisitedNeighbour);
}
else
{
//if the cell doesn't have any unvisited neighbours, remove it from the bag
bag.Remove(randomCell);
}
}
}
/// <summary>
/// Returns an Enumerator that creates a maze by linking cells using the growing tree algorithm its prim version
/// </summary>
/// <param name="service"></param>
public override IEnumerator CreateMazeRoutine(MazeGenerationService service)
{
//create data structures necessary for algorithm to work
List<MazeCell> bag = new List<MazeCell>();
List<MazeCell> visited = new List<MazeCell>();
//set starting point
MazeCell startPoint = service.RootCell;
startPoint.MarkAsVisited();
visited.Add(startPoint);
bag.Add(startPoint);
//loop until all cells have been visited
long totalCells = service.Cells.LongLength;
while (visited.Count != totalCells)
{
//pick a random cell from the bag and check if it has unvisited neighbours
MazeCell randomCell = bag[Random.Range(0, bag.Count)];
List<MazeCell> unvisitedNeighbours = service.GetUnVisitedNeighbours(randomCell);
if (unvisitedNeighbours.Count > 0)
{
//pick a random unvisited neighbour and mark it as visited
MazeCell randomUnvisitedNeighbour = unvisitedNeighbours[Random.Range(0, unvisitedNeighbours.Count)];
randomUnvisitedNeighbour.MarkAsVisited();
visited.Add(randomUnvisitedNeighbour);
//create passage between cell and neighbour
randomCell.CreatePassage(randomUnvisitedNeighbour);
randomUnvisitedNeighbour.CreatePassage(randomCell);
//add the random unvisited neighbour to the bag
bag.Add(randomUnvisitedNeighbour);
}
else
{
//if the cell doesn't have any unvisited neighbours, remove it from the bag
bag.Remove(randomCell);
}
randomCell.ShowAsStep(true);
yield return null;
randomCell.ShowAsStep(false);
}
CompletedRoutine();
}
}
}