public override void Generate() { status = Status.InProgress; //todo use seed int total = this.Nodes.Count * this.Nodes[0].Count; int visited = 1; List <Node> frontier = new List <Node>(); Node current = this.Nodes[(int)(_random.NextDouble() * 10) % this.Nodes.Count][(int)(_random.NextDouble() * 50) % this.Nodes[0].Count]; current.IsStart = true; for (int i = 0; i < current.Count; i++) { if (current[i] != null) { current[i].ParentInfo.Add(new ParentInfo(current, i)); frontier.Add(current[i]); current[i].IsFrontier = true; } } while (frontier.Count > 0) { current = frontier[(int)(_random.NextDouble() * 10) % frontier.Count]; current.IsFrontier = false; frontier.Remove(current); //random select a parent ParentInfo parentInfo = current.ParentInfo[(int)(_random.NextDouble() * 15) % current.ParentInfo.Count]; //break the wall //0-2 1-3 parentInfo.Parent.UnWall(parentInfo.Index); current.UnWall((parentInfo.Index + 2) % 4); //add new frontier for (int i = 0; i < current.Count; i++) { //not a frontier yet //and no walls destroyed if (current[i] != null && current[i].ParentInfo.Count == 0 && current[i].Wall == 0) { frontier.Add(current[i]); current[i].ParentInfo.Add(new ParentInfo(current, i)); current[i].IsFrontier = true; } } visited++; OnProgressChanged(visited, total); } OnComplete(); status = Status.Complete; }
public override void Generate() { int total = this.Nodes.Count * this.Nodes[0].Count; int visited = 1; List <Node> cells = new List <Node>(); Random r = new Random(); cells.Add(this.Nodes[(int)(r.NextDouble() * 10) % this.Nodes.Count][(int)(r.NextDouble() * 10) % this.Nodes[0].Count]); cells[0].isStart = true; //Selection method //0 = Lastest //1 = Oldest //2 = Random //default = 0 Func <Node> selMethod = () => { return(cells.Last()); }; switch (this.SelectionMethod) { case 1: selMethod = () => { return(cells[0]); }; break; case 2: selMethod = () => { return(cells[(int)(r.NextDouble() * 10) % cells.Count]); }; break; } while (cells.Count > 0) { Node current = selMethod(); //List all available neighbour cells List <Node> readyNeighbourCells = new List <Node>(); //Store the index of the neighbour cells List <int> readyNeighbourCellsIndex = new List <int>(); for (int i = 0; i < current.Count; i++) { if (current[i] != null && current[i].Wall == 0) { readyNeighbourCells.Add(current[i]); readyNeighbourCellsIndex.Add(i); } } //no cells found if (readyNeighbourCells.Count == 0) { current.isBacktracked = true; cells.Remove(current); OnProgressChanged(visited, total); continue; } //Random select a cell int randIndex = (int)(r.NextDouble() * 10) % readyNeighbourCells.Count; int index = readyNeighbourCellsIndex[randIndex]; Node neighbour = readyNeighbourCells[randIndex]; // Knock the wall // 0-2 1-3 current.UnWall(index); neighbour.UnWall((index + 2) % 4); OnProgressChanged(visited, total); cells.Add(neighbour); visited++; } OnComplete(); }
public override void Generate() { int total = this.Nodes.Count * this.Nodes[0].Count; int visited = 1; List <Node> frontier = new List <Node>(); Random r = new Random(); //Select start node int x, y; if (wantedStartPos.x > -1) { x = wantedStartPos.x; } else { x = (int)(r.NextDouble() * 10) % this.Nodes.Count; } if (wantedStartPos.y > -1) { y = wantedStartPos.y; } else { y = (int)(r.NextDouble() * 50) % this.Nodes[0].Count; } Node current = this.Nodes[x][y]; current.isStart = true; for (int i = 0; i < current.Count; i++) { if (current[i] != null) { current[i].parentInfo.Add(new ParentInfo(current, i)); frontier.Add(current[i]); current[i].isFrontier = true; } } System.Diagnostics.Debug.WriteLine("max room: " + maxRoom); while (frontier.Count > 0) { current = frontier[(int)(r.NextDouble() * 10) % frontier.Count]; current.isFrontier = false; frontier.Remove(current); //random select a parent ParentInfo parentInfo = current.parentInfo[(int)(r.NextDouble() * 15) % current.parentInfo.Count]; //break the wall //0-2 1-3 parentInfo.Parent.UnWall(parentInfo.Index); current.UnWall((parentInfo.Index + 2) % 4); //add new frontier for (int i = 0; i < current.Count; i++) { //not a frontier yet //and no walls destroyed if (current[i] != null && current[i].parentInfo.Count == 0 && current[i].Wall == 0) { frontier.Add(current[i]); current[i].parentInfo.Add(new ParentInfo(current, i)); current[i].isFrontier = true; } } visited++; OnProgressChanged(visited, total); /* * if (current.Pos.X == 1 && current.Pos.Y == 1) * { * break; * } */ if (maxRoom > -1 && visited >= maxRoom)// -1 because start { break; } } System.Diagnostics.Debug.WriteLine("visited: " + visited); OnComplete(); }
public override void Generate() { int visitedCount = 1; int total = this.Nodes.Count * this.Nodes[0].Count; Stack <Node> visitedCell = new Stack <Node>(); Random r = new Random(); //Node current = this.Nodes[r.Next(this.Nodes.Count-1)][r.Next(this.Nodes[0].Count-1)]; Node current = this.Nodes[(int)(r.NextDouble() * this.Nodes.Count * 10) % this.Nodes.Count][(int)(r.NextDouble() * this.Nodes[0].Count * 10) % this.Nodes.Count]; current.isStart = true; //Node end = this.End.X == -1 ? this.Nodes[(int)(r.NextDouble() * this.Nodes.Count * 10) % this.Nodes.Count][(int)(r.NextDouble() * this.Nodes[0].Count * 10) % this.Nodes.Count] : this.Nodes[this.End.X][this.End.Y]; //end.isEnd = true; while (visitedCount < total) { //List all available neighbour cells List <Node> readyNeighbourCells = new List <Node>(); //Store the index of the neighbour cells List <int> readyNeighbourCellsIndex = new List <int>(); for (int i = 0; i < current.Count; i++) { if (current[i] != null && current[i].Wall == 0) { readyNeighbourCells.Add(current[i]); readyNeighbourCellsIndex.Add(i); } } //no cells found if (readyNeighbourCells.Count == 0) { current = visitedCell.Pop(); current.isBacktracked = true; OnProgressChanged(visitedCount, total); continue; } //Random select a cell int randIndex = (int)(r.NextDouble() * 10) % readyNeighbourCells.Count; int index = readyNeighbourCellsIndex[randIndex]; Node neighbour = readyNeighbourCells[randIndex]; // Knock the wall // 0-2 1-3 current.UnWall(index); neighbour.UnWall((index + 2) % 4); visitedCell.Push(neighbour); current = neighbour; visitedCount++; OnProgressChanged(visitedCount, total); } //Backtrack to start point while (visitedCell.Count > 0) { current = visitedCell.Pop(); current.isBacktracked = true; OnProgressChanged(visitedCount, total); } OnComplete(); }