예제 #1
0
        /// <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
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Graph graph         = null;
            int   startVertex   = 0;
            bool  keepSearching = false;

            DA.GetData(0, ref graph);
            DA.GetData(1, ref startVertex);
            DA.GetData(2, ref keepSearching);

            Bfs bfs = new Bfs(graph);

            bfs.Search(startVertex, keepSearching);

            DA.SetDataList(0, bfs.VisitedVertices);
            DA.SetDataList(1, bfs.PreviousArray);
        }
예제 #3
0
        private void Update()
        {
            FireSlider.value = _gunTime;
            _gunTime         = Mathf.Max(0, _gunTime - Time.deltaTime);
            if (_gunTime == 0)
            {
                Gun = God.Instance.DefaultGun;
                GunImage.gameObject.SetActive(false);
            }
            ScoreText.text = "SCORE: " + Score;
            _tick         += Time.deltaTime;

            if (_tick >= PathDelay)
            {
                _tick = 0;
                Bfs.Search(Walls, new Vector2Int((int)transform.position.x, (int)transform.position.y));
            }
            if (God.Instance.UseMouse)
            {
                //transform.LookAt2D(God.Instance.Camera.ScreenToWorldPoint(Input.mousePosition));
                GunSprite.transform.LookAt2D(God.Instance.Camera.ScreenToWorldPoint(Input.mousePosition));
            }
            Vector2 moving = new Vector2(Input.GetAxis(_horAxis), Input.GetAxis(_verAxis));

            if (moving.sqrMagnitude > 0.01f)
            {
                Animator.speed = 1f;
                _direction     = moving.normalized;
                transform.LookAt2D((Vector2)transform.position + _direction);
                _rigidbody.MovePosition((Vector2)transform.position
                                        + moving.normalized * God.Instance.PlayerSpeed * Time.deltaTime);
            }
            else
            {
                Animator.speed = 0.1f;
            }
            _fireCoolDown -= Time.deltaTime;
            if (_fireCoolDown < 0 && Input.GetAxis(_fireAxis) > 0)
            {
                _fireCoolDown = Gun.Delay * God.Instance.FireCoolDown;
                Fire(_direction);
            }
        }
예제 #4
0
        /// <summary>
        /// Solves the maze BFS.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>Solution</returns>
        public Solution <Position> solveMazeBFS(string name)
        {
            Solution <Position> solution = null;

            // Check if the maze exist.
            if (modelData.Mazes.ContainsKey(name))
            {
                ISearchable <Position> mazeObjectAdapter = new MazeAdapter(modelData.Mazes[name]);
                ISearcher <Position>   BFS = new Bfs <Position>();
                // Check if the solution exist.
                if (modelData.BfsSolutions.ContainsKey(name))
                {
                    solution = modelData.BfsSolutions[name];
                }
                else
                {
                    // Calculate the solution.
                    solution = BFS.Search(mazeObjectAdapter);
                    modelData.BfsSolutions.Add(name, solution);
                }
            }
            return(solution);
        }
예제 #5
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();
        }
예제 #6
0
파일: Program.cs 프로젝트: mordehg/ex2
        /// <summary>
        /// Roy's example.
        /// </summary>
        public static void test()
        {
            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());
            Adapter <Position>     adapter        = new MazeToSearchableAdapter <Position>(maze);
            ISearchable <Position> searchableMaze = new Searchable <Position, Direction>(adapter);
            ISearcher <Position>   bfs            = new Bfs <Position>();
            ISearcher <Position>   dfs            = new Dfs <Position>();
            Solution <Position>    solBfs         = bfs.Search(searchableMaze);
            Solution <Position>    solDfs         = dfs.Search(searchableMaze);

            Console.WriteLine("bfs " + solBfs.NodesEvaluated.ToString());
            SolutionRepresent <MazeLib.Direction, MazeLib.Position, int> solRepresent = new MazeSolRepreset(solBfs);

            solRepresent.ConvertSolution();

            Console.WriteLine("bfs sol " + solRepresent.ToJSON());
            Console.WriteLine("dfs " + solDfs.NodesEvaluated);
            Console.ReadKey();
        }