public string SolveMaze(string mazeName, string algorithm) { if (!NameExistsInDictionary(SinglePlayerMazes, mazeName)) { throw new Exception($"there is no maze with this name {mazeName}"); } if (MazeExistsInSolutions(mazeName)) { SolutionAdapter solutionAdapter1 = new SolutionAdapter(MazeSolutions[mazeName], mazeName); return(solutionAdapter1.ToJson()); } MazeAdapter mazeAdapter = new MazeAdapter(SinglePlayerMazes[mazeName]); ISearcher <Position> searchAlgorithm = GetAlgorithmAccordingToIndicator(algorithm); Solution <Position> solution = searchAlgorithm.Search(mazeAdapter); SolutionAdapter solutionAdapter = new SolutionAdapter(solution, mazeName); MazeSolutions[mazeName] = solution; JObject jobject = new JObject(); jobject["Name"] = mazeName; jobject["Path"] = solutionAdapter.ToJson(); jobject["NodesEvaluated"] = solutionAdapter.Solution.NodesEvaluated;//check if it is connected to the solutiion property return(jobject.ToString()); }
/// <summary> /// Mains the specified arguments. /// </summary> /// <param name="args">The arguments.</param> 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> solution1 = bfsSearcher.Search(mazeAdapter); SolutionAdapter solutionAd1 = new SolutionAdapter(solution1, "bla"); Solution <Position> solution2 = dfsSearcher.Search(mazeAdapter); SolutionAdapter solutionAd2 = new SolutionAdapter(solution2, "bla"); string a = solutionAd1.ToJson(); Console.WriteLine("the bfs solved the maze int {0}", solution1.NodesEvaluated); Console.WriteLine("the dfs solved the maze int {0}", solution2.NodesEvaluated); Console.ReadKey(); }
/// <summary> /// Solves the maze. /// </summary> /// <param name="mazeName">Name of the maze.</param> /// <param name="algorithm">The algorithm.</param> /// <param name="player">The player.</param> /// <returns></returns> public string SolveMaze(string mazeName, string algorithm, Player player) { if (!NameExistsInDictionary(SinglePlayerMazes, mazeName)) { return($"Error: there is no maze with this name {mazeName}"); } if (MazeExistsInSolutions(mazeName)) { SolutionAdapter solutionAdapter1 = new SolutionAdapter(MazeSolutions[mazeName], mazeName); return(solutionAdapter1.ToJson()); } MazeAdapter mazeAdapter = new MazeAdapter(SinglePlayerMazes[mazeName]); ISearcher <Position> searchAlgorithm = GetAlgorithmAccordingToIndicator(algorithm); Solution <Position> solution = searchAlgorithm.Search(mazeAdapter); SolutionAdapter solutionAdapter = new SolutionAdapter(solution, mazeName); MazeSolutions[mazeName] = solution; return(solutionAdapter.ToJson()); }
static void Main(string[] args) { string json = @"{ 'Name': 'mymaze', 'Maze': '0001010001010101110101010000010111111101000001000111010101110001010001011111110100000000011111111111', 'Rows': 10, 'Cols': 10, 'Start': { 'Row': 0, 'Col': 4 }, 'End': { 'Row': 0, 'Col': 0 } }"; Maze maze = Maze.FromJSON(json); DFSMazeGenerator mazeGen = new DFSMazeGenerator(); // Maze maze = mazeGen.Generate(10,10); 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> solution1 = bfsSearcher.Search(mazeAdapter); SolutionAdapter solutionAd1 = new SolutionAdapter(solution1, "bla"); Solution <Position> solution2 = dfsSearcher.Search(mazeAdapter); SolutionAdapter solutionAd2 = new SolutionAdapter(solution2, "bla"); string a = solutionAd1.ToJson(); Console.WriteLine("the bfs solved the maze int {0}", solution1.NodesEvaluated); Console.WriteLine("the dfs solved the maze int {0}", solution2.NodesEvaluated); Console.ReadKey(); }