//Draw process of expanding node public void Draw(point2D initial, point2D goal, List <grid> wall, point2D visitedNode, int mapWidth, int mapLength) { Console.Clear(); bool wallDrawn = false; for (int i = 0; i < mapWidth; i++) { for (int j = 0; j < mapLength; j++) { if ((initial.X == j) && (initial.Y == i)) { Console.Write("|i"); continue; } if ((goal.X == j) && (goal.Y == i)) { Console.Write("|g"); continue; } if ((visitedNode.X == j) && (visitedNode.Y == i)) { Console.Write("|x"); continue; } foreach (grid g in wall) { if ((g.IsWall == true) && (g.Pos.X == j) && (g.Pos.Y == i)) { Console.Write("|w"); wallDrawn = true; break; } wallDrawn = false; } if (wallDrawn == false) { Console.Write("| "); } } Console.WriteLine("|"); } if ((visitedNode.X == goal.X) && (visitedNode.Y == goal.Y)) { Console.WriteLine("\nSolution found at: ({0},{1})", visitedNode.X, visitedNode.Y); } else { Console.WriteLine("\nExpanding node: ({0},{1})", visitedNode.X, visitedNode.Y); } }
//Robot constructor public robot(string initialState, string goalState, map map) { IntFromString ifs = new IntFromString(initialState); List <int> coordinate = ifs.getIntFromString(); pos = new point2D(coordinate[0], coordinate[1]); ifs = new IntFromString(goalState); coordinate = ifs.getIntFromString(); goalPos = new point2D(coordinate[0], coordinate[1]); robotMap = map; }
//Draw solution public void DrawPath(point2D initial, point2D goal, List <grid> wall, int mapWidth, int mapLength, List <point2D> path) { Console.Clear(); bool wallDrawn = false; for (int i = 0; i < mapWidth; i++) { for (int j = 0; j < mapLength; j++) { if ((initial.X == j) && (initial.Y == i)) { Console.Write("|i"); continue; } if ((goal.X == j) && (goal.Y == i)) { Console.Write("|g"); continue; } if (path.Any(x => x.X == j && x.Y == i)) { Console.Write("|x"); continue; } foreach (grid g in wall) { if ((g.IsWall == true) && (g.Pos.X == j) && (g.Pos.Y == i)) { Console.Write("|w"); wallDrawn = true; break; } wallDrawn = false; } if (wallDrawn == false) { Console.Write("| "); } } Console.WriteLine("|"); } Console.WriteLine(); }
public point2D(point2D parent) { x = parent.X; y = parent.Y; }
//Produce solution action public string produceSolution(string method, point2D initial, point2D child, List <point2D> expanded) { string solution = ""; List <point2D> path = new List <point2D>(); List <string> action = new List <string>(); expanded.Reverse(); foreach (point2D p in expanded) { if ((p.X == child.X) && (p.Y == child.Y)) { path.Add(p); } if (path.Count() != 0) { if ((path.Last().ParentNode.X == p.X) && (path.Last().ParentNode.Y == p.Y)) { path.Add(p); } } } path.Reverse(); //Produce action from path for (int i = 0; i < path.Count(); i++) { if (i == path.Count() - 1) { break; } if (path[i + 1].X == path[i].X + 1) { action.Add(MoveRight()); } if (path[i + 1].X == path[i].X - 1) { action.Add(MoveLeft()); } if (path[i + 1].Y == path[i].Y + 1) { action.Add(MoveDown()); } if (path[i + 1].Y == path[i].Y - 1) { action.Add(MoveUp()); } } foreach (string a in action) { solution = solution + a + "; "; } ui.DrawPath(pos, goalPos, robotMap.WallList, robotMap.Width, robotMap.Length, path); return(method + " " + expanded.Count() + " " + solution); }
public grid(point2D ppos, bool wall) { pos = ppos; isWall = wall; }