private static Coord FindEmptySquare(IMapView <bool> map) { // Try random positions first for (int i = 0; i < 100; i++) { var location = map.RandomPosition(false); if (IsPointConsideredEmpty(map, location)) { return(location); } } // Start looping through every single one for (int i = 0; i < map.Width * map.Height; i++) { var location = Coord.Get(i % map.Width, i / map.Width); if (IsPointConsideredEmpty(map, location)) { return(location); } } return(null); }
private static Coord FindEmptySquare(IMapView <bool> map, IGenerator rng) { // Try random positions first for (int i = 0; i < 100; i++) { var location = map.RandomPosition(false, rng); if (IsPointConsideredEmpty(map, location)) { return(location); } } // Start looping through every single one for (int i = 0; i < map.Width * map.Height; i++) { var location = Coord.ToCoord(i, map.Width); if (IsPointConsideredEmpty(map, location)) { return(location); } } return(Coord.NONE); }
/// <summary> /// Gets a random position in the map view, whose value in map view is one of the ones /// specified in the HashSet. Random positions will continually be generated until one that /// has one of the specified values is found. /// </summary> /// <typeparam name="T">Type of items being exposed by the MapView.</typeparam> /// <param name="mapView"> /// Map view to select from -- never specified manually as this is an extension method. /// </param> /// <param name="validValues"> /// A set of values to look for in the MapView to determine whether or not a generated Coord /// is valid. /// </param> /// <param name="rng">The rng to use. Defaults to SingletonRandom.DefaultRNG.</param> /// <returns> /// A random position whose value in this MapView is equal to one of the values specified. /// </returns> public static Coord RandomPosition <T>(this IMapView <T> mapView, HashSet <T> validValues, IGenerator rng = null) => mapView.RandomPosition((c, i) => validValues.Contains(i), rng);
/// <summary> /// Gets a random position in the map view, whose value in that map view is the specified /// one. Random positions will continually be generated until one with the specified value is found. /// </summary> /// <typeparam name="T">Type of items being exposed by the MapView.</typeparam> /// <param name="mapView"> /// Map view to select from -- never specified manually as this is an extension method. /// </param> /// <param name="validValue"> /// A value to look for in the MapView to determine whether or not a generated Coord is valid. /// </param> /// <param name="rng">The rng to use. Defaults to SingletonRandom.DefaultRNG.</param> /// <returns>A random position whose value in this MapView is equal to the one specified.</returns> public static Coord RandomPosition <T>(this IMapView <T> mapView, T validValue, IGenerator rng = null) => mapView.RandomPosition((c, i) => i.Equals(validValue), rng);