public AlgorithmRunResults GenerateMaze(IMazeCarver maze, MazeGenerationSettings settings)
        {
            var pointsAndDirections = new List <DirectionAndPoint>();

            _mazeHelper.DoForEachPoint(maze.Size, p =>
            {
                maze.JumpToPoint(p);
                var directions = _directions.Where(maze.CanCarveInDirection).ToList();
                _arrayHelper.Shuffle(directions);
                if (directions.Any())
                {
                    var first = directions.First();
                    pointsAndDirections.Add(new DirectionAndPoint {
                        Direction = first, MazePoint = maze.CurrentPoint
                    });
                    maze.CarveInDirection(first);
                }
            });

            return(new AlgorithmRunResults
            {
                Carver = maze,
                DirectionsCarvedIn = pointsAndDirections
            });
        }
예제 #2
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
            });
        }
예제 #3
0
        private void DeadEndCarver(IMazeCarver carver, int numberOfWalls, bool hasPreferredDirection)
        {
            var pointsAndDirections = _pointsAndDirectionsRetriever.GetDeadEnds(carver).ToList();

            _arrayHelper.Shuffle(pointsAndDirections);
            foreach (var pointAndDirections in pointsAndDirections)
            {
                if (numberOfWalls > 0)
                {
                    Direction preferredDirection = Direction.None;
                    if (hasPreferredDirection)
                    {
                        preferredDirection = _directionsFlagParser
                                             .OppositeDirection(pointAndDirections.Directions.First());
                    }
                    numberOfWalls = CheckPoint(pointAndDirections.Point, carver, numberOfWalls, preferredDirection);
                }
                else
                {
                    break;
                }
            }
            RandomCarveWalls(carver, numberOfWalls);
        }
        private List <DirectionAndPoint> GetPathToLastPoint(List <DirectionAndPoint> previousPoints,
                                                            IMaze maze)
        {
            //If we are at the end return a list with the final Point added with No direction
            if (maze.CurrentPoint.Equals(maze.EndPoint))
            {
                previousPoints.Add(new DirectionAndPoint {
                    Direction = Direction.None, MazePoint = maze.CurrentPoint
                });
                return(previousPoints);
            }
            //If the cell has been visited immediately return the current List of previous points
            if (previousPoints.Any(x => x.MazePoint.Equals(maze.CurrentPoint)))
            {
                return(previousPoints);
            }
            var directions = maze.GetsDirectionsFromPoint().ToList();

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

            //check each direction for path to end
            foreach (var direction in directions)
            {
                maze.MoveInDirection(direction);
                var newPreviousPoints = previousPoints.Concat(new[]
                {
                    new DirectionAndPoint
                    {
                        Direction = direction,
                        MazePoint = currentPoint
                    }
                }).ToList();
                var path = GetPathToLastPoint(newPreviousPoints, maze);
                var last = path.Last();
                if (last.MazePoint.Equals(maze.EndPoint))
                {
                    return(path);
                }
                //Move back to start point if no path is found.
                maze.MoveInDirection(_directionsFlagParser.OppositeDirection(direction));
            }
            //If no path in any direction reaches end then return previous points.
            return(previousPoints);
        }
        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
            });
        }
예제 #6
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
            });
        }
예제 #7
0
        public override AgentResults RunAgentBase(IMaze maze)
        {
            var pointAndDirectionsList = new List <DirectionAndPoint>();

            while (!maze.CurrentPoint.Equals(maze.EndPoint))
            {
                var directions = maze.GetsDirectionsFromPoint().ToList();
                _arrayHelper.Shuffle(directions);
                var first = directions.First();
                pointAndDirectionsList.Add(new DirectionAndPoint {
                    Direction = first, MazePoint = maze.CurrentPoint
                });
                maze.MoveInDirection(first);
            }
            var reducedList = pointAndDirectionsList.Distinct(DirectionAndPoint.DirectionMazePointComparer).ToList();

            return(new AgentResults
            {
                Movements = reducedList
            });
        }
예제 #8
0
        public override AgentResults RunAgentBase(IMaze maze)
        {
            var pointAndDirectionsList = new LinkedList <DirectionAndPoint>();

            if (!maze.CurrentPoint.Equals(maze.EndPoint))
            {
                var firstDirections = maze.GetsDirectionsFromPoint().ToList();
                _arrayHelper.Shuffle(firstDirections);
                var first        = firstDirections.First();
                var currentPoint = maze.CurrentPoint;
                maze.MoveInDirection(first);
                var lastDirectionMoved = first;
                pointAndDirectionsList.AddLast(new DirectionAndPoint {
                    MazePoint = currentPoint, Direction = first
                });
                while (!maze.CurrentPoint.Equals(maze.EndPoint))
                {
                    var directions         = maze.GetsDirectionsFromPoint().ToList();
                    var reverseDirection   = _directionsFlagParser.OppositeDirection(lastDirectionMoved);
                    var filteredDirections = directions.Where(x => x != reverseDirection).ToList();
                    _arrayHelper.Shuffle(filteredDirections);
                    if (_pointsAndDirectionsRetriever.IsJunction(directions))
                    {
                        var direction = filteredDirections.First();
                        pointAndDirectionsList.AddLast(new DirectionAndPoint
                        {
                            Direction = direction,
                            MazePoint = maze.CurrentPoint
                        });
                        maze.MoveInDirection(direction);
                        lastDirectionMoved = direction;
                    }
                    else
                    {
                        if (filteredDirections.Any())
                        {
                            var direction = filteredDirections.First();
                            pointAndDirectionsList.AddLast(new DirectionAndPoint
                            {
                                Direction = direction,
                                MazePoint = maze.CurrentPoint
                            });
                            maze.MoveInDirection(direction);
                            lastDirectionMoved = direction;
                        }
                        else
                        {
                            var direction = reverseDirection;
                            pointAndDirectionsList.AddLast(new DirectionAndPoint
                            {
                                Direction = direction,
                                MazePoint = maze.CurrentPoint
                            });
                            maze.MoveInDirection(direction);
                            lastDirectionMoved = direction;
                        }
                    }
                }
            }
            var list = pointAndDirectionsList.ToList();

            return(new AgentResults
            {
                Movements = list.Distinct(DirectionAndPoint.DirectionMazePointComparer).ToList()
            });
        }