Exemplo n.º 1
0
        /// <summary>
        /// build a maze2d that we can solve - break walls to get to the goal position
        /// </summary>
        /// <param name="maze"></param>
        /// <returns>maze2d</returns>
        ///
        ////IMaze maze in function
        public void generateMaze2d(Maze2d maze2d)
        {
            int      randomWallLocation;
            Position currentWall;
            Position pMaze;
            Position startPosition = new Position(0, 0, 0);

            positionsInMaze = new ArrayList();
            ArrayList neighborWallsList = new ArrayList();

            maze2d.setGoalPosition();
            Position goalPosition = maze2d.getGoalPosition();

            neighborWallsList.AddRange(maze2d.getOptionsToMove(goalPosition)); // add all the neighbors to neighborsWallList
            positionsInMaze.Add(goalPosition);                                 // add the goalPosition to List
            while (neighborWallsList.Count != 0)                               // while there is positions in neighborsWallsPOsitions
            {
                randomWallLocation = ran.Next(0, neighborWallsList.Count - 1); // random and get 1 of the walls in the List
                currentWall        = ((Position)neighborWallsList[randomWallLocation]);
                pMaze = getUnvisitedWallNeighborCell(currentWall);             // get all the UnvisitedNeighbors of the current wall
                if (pMaze != null)
                {
                    startPosition.setPosition(pMaze);                      // cheange the Position
                    maze2d.setMazeWallsCell(currentWall.X, currentWall.Y); // change the cell to 0 - we visited it
                    if (!isPositionExist(pMaze, positionsInMaze))
                    {
                        positionsInMaze.Add(pMaze); // add it to positionInMAze - all the position we have visited
                    }
                    neighborWallsList.AddRange(filterExistNeighbors(neighborWallsList, maze2d.getOptionsToMove(pMaze)));
                }
                neighborWallsList.RemoveAt(randomWallLocation); // remove the current wall from the neighborWallsList
            }
            maze2d.setStartPosition(startPosition);             // change the startPosition
        }
Exemplo n.º 2
0
        /// <summary>
        /// build the maze 2d in a simple way,
        /// always start from the left side and have options to move (up,down,right), the goalPosition is in the right side
        /// </summary>
        /// <param name="x">dim X of the maze2d</param>
        /// <param name="y">dim Y of the maze2d</param>
        /// <param name="z">dim Z of the maze2d</param>
        /// <returns>a generated 2dMaze</returns>
        public override Maze generate(int x, int y, int z)
        {
            Position currentWall;
            Position startPosition = new Position(0, 0, 0);
            Maze2d   maze2d        = new Maze2d(x, y, z);

            maze2d.setGoalPosition();
            Position goalPosition = maze2d.getGoalPosition();

            startPosition.setPosition(goalPosition);

            while (startPosition.Y != maze2d.MY * 2 - 2)
            {
                currentWall = ((Position)MoveTONextPosition(startPosition, maze2d));   //get the wall to break
                currentWall.setFatherPosition(startPosition);
                maze2d.setMazeWallsCell(currentWall.X, currentWall.Y);                 //break the wall
                startPosition.setPosition(MoveToNextCell(startPosition, currentWall)); //get the prev position and the wall and move to the next cell
            }
            maze2d.setStartPosition(startPosition);
            breakMoreWalls(maze2d);
            return(maze2d);
        }
Exemplo n.º 3
0
        /// <summary>
        /// randomic function that break more walls
        /// </summary>
        /// <param name="maze2d">maze2d of walls</param>
        public void breakMoreWalls(Maze2d maze2d)
        {
            int numOfWallsToBreak;
            int dim_x_size = maze2d.MX;
            int dim_y_size = maze2d.MY;
            int size_all   = dim_x_size * dim_y_size;

            numOfWallsToBreak = Convert.ToInt32(0.4 * size_all); // get How many walls to Break
            int    x;
            int    y;
            Random ran = new Random();

            while (numOfWallsToBreak != 0)
            {
                x = ran.Next(0, dim_x_size * 2 - 1);
                y = ran.Next(0, dim_y_size * 2 - 1);
                if (maze2d.getWallsCell(x, y) == 1)
                {
                    maze2d.setMazeWallsCell(x, y); // break the walls
                    numOfWallsToBreak--;           // size--
                }
            }
        }