/// <summary> /// Solves the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns></returns> public MazeSolution SolveMaze(SolveRequest request) { // if there is a maze with this name if (!mazes.ContainsKey(request.MazeName)) { throw new InvalidOperationException(string.Format("There is no maze with the name \"{0}\"", request.MazeName)); } // if solution exists in the cache if (solutionCache.ContainsKey(request.MazeName)) { return(solutionCache[request.MazeName]); } // initialize the search components MazeAdapter adapter = new MazeAdapter(mazes[request.MazeName]); ISearcher <Position> algorithm = GetAlgorithm(request.Algorithm); // search for the solution Solution <Position> sol = algorithm.Search(adapter); // create the mazeSolution object. MazeSolution path = new MazeSolution(algorithm.GetNumberOfNodesEvaluated(), request.MazeName, sol); // save the solution in the cache solutionCache[request.MazeName] = path; //return the solution return(path); }
/// <summary> /// Solves the maze. /// </summary> /// <param name="name">The maze's name.</param> /// <param name="algorithm">The algorithm type to solve the maze.</param> /// <returns></returns> public string SolveMaze(string name, ISearcher <Position> algorithm) { Solution <Position> sol; if (!this.solutions.ContainsKey(mazes[name])) { ObjectAdapter mazeAdapter = new ObjectAdapter(mazes[name]); sol = algorithm.Search(mazeAdapter); sol.EvaluatedNodes = algorithm.GetNumberOfNodesEvaluated(); this.solutions.Add(this.mazes[name], sol); } sol = this.solutions[this.mazes[name]]; StringBuilder way = new StringBuilder(string.Empty); State <Position> first, second; for (int i = 0; i < sol.Trace.Count() - 1; ++i) { first = sol.Trace.ElementAt(i); second = sol.Trace.ElementAt(i + 1); // left if (first.myState.Col < second.myState.Col) { way.Append(0); } // right else if (first.myState.Col > second.myState.Col) { way.Append(1); } // up else if (first.myState.Row < second.myState.Row) { way.Append(2); } // down else if (first.myState.Row > second.myState.Row) { way.Append(3); } } return(way.ToString()); }
static void Main(string[] args) { DFSMazeGenerator mazeGen = new DFSMazeGenerator(); Maze maze = mazeGen.Generate(50, 50); Console.WriteLine(maze.ToString()); MazeAdapter mazeAdapter = new MazeAdapter(maze); SearchAlgorithmFactory <Position> factory = new SearchAlgorithmFactory <Position>(); ISearcher <Position> bfsSearcher = factory.GetSearchAlgorithm("bfs"); ISearcher <Position> dfsSearcher = factory.GetSearchAlgorithm("dfs"); Solution <Position> solution = bfsSearcher.Search(mazeAdapter); Solution <Position> solution1 = dfsSearcher.Search(mazeAdapter); Console.WriteLine("the bfs solved the maze int {0}", bfsSearcher.GetNumberOfNodesEvaluated()); Console.WriteLine("the dfs solved the maze int {0}", dfsSearcher.GetNumberOfNodesEvaluated()); Console.ReadKey(); }