/// <summary> /// Creates all the outer walls of maze, taking the exit into account. /// </summary> private void CreateOuterWalls() { // Creates left outer walls. indexXInt = 0; indexYInt = 0; // While not yet through entire y-axis. while (indexYInt < mazeSizeInt) { // If not on same spot as exit. if (indexYInt != mazeExitYInt || indexXInt != mazeExitXInt) { mazeLayout[indexYInt, indexXInt] = new MazeTile('#'); } indexYInt++; } // Creates Top outer walls. indexXInt = 0; indexYInt = 0; // While not yet through entire y-axis. while (indexXInt < mazeSizeInt) { // If not on same spot as exit. if (indexYInt != mazeExitYInt || indexXInt != mazeExitXInt) { mazeLayout[indexYInt, indexXInt] = new MazeTile('#'); } indexXInt++; } // Creates Right outer walls. indexXInt = mazeSizeInt - 1; indexYInt = 0; // While not yet through entire y-axis. while (indexYInt < mazeSizeInt) { // If not on same spot as exit. if (indexYInt != mazeExitYInt || indexXInt != mazeExitXInt) { mazeLayout[indexYInt, indexXInt] = new MazeTile('#'); } indexYInt++; } // Creates Bottom outer walls. indexXInt = 0; indexYInt = mazeSizeInt - 1; // While not yet through entire y-axis. while (indexXInt < mazeSizeInt) { // If not on same spot as exit. if (indexYInt != mazeExitYInt || indexXInt != mazeExitXInt) { mazeLayout[indexYInt, indexXInt] = new MazeTile('#'); } indexXInt++; } }
/// <summary> /// Determines initial location for maze generation. This tile becomes the maze exit. /// </summary> private void DetermineMazeStart() { // Creates temp array of size equal to mazeSizeInt. mazeLayout = new MazeTile[mazeSizeInt, mazeSizeInt]; /* Pulls a random number between 0 and 3. * Result 0 sets exit to Left outer wall. * Result 1 sets exit to Top outer wall. * Result 2 sets exit to Right outer wall. * Result 3 sets exit to Bottom outer wall. */ randomInt = random.Next(4); savedRandomInt = randomInt; // Stores value so program knows which wall exit is on. Prevents // Accidentally checking for null tiles outside of array bounds. randomInt = random.Next(1, mazeSizeInt - 1); // Set to left wall. if (savedRandomInt == 0) { mazeLayout[randomInt, 0] = new MazeTile('!'); mazeExitXInt = 0; mazeExitYInt = randomInt; } else { // Set to top wall. if (savedRandomInt == 1) { mazeLayout[0, randomInt] = new MazeTile('!'); mazeExitXInt = randomInt; mazeExitYInt = 0; } else { // Set to right wall. if (savedRandomInt == 2) { mazeLayout[randomInt, mazeSizeInt - 1] = new MazeTile('!'); mazeExitXInt = mazeSizeInt - 1; mazeExitYInt = randomInt; } else { // Set to bottom wall. mazeLayout[mazeSizeInt - 1, randomInt] = new MazeTile('!'); mazeExitXInt = randomInt; mazeExitYInt = mazeSizeInt - 1; } } } }
/// <summary> /// Checks adj tiles of right of current and generate appropriate wall tiles. /// Also has a 2% chance per check of generating a random wall. /// </summary> /// <param name="currentY">Current Y location.</param> /// <param name="currentX">Current X location.</param> private void CheckTileToRight(int currentY, int currentX) { // Checks if tile to right is null. if so, check tiles adj to it. if (mazeLayout[currentY, currentX + 1] == null) { // First off, 2% chance that it is simply turned into a wall. If so, removes need for checking adj tiles. randomInt = random.Next(100); if (randomInt < 2) { mazeLayout[currentY, currentX + 1] = new MazeTile('#'); } else { // Checks tiles directly adjecent to tile right of current. // If any adj space is a path, make tile right of current into a wall. // Check right adj. if (mazeLayout[currentY, currentX + 2] != null && mazeLayout[currentY, currentX + 2].IDChar == '.') { mazeLayout[currentY, currentX + 1] = new MazeTile('#'); } else { // Check below adj. if (mazeLayout[currentY + 1, currentX + 1] != null && mazeLayout[currentY + 1, currentX + 1].IDChar == '.') { mazeLayout[currentY, currentX + 1] = new MazeTile('#'); } else { // Check above adj. if (mazeLayout[currentY - 1, currentX + 1] != null && mazeLayout[currentY - 1, currentX + 1].IDChar == '.') { mazeLayout[currentY, currentX + 1] = new MazeTile('#'); } } } } } }
/// <summary> /// Updates specific maze tile for player interaction. /// </summary> /// <param name="currentY">Player's current Y coordinate.</param> /// <param name="currentX">Player's current X coordinate.</param> public void UpdateMazeTile(int currentY, int currentX) { int directionCounter = 0; // Holds how many nearby tiles are non-accessable. // Check tile to the right. If wall or dead end. if (mazeLayout[currentY, currentX + 1].IDString == MazeTile.ID_Wall || mazeLayout[currentY, currentX + 1].IDString == MazeTile.ID_DeadEnd) { directionCounter++; } // Check tile below. If wall or dead end. if (mazeLayout[currentY + 1, currentX].IDString == MazeTile.ID_Wall || mazeLayout[currentY + 1, currentX].IDString == MazeTile.ID_DeadEnd) { directionCounter++; } // Check tile to the left. If wall or dead end. if (mazeLayout[currentY, currentX - 1].IDString == MazeTile.ID_Wall || mazeLayout[currentY, currentX - 1].IDString == MazeTile.ID_DeadEnd) { directionCounter++; } // Check tile to the above. If wall or dead end. if (mazeLayout[currentY - 1, currentX].IDString == MazeTile.ID_Wall || mazeLayout[currentY - 1, currentX].IDString == MazeTile.ID_DeadEnd) { directionCounter++; } // If counter is 3 or higher, then space is a dead end. if (directionCounter >= 3) { mazeLayout[currentY, currentX] = new MazeTile('O'); } else { mazeLayout[currentY, currentX] = new MazeTile('X'); } }
/// <summary> /// Reads in predefined maze and populates tiles accordingly. /// </summary> public void ReadMaze() { indexXInt = 0; indexYInt = 0; mazeSizeInt = preDefinedMaze.GetLength(0); mazeLayout = new MazeTile[mazeSizeInt, mazeSizeInt]; while (indexYInt < mazeSizeInt) { // If not yet at end of horizontal, add 1 to X. Otherwise reset x and add 1 to Y. if (indexXInt < mazeSizeInt) { // Creates a full MazeTile spot based on the given ID of predefined maze spot. mazeLayout[indexYInt, indexXInt] = new MazeTile(preDefinedMaze[indexYInt, indexXInt]); indexXInt++; } else { indexXInt = 0; indexYInt++; } } Settings.PremadeMazeStartingLocation(); CreatePlaceHolderMaze(); }
/// <summary> /// Accounts for any possible skipped tiles and sets them to be walls. /// </summary> private void VoidOutExtraTiles() { indexXInt = 0; indexYInt = 0; while (indexYInt < mazeSizeInt) { while (indexXInt < mazeSizeInt) { if (mazeLayout[indexYInt, indexXInt] == null) { mazeLayout[indexYInt, indexXInt] = new MazeTile('#'); } indexXInt++; } indexXInt = 0; indexYInt++; } }
/// <summary> /// Sets current tile to a valid path. Then checks which directions (from current tile) still need generation. /// Also has a 2% chance of creating a new wall tile. /// </summary> /// <param name="currentY"></param> /// <param name="currentX"></param> /// <param name="currentDistance">Current distance from maze start. Passing due to nature of recursion.</param> /// <param name="previousDirection"></param> private void GenerateTile(int currentY, int currentX, int previousDirection, int currentDistance) { // Specify current tile as a valid path. mazeLayout[currentY, currentX] = new MazeTile('.'); // Check how many directions are null (uncreated tiles). // Initialize to first enter while loop. int generateMazeInt = 4; // Keeps recursive method from backtracking out of tile until all directions are explored. while (generateMazeInt > 0) { CheckNearbyTiles(currentY, currentX); // Checks on each cycle for exit of loop. generateMazeInt = 4; int maxXInt = 2; int minXInt = 0; int maxYInt = 2; int minYInt = 0; // Check left. if (mazeLayout[currentY, currentX - 1] != null) { generateMazeInt--; minXInt++; } // Check down. if (mazeLayout[currentY + 1, currentX] != null) { generateMazeInt--; maxYInt--; } // Check right. if (mazeLayout[currentY, currentX + 1] != null) { generateMazeInt--; maxXInt--; } // Check up. if (mazeLayout[currentY - 1, currentX] != null) { generateMazeInt--; minYInt++; } // If any of the spots are null (uncreated), then choose a new direction. if (generateMazeInt > 0) { ChooseRandomDirection(currentY, currentX, maxYInt, maxXInt, minYInt, minXInt, currentDistance); } } }