Exemple #1
0
        static void Main(string[] args)
        {
            DFSMazeGenerator mazeGenerator = new DFSMazeGenerator();
            Maze             maze          = mazeGenerator.Generate(5, 5);

            Console.WriteLine(maze.ToString());

            SearchableMaze searchableMaze = new SearchableMaze(maze);
            BFS <Position> bfsSolver      = new BFS <Position>();
            DFS <Position> dfsSolver      = new DFS <Position>();

            Console.WriteLine("BFS:");
            Console.WriteLine(bfsSolver.search(searchableMaze));
            Console.WriteLine(bfsSolver.getNumberOfNodesEvaluated());

            Console.WriteLine("DFS:");
            Console.WriteLine(dfsSolver.search(searchableMaze));
            Console.WriteLine(dfsSolver.getNumberOfNodesEvaluated());

            //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);
            //Console.Write(maze.ToString());
            //SearchableMaze searchableMaze = new SearchableMaze(maze);
            //BFS<Position> bfsSolver = new BFS<Position>();
            //DFS<Position> dfsSolver = new DFS<Position>();
            //Console.WriteLine("BFS:");
            //Console.WriteLine(bfsSolver.search(searchableMaze));
            //Console.WriteLine(bfsSolver.getNumberOfNodesEvaluated());

            //Console.WriteLine("DFS:");
            //Console.WriteLine(dfsSolver.search(searchableMaze));
            //Console.WriteLine(dfsSolver.getNumberOfNodesEvaluated());



            Console.Read();
        }
Exemple #2
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());
        }