private void CreateDenseMaze(CellDungeon dungeon) { var currentLocation = dungeon.PickRandomCellAndSetItAsVisited(); var previousDirection = Direction.North; while (!dungeon.AllCellsAreVisited) { var directionPicker = new DirectionPicker(previousDirection, ChangeDirectionModifier); var direction = directionPicker.PickRandomDirection(); while (!dungeon.HasAdjacentCell(currentLocation, direction) || dungeon.AdjacentCellIsVisited(currentLocation, direction)) { if (directionPicker.HasDirectionToPick) { direction = directionPicker.PickRandomDirection(); } else { currentLocation = dungeon.GetRandomVisitedCell(currentLocation); // Get a new previously visited location directionPicker = new DirectionPicker(previousDirection, ChangeDirectionModifier); // Reset the direction picker direction = directionPicker.PickRandomDirection(); // Get a new direction } } currentLocation = dungeon.CreateCorridor(currentLocation, direction); dungeon.SetCellAsVisited(currentLocation); previousDirection = direction; } }
private void RemoveDeadEnds(CellDungeon dungeon) { foreach (var deadEndLocation in dungeon.DeadEndCellLocations) { if (ShouldRemoveDeadend()) { var currentLocation = deadEndLocation; do { // Initialize the direction picker not to select the dead-end corridor direction var directionPicker = new DirectionPicker(dungeon[currentLocation].CalculateDeadEndCorridorDirection(), 100); var direction = directionPicker.PickRandomDirection(); while (!dungeon.HasAdjacentCell(currentLocation, direction)) { if (directionPicker.HasDirectionToPick) { direction = directionPicker.PickRandomDirection(); } else { throw new InvalidOperationException("This should not happen"); } } // Create a corridor in the selected direction currentLocation = dungeon.CreateCorridor(currentLocation, direction); } while (dungeon[currentLocation].IsDeadEnd); // Stop when you intersect an existing corridor. } } }