/// <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 }
/// <summary> /// build the 3d maze, according to the size of the maze. it calls to maze2d that build /// for every layer maze2d. /// </summary> /// <param name="x">dim X of the maze</param> /// <param name="y">dim Y of the maze</param> /// <param name="z">dim Z of the maze</param> /// <returns>maze 3d</returns> public override Maze generate(int x, int y, int z) { MyMaze3dGenerator g = new MyMaze3dGenerator(); x = x / 2 + 1; y = y / 2 + 1; Maze3d maze3d = new Maze3d(x, y, z); for (int layer = 0; layer < maze3d.MZ; layer++) { Maze2d maze2d = new Maze2d(x, y, layer); g.generateMaze2d(maze2d); maze3d.MAZE3dArray.Add(maze2d); // maze3d.finalGoalPosition(maze2d.getStartPosition()); // maze3d.getGoalPosition().Z = maze3d.MZ - 1; if (layer == 0) { maze3d.setStartPosition(maze2d.getStartPosition()); } if (layer == maze3d.MZ - 1) { maze3d.finalGoalPosition(maze2d.getGoalPosition()); maze3d.getGoalPosition().Z = maze3d.MZ - 1; } } return(maze3d); }
/// <summary> /// print the maze2d for each layer in maze3d /// </summary> /// <param name="maze2d">return maze2d</param> public void printMaze2d(Maze2d maze2d) { string wall = "█"; string path = " "; string start = "S"; string end = "E"; for (int row = 0; row < MX * 2 - 1; row++) { for (int column = 0; column < MY * 2 - 1; column++) { if (maze2d.MAZE2d[row, column] == 1) { Console.BackgroundColor = ConsoleColor.White; //// Console.Write(" "); //// //Console.Write("{0}", wall); } else { if (row == maze2d.getStartPosition().X&& column == maze2d.getStartPosition().Y&& maze2d.MZ == 0) { Console.BackgroundColor = ConsoleColor.DarkGreen;//// Console.Write(" {0}", start); } else { if (row == maze2d.getGoalPosition().X&& column == maze2d.getGoalPosition().Y&& maze2d.MZ == MZ - 1) { Console.BackgroundColor = ConsoleColor.DarkGreen;//// Console.Write(" {0}", end); } else { Console.BackgroundColor = ConsoleColor.DarkRed;//// Console.Write(" {0}", path); } } } } Console.WriteLine(); } Console.BackgroundColor = ConsoleColor.Black; Console.WriteLine(); }
/// <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); }