}// End of IntializeMap /// <summary> /// Set the starting room /// </summary> /// <param name="room">Room number</param> public void SetStartingPosition(int room) { startVertex = FindRoom(room); }
/// <summary> /// Parse XML file /// </summary> private void ParseFile(XmlDocument doc) { vertices.Clear(); edges.Clear(); //Go through each node in XML file foreach (XmlNode node in doc.DocumentElement.ChildNodes) { var vertex = new Vertex(); //One node (room) if (node.Name.Equals("Node")) { //Go through properties of one node (room) foreach (XmlNode n in node.ChildNodes) { if (n.Name.Equals("Identifier")) { vertex.Id = Int32.Parse(n.InnerText); if (vertex.Id > maxId) { maxId = vertex.Id; } } else if (n.Name.Equals("X")) { vertex.X = int.Parse(n.InnerText); } else if (n.Name.Equals("Y")) { vertex.Y = int.Parse(n.InnerText); } else if (n.Name.Equals("Rooms")) { foreach (XmlNode nc in n.ChildNodes) { if (nc.Name.Equals("Rm")) { vertex.Rooms.Add(int.Parse(nc.InnerText)); } } } else if (n.Name.Equals("StartLocation") && bool.Parse(n.InnerText)) { // Set starting location. startVertex = vertex; } } vertices.Add(vertex); } else if (node.Name.Equals("Connection")) { int a = -1, b = -1; double weight = -1; foreach (XmlNode n in node.ChildNodes) { if (n.Name.Equals("A")) { a = Int32.Parse(n.InnerText); } else if (n.Name.Equals("B")) { b = Int32.Parse(n.InnerText); } else if (n.Name.Equals("Weight")) { weight = double.Parse(n.InnerText); } } if (a != -1 && b != -1 && weight != -1) { var edge1 = new DirectedEdge(a, b, weight); var edge2 = new DirectedEdge(b, a, weight); edges.Add(edge1); edges.Add(edge2); } } else { Debug.WriteLine("File is not formatted correctly."); } } graph = new EdgeWeightedDigraph(maxId + 1); foreach (var edge in edges) { graph.AddEdge(edge); } }
/// <summary> /// Get instructions for Roomba to get to room /// </summary> /// <param name="room"></param> /// <returns></returns> public List <Instruction> GetDirections(int room) { _directions = new List <Instruction>(); _distance = 0; int tempX = 0, tempY = 0; //for comparing X and Y of previous node var instructions = new List <Instruction>(); endVertex = FindRoom(room); if (endVertex != null) { var edgePath = DijkstraShortestPath.FindPath(graph, startVertex.Id, endVertex.Id); foreach (var edge in edgePath) { var from = GetVertex(edge.From); var to = GetVertex(edge.To); //Turning from start if (edge.From == startVertex.Id) { //ASSUME STARTING POSITION IS FACING NORTH (north as defined by map) int xDiff = to.X - startVertex.X; if (xDiff > 0) { //go east //TURN RIGHT instructions.Add(new Instruction(InstructionType.Angle, 90)); currentDirection += 90; } else if (xDiff < 0) { //go west //TURN LEFT instructions.Add(new Instruction(InstructionType.Angle, -90)); currentDirection -= 90; } else { int yDiff = to.Y - startVertex.Y; if (yDiff < 0) { //go south //TURN AROUND instructions.Add(new Instruction(InstructionType.Angle, 180)); currentDirection += 180; } else if (yDiff > 0) { //go up (roomba starting should face north so no turn) } } } //Turning from a previous place else { //Only turn if both X AND Y value is different FindTurn(tempX, tempY, from.X, from.Y, to.X, to.Y); } //Add onto move distance (continuing in same direction) _distance += edge.Weight; //Check if we've reached the end and are done getting directions! if (to.Id == endVertex.Id) { _directions.Add(new Instruction(InstructionType.Distance, _distance)); break; } //Set for next iteration of loop tempX = from.X; tempY = from.Y; } PrepareNextNavigation(); } return(_directions); }