///<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; } } } }
///<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); } }
public Wall(GridNode root, GridNode other) { rootNode = root; otherNode = other; }
/// <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; }
/// <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; }
/// <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; }
///<summary> Remove an edge between this node and the specified node </summary> public void removeEdge(GridNode node) { edges.Remove(node); }
/// <summary> Check if this node is connected to the specified node </summary> public bool isConnectedTo(GridNode node) { return edges.Contains(node); }