Esempio n. 1
0
        ///<summary> Indexer that takes a key node as an argument </summary>
        ///<returns> A node with equal indexes as the key node ( they may have different edges ) </returns>
        public GridNode this[GridNode key]
        {
            get
            {
                for (int i = 0; i < m; i++)
                    for (int j = 0; j < n; j++)
                    {
                        if (grid[i, j] == key)
                        {
                            return grid[i, j];
                        }
                    }

                return null;
            }

            set
            {
                for (int i = 0; i < m; i++)
                    for (int j = 0; j < n; j++)
                    {
                        if (grid[i, j] == key)
                        {
                            grid[i, j] = value;
                        }
                    }

            }
        }
Esempio n. 2
0
        ///<summary> A public constructor for a GridGraph</summary>
        ///<param name="m"> Number of rows in a grid </param>
        ///<param name="n"> Number of columns in a grid </param>
        public GridGraph(int m, int n)
        {
            // make a new node matrix
            grid = new GridNode[m, n];
            this.m = m;
            this.n = n;

            // make all the nodes and connect them to their adjacent nodes
            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                {
                    grid[i, j] = new GridNode(i, j);
                    grid[i, j].Edges = this.adjacentToNode(i, j);
                }
        }
Esempio n. 3
0
 public Wall(GridNode root, GridNode other)
 {
     rootNode = root;
     otherNode = other;
 }
Esempio n. 4
0
        /// <summary> get edges of a node in a graph with given indexes </summary>
        /// <returns> A list of nodes that the given node is connected to </returns>
        public List<GridNode> getNodeEdges(GridNode key)
        {
            var edges = new List<GridNode>();

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                {
                    if (grid[i, j] == key)
                    {
                        edges = grid[i, j].Edges;
                    }
                }

            return edges;
        }
Esempio n. 5
0
        /// <summary>
        /// Calculates the distances of all the other nodes from a given node
        /// </summary>
        /// <param name="v"> A node</param>
        /// <returns> A dictionary of nodes and their distances from the node v</returns>
        public Dictionary<GridNode, int> DistancesFromNode(GridNode v)
        {
            // result dictionary
            var distances = new Dictionary<GridNode, int>();

            // list of traversed nodes
            var traversed = new List<GridNode>();

            // active list of nodes
            var neighbourList = new List<GridNode>();

            //initial step
            neighbourList.Add(v);
            distances[v] = 0;

            // while there are nodes to be traversed
            while (neighbourList.Count > 0)
            {

                // pop the first node
                var currentNode = neighbourList[0];
                neighbourList.RemoveAt(0);

                // mark it as traversed
                traversed.Add(currentNode);

                foreach (var neighbour in this[currentNode].Edges)
                {

                    if (!traversed.Contains(neighbour))
                    {
                        neighbourList.Add(neighbour);
                        // distance is one step away from the current node
                        distances[neighbour] = distances[currentNode] + 1;
                    }
                }

            }

            return distances;
        }
Esempio n. 6
0
        /// <summary>
        /// Calculates the optimal spawn nodes for Prim, Dark Prim and the exit
        /// </summary>
        /// <returns> Dictionary: "Prim" -> GridNode, "DarkPrim" -> GridNode, "Exit" -> GridNode</returns>
        public Dictionary<string, GridNode> GetSpawnNodes()
        {
            var spawnNodes = new Dictionary<string, GridNode>();

            Random rand = new Random();

            int side = rand.Next(1, 5);

            // Prim gets a random edge node for his spawn point
            GridNode primSpawnPoint = new GridNode(0, 0);

            // up
            if (side == 1)
            {
                int i = 0;
                int j = rand.Next(0, 4);

                primSpawnPoint = Graph[i, j];
            }
            // right
            if (side == 2)
            {
                int j = 3;
                int i = rand.Next(0, 4);

                primSpawnPoint = Graph[i, j];
            }
            //down
            if (side == 3)
            {
                int i = 3;
                int j = rand.Next(0, 4);

                primSpawnPoint = Graph[i, j];
            }
            //left
            if (side == 4)
            {
                int j = 0;
                int i = rand.Next(0, 4);

                primSpawnPoint = Graph[i, j];
            }

            spawnNodes.Add("Prim", primSpawnPoint);

            var distances = Graph.DistancesFromNode(primSpawnPoint);

            // Exit is the node that is farthest from Prim
            var farthestFromPrim = (from d in distances
                                    where d.Value == distances.Max(x => x.Value)
                                    select d.Key).First();

            spawnNodes.Add("Exit", farthestFromPrim);

            // Dark Prim gets spawned at a node that is farther then the average distance from Prim
            var fartherThenAverage = from d in distances
                                     where d.Value >= distances.Average(x => x.Value)
                                     select d.Key;

            int r = rand.Next(0, fartherThenAverage.Count());

            spawnNodes.Add("DarkPrim", fartherThenAverage.ElementAt(r));

            return spawnNodes;
        }
Esempio n. 7
0
 ///<summary> Remove an edge between this node and the specified node </summary>
 public void removeEdge(GridNode node)
 {
     edges.Remove(node);
 }
Esempio n. 8
0
 /// <summary> Check if this node is connected to the specified node </summary>
 public bool isConnectedTo(GridNode node)
 {
     return edges.Contains(node);
 }