/// <summary> /// add a solution to a maze. /// </summary> /// <param name="name">the name of the maze</param> /// <param name="searcher">the searcher algo</param> private void AddSolution(string name, int searcher) { // the solution. Solution <State <Position> > solution = new Solution <State <Position> >(); int nodesEv = 0; ISearchable <Position> srMaze = new MazeSearchable(singleMazes[name]); // bfs algo. if (searcher == 0) { ISearcher <Position> bfs = new Bfs <Position>(); solution = bfs.Search(srMaze); nodesEv = bfs.GetNumberOfNodesEvaluated(); } // dfs algo. else { ISearcher <Position> dfs = new Dfs <Position>(); solution = dfs.Search(srMaze); nodesEv = dfs.GetNumberOfNodesEvaluated(); } string strSol = " "; // backtrace the maze solution. for (int i = solution.SolLst.Count - 1; i > 0; i--) { // went down. if (solution.SolLst[i].StateType.Row > solution.SolLst[i - 1].StateType.Row) { strSol += "2"; } // went up. else if (solution.SolLst[i].StateType.Row < solution.SolLst[i - 1].StateType.Row) { strSol += "3"; } // went left. else if (solution.SolLst[i].StateType.Col > solution.SolLst[i - 1].StateType.Col) { strSol += "0"; } // went right. else if (solution.SolLst[i].StateType.Col < solution.SolLst[i - 1].StateType.Col) { strSol += "1"; } } // the solution in json. JObject sol = new JObject { { "Name", name }, { "Solution", strSol }, { "NodesEvaluated", nodesEv } }; string Jsol = JsonConvert.SerializeObject(sol); JsonConvert.DeserializeObject(Jsol); // add the solutin. solvedMazes.Add(name, Jsol); }
/// <summary> /// Solves a requested maze /// </summary> /// <param name="name"> name of maze to solve </param> /// <param name="searchType"> type of search to perform </param> /// <returns> Solution for the maze </returns> public Solution <Position> SolveMaze(string name, int searchType) { //Already solved this maze if (solvedMazes.ContainsKey(name)) { Console.WriteLine("The maze '{0}' has already been solved...Returning solution", name); // return solvedMazes[name]; return(null); } //Selects wanted game from dictionary MazeGame game; if (singlePlayerGames.Keys.Contains(name)) { game = singlePlayerGames[name]; } else { game = multiPlayerGames[name]; } Maze mazeToSolve = game.Maze; //Creates a new searchable with start and end point of selected maze MazeSearchable searchable = new MazeSearchable(new State <Position>(mazeToSolve.InitialPos), new State <Position>(mazeToSolve.GoalPos)); //Sets the searchable's maze searchable.SetMaze(mazeToSolve); //selects the wanted search method ISearcher <Position> mazeSearcher = searchers[searchType]; //solves the maze Solution <Position> solution = mazeSearcher.Search(searchable); //Adds solution to Dictionary solvedMazes.Add(name, solution); //Convert to JSON and return to client HERE OR IN COMMAND? return(solution); }