Exemplo n.º 1
0
        /// <summary>
        /// Solves the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="algo">The algo.</param>
        /// <returns>the solution by the desired algo</returns>
        public string Solve(string name, int algo)
        {
            if (algo == 0)
            {
                //checks if the  BFS solution is already exists.
                if (mazeBFSSolutions.ContainsKey(name))
                {
                    return(mazeBFSSolutions[name]);
                }
            }
            else if (mazeDFSSolutions.ContainsKey(name))
            //checks if the DFS solution is already exists.
            {
                return(mazeDFSSolutions[name]);
            }


            //if there is no existing solution - solving it.
            Maze           maze           = singleplayerMazesDictionary[name];
            SearchableMaze searchableMaze = new SearchableMaze(maze);
            JObject        solveObj       = new JObject();

            solveObj["Name"] = name;

            if (algo == 0)
            {
                Solution <Position> bfsSolution = new Solution <Position>();
                bfsSolution = bfsSolver.search(searchableMaze);
                //Json
                solveObj["Solution"]       = CalculateSolution(bfsSolution.PathToGoal());
                solveObj["NodesEvaluated"] = bfsSolver.getNumberOfNodesEvaluated().ToString();
                mazeBFSSolutions.Add(name, solveObj.ToString());
            }
            else
            {
                Solution <Position> dfsSolution = new Solution <Position>();
                dfsSolution = dfsSolver.search(searchableMaze);
                //Json
                solveObj["Solution"]       = CalculateSolution(dfsSolution.PathToGoal());
                solveObj["NodesEvaluated"] = dfsSolver.getNumberOfNodesEvaluated().ToString();
                mazeDFSSolutions.Add(name, solveObj.ToString());
            }
            //adding the solution to the dictionary.

            return(solveObj.ToString());
        }
Exemplo n.º 2
0
        public Solution <Position> SolveMaze(string nameOfGame, int algorithm)
        {
            MazeInfo mazeInfo = GetMazeInfoOf(nameOfGame);

            if (mazeInfo == null)
            {
                throw new Exception($"There is no game with the name '{nameOfGame}'");
            }
            if (mazeInfo.Solution == null)
            {
                // Solution is not inside the cache, so create one.
                ISearchable <Position> searchableMaze = new SearchableMaze(mazeInfo.Maze);
                ISearcher <Position>   searcher       = SearcherFactory <Position> .Create(algorithm);

                mazeInfo.Solution = searcher.Search(searchableMaze);
            }
            return(mazeInfo.Solution);
        }