Ejemplo n.º 1
0
        /// <summary>
        /// Solves the specified maze.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="algorithmNumber">The algorithm number.</param>
        /// <returns>Solution.</returns>
        public Solution solve(string name, int algorithmNumber)
        {
            var maze           = DictionaryOfMazes[name];
            var searchableMaze = new SearchableMaze(maze);

            if (DictionaryOfMazesAndSolutions.ContainsKey(searchableMaze))
            {
                return(DictionaryOfMazesAndSolutions[searchableMaze]);
            }
            Solution  solution;
            ISearcher searchAlgorithm = null;

            switch (algorithmNumber)
            {
            case 0:
                searchAlgorithm = new BestFirstSearch <PointState>();
                break;

            case 1:
                searchAlgorithm = new DepthFirstSearch <PointState>();
                break;
            }

            solution      = searchAlgorithm.search(searchableMaze);
            EvaluateNodes = searchAlgorithm.getNumberOfNodesEvaluated();
            DictionaryOfMazesAndSolutions.Add(searchableMaze, solution);
            return(solution);
        }
Ejemplo n.º 2
0
 // The maze is provided by the constructor, and the game holds a reference to the players
 // In a dictionary
 public MultiplayerMazeGame(SearchableMaze maze)
 {
     this.maze         = maze;
     this.name         = maze.name;
     joinable          = true;
     players           = new Dictionary <TcpClient, Position>();
     playerConnections = new Dictionary <TcpClient, StreamWriter>();
 }
Ejemplo n.º 3
0
        /// <summary>
        /// The GetGeneratedMaze function generates a maze using the model
        /// </summary>
        /// <param name="name">The maze name</param>
        /// <param name="rows">Number of rows to generate</param>
        /// <param name="columns">Number of columns to generate</param>
        /// <returns></returns>
        // GET: api/Maze/Benjy/5/5
        public JObject GetGeneratedMaze(string name, int rows, int columns)
        {
            SearchableMaze m    = model.GenerateMaze(name, rows, columns);
            string         json = m.toJSON();

            Console.WriteLine(json);
            // The maze is re-serialized and returned as a jObject (which is auto-converted to json)
            return(JObject.Parse(json));
        }
