Example #1
0
        private void CarveMap(Cell currentCell, Cell selectedNeighbor)
        {
            var xDifference = currentCell.PositionX - selectedNeighbor.PositionX;
            var yDifference = currentCell.PositionY - selectedNeighbor.PositionY;
            Direction direction;

            if (xDifference != 0)
            {
                direction = xDifference > 0 ? Direction.Up : Direction.Down;
            }
            else
            {
                direction = yDifference > 0 ? Direction.Left : Direction.Right;
            }

            switch (direction)
            {
                case Direction.Up:
                {
                    currentCell.NorthBorder = false;
                    selectedNeighbor.SouthBorder = false;
                    break;
                }
                case Direction.Down:
                {
                    selectedNeighbor.NorthBorder = false;
                    currentCell.SouthBorder = false;
                    break;
                }
                case Direction.Left:
                {
                    currentCell.EastBorder = false;
                    selectedNeighbor.WestBorder = false;
                    break;
                }
                case Direction.Right:
                {
                    currentCell.WestBorder = false;
                    selectedNeighbor.EastBorder = false;
                    break;
                }
            }
        }
Example #2
0
        private void PrintMazeToFile(Cell[,] map, Cell currentCell)
        {
            var path = @"maze.txt";

            using (var sw = File.AppendText(path))
            {
                for (var i = 0; i < height; i += 1)
                {
                    for (var j = 0; j < width; j += 1)
                    {
                        if (map[currentCell.PositionX, currentCell.PositionY] == map[i, j])
                        {
                            sw.Write("*");
                        }
                        else if (map[i, j].State == States.Visited)
                        {
                            sw.Write(" ");
                        }
                        else if (map[i, j].State == States.NotVisited)
                        {
                            sw.Write("0");
                        }
                        else if (map[i, j].State == States.Border)
                        {
                            sw.Write("#");
                        }
                    }
                    sw.WriteLine();
                }
                sw.WriteLine();
            }
        }
Example #3
0
        private void Intialize()
        {
            mazeMap = new Cell[height, width];
            stack = new Stack<Cell>();

            for (var i = 0; i < height; i++)
            {
                for (var j = 0; j < width; j++)
                {
                    mazeMap[i, j] = new Cell(i, j);

                    if (i == 0 || i == width - 1 || j == 0 || j == width - 1)
                    {
                        mazeMap[i, j].State = States.Border;
                    }
                    else
                    {
                        mazeMap[i, j].State = States.NotVisited;
                    }
                }
            }
        }
Example #4
0
        /* *
         * Returns a List of neighboring cells
         *
         * */
        private List<Cell> CheckNeighbors(Cell currentCell, List<Cell> unvisitedNeighbors)
        {
            if (mazeMap[currentCell.PositionX + 1, currentCell.PositionY].State == States.NotVisited)
            {
                unvisitedNeighbors.Add(mazeMap[currentCell.PositionX + 1, currentCell.PositionY]);
            }
            if (mazeMap[currentCell.PositionX - 1, currentCell.PositionY].State == States.NotVisited)
            {
                unvisitedNeighbors.Add(mazeMap[currentCell.PositionX - 1, currentCell.PositionY]);
            }
            if (mazeMap[currentCell.PositionX, currentCell.PositionY + 1].State == States.NotVisited)
            {
                unvisitedNeighbors.Add(mazeMap[currentCell.PositionX, currentCell.PositionY + 1]);
            }
            if (mazeMap[currentCell.PositionX, currentCell.PositionY - 1].State == States.NotVisited)
            {
                unvisitedNeighbors.Add(mazeMap[currentCell.PositionX, currentCell.PositionY - 1]);
            }

            return unvisitedNeighbors;
        }
