Пример #1
0
        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());
        }
Пример #2
0
        public JObject SolveMaze(string name)
        {
            Maze mazeFromModel = GetMaze(name);
            //create new adapter
            Maze maze = GetMaze(name);

            adapter = new MazeAdapter <Position>(maze);
            ser     = new BestFirstSearch <Position>();
            sol     = ser.search(adapter);
            Console.WriteLine(sol.Path.Count());
            Console.WriteLine(sol.Path.Count());
            solAdapter = new SolutionAdapter(sol);
            JObject mazeSolutionObj = new JObject();

            mazeSolutionObj["Name"]     = name;
            mazeSolutionObj["Solution"] = solAdapter.ToString();
            return(mazeSolutionObj);
        }
Пример #3
0
        public IHttpActionResult SolveMaze(SolveRequest solveRequest)
        {
            string searchAlgo = "1";

            if (solveRequest.SearchAlgo == "BFS")
            {
                searchAlgo = "0";
            }
            SolveInfo       solveInfo       = Model.SolveMaze(solveRequest.Name, searchAlgo);
            SolutionAdapter solutionAdapter = new SolutionAdapter(solveInfo.Solution);
            string          strSolution     = solutionAdapter.CreateString();

            JObject solutionObj = new JObject();

            solutionObj["Name"]     = solveRequest.Name;
            solutionObj["Solution"] = strSolution;

            return(Ok(solutionObj));
        }
Пример #4
0
        /// <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();
        }
Пример #5
0
        /// <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());
        }
Пример #6
0
        /// <summary>
        /// Executes the specified arguments.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <param name="client">The client.</param>
        /// <returns></returns>
        public ConnectionInfo Execute(string[] args, TcpClient client = null)
        {
            string name = args[0];
            string algo = args[1];

            SolveInfo       solveInfo       = model.SolveMaze(name, algo);
            SolutionAdapter solutionAdapter = new SolutionAdapter(solveInfo.Solution);
            string          strSolution     = solutionAdapter.CreateString();

            JObject solutionObj = new JObject();

            solutionObj["Name"]           = name;
            solutionObj["Solution"]       = strSolution;
            solutionObj["NodesEvaluated"] = solveInfo.NodesEvaluated;

            ConnectionInfo connectionInfo = new ConnectionInfo();

            connectionInfo.Answer          = solutionObj.ToString();
            connectionInfo.CloseConnection = true;

            return(connectionInfo);
        }
Пример #7
0
        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();
        }
Пример #8
0
        public string Execute(string[] args, TcpClient client = null)
        {
            string name      = args[0];
            string algorithm = args[1];
            //get the maze from the model
            Maze mazeFromModel = this.model.GetMaze(name);

            if (mazeFromModel == null)
            {
                //return there is no maze
            }

            //create new adapter
            adapter = new MazeAdapter <Position>(mazeFromModel);

            //if 0 then bfs, if 1 dfs, otherwise print error
            if (algorithm.Equals("0"))
            {
                ser = new BestFirstSearch <Position>();
            }
            else if (algorithm.Equals("1"))
            {
                ser = new DFS <Position>();
            }
            else
            {
                //return algorithm input invalid
            }

            sol = ser.search(adapter);
            Console.WriteLine(sol.Path.Count());
            Console.WriteLine(sol.Path.Count());
            solAdapter = new SolutionAdapter(sol);
            //add the solved maze to the solved mazes dictionary in the model
            this.model.AddSolvedMaze(name, solAdapter.ToString());
            solJson = new SolutionJson(name, solAdapter.ToString(), ser.getNumberOfNodesEvaluated());
            return(solJson.solveToJSON());
        }
        /// <summary>
        /// Solves a maze.
        /// </summary>
        /// <param name="mazeName">Maze name.</param>
        /// <param name="algorithmType">Algorithm type.</param>
        /// <returns>Solution.</returns>
        public Solution <Position> SolveMaze(string mazeName, int algorithmType)
        {
            //Check is solution already exists.
            if (SolvedMazes.ContainsKey(mazeName))
            {
                return(SolvedMazes[mazeName]);
            }

            //Check if maze exists.
            if (!this.GenerateMazes.ContainsKey(mazeName))
            {
                return(null);
            }

            Maze maze = GenerateMazes[mazeName];

            //Solve maze.
            StatePool <Position> statePool = new StatePool <Position>();
            SolutionAdapter      ad        = new SolutionAdapter(maze, statePool);
            ISearcher <Position> algorithm =
                this.AlgorithmFactory.CreateAlgorithm(algorithmType);

            return(algorithm.Search(ad));
        }