예제 #1
0
        /// <summary>
        /// Solves the maze.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="typeAlgo">The type algo.</param>
        /// <returns></returns>
        public SolveInfo SolveMaze(string name, string typeAlgo)
        {
            Maze                maze           = singleplayerMazeList[name];
            MazeAdapter         mazeSearchable = new MazeAdapter(maze);     // Isearchable
            var                 temp           = new CompareStates <Position>();
            Solution <Position> solution       = null;
            int                 nodesEvaluated = 0;

            if (solutionsList.ContainsKey(maze))
            {
                return(solutionsList[maze]);
            }

            // find solution of the maze:
            switch (typeAlgo)
            {
            case "0":
                BFS <Position> bfs = new BFS <Position>(temp);
                solution       = bfs.Search(mazeSearchable);
                nodesEvaluated = bfs.GetNumberOfNodesEvaluated();
                break;

            case "1":
                DFS <Position> dfs = new DFS <Position>(temp);
                solution       = dfs.Search(mazeSearchable);
                nodesEvaluated = dfs.GetNumberOfNodesEvaluated();
                break;
            }
            SolveInfo solveInfo = new SolveInfo(nodesEvaluated, solution);

            // add the solution to the solutions dictionary
            solutionsList.Add(maze, solveInfo);
            return(solveInfo);
        }
예제 #2
0
        /// <summary>
        /// solving a private maze
        /// if the solution for this maze already exists it returns it
        /// </summary>
        /// <param name="name"></param>
        /// <param name="algorithm"></param>
        /// <returns></returns>
        public string SolveMaze(string name, int algorithm)
        {
            Maze m = privateMazeDict[name];
            MazeAdapter <Position> maze = new MazeAdapter <Position>(m);
            Searcher <Position>    searcher;
            Solution <Position>    sol;

            if (!privateSolDict.Keys.Contains(name))
            {
                if (algorithm == 0)
                {
                    searcher = new BFS <Position>();
                    CostComparator <Position> compare = new CostComparator <Position>();
                    sol = searcher.Search(maze, compare);
                }
                else
                {
                    searcher = new DFS <Position>();
                    sol      = searcher.Search(maze);
                }

                privateSolDict.Add(name, sol);
            }
            else
            {
                sol = privateSolDict[name];
            }

            string stringSolution         = maze.ToSolution(sol);
            int    numberOfNodesevaluated = sol.EvaluatedNodes;

            stringSolution += " ";
            stringSolution += numberOfNodesevaluated;
            return(stringSolution);
        }
예제 #3
0
        /// <summary>
        /// Solves the maze.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="alg">The alg.</param>
        /// <returns>MazeSolution.</returns>
        public MazeSolution SolveMaze(string name, int alg)
        {
            Solution <Position> sol;
            int nodesEvaluated;

            if (this.mazeSolutionCache.ContainsKey(name))
            {
                return(this.mazeSolutionCache[name]);
            }

            if (this.mazeCache.ContainsKey(name))
            {
                Maze m = this.mazeCache[name];
                if (alg == 0)
                {
                    BFS <Position>         bfs     = new BFS <Position>();
                    ISearchable <Position> newMaze = new MazeAdapter(m);
                    sol            = bfs.Search(newMaze);
                    nodesEvaluated = bfs.GetNumOfNodesEvaluated();
                }
                else
                {
                    DFS <Position>         dfs     = new DFS <Position>();
                    ISearchable <Position> newMaze = new MazeAdapter(m);
                    sol            = dfs.Search(newMaze);
                    nodesEvaluated = dfs.GetNumOfNodesEvaluated();
                }

                State <Position> .StatePool.ClearPool();

                MazeSolution ms = new MazeSolution(sol, name, nodesEvaluated);
                ms.SolutionPath();
                this.mazeSolutionCache.Add(name, ms);
                return(ms);
            }
            else
            {
                return(null);
            }
        }