Example #1
0
        public void Dijkstra_Solves_Raw_Data(int mapId, int xStart, int yStart, int xGoal, int yGoal, int expectedLength)
        {
            byte[,] map = mapId == 0 ? m_Map_0 : m_Map_1;

            List <Vector2Int> accessibles = new List <Vector2Int>();

            //We flip the array values to match horizontal values with X coords and vertical values with Y coords
            //So (0 , 0) coords starts from the bottom left and not from the top left and Y coords from bottom to top and not
            //from top to bottom as the default indexing 2D array system.
            for (int iRun = map.GetLength(0) - 1, i = 0; iRun >= 0; iRun--, i++)
            {
                for (int j = 0; j < map.GetLength(1); j++)
                {
                    if (map[iRun, j] == 0)
                    {
                        accessibles.Add(new Vector2Int(j, i));
                    }
                }
            }

            IPathFinder pathFinder        = new BreadthFirst(accessibles); //<-- TODO: Create Dijsktra pathfinder class here, TIP-->> Use accessible tiles
            IEnumerable <Vector2Int> path = pathFinder.FindPath(new Vector2Int(xStart, yStart), new Vector2Int(xGoal, yGoal));

            Assert.AreEqual(expectedLength, path.Count());
        }
Example #2
0
        public void ReturnsEmptyPathIfNoPathIsFound()
        {
            var path = (object)BreadthFirst.FindPath(CreateSimpleGraph(),
                                                     NodeIdentity.Of("A"),
                                                     node => node == NodeIdentity.Of("notfound"));

            path.Should().Be(Path.Empty);
        }
Example #3
0
 public void CanFindStraightPath()
 {
     BreadthFirst.FindPath(GraphFactory.BuildGraph("A-B", "B-C", "C-D"), NodeIdentity.Of("A"), node => node == NodeIdentity.Of("D"))
     .Should().ContainInOrder(
         NodeIdentity.Of("A"),
         NodeIdentity.Of("B"),
         NodeIdentity.Of("C"),
         NodeIdentity.Of("D")
         ).And.HaveCount(4);
 }
Example #4
0
 public void FindsShortestPath()
 {
     BreadthFirst.FindPath(
         GraphFactory.BuildGraph("A-B", "B-C", "C-D", "A-D"),
         NodeIdentity.Of("A"),
         node => node == NodeIdentity.Of("D"))
     .Should().ContainInOrder(
         NodeIdentity.Of("A"),
         NodeIdentity.Of("D")
         ).And.HaveCount(2);
 }
Example #5
0
        public static Path Extend(this Path firstPath, Path secondPath, DirectedGraph graph)
        {
            ArgumentHelpers.ThrowIfNull(() => firstPath);
            ArgumentHelpers.ThrowIfNull(() => secondPath);
            ArgumentHelpers.ThrowIfNull(() => graph);

            var overlap = OverlapFinder.FindOverlap(firstPath, secondPath);

            if (!Path.IsEmpty(overlap))
            {
                return(overlap);
            }
            var shortestPath = BreadthFirst.FindPath(graph, firstPath.End, node => node == secondPath.Start);

            if (Path.IsEmpty(shortestPath))
            {
                return(Path.Empty);
            }
            return(Path.Join(firstPath, Path.Of(shortestPath.GetRange(1, shortestPath.Length - 2)), secondPath));
        }