//We have an augmented grid of doors and exits, with this, we can build a graph //TS private void createFromFound(int[,,] found) { Dictionary <int, List <int> > roomsPerDoor = new Dictionary <int, List <int> >(); Dictionary <int, List <int> > doorsPerRoom = new Dictionary <int, List <int> >(); Dictionary <int, bool> isExit = new Dictionary <int, bool>(); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { if (found[i, j, 0] != 0) //If there is a door { int id = found[i, j, 0]; if (!roomsPerDoor.ContainsKey(id)) { roomsPerDoor.Add(id, new List <int> { found[i, j, 1], found[i, j, 2] }); isExit.Add(id, found[i, j, 3] == 2); if (!(doorsPerRoom.ContainsKey(found[i, j, 1]))) { doorsPerRoom.Add(found[i, j, 1], new List <int> { id }); } else { doorsPerRoom[found[i, j, 1]].Add(id); } if (!(doorsPerRoom.ContainsKey(found[i, j, 2]))) { doorsPerRoom.Add(found[i, j, 2], new List <int> { id }); } else { doorsPerRoom[found[i, j, 2]].Add(id); } } } } } //After we have built the two new datastructures, we can finally create the graph using DFS //TODO: DFS IMPLEMENTATION //Add the first node to the graph fGraph.AddNode(1, new List <int>(), isExit[1]); centralEdgeId = 1; DFSGraphBuilder(roomsPerDoor, doorsPerRoom, isExit, new List <int>()); string s = ""; }
//Use DFS to expand both mental maps private void DFSExpand(FloorGraph extGraph, int commonNode) { //Start at common node, travel through the tree preferring lower EDGE id's //Travel along the external graphs path, add nodes, edges and references to internal graph //visited edges List <int> visited = new List <int>(); //Set the highest certainty and liveness if (fGraph.Nodes[commonNode].Certainty > extGraph.Nodes[commonNode].Certainty) { extGraph.Nodes[commonNode].Certainty = fGraph.Nodes[commonNode].Certainty; extGraph.Nodes[commonNode].Live = fGraph.Nodes[commonNode].Live; } else { fGraph.Nodes[commonNode].Certainty = extGraph.Nodes[commonNode].Certainty; fGraph.Nodes[commonNode].Live = extGraph.Nodes[commonNode].Live; } foreach (int e in extGraph.Nodes[commonNode].GetIncident) { //If edge has not been visited yet if (!(visited.Contains(e))) { //Set edge to visited visited.Add(e); //If iNodes does not contain a refrence to the edge if (!(fGraph.Nodes[commonNode].GetIncident.Contains(e))) { int destNode = extGraph.Edges[e].GetDestination; //Add reference in commonnode fGraph.Nodes[commonNode].GetIncident.Add(e); //Copy edge to iEdges extGraph.AddEdge(extGraph.Edges[e].Id, extGraph.Edges[e].Room, extGraph.Edges[e].GetOrigin, extGraph.Edges[e].GetDestination, extGraph.Edges[e].Certainty, extGraph.Edges[e].Live); //Add destination node to iNodes fGraph.AddNode(destNode, new List <int>(), extGraph.Nodes[destNode].Exit, extGraph.Nodes[destNode].Certainty, extGraph.Nodes[destNode].Live); //Recurse on destination DFSExpand(extGraph, destNode); } else { //set certainty and liveness if (fGraph.Edges[e].Certainty > extGraph.Edges[e].Certainty) { extGraph.Edges[e].Certainty = fGraph.Edges[e].Certainty; extGraph.Edges[e].Live = fGraph.Edges[e].Live; } else { fGraph.Edges[e].Certainty = extGraph.Edges[e].Certainty; fGraph.Edges[e].Live = extGraph.Edges[e].Live; } //Recurse on destination DFSExpand(extGraph, fGraph.Edges[e].GetDestination); } } } }