Beispiel #1
0
        private Path FinalPath(MazeCoordinate start,
                               MazeCoordinate target,
                               IDictionary <MazeCoordinate, MazeCoordinate> previous)
        {
            var final = new List <MazeCoordinate>();

            var u = target;

            if (!previous.ContainsKey(u))
            {
                return(Path.Empty);
            }

            while (previous[u] != null)
            {
                if (final.Contains(u))
                {
                    throw new InvalidOperationException("How are we doing this?");
                }

                final.Insert(0, u);
                u = previous[u];
            }

            final.Insert(0, u);

            return(Path.From(start, target, final));
        }
Beispiel #2
0
        public static Path From(MazeCoordinate start, MazeCoordinate end, IEnumerable <MazeCoordinate> route)
        {
            if (!route.First().Equals(start) || !route.Last().Equals(end))
            {
                return(Path.Empty);
            }

            return(new Path(start, end, route));
        }
Beispiel #3
0
        private static IEnumerable <MazeCoordinate> Neighbors(MazeCoordinate current,
                                                              int minX, int maxX,
                                                              int minY, int maxY,
                                                              int input)
        {
            var surrounding = new[]
            {
                new MazeCoordinate(current.X - 1, current.Y), new MazeCoordinate(current.X + 1, current.Y),
                new MazeCoordinate(current.X, current.Y - 1), new MazeCoordinate(current.X, current.Y + 1)
            };

            return(surrounding.Where(s => IsOpenSpace(s.X, s.Y, input))
                   .Where(s => s.X >= minX && s.X <= maxX && s.Y >= minY && s.Y <= maxY));
        }
Beispiel #4
0
        public IDictionary <MazeCoordinate, Path> StepsToAllSpaces(MazeCoordinate start)
        {
            var paths = new ConcurrentDictionary <MazeCoordinate, Path>();

            Parallel.ForEach(this.graph.Keys, node =>
            {
                var route = FindOptimalRoute(start, node);

                if (!paths.TryAdd(node, route))
                {
                    throw new InvalidOperationException("Bang");
                }
            });

            return(paths.ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
        }
Beispiel #5
0
        private Path(MazeCoordinate start, MazeCoordinate end, IEnumerable <MazeCoordinate> path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (!path.Any())
            {
                throw new ArgumentException("Path does not contain any coordinates");
            }

            this.Start    = start;
            this.End      = end;
            this.FullPath = path;
            this.Length   = path.Count() - 1;
        }
Beispiel #6
0
        public Path FindOptimalRoute(MazeCoordinate start, MazeCoordinate end)
        {
            var notVisited            = new HashSet <MazeCoordinate>();
            var distanceToStart       = new Dictionary <MazeCoordinate, int>();
            var previousInOptimalPath = new Dictionary <MazeCoordinate, MazeCoordinate>();

            foreach (var openSpace in this.graph.Keys)
            {
                distanceToStart[openSpace]       = Int32.MaxValue;
                previousInOptimalPath[openSpace] = null;
                notVisited.Add(openSpace);
            }

            distanceToStart[start] = 0;

            while (notVisited.Any())
            {
                var u = SelectClosestUnvisited(notVisited, distanceToStart);

                notVisited.Remove(u);

                foreach (var neighbor in this.graph[u].Neighbors.Where(n => notVisited.Contains(n)))
                {
                    var a = (distanceToStart[u] == Int32.MaxValue) ? 1 : distanceToStart[u] + 1;
                    if (a < distanceToStart[neighbor])
                    {
                        distanceToStart[neighbor]       = a;
                        previousInOptimalPath[neighbor] = u;
                    }
                }
            }

            var finalPath = FinalPath(start, end, previousInOptimalPath);

            return(finalPath);
        }
Beispiel #7
0
        private bool CoordinateNotReachable(MazeCoordinate start, MazeCoordinate end, IEnumerable <MazeCoordinate> route)
        {
            var reachable = route.Contains(start) && route.Contains(end);

            return(reachable);
        }
Beispiel #8
0
 public MazeNode AddNeighbor(MazeCoordinate neighbor)
 {
     this.neighbors.Add(neighbor);
     return(this);
 }
Beispiel #9
0
 public MazeNode(MazeCoordinate coordinate)
 {
     this.Coordinate = coordinate;
 }