Ejemplo n.º 1
0
 /// <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>
        /// method that creates a maze and solves it by dfs and bfs
        /// </summary>
        public static void CompareSolvers()
        {
            DFSMazeGenerator maze        = new DFSMazeGenerator();
            Maze             currentMaze = maze.Generate(100, 100);

            Console.WriteLine(currentMaze.ToString());
            MazeSearcher         newMaze   = new MazeSearcher(currentMaze);
            ISearcher <Position> bfsSearch = new Bfs <Position>();
            ISearcher <Position> dfsSearch = new Dfs <Position>();
            Solution <Position>  solBfs    = bfsSearch.search(newMaze);
            Solution <Position>  solDfs    = dfsSearch.search(newMaze);

            //printing the num of evaluated nodes
            Console.WriteLine("bfs " + bfsSearch.EvaluatedNodes);
            Console.WriteLine("dfs " + dfsSearch.EvaluatedNodes);
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        /// <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();
        }