예제 #1
0
        /// <summary>
        /// Generates a new random maze.
        /// </summary>
        public void GenerateRandomMaze()
        {
            // reset lists
            _mergyMazeCells = new List <MazeCell_Mergy>();

            // initialize the maze.
            for (int row = 0; row < _rows; row++)
            {
                for (int col = 0; col < _columns; col++)
                {
                    // create a new cell in this position
                    MazeCells[row, col] = new MazeCell(new Rectangle(col * _cellWidth, row * _cellHeight, _cellWidth, _cellHeight), row, col, _cellWidth, _cellHeight);
                    _mergyMazeCells.Add(new MazeCell_Mergy(new List <MazeCell_Wall>()));

                    // add wall on up or left if appropriate
                    if (row > 0)
                    {
                        // add up wall
                        _mazeCellWalls[row - 1, col] = new MazeCell_Wall(WallConnectability.ConnectsVertically,
                                                                         MazeCells[row - 1, col], _mergyMazeCells[(((row - 1) * _columns) + col)],
                                                                         MazeCells[row, col], _mergyMazeCells[((row * _columns) + col)]);
                        _mergyMazeCells[(((row - 1) * _columns) + col)].Walls.Add(_mazeCellWalls[row - 1, col]);
                        _mergyMazeCells[((row * _columns) + col)].Walls.Add(_mazeCellWalls[row - 1, col]);
                    }
                    if (col > 0)
                    {
                        // add left wall
                        _mazeCellWalls[row, col - 1] = new MazeCell_Wall(WallConnectability.ConnectsHorizontally,
                                                                         MazeCells[row, col - 1], _mergyMazeCells[((row * _columns) + (col - 1))],
                                                                         MazeCells[row, col], _mergyMazeCells[((row * _columns) + col)]);
                        _mergyMazeCells[((row * _columns) + (col - 1))].Walls.Add(_mazeCellWalls[row, col - 1]);
                        _mergyMazeCells[((row * _columns) + col)].Walls.Add(_mazeCellWalls[row, col - 1]);
                    }
                }
            }

            // variables needed for merging
            MazeCell_Mergy mergy1;
            MazeCell_Mergy mergy2;
            MazeCell_Wall  wall;

            // merge untill all connected
            int mergeCount = (_rows * _columns) - 1;

            for (int i = 0; i < mergeCount; i++)
            {
                // choose random element in _mergyMazeCells (mergy cell 1)
                mergy1 = _mergyMazeCells[Utils.Random.Next(0, _mergyMazeCells.Count)];

                // choose random wall in that mergy maze cell (other side is mergy cell 2)
                wall = mergy1.Walls[Utils.Random.Next(0, mergy1.Walls.Count)];
                if (wall.MergyCell1 == mergy1)
                {
                    mergy2 = wall.MergyCell2;
                }
                else
                {
                    mergy2 = wall.MergyCell1;
                }

                // tell cells on both sides of wall which direction they can move in
                if (wall.WallConnectability == WallConnectability.ConnectsHorizontally)
                {
                    wall.Cell1.CanGoRight = true;
                    wall.Cell2.CanGoLeft  = true;
                }
                else
                {
                    wall.Cell1.CanGoDown = true;
                    wall.Cell2.CanGoUp   = true;
                }

                // delete that wall on both mergy cells
                mergy1.Walls.Remove(wall);
                mergy2.Walls.Remove(wall);


                // check all walls
                for (int j = 0; j < mergy2.Walls.Count; j++)
                {
                    // get the current wall
                    wall = mergy2.Walls[j];

                    // if wall is between mergy cell 1 and mergy cell 2, delete it
                    if ((wall.MergyCell1 == mergy1 && wall.MergyCell2 == mergy2) || (wall.MergyCell1 == mergy2 && wall.MergyCell2 == mergy1))
                    {
                        mergy1.Walls.Remove(wall);
                        mergy2.Walls.Remove(wall);
                    }
                    // if wall is between mergy cell 2 and other mergy cell, make mergy cell 2 side into mergy cell 1
                    else
                    {
                        // mergy 1 takes over mergy 2's walls
                        if (wall.MergyCell1 == mergy2)
                        {
                            wall.MergyCell1 = mergy1;
                        }
                        else
                        {
                            wall.MergyCell2 = mergy1;
                        }

                        // remove the wall from mergy 2
                        mergy2.Walls.Remove(wall);

                        // add the wall to mergy 1
                        mergy1.Walls.Add(wall);
                    }
                    j--;
                }
                // delete mergy 2
                _mergyMazeCells.Remove(mergy2);
            }
        }
예제 #2
0
 public MazeCell_Wall(WallConnectability wallConnectability, MazeCell cell1, MazeCell_Mergy mergyCell1, MazeCell cell2, MazeCell_Mergy mergyCell2)
 {
     WallConnectability = wallConnectability;
     Cell1      = cell1;
     MergyCell1 = mergyCell1;
     Cell2      = cell2;
     MergyCell2 = mergyCell2;
 }