/// <summary> /// solve maze /// </summary> /// <param name="name">name to solve</param> /// <param name="algoChoose">solve with...</param> /// <returns></returns> public Solution <Position> solveMaze(string name, int algoChoose) { // check if the solution is already exist.. if (solutionCache.ContainsKey(name)) { Solution <Position> sol = solutionCache[name]; return(sol); } // check if we need caculste dfs or bfs if (algoChoose == 1) { // dfs if (poolMaze.ContainsKey(name)) { Maze maze = poolMaze[name]; MazeSearcher mazeSearchable = new MazeSearcher(maze); Searcher <Position> dfsAlgo = new Dfs <Position>(); Solution <Position> sol1 = dfsAlgo.search(mazeSearchable); sol1.setEvaluatedNodes(dfsAlgo.getNumberOfNodesEvaluated()); solutionCache.Add(name, sol1); return(sol1); } else { Console.WriteLine("Error in poolMaze request"); return(null); } } else { // bfs if (poolMaze.ContainsKey(name)) { Maze maze = poolMaze[name]; MazeSearcher mazeSearchable = new MazeSearcher(maze); Searcher <Position> bfsAlgo = new Bfs <Position>(); Solution <Position> sol2 = bfsAlgo.search(mazeSearchable); sol2.setEvaluatedNodes(bfsAlgo.getNumberOfNodesEvaluated()); solutionCache.Add(name, sol2); return(sol2); } else { Console.WriteLine("Error in poolMaze request"); return(null); } } }
/// <summary> /// Solves the game according the type of algorithm /// </summary> /// <param name="name">The name of the maze.</param> /// <param name="algorithm">The algorithm we want to solve by.</param> /// <returns> /// a string of the solution /// </returns> public string solve(string name, ISearcher <Position> algorithm) { if (!allMazes.ContainsKey(name)) { return("the maze doesn't exist in the server"); } if (solutions.ContainsKey(name)) { return(solutions[name]); } else { Maze currentMaze = allMazes[name]; MazeSearcher newMaze = new MazeSearcher(currentMaze); Solution <Position> sol = algorithm.search(newMaze); //translate the solution to string string solString = solutionToString(sol); solutions.Add(name, solString); return(solString); } }
/// <summary> /// compare the slovers. /// </summary> public void compareSolvers() { DFSMazeGenerator mazeCreator = new DFSMazeGenerator(); Maze maze = mazeCreator.Generate(25, 25); MazeSearcher mazeSearchable = new MazeSearcher(maze); // dfs Searcher <Position> dfsAlgo = new Dfs <Position>(); Solution <Position> sol1 = dfsAlgo.search(mazeSearchable); Console.WriteLine(dfsAlgo.getNumberOfNodesEvaluated()); List <State <Position> > pathdfs = sol1.getList(); // bfs Searcher <Position> bfsAlgo = new Bfs <Position>(); // MazeSearcher mazeSearchable = new MazeSearcher(maze); Solution <Position> sol2 = bfsAlgo.search(mazeSearchable); Console.WriteLine(bfsAlgo.getNumberOfNodesEvaluated()); List <State <Position> > pathbfs = sol2.getList(); foreach (State <Position> s in pathdfs) { int x = s.getState().Row; int y = s.getState().Col; Console.Write("({0},{1}), ", x, y); } Console.WriteLine(); // convert maze to string before the print String mazeString = maze.ToString(); Console.WriteLine(mazeString); Console.WriteLine(); foreach (State <Position> s in pathbfs) { int x = s.getState().Row; int y = s.getState().Col; Console.Write("({0},{1}) ", x, y); } Console.ReadKey(); }