Exemple #1
0
        public void Carve(IMaze maze)
        {
            _ = maze ?? throw new ArgumentNullException(nameof(maze));

            active.Add(maze.Grid[random.Next(maze.Grid.Count)]);

            while (active.Any())
            {
                var current    = active[random.Next(active.Count)];
                var neighbours = maze.GetNeighbours(current).
                                 Where(c => !c.Connected.Any()).
                                 ToList();

                if (neighbours.Any())
                {
                    var next = Selector(neighbours);
                    maze.ConnectCells(current, next);
                    active.Add(next);
                }
                else
                {
                    active.Remove(current);
                }
            }
        }
Exemple #2
0
        public void Carve(IMaze maze)
        {
            _ = maze ?? throw new ArgumentNullException(nameof(maze));

            var stack = new Stack <Cell>();

            stack.Push(maze.Grid[random.Next(maze.Grid.Count)]);

            while (stack.Count > 0)
            {
                var unvisited =
                    maze.GetNeighbours(stack.Peek()).
                    Where(c => !c.Connected.Any()).
                    ToList();

                if (unvisited.Any())
                {
                    var next = unvisited[random.Next(unvisited.Count())];
                    maze.ConnectCells(stack.Peek(), next);
                    stack.Push(next);
                }
                else
                {
                    stack.Pop();
                }
            }
        }
Exemple #3
0
        public void Carve(IMaze maze)
        {
            _ = maze ?? throw new ArgumentNullException(nameof(maze));
            
            for(int i = 0; i < maze.Height; ++i)
            {
                var run = new List<Cell>();
                foreach(Cell cell in maze.GetRow(i))
                {
                    if(run.Count() > 0)
                    {
                        maze.ConnectCells(cell, run[run.Count() - 1]);
                    }
                    run.Add(cell);                    

                    if(i != maze.Height - 1)
                    {
                        if(random.NextDouble() >= 0.5)
                        {
                            CloseRun(run, maze);                   
                        }
                    }
                }

                if(run.Any())
                {
                    CloseRun(run, maze);
                }
            }
        }
Exemple #4
0
        private void ReduceSets(int max = 1)
        {
            while (cellSets.Count > max)
            {
                var cellSet   = cellSets[random.Next(cellSets.Count)];
                var rowNumber = cellSet.Max(c => c.Row);

                var potentials = maze.GetNeighbours(cellSet.ToList())
                                 .Where(c => c.Row == rowNumber);


                var next    = potentials.FirstOrDefault(c => !cellSet.Contains(c));
                var current = maze.GetNeighbours(next).FirstOrDefault(c => cellSet.Contains(c));

                joinSets(current, next);
                maze.ConnectCells(current, next);
            }
        }
Exemple #5
0
        private void CloseRun(List<Cell> run, IMaze maze)
        {
            var chosen = run[random.Next(run.Count())];

            if(chosen.Row + 1 < maze.Height)
            {
                var connectTo = maze.GetCell((chosen.Row + 1), chosen.Column);
                maze.ConnectCells(chosen, connectTo);
            }  
            run.Clear();
        }
Exemple #6
0
        public void Carve(IMaze maze)
        {
            _ = maze ?? throw new ArgumentNullException(nameof(maze));

            var current = maze.Grid[random.Next(maze.Grid.Count)];

            while (current != null)
            {
                var unvisited =
                    maze.GetNeighbours(current).
                    Where(c => !c.Connected.Any()).
                    ToList();

                if (unvisited.Any())
                {
                    var next = unvisited[random.Next(unvisited.Count())];
                    maze.ConnectCells(current, next);
                    current = next;
                }
                else
                {
                    current = null;

                    var next = maze.Grid.Where((c) =>
                    {
                        return(!c.Connected.Any() &&
                               maze.GetNeighbours(c)
                               .Any(l => l.Connected.Any()));
                    }).FirstOrDefault();

                    if (next != null)
                    {
                        current = next;
                        next    = maze.GetNeighbours(current)
                                  .Where(c => c.Connected.Any()).First();
                        maze.ConnectCells(current, next);
                    }
                }
            }
        }
Exemple #7
0
        public void Carve(IMaze maze)
        {
            _ = maze ?? throw new ArgumentNullException(nameof(maze));

            foreach (Cell cell in maze.Grid)
            {
                var neighbours = maze.GetNeighbours(cell)
                                 .Where(c => c.Column == cell.Column + 1 || c.Row == cell.Row - 1)
                                 .ToList();

                if (neighbours.Any())
                {
                    maze.ConnectCells(cell, neighbours[random.Next(neighbours.Count())]);
                }
            }
        }
Exemple #8
0
        private void joinSets(IMaze maze, Cell current, Cell potential)
        {
            var currentSet = cellSets.FirstOrDefault(s => s.Contains(current));
            var nextSet    = cellSets.FirstOrDefault(s => s.Contains(potential));

            if (!currentSet.Equals(nextSet))
            {
                maze.ConnectCells(current, potential);
                foreach (var cell in nextSet)
                {
                    currentSet.Add(cell);
                }

                cellSets.Remove(nextSet);
                nextSet.Clear();
            }
        }
Exemple #9
0
        public void Carve(IMaze maze)
        {
            _ = maze ?? throw new ArgumentNullException(nameof(maze));

            var currentCell    = maze.Grid[random.Next(maze.Grid.Count)];
            var unvisitedCells = maze.Grid.Count - 1;

            while (unvisitedCells > 0)
            {
                var neighbours = maze.GetNeighbours(currentCell);
                var nextCell   = neighbours[random.Next(neighbours.Count)];

                if (nextCell.Connected.Count == 0)
                {
                    maze.ConnectCells(currentCell, nextCell);
                    --unvisitedCells;
                }

                currentCell = nextCell;
            }
        }
        private void JoinDivisions(Cell leftStart, Cell leftEnd, Cell rightStart, Cell rightEnd)
        {
            var  selected = int.MinValue;
            Cell left     = null;
            Cell right    = null;

            if (rightStart.Column > leftEnd.Column)
            {
                selected = random.Next(leftStart.Row, leftEnd.Row);
                left     = maze.GetCell(selected, leftEnd.Column);
                right    = maze.GetCell(selected, rightStart.Column);
            }
            else
            {
                selected = random.Next(leftStart.Column, leftEnd.Column);
                left     = maze.GetCell(leftEnd.Row, selected);
                right    = maze.GetCell(rightStart.Row, selected);
            }

            maze.ConnectCells(left, right);
        }
Exemple #11
0
 public void ConnectCells(Cell first, Cell second) => maze.ConnectCells(first, second);