Ejemplo n.º 4
0
        public void Equals_Obj_Test_Matching_Same_Solotion()
        {
            IHeuristic          AirDistance      = new MazeAirDistance();
            ASearchingAlgorithm astarAir         = new Astar(AirDistance);
            MazeGenerator       DfsMazeGenerator = new DfsMazeGenerator();
            maze        Dfs            = DfsMazeGenerator.generatMaze(20, 20);
            ISearchable SearchableMaze = new SearchableMaze(Dfs, false);
            Solution    target         = astarAir.Solve(SearchableMaze);

            Solution inObject = astarAir.Solve(SearchableMaze);
            bool     expected = true;
            bool     actual   = target.GetSolutionPath().Capacity.Equals(inObject.GetSolutionPath().Capacity);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        public void Equals_Sol_Test_null()
        {
            IHeuristic          AirDistance      = new MazeAirDistance();
            ASearchingAlgorithm astarAir         = new Astar(AirDistance);
            MazeGenerator       DfsMazeGenerator = new DfsMazeGenerator();
            maze        Dfs             = DfsMazeGenerator.generatMaze(20, 20);
            ISearchable SearchableMazeA = new SearchableMaze(Dfs, false);
            ISearchable SearchableMazeB = new SearchableMaze(Dfs, true);

            Solution target   = astarAir.Solve(SearchableMazeA);
            Solution inObject = astarAir.Solve(SearchableMazeB);
            bool     expected = false;
            bool     actual   = target.Equals(inObject);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 6
0
        public JObject Solve(string name, int algo)
        {
            MazeGame game = Multi.ContainsKey(name) ? Multi[name] : Single[name];

            if (game.Solution == null)
            {
                ISearcher <Position> searcher = algo == 0 ? (ISearcher <Position>)
                                                new BFS <Position, int>((s1, s2) => 1, (i, j) => i + j) :
                                                new DFS <Position>();
                SearchableMaze smaze = new SearchableMaze(game.Maze);
                game.Solution          = MazeSolution.FromSolution(searcher.Search(smaze));
                game.Solution.MazeName = name;
            }

            return(JObject.Parse(game.Solution.ToJSON()));
        }
Ejemplo n.º 7
0
        public void Run100()
        {
            IHeuristic          AirDistance      = new MazeAirDistance();
            ASearchingAlgorithm astarAir         = new Astar(AirDistance);
            MazeGenerator       DfsMazeGenerator = new DfsMazeGenerator();
            maze        Dfs            = DfsMazeGenerator.generatMaze(20, 20);
            ISearchable SearchableMaze = new SearchableMaze(Dfs, true);
            Solution    inObject       = astarAir.Solve(SearchableMaze);
            Solution    target;

            bool expected = true;

            for (int i = 0; i < 100; i++)
            {
                target = astarAir.Solve(SearchableMaze);
                bool actual = target.GetSolutionPath().Capacity.Equals(inObject.GetSolutionPath().Capacity);
                Assert.AreEqual(expected, actual);
            }
        }
Ejemplo n.º 8
0
        ///ThreadPoolSolveMaze <summary>
        /// thrding to solve maze
        /// </summary>
        /// <param name="p">name to solve</param>
        public void ThreadPoolSolveMaze(object p)
        {
            bool         flag   = false;
            bool         exict  = false;
            string       Name   = (string)p;
            extendedMaze m_Maze = new extendedMaze();

            m_Status = string.Format("started Solving Maze {0}...", p);
            PrintEvent();
            try
            {
                m_Maze = MazeDictionary[Name];
                if (SolutionDictionary.ContainsKey(m_Maze))
                {
                    m_Status = string.Format("Solution For {0} Allready Exict In Dictionary", p);
                    PrintEvent();
                    exict = true;
                }
            }
            catch (System.Exception ex)
            {
                m_Status = ex.Message;
                PrintEvent();
            }


            if (!exict)
            {
                IStoppable Alg;
                if (Curr_Alg == "A*")                                           //Setting Cunfigure
                {                                                               //*****************
                    if (Curr_Heuristic == "MazeAirDistance")                    //3 Diffrent Algorithm
                    {
                        Alg = new Astar(new MazeAirDistance());                 //Astar->MazeAirDistance
                    }
                    else                                                        //Astar->MazeManhattanDistance
                    {
                        Alg = new Astar(new MazeManhattanDistance());           //UCS
                    }
                }
                else
                {
                    Alg = new UCS();
                }
                Workers.Add(Alg);
                try
                {
                    foreach_Mutex.WaitOne();
                    foreach (KeyValuePair <extendedMaze, Solution> item in SolutionDictionary)
                    {
                        if (item.Key.getGrid() == m_Maze.getGrid())                                                                               // Ovaride equals to chack if
                        {                                                                                                                         //a sol is Allready  Exict
                            m_Status = string.Format("Solution  for {0} Allready Exict In {1}", m_Maze.getName(), item.Key.getName());            // if he does copy it
                            PrintEvent();
                            Solution sol = SolutionDictionary[item.Key];
                            if (sol != null && sol.GetSolutionPath().Count > 0)
                            {
                                addSol(m_Maze, sol);
                            }
                            flag = true;
                            break;
                        }
                    }
                    foreach_Mutex.ReleaseMutex();

                    if (!flag)
                    {
                        ISearchable Searchable_Maze;
                        if (!IsDiagonal)                                                               //Diagonal Setting
                        {
                            Searchable_Maze = new SearchableMaze(m_Maze, false);                       //****************
                        }
                        else                                                                           //False->Without
                        {
                            Searchable_Maze = new SearchableMaze(m_Maze, true);                        //True->With
                        }
                        Solution  sol    = (Solution)(((ASearchingAlgorithm)Alg).Solve(Searchable_Maze));
                        ArrayList list   = sol.GetSolutionPath();
                        ArrayList states = new ArrayList();
                        foreach (AState item in list)
                        {
                            states.Add(item.GetState());
                        }
                        m_Maze.m_Solution = m_Maze.printAfterSolution(states);

                        m_Status = string.Format("{0} result Finished...", p);
                        PrintEvent();
                        if (sol != null && sol.GetSolutionPath().Count > 0)
                        {
                            addSol(m_Maze, sol);
                        }
                    }
                }
                catch
                {
                    m_Status = "Cant Find Maze";
                    PrintEvent();
                }
            }
        }