コード例 #1
0
ファイル: MazeModel.cs プロジェクト: AyeletEhrman/Ex1
        /// <summary>
        /// add a solution to a maze.
        /// </summary>
        /// <param name="name">the name of the maze</param>
        /// <param name="searcher">the searcher algo</param>
        private void AddSolution(string name, int searcher)
        {
            // the solution.
            Solution <State <Position> > solution = new Solution <State <Position> >();
            int nodesEv = 0;
            ISearchable <Position> srMaze = new MazeSearchable(singleMazes[name]);

            // bfs algo.
            if (searcher == 0)
            {
                ISearcher <Position> bfs = new Bfs <Position>();
                solution = bfs.Search(srMaze);
                nodesEv  = bfs.GetNumberOfNodesEvaluated();
            }
            // dfs algo.
            else
            {
                ISearcher <Position> dfs = new Dfs <Position>();
                solution = dfs.Search(srMaze);
                nodesEv  = dfs.GetNumberOfNodesEvaluated();
            }
            string strSol = " ";

            // backtrace the maze solution.
            for (int i = solution.SolLst.Count - 1; i > 0; i--)
            {
                // went down.
                if (solution.SolLst[i].StateType.Row > solution.SolLst[i - 1].StateType.Row)
                {
                    strSol += "2";
                }
                // went up.
                else if (solution.SolLst[i].StateType.Row < solution.SolLst[i - 1].StateType.Row)
                {
                    strSol += "3";
                }
                // went left.
                else if (solution.SolLst[i].StateType.Col > solution.SolLst[i - 1].StateType.Col)
                {
                    strSol += "0";
                }
                // went right.
                else if (solution.SolLst[i].StateType.Col < solution.SolLst[i - 1].StateType.Col)
                {
                    strSol += "1";
                }
            }
            // the solution in json.
            JObject sol = new JObject
            {
                { "Name", name },
                { "Solution", strSol },
                { "NodesEvaluated", nodesEv }
            };
            string Jsol = JsonConvert.SerializeObject(sol);

            JsonConvert.DeserializeObject(Jsol);
            // add the solutin.
            solvedMazes.Add(name, Jsol);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: AyeletEhrman/Ex1
        /// <summary>
        /// compares between a bfs and dfs searchers on a maze.
        /// </summary>
        public static void CompareSolvers()
        {
            IMazeGenerator gen = new DFSMazeGenerator();
            // get a random maze size 50X50.
            Maze maze = gen.Generate(50, 50);

            /// print the maze.
            Console.Write(maze);
            // make the maze searchable.
            ISearchable <Position> myMaze = new MazeSearchable(maze);
            // bfs solution.
            ISearcher <Position> bfs = new Bfs <Position>();

            bfs.Search(myMaze);
            // dfs solution.
            ISearcher <Position> dfs = new Dfs <Position>();

            dfs.Search(myMaze);
            // write number of nodes evaluated in each search.
            Console.WriteLine(bfs.GetNumberOfNodesEvaluated());
            Console.WriteLine(dfs.GetNumberOfNodesEvaluated());
            Console.ReadKey();
        }