예제 #1
0
파일: MazeModle.cs 프로젝트: mordehg/ex2
        /// <summary>
        /// solve the maze problem - singal player.
        /// </summary>
        /// <param name="name">maze name</param>
        /// <param name="algo">0-bfs, 1-dfs</param>
        /// <returns>get solution of maze problem</returns>
        public Solution <Position> Solve(string name, int algo)
        {
            if (this.mazesSinglePlayerPool.ContainsKey(name))
            {
                if (!this.solutionsSinglePlayerPool.ContainsKey(name))
                {
                    ISearcher <Position> searchAlgo;
                    Solution <Position>  solution;
                    Maze maze = this.mazesSinglePlayerPool[name];
                    Adapter <Position>     adapter        = new MazeToSearchableAdapter <Position>(maze);
                    ISearchable <Position> searchableMaze = new Searchable <Position, Direction>(adapter);
                    switch (algo)
                    {
                    case 0:
                        searchAlgo = new Bfs <Position>();
                        break;

                    case 1:
                        searchAlgo = new Dfs <Position>();
                        break;

                    default:
                        //Error at algorithem numeber: 0 - for bfs, 1 - for dfs
                        return(null);
                    }
                    solution = searchAlgo.Search(searchableMaze);
                    this.solutionsSinglePlayerPool.Add(name, solution);
                }
                return(this.solutionsSinglePlayerPool[name]);
            }
            //name of maze doesn't exist at maze single player pool"
            return(null);
        }
예제 #2
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Graph graph = null;

            DA.GetData(0, ref graph);

            Bfs bfs = new Bfs(graph);

            DA.SetData(0, bfs.IsGraphConnected());
        }
예제 #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);
            }
        }
        public static void Client()
        {
            var vertices = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var edges    = new[]
            {
                Tuple.Create(1, 2), Tuple.Create(1, 3), Tuple.Create(2, 4),
                Tuple.Create(3, 5), Tuple.Create(3, 6), Tuple.Create(4, 7),
                Tuple.Create(5, 7), Tuple.Create(5, 8), Tuple.Create(5, 6),
                Tuple.Create(8, 9), Tuple.Create(9, 10), Tuple.Create(8, 10),
            };

            var graph = new Graph <int>(vertices, edges);
            var bfs   = new Bfs();

            var path = new List <int>();

            // Tracing BFS path
            Console.WriteLine(string.Join(", ", bfs.ApplyBfs(graph, 1, v => path.Add(v))));
            // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

            Console.WriteLine(string.Join(", ", path));
            // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

            // Find shorest path
            var startVertex  = 1;
            var shortestPath = bfs.ShortestPathFunction(graph, startVertex);

            foreach (var vertex in vertices)
            {
                Console.WriteLine("shortest path to {0,2}: {1}",
                                  vertex, string.Join(", ", shortestPath(vertex)));
            }
            //shortest path to  1: 1
            //shortest path to  2: 1, 2
            //shortest path to  3: 1, 3
            //shortest path to  4: 1, 2, 4
            //shortest path to  5: 1, 3, 5
            //shortest path to  6: 1, 3, 6
            //shortest path to  7: 1, 2, 4, 7
            //shortest path to  8: 1, 3, 5, 8
            //shortest path to  9: 1, 3, 5, 8, 9
            //shortest path to 10: 1, 3, 5, 8, 10
        }
예제 #5
0
        internal void EnumerateVertices(GraphParameter <Graph> p)
        {
            SimpleIncidenceGraph graph = p.Graph;

            // Arrange

            int source = graph.VertexCount >> 1;

            byte[] setBackingStore = ArrayPool <byte> .Shared.Rent(Math.Max(graph.VertexCount, source + 1));

            Array.Clear(setBackingStore, 0, setBackingStore.Length);
            IndexedSet exploredSet = new(setBackingStore);

            // Act

            IEnumerator <int> basicSteps      = Bfs.EnumerateVertices(graph, source, graph.VertexCount) !;
            IEnumerator <int> enumerableSteps = EnumerableBfs.EnumerateVertices(graph, source, exploredSet) !;

            // Assert

            while (true)
            {
                bool expectedHasCurrent = enumerableSteps.MoveNext();
                bool actualHasCurrent   = basicSteps.MoveNext();

                Assert.Equal(expectedHasCurrent, actualHasCurrent);

                if (!expectedHasCurrent || !actualHasCurrent)
                {
                    break;
                }

                int expected = enumerableSteps.Current;
                int actual   = basicSteps.Current;

                if (expected != actual)
                {
                    Assert.Equal(expected, actual);
                    break;
                }
            }
        }
예제 #6
0
        public JObject GetSolve(string name, int alg)
        {
            ISearcher <Position> algorithm;

            if (alg == 0)
            {
                //BFS way
                algorithm = new Bfs <Position>();
            }
            else
            {
                //DFS way
                algorithm = new Dfs <Position>();
            }
            String  mazeSolve = myModel.solve(name, algorithm);
            JObject solveObj  = new JObject();

            solveObj["Solution"] = mazeSolve;
            return(solveObj);
        }
예제 #7
0
        public void Test_SGG_Creation()
        {
            var g = new SquireGridGraph(ROW, COL, false);


            for (int i = 0; i < g.VerticesNum(); i++)
            {
                int row, col;
                g.GetRowAndColFromVertex(i, out row, out col);

                g.SetNeighbor(row, col, Direction.East, 1);
                g.SetNeighbor(row, col, Direction.South, 1);
                g.SetNeighbor(row, col, Direction.West, 1);
                g.SetNeighbor(row, col, Direction.North, 1);
            }

            ITravel dfs = new Bfs(g, preVisit);

            dfs.Travel(16);
            Assert.AreEqual(6280, g.EdgeNum());
        }
예제 #8
0
        /// <summary>
        /// Executes the commands that the client sent
        /// </summary>
        /// <param name="args">The arguments of the commands.</param>
        /// <param name="client">The client that sent the command.</param>
        /// <returns>a string of the result to the client</returns>
        public string Execute(string[] args, TcpClient client)
        {
            string name          = args[0];
            int    algorithmType = int.Parse(args[1]);
            ISearcher <Position> algorithm;

            //find what algorithm it is
            if (algorithmType == 0)
            {
                //BFS way
                algorithm = new Bfs <Position>();
            }
            else
            {
                //DFS way
                algorithm = new Dfs <Position>();
            }
            string sol            = model.solve(name, algorithm);
            int    nodesEvaluated = algorithm.EvaluatedNodes;

            return(ToJSON(name, sol, nodesEvaluated));
        }
예제 #9
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);
        }
예제 #10
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();
        }