コード例 #1
0
        public void Generate()
        {
            IRandom random = MockRepository.GenerateMock <IRandom>();

            random.Stub(r => r.Next(Arg <int> .Is.Anything)).Return(0);

            IMazeGenerator generator = new DepthFirstMazeGenerator(random);

            MazeGenerationOptions options = new MazeGenerationOptions
            {
                Width  = 3,
                Height = 3
            };
            Maze maze = generator.Generate(options);

            Assert.AreEqual(3, maze.Length);
            Assert.IsTrue(maze.All(m => m.Length == 3));

            Assert.IsTrue(maze[0][0].HasWall(MazeWall.Left | MazeWall.Right));
            Assert.IsTrue(maze[0][1].HasWall(MazeWall.Left | MazeWall.Right));
            Assert.IsTrue(maze[0][2].HasWall(MazeWall.Left));

            Assert.IsTrue(maze[1][2].HasWall(MazeWall.Top | MazeWall.Right));
            Assert.IsTrue(maze[1][1].HasWall(MazeWall.Right | MazeWall.Left));
            Assert.IsTrue(maze[1][0].HasWall(MazeWall.Bottom | MazeWall.Left));

            Assert.IsTrue(maze[2][0].HasWall(MazeWall.Bottom | MazeWall.Right));
            Assert.IsTrue(maze[2][1].HasWall(MazeWall.Left | MazeWall.Right));
            Assert.IsTrue(maze[2][2].HasWall(MazeWall.Top | MazeWall.Left | MazeWall.Right));
        }
コード例 #2
0
        public MazeGraphic Paint(Maze maze, MazeGenerationOptions options)
        {
            ImageSizesHelper helper = new ImageSizesHelper();
            ImageSizes       sizes  = helper.CalculateSizes(maze, options);

            using (Bitmap bitmap = new Bitmap(sizes.Width, sizes.Height))
            {
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    this.DrawMaze(maze, graphics, sizes);

                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        bitmap.Save(memoryStream, ImageFormat.Jpeg);

                        return(new MazeGraphic
                        {
                            Name = Guid.NewGuid().ToString(GuidFormats.Digits),
                            GraphicType = "image",
                            Content = memoryStream.ToArray()
                        });
                    }
                }
            }
        }
コード例 #3
0
        /* Depth-First Search algorithm:
         * 1. create a CellStack (LIFO) to hold a list of cell locations
         * 2. set TotalCells = number of cells in grid
         * 3. choose a cell at random and call it CurrentCell
         * 4. set VisitedCells = 1
         * 5. while VisitedCells < TotalCells
         * 6.    find all neighbors of CurrentCell with all walls intact
         * 7.    if one or more found
         * 8.         choose one at random
         * 9.         knock down the wall between it and CurrentCell
         * 10.        push CurrentCell location on the CellStack
         * 11.        make the new cell CurrentCell
         * 12.        add 1 to VisitedCells
         * 13.     else
         * 14.        pop the most recent cell entry off the CellStack
         * 15.        make it CurrentCell
         * 16.	   endIf
         * 17. endWhile
         */
        public Maze Generate(MazeGenerationOptions options)
        {
            Maze             maze  = new Maze(options.Width, options.Height);
            Stack <MazeCell> cells = new Stack <MazeCell>();

            MazeCell currentCell = maze[this.random.Next(options.Height)][this.random.Next(options.Width)];

            int totalCells   = options.Width * options.Height;
            int visitedCells = 1;

            while (visitedCells < totalCells)
            {
                List <NeighborMazeCell> neighbors = this.neighborFinder.FindNeighborsWithAllWalls(currentCell, maze);

                if (neighbors.Count > 0)
                {
                    NeighborMazeCell chosenNeighbor = neighbors[this.random.Next(neighbors.Count)];

                    this.wallKnocker.KnockDownWallBetweenNeighbors(chosenNeighbor);

                    visitedCells++;
                    cells.Push(currentCell);
                    currentCell = chosenNeighbor.Neighbor;
                }
                else
                {
                    currentCell = cells.Pop();
                }
            }

            this.wallKnocker.CreateEntrance(maze, this.random);
            this.wallKnocker.CreateExit(maze, this.random);

            return(maze);
        }
コード例 #4
0
        public async Task <ActionResult> Generate(MazeGenerationOptions options)
        {
            Maze        maze    = this.mazeGenerator.Generate(options);
            MazeGraphic graphic = this.painter.Paint(maze, options);

            await this.mazeGraphicRepository.Create(graphic);

            return(this.File(graphic.Content, "image/jpeg"));
        }
コード例 #5
0
        public ImageSizes CalculateSizes(Maze maze, MazeGenerationOptions options)
        {
            ImageSizes result = new ImageSizes();

            if (options.FixedSize)
            {
                result.Width      = (int)(sizeX + 2 * marginX);
                result.Height     = (int)(sizeY + 2 * marginY);
                result.CellWidth  = (int)(sizeX / maze.Length);
                result.CellHeight = (int)(sizeY / maze[0].Length);
            }
            else
            {
                result.CellWidth  = 25;
                result.CellHeight = 25;
                result.Width      = (int)(marginX * 2 + result.CellWidth * maze.Length);
                result.Height     = (int)(marginY * 2 + result.CellHeight * maze[0].Length);
            }

            result.MarginX = marginX;
            result.MarginY = marginY;

            return(result);
        }