Пример #1
0
        static G RandomAlgorithm <G, T>(Grid grid, Algorithm algorithm) where G : Grid where T : Cell
        {
            switch (algorithm)
            {
            case Algorithm.AldousBroder: return(AldousBroder.CreateMaze <G, T>(grid as G));

            case Algorithm.BinaryTree: return(BinaryTree.CreateMaze(grid) as G);

            case Algorithm.HuntAndKill: return(HuntAndKill.CreateMaze <G, T>(grid as G));

            case Algorithm.RecursiveBacktracker: return(RecursiveBacktracker.CreateMaze <G, T>(grid as G));

            case Algorithm.Sidewinder: return(Sidewinder.CreateMaze(grid) as G);

            case Algorithm.Wilsons: return(Wilsons.CreateMaze <G, T>(grid as G));

            case Algorithm.Kruskals: return(Kruskals.CreateMaze(grid) as G);

            case Algorithm.Prims: return(Prims.CreateMaze <G, T>(grid as G));

            case Algorithm.TruePrims: return(TruePrims.CreateMaze <G, T>(grid as G));

            case Algorithm.GrowingTree: return(GrowingTree.CreateMaze <G, T>(grid as G));

            case Algorithm.RecursiveDivision: return(RecursiveDivision.CreateMaze(grid) as G);

            case Algorithm.Ellers: return(Ellers.CreateMaze(grid) as G);

            case Algorithm.Houstons: return(Houstons.CreateMaze <G, T>(grid as G));
            }
            return(null);
        }
Пример #2
0
        public void TestRecursiveDivisionColoredGrid()
        {
            var coloredGrid = new ColoredGrid(25, 25);

            RecursiveDivision.On(coloredGrid);
            coloredGrid.Distances = coloredGrid.GetCenterCell().Distances;
            coloredGrid.ToBitmap().Save("recursivedivision-colored.png");
        }
        private static void Main(string[] args)
        {
            RecursiveDivision recursiveDivision = new RecursiveDivision(30, 30);

            // Kick off algorithm
            recursiveDivision.MakeMaze();

            recursiveDivision.PrintMaze();
        }
Пример #4
0
        public void TestRecursiveDivision()
        {
            var grid = new Grid(5, 5);

            RecursiveDivision.On(grid).ToBitmap().Save("recursivedivision.png");
        }
Пример #5
0
        public static void Main(string[] args)
        {
            bool saveAnimation = false;

            for (int i = 0; i < args.Length; i++)
            {
                saveAnimation |= (args[i] == "-animation" || args[i] == "/animation");
            }

            Console.Write("Load or Generate? [l/g] ");
            string cmd = Console.ReadLine();
            Image  solveit;
            string filename;

            if (cmd == "g")
            {
                Console.WriteLine("Choose the generator:");
                Console.WriteLine("0 - Recursive backtracking");
                Console.WriteLine("1 - Recursive division");
                Console.WriteLine("2 - Noise");
                Console.Write("#");
                cmd = Console.ReadLine();

                Generator generator;
                switch (cmd)
                {
                case "0":
                    generator = new RecursiveBacktracing();
                    break;

                case "1":
                    generator = new RecursiveDivision();
                    break;

                case "2":
                    generator = new Noise();
                    break;

                default:
                    return;
                }

                int w, h;

                try {
                    Console.Write("Width: ");
                    w = int.Parse(Console.ReadLine());
                    Console.Write("Height: ");
                    h = int.Parse(Console.ReadLine());
                } catch (Exception e) {
                    Console.WriteLine("Exception: " + e.Message);
                    return;
                }

                filename = cmd + "x" + w + "x" + h;

                if (w % 2 == 0)
                {
                    w++;
                }
                if (h % 2 == 0)
                {
                    h++;
                }

                if (saveAnimation)
                {
                    generator.enableHistory();
                }

                bool[,] lab = generator.generate(w, h);
                Console.WriteLine(generator.getdeadends() + " dead ends.");
                solveit = saveMazeImage(lab, filename + ".png");
                //saveMazeCheat((Image)solveit.Clone(), generator.solution(), filename + "-cheat.png");

                if (saveAnimation)
                {
                    List <bool[, ]> ht      = generator.getHistory();
                    ProcessManager  process = new ProcessManager("Saving Animation");
                    for (int i = 0; i < ht.Count; i++)
                    {
                        saveMazeImage(ht[i], "mazeanimation\\" + filename + "-" + i + ".png");
                        process.progress(i, ht.Count);
                    }
                    process.done();
                }

                Console.Write("Solve it? [y/n] ");
                cmd = Console.ReadLine();
                if (cmd != "y")
                {
                    return;
                }

                Console.WriteLine("--- Deleting memory, and solving the maze from zero ---");

                lab       = null;
                generator = null;
            }
            else if (cmd == "l")
            {
                Console.Write("Image: ");
                cmd = Console.ReadLine();

                filename = cmd.Split('.')[0];

                try {
                    solveit = Image.FromFile(cmd);
                } catch (Exception e) {
                    Console.WriteLine("Exception: " + e.Message);
                    return;
                }
            }
            else if (cmd == "c")
            {
                double w1, w2;
                try {
                    w1 = int.Parse(Console.ReadLine());
                    w2 = int.Parse(Console.ReadLine());
                } catch (Exception e) {
                    Console.WriteLine(e.Message);
                    return;
                }
                Console.WriteLine(string.Format("w12/w22={0:0.0000} w22/w12={1:0.0000}", (w1 * w1) / (w2 * w2), (w2 * w2) / (w1 * w1)));
                Console.ReadLine();
                return;
            }
            else
            {
                return;
            }

            Console.WriteLine("Choose the solver:");
            Console.WriteLine("0 - A*");
            Console.Write("#");
            cmd = Console.ReadLine();

            MazeSolver solver;

            switch (cmd)
            {
            case "0":
                solver = new AStar(imageToMaze(solveit));
                break;

            default:
                return;
            }

            Console.Write("Generate graph map? [y/n] ");
            cmd = Console.ReadLine();
            if (cmd == "y")
            {
                savePathGraph((Image)solveit.Clone(), solver.getGraphPoints(), filename + "-pathgraph.png");
            }

            if (saveAnimation)
            {
                solver.enableHistory();
            }
            solver.Point[] sol = solver.solve(0, solver.getLength() - 1);
            if (saveAnimation)
            {
                ProcessManager        process = new ProcessManager("Saving Animation");
                List <solver.Point[]> history = solver.getHistory();
                for (int i = 0; i < history.Count; i++)
                {
                    saveMazeSolutinImage((Image)solveit.Clone(), history[i], "animation\\" + filename + "-" + i + ".png");
                    process.progress(i, history.Count);
                }
                process.done();
            }

            if (sol != null)
            {
                Console.WriteLine("Solution saved!");
                saveMazeSolutinImage((Image)solveit.Clone(), sol, filename + "-solution.png");
            }
            else
            {
                Console.WriteLine("No solution!");
                Console.ReadLine();
            }
        }