Exemple #1
0
        public AlgorithmRunResults RecursiveBackTracker(IMazeCarver carver)
        {
            var pointsAndDirections = new List <DirectionAndPoint>();
            var directions          = carver.CarvableDirections().ToList();

            _arrayHelper.Shuffle(directions);
            var currentPoint = carver.CurrentPoint;

            foreach (var direction in directions)
            {
                carver.JumpInDirection(direction);
                var carvedDirections = carver.AlreadyCarvedDirections();
                if (!carvedDirections.Any())
                {
                    pointsAndDirections.Add(new DirectionAndPoint {
                        Direction = direction, MazePoint = currentPoint
                    });
                    var oppositeDirection = _directionsFlagParser.OppositeDirection(direction);
                    carver.CarveInDirection(oppositeDirection);
                    RecursiveBackTracker(carver);
                }
                carver.JumpToPoint(currentPoint);
            }
            return(new AlgorithmRunResults
            {
                Carver = carver,
                DirectionsCarvedIn = pointsAndDirections
            });
        }
        public AlgorithmRunResults GenerateMaze(IMazeCarver maze, MazeGenerationSettings settings)
        {
            var pointAndDirection = new List <DirectionAndPoint>();
            var randomPoint       = _randomPointGenerator.RandomPoint(maze.Size);

            maze.JumpToPoint(randomPoint);
            var activeCells = new LinkedList <MazePoint>();

            activeCells.AddLast(randomPoint);
            while (activeCells.Any())
            {
                var currentPoint = activeCells.Last.Value;
                maze.JumpToPoint(currentPoint);
                var carvableDirections = maze.CarvableDirections().ToList();
                _arrayHelper.Shuffle(carvableDirections);
                var carved = false;
                foreach (var direction in carvableDirections)
                {
                    maze.JumpInDirection(direction);
                    var carvedDirections = maze.AlreadyCarvedDirections();
                    if (!carvedDirections.Any())
                    {
                        pointAndDirection.Add(new DirectionAndPoint {
                            Direction = direction, MazePoint = currentPoint
                        });
                        var oppositeDirection = _directionsFlagParser.OppositeDirection(direction);
                        maze.CarveInDirection(oppositeDirection);
                        activeCells.AddLast(maze.CurrentPoint);
                        carved = true;
                        break;
                    }
                    maze.JumpToPoint(currentPoint);
                }
                if (!carved)
                {
                    activeCells.RemoveLast();
                }
            }
            return(new AlgorithmRunResults
            {
                Carver = maze,
                DirectionsCarvedIn = pointAndDirection
            });
        }
Exemple #3
0
        private AlgorithmRunResults GenerateMaze(IMazeCarver maze, List <GrowingTreeStrategy> strategies)
        {
            var pointsAndDirections = new List <DirectionAndPoint>();
            var randomPoint         = _randomPointGenerator.RandomPoint(maze.Size);
            var activeCells         = new List <MazePoint> {
                randomPoint
            };

            while (activeCells.Any())
            {
                var currentPoint = GetNextPoint(activeCells, strategies);
                maze.JumpToPoint(currentPoint);
                var carvableDirections = maze.CarvableDirections().ToList();
                _arrayHelper.Shuffle(carvableDirections);
                var carved = false;
                foreach (var direction in carvableDirections)
                {
                    maze.JumpInDirection(direction);
                    var carvedDirections = maze.AlreadyCarvedDirections();
                    if (!carvedDirections.Any())
                    {
                        pointsAndDirections.Add(new DirectionAndPoint {
                            Direction = direction, MazePoint = currentPoint
                        });
                        var oppositeDirection = _directionsFlagParser.OppositeDirection(direction);
                        maze.CarveInDirection(oppositeDirection);
                        activeCells.Add(maze.CurrentPoint);
                        carved = true;
                        break;
                    }
                    maze.JumpToPoint(currentPoint);
                }
                if (!carved)
                {
                    activeCells.Remove(currentPoint);
                }
            }
            return(new AlgorithmRunResults
            {
                Carver = maze,
                DirectionsCarvedIn = pointsAndDirections
            });
        }