Пример #1
0
        public static Graph CreateGraph(
            int x, int y,
            int columns, int rows,
            float sizePerEdge)
        {
            Graph graph = new Graph();
            GraphNode[,] nodes = new GraphNode[columns, rows];

            // Create all GraphNodes.
            for (int i = 0; i < columns; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    double xPos = x * sizePerEdge + i * sizePerEdge;
                    double yPos = y * sizePerEdge + j * sizePerEdge;
                    nodes[i, j] = new GraphNode(new Vector2(xPos, yPos));
                }
            }

            // Create all GraphEdges and add them to the graph.
            for (int i = 0; i < columns; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    CreateEdges(graph, nodes, i, j, columns, rows);
                }
            }
            return graph;
        }
Пример #2
0
 public Explorer(World world)
 {
     this.Mass = 1;
     this.MaxSpeed = 100;
     this._speedBoost = 1;
     this._graph = world.Graph;
     this._steering = new SeekAtSteering();
     this._sprite = new AnimatedSpriteSet("explorer.png", 32, 64);
     Initialize();
 }
Пример #3
0
 public void ConnectedGraph_GetDepthFirstEnumerable_StartsWithStartNode()
 {
     Graph graph = new Graph();
     GraphNode start = new GraphNode(new Vector2(0, 0));
     GraphNode finish = new GraphNode(new Vector2(1, 1));
     graph.AddEdge(new GraphEdge(start, finish));
     var enumerator = graph.GetDepthFirstEnumerable(start).GetEnumerator();
     Assert.IsTrue(enumerator.Current == start,
         "Enumerator did not start with the correct node");
 }
Пример #4
0
 public World()
 {
     // We don't need to access individual elements of this collection,
     // so a linked list makes sense here.
     _entities = new LinkedList<Entity>();
     _addable = new LinkedList<Entity>();
     _removable = new LinkedList<Entity>();
     _graph = WorldGraphFactory.CreateGraph(
         GraphWidth / -2, GraphHeight / -2,
         GraphWidth, GraphHeight,
         GraphEdgeSize);
 }
Пример #5
0
 private static void CreateEdges(Graph graph, GraphNode[,] nodes, int x, int y,
     int columns, int rows)
 {
     for (int i = x - 1; i < x + 2; i++)
     {
         for (int j = y - 1; j < y + 2; j++)
         {
             if (i > 0 && i < columns && j > 0 && j < rows &&
                 !(i == x && j == y))
             {
                 graph.AddEdge(new GraphEdge(nodes[x, y], nodes[i, j]));
             }
         }
     }
 }
Пример #6
0
        public void GraphWithDisconnectedNodes_GetShortestPath_ThrowsArgumentException()
        {
            GraphNode source = new GraphNode(new Vector2(0f, 0f));
            GraphNode destination = new GraphNode(new Vector2(10f, 10f));
            Graph disconnectedGraph = new Graph();
            disconnectedGraph.AddEdge(
                new GraphEdge(source, new GraphNode(new Vector2(1f, 1f)))
            );
            disconnectedGraph.AddEdge(
                new GraphEdge(new GraphNode(new Vector2(5f, 5f)), destination)
            );
            AStarAlgorithm astar = new AStarAlgorithm(disconnectedGraph);

            // Should throw an argument exception, because it cannot
            // calculate the shortest path between disconnected nodes.
            astar.GetShortestPath(source, destination);
        }
Пример #7
0
        public void ConnectedGraph_GetShortestPath_ReturnsCorrectPath()
        {
            // Create a graph with four nodes, connected in a straight line.
            Graph graph = new Graph();

            const int NodeCount = 4;
            GraphNode start = new GraphNode(new Vector2(0f, 0f));
            GraphNode midFirst = new GraphNode(new Vector2(1f, 1f));
            GraphNode midSecond = new GraphNode(new Vector2(2f, 2f));
            GraphNode destination = new GraphNode(new Vector2(3f, 3f));

            graph.AddEdge(new GraphEdge(start, midFirst));
            graph.AddEdge(new GraphEdge(midFirst, midSecond));
            graph.AddEdge(new GraphEdge(midSecond, destination));

            AStarAlgorithm astar = new AStarAlgorithm(graph);
            IEnumerable<GraphNode> path =
                astar.GetShortestPath(start, destination).Path;
            Assert.IsTrue(path.Count() == NodeCount,
                "Path has an invalid amount of nodes");
        }
Пример #8
0
 public void ConnectedGraph_GetDepthFirstEnumerable_TraversesCorrectAmountOfNodes()
 {
     const int EdgeCount = 4;
     const int NodeCount = EdgeCount + 1;
     Graph graph = new Graph();
     GraphNode start = new GraphNode(new Vector2());
     GraphNode currentSource = start;
     GraphNode currentDestination;
     for (int i = 0; i < EdgeCount; i++)
     {
         currentDestination = new GraphNode(new Vector2());
         graph.AddEdge(new GraphEdge(currentSource, currentDestination));
         currentSource = currentDestination;
     }
     var enumerator = graph.GetDepthFirstEnumerable(start).GetEnumerator();
     int actualCount = 1;
     while (enumerator.MoveNext())
     {
         actualCount++;
     }
     Assert.AreEqual(NodeCount, actualCount,
         "DeothFirstEnumerator did not traverse the correct amount of nodes.");
 }
Пример #9
0
 public AStarAlgorithm(Graph graph)
 {
     this._graph = graph;
 }