Example #5
0
        public byte[,] GenerateMaze(int mazeWidth, int mazeHeight, int cavernRate = 3)
        {
            var width  = mazeWidth * 2 + 1;
            var height = mazeHeight * 2 + 1;

            // 1 means 100%, 10 means 1%
            if (cavernRate > 10)
            {
                cavernRate = 10;
            }
            else if (cavernRate < 1)
            {
                cavernRate = 1;
            }

            var maze = new byte[width, height];

            //Generate initial cell
            var randX = random.Next(mazeWidth);
            var randY = random.Next(mazeHeight);
            var cell  = new Cell(randX * 2 + 1, randY * 2 + 1);

            cells.Add(cell);

            maze[cell.X, cell.Y] = 1;

            while (cells.Count > 0)
            {
                if (random.Next(2) == 0)
                {
                    currentCell = cells[random.Next(cells.Count)];
                }
                else
                {
                    currentCell = cells[cells.Count - 1];
                }

                var unvisitedNeighbours = new List <Neighbour>();

                //Right = 1. Bottom = 2. Left = 3. Top = 4.
                if ((currentCell.X + 2 < width) && (maze[currentCell.X + 2, currentCell.Y] == 0))
                {
                    unvisitedNeighbours.Add(new Neighbour(new Cell(currentCell.X + 2, currentCell.Y), new Cell(currentCell.X + 1, currentCell.Y)));
                }

                if ((currentCell.Y + 2 < height) && maze[currentCell.X, currentCell.Y + 2] == 0)
                {
                    unvisitedNeighbours.Add(new Neighbour(new Cell(currentCell.X, currentCell.Y + 2), new Cell(currentCell.X, currentCell.Y + 1)));
                }

                if ((currentCell.X - 2 >= 0) && maze[currentCell.X - 2, currentCell.Y] == 0)
                {
                    unvisitedNeighbours.Add(new Neighbour(new Cell(currentCell.X - 2, currentCell.Y), new Cell(currentCell.X - 1, currentCell.Y)));
                }

                if ((currentCell.Y - 2 >= 0) && maze[currentCell.X, currentCell.Y - 2] == 0)
                {
                    unvisitedNeighbours.Add(new Neighbour(new Cell(currentCell.X, currentCell.Y - 2), new Cell(currentCell.X, currentCell.Y - 1)));
                }

                if (unvisitedNeighbours.Count == 0)
                {
                    cells.Remove(currentCell);
                    continue;
                }

                var randDirection = random.Next(unvisitedNeighbours.Count);

                maze[unvisitedNeighbours[randDirection].TargetCell.X, unvisitedNeighbours[randDirection].TargetCell.Y] = 1;
                maze[unvisitedNeighbours[randDirection].WallCell.X, unvisitedNeighbours[randDirection].WallCell.Y]     = 1;

                cells.Add(unvisitedNeighbours[randDirection].TargetCell);

                if (random.Next(cavernRate) == 0)
                {
                    unvisitedNeighbours.RemoveAt(randDirection);

                    var diagonalNeighbours = new List <Neighbour>();

                    if (currentCell.X + 2 < width && currentCell.Y + 2 < height && (maze[currentCell.X + 2, currentCell.Y + 2] == 0))
                    {
                        diagonalNeighbours.Add(new Neighbour(new Cell(currentCell.X + 2, currentCell.Y + 2), new Cell(currentCell.X + 1, currentCell.Y + 1)));
                    }

                    if (currentCell.X + 2 < width && currentCell.Y - 2 >= 0 && (maze[currentCell.X + 2, currentCell.Y - 2] == 0))
                    {
                        diagonalNeighbours.Add(new Neighbour(new Cell(currentCell.X + 2, currentCell.Y - 2), new Cell(currentCell.X + 1, currentCell.Y - 1)));
                    }

                    if (currentCell.X - 2 >= 0 && currentCell.Y - 2 >= 0 && (maze[currentCell.X - 2, currentCell.Y - 2] == 0))
                    {
                        diagonalNeighbours.Add(new Neighbour(new Cell(currentCell.X - 2, currentCell.Y - 2), new Cell(currentCell.X - 1, currentCell.Y - 1)));
                    }

                    if (currentCell.X - 2 >= 0 && currentCell.Y + 2 < height && (maze[currentCell.X - 2, currentCell.Y + 2] == 0))
                    {
                        diagonalNeighbours.Add(new Neighbour(new Cell(currentCell.X - 2, currentCell.Y + 2), new Cell(currentCell.X - 1, currentCell.Y + 1)));
                    }

                    foreach (var neighbour in unvisitedNeighbours)
                    {
                        maze[neighbour.WallCell.X, neighbour.WallCell.Y] = 1;
                    }

                    foreach (var diaNeighbour in diagonalNeighbours)
                    {
                        maze[diaNeighbour.WallCell.X, diaNeighbour.WallCell.Y] = 1;
                    }
                }
            }

            return(maze);
        }
Example #6
0
        private Cell getOppositeCell(Cell c, MazeWall wall)
        {
            bool xMin = false;
            bool xMax = false;
            bool yMin = false;
            bool yMax = false;

            if (c.X == 0)
                xMin = true;
            if (c.X == this.width - 1)
                xMax = true;
            if (c.Y == 0)
                yMin = true;
            if (c.Y == this.height - 1)
                yMax = true;

            Cell temp = null;

            switch (wall)
            {
                case MazeWall.RIGHT:
                    if (!xMax)
                        temp = this.maze[c.Y, c.X + 1];
                    break;
                case MazeWall.LEFT:
                    if (!xMin)
                        temp = this.maze[c.Y, c.X - 1];
                    break;
                case MazeWall.BOTTOM:
                    if (!yMax)
                        temp = this.maze[c.Y + 1, c.X];
                    break;
                case MazeWall.TOP:
                    if (!yMin)
                        temp = this.maze[c.Y - 1, c.X];
                    break;
            }

            if (temp != null)
                return temp;

            return null;
        }
Example #7
0
        private void addCellWallsToList(Cell c)
        {
            bool xMin = false;
            bool xMax = false;
            bool yMin = false;
            bool yMax = false;

            if (c.X == 0)
                xMin = true;
            if (c.X == this.width - 1)
                xMax = true;
            if (c.Y == 0)
                yMin = true;
            if (c.Y == this.height - 1)
                yMax = true;

            Cell temp = null;

            if(!xMin)
            {
                temp = this.maze[c.Y, c.X - 1];
                    this.wallList.Add(temp);
            }

            if(!xMax)
            {
                temp = this.maze[c.Y, c.X + 1];
                        this.wallList.Add(temp);
            }

            if(!yMin)
            {
                temp = this.maze[c.Y - 1, c.X];
                        this.wallList.Add(temp);
            }

            if(!yMax)
            {
                temp = this.maze[c.Y + 1, c.X];
                        this.wallList.Add(temp);
            }

            if (temp == null)
            {
                temp = this.maze[c.Y, c.X];
                this.wallList.Add(temp);
            }
        }