Beispiel #1
0
        public void ShouldCalculateRoute()
        {
            var   stationA = new TrainStation('A');
            var   stationB = new TrainStation('B');
            Route route    = new Route();

            stationA.AddConnection(new TrainConnection(3, stationA, stationB));

            route.AddNode(stationA);
            route.AddNode(stationB);

            int result = route.CalculateRouteDistance();

            Assert.Equal(result, 3);
        }
Beispiel #2
0
        public void ShouldAddNode()
        {
            Route route = new Route();

            route.AddNode(new TrainStation('A'));

            Assert.Equal(route.Nodes.Count, 1);
        }
Beispiel #3
0
    public static Route Find(int fromX, int fromY, int toX, int toY)
    {
        var searchSet = new Heap <Route>();

        foreach (var element in world)
        {
            if (element.x == fromX && element.y == fromY)
            {
                var first = new Route();
                first.AddNode(element);
                searchSet.Insert(first);
                break;
            }
        }

        if (!searchSet.hasItems)
        {
            Debug.LogError("Pathfinding from an out of bounds location " + fromX + " " + fromY);
            return(null);
        }

        var visited = new Dictionary <Node, bool>();

        visited.Add(searchSet.root.lastNode, false);
        while (searchSet.hasItems)
        {
            var testRoute = searchSet.PopRoot();
            foreach (var neighbor in testRoute.lastNode.neighbors)
            {
                if (visited.ContainsKey(neighbor))
                {
                    continue;
                }
                visited.Add(neighbor, false);

                if (neighbor.x == toX && neighbor.y == toY)
                {
                    return(testRoute.AndThen(neighbor));
                }

                searchSet.Insert(testRoute.AndThen(neighbor));
            }
        }
        return(null);
    }