private static Point FindEmptySquare(IGridView <bool> map, IEnhancedRandom rng) { // Try random positions first // TODO: Port to retries option for (var i = 0; i < 100; i++) { var location = rng.RandomPosition(map, false); if (IsPointConsideredEmpty(map, location)) { return(location); } } // Start looping through every single one for (var i = 0; i < map.Width * map.Height; i++) { var location = Point.FromIndex(i, map.Width); if (IsPointConsideredEmpty(map, location)) { return(location); } } return(Point.None); }
/// <summary> /// Rolls the dice, returning the sum. /// </summary> /// <param name="rng">The RNG to use for rolling,</param> /// <returns>The sum of the roll.</returns> public int GetResult(IEnhancedRandom rng) { _diceResults.Clear(); var sum = 0; LastMultiplicity = Multiplicity.GetResult(rng); LastSidedness = Sides.GetResult(rng); if (LastMultiplicity < 0) { throw new InvalidMultiplicityException(); } if (LastSidedness <= 0) { throw new ImpossibleDieException(); } for (var i = 0; i < LastMultiplicity; i++) { var diceVal = rng.NextInt(1, LastSidedness + 1); sum += diceVal; _diceResults.Add(diceVal); } return(sum); }
/// <summary> /// Performs a percentage check that has the specified chance to succeed. The percentage should be in range /// [0, 100] (inclusive). /// </summary> /// <param name="rng" /> /// <param name="percentage">Percentage chance (out of 100) that this check will succeed. Must be in range [0, 100].</param> /// <returns></returns> public static bool PercentageCheck(this IEnhancedRandom rng, float percentage) { if (percentage > 100 || percentage < 0) { throw new ArgumentException($"Percentage given to {nameof(PercentageCheck)} must be in range [0, 100].", nameof(percentage)); } return(rng.NextBool(percentage * 0.01f)); }
/// <summary> /// Evaluates the term (as well as the dice expression), returning the sum of the highest n /// rolls in the dice term. /// </summary> /// <param name="rng">The rng to use -- passed to the dice term being operated on.</param> /// <returns> /// The sum of the highest n rolls of the dice term being operated on, where n is equal to /// the value of the keep variable taken in the constructor. /// </returns> public int GetResult(IEnhancedRandom rng) { var keepVal = Keep.GetResult(rng); if (keepVal < 0) { throw new InvalidChooseException(); } DiceTerm.GetResult(rng); // Roll so we can check chooses if (keepVal > DiceTerm.LastMultiplicity) { throw new InvalidChooseException(); } return(DiceTerm.DiceResults.OrderByDescending(value => value).Take(keepVal).Sum()); }
private static int GetDirectionIndex(bool[] validDirections, IEnhancedRandom rng) { // 10 tries to find random ok valid var randomSuccess = false; var tempDirectionIndex = 0; for (var randomCounter = 0; randomCounter < 10; randomCounter++) { tempDirectionIndex = rng.NextInt(4); if (!validDirections[tempDirectionIndex]) { continue; } randomSuccess = true; break; } if (randomSuccess) { return(tempDirectionIndex); } // Couldn't find an active valid, so just run through each one if (validDirections[0]) { tempDirectionIndex = 0; } else if (validDirections[1]) { tempDirectionIndex = 1; } else if (validDirections[2]) { tempDirectionIndex = 2; } else { tempDirectionIndex = 3; } return(tempDirectionIndex); }
/// <summary> /// Adds its two terms together, evaluating those two terms as necessary. /// </summary> /// <param name="rng">The rng to use, passed to other terms.</param> /// <returns>The result of adding <see cref="Term1" /> and <see cref="Term2" />.</returns> public int GetResult(IEnhancedRandom rng) => Term1.GetResult(rng) + Term2.GetResult(rng);
/// <summary> /// Divides the first term by the second, evaluating those two terms as necessary. /// </summary> /// <param name="rng">The rng to used -- passed to other terms.</param> /// <returns>The result of evaluating <see cref="Term1" /> / <see cref="Term2" />.</returns> public int GetResult(IEnhancedRandom rng) => (int)Math.Round((double)Term1.GetResult(rng) / Term2.GetResult(rng));