private Direction?GetRandomValidDirection(Map <T> map, T currentCell, T previousCell, IRandomizer randomizer) { var invalidDirections = new List <Direction>(); var squareDirections = new List <Direction>(); while (invalidDirections.Count + squareDirections.Count < GetAll.ValuesOf <Direction>().Count()) { var direction = randomizer.GetRandomEnumValue(invalidDirections.Union(squareDirections)); if (IsDirectionValid(map, currentCell, direction, previousCell)) { var nextCell = map.GetAdjacentCell(currentCell, direction); //Try to avoid creating squares, but do it if there's no other way if (nextCell.IsOpen && ((nextCell.Sides[direction.Rotate()] && currentCell.Sides[direction.Rotate()]) || (nextCell.Sides[direction.Rotate(false)] && currentCell.Sides[direction.Rotate(false)]))) { squareDirections.Add(direction); } else { return(direction); } } else { invalidDirections.Add(direction); } } return(squareDirections.Any() ? randomizer.GetRandomItem(squareDirections) : (Direction?)null); }
private Direction?GetRandomValidDirection(Map <T> map, T cell, ICollection <T> visitedCells, double randomness, Direction?previousDirection, IRandomizer randomizer) { //Randomness determines how often the direction of a corridor changes if (previousDirection.HasValue && randomness < 1 && randomizer.GetRandomDouble() > randomness && IsDirectionValid(map, cell, previousDirection.Value, visitedCells)) { return(previousDirection); } var invalidDirections = new List <Direction>(); while (invalidDirections.Count < GetAll.ValuesOf <Direction>().Count()) { var direction = randomizer.GetRandomEnumValue(invalidDirections); if (IsDirectionValid(map, cell, direction, visitedCells)) { return(direction); } invalidDirections.Add(direction); } return(null); }