Beispiel #1
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()
            });
        }
Beispiel #2
0
 private bool IsStartOrEndPointOrJunction(IMazeCarver carver, List <Direction> directions)
 {
     return(_directionsRetriever.PointIsStartOrEnd(carver.CurrentPoint, carver.StartPoint, carver.EndPoint) ||
            _directionsRetriever.IsJunction(directions));
 }