Exemple #1
0
        /// <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);
        }
Exemple #2
0
        /// <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();
        }