public Map Generate(int width, int height, int changeDirectionModifier) { Map map = new Map(width, height); map.MarkCellsUnvisited(); Point currentLocation = map.PickRandomCellAndMarkItVisited(); DirectionType previousDirection = DirectionType.North; while (!map.AllCellsVisited) { DirectionPicker directionPicker = new DirectionPicker(previousDirection, changeDirectionModifier); DirectionType direction = directionPicker.GetNextDirection(); while (!map.HasAdjacentCellInDirection(currentLocation, direction) || map.AdjacentCellInDirectionIsVisited(currentLocation, direction)) { if (directionPicker.HasNextDirection) direction = directionPicker.GetNextDirection(); else { currentLocation = map.GetRandomVisitedCell(currentLocation); // Get a new previously visited location directionPicker = new DirectionPicker(previousDirection, changeDirectionModifier); // Reset the direction picker direction = directionPicker.GetNextDirection(); // Get a new direction } } currentLocation = map.CreateCorridor(currentLocation, direction); map.FlagCellAsVisited(currentLocation); previousDirection = direction; } return map; }