Example #1
0
        /// <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);
        }
Example #2
0
        /// <summary>
        /// Rolls the expression using the RNG given, returning the result.
        /// </summary>
        /// <param name="rng">The RNG to use. If null is specified, the default RNG is used.</param>
        /// <returns>The result obtained by rolling the dice expression.</returns>
        public int Roll(IGenerator rng = null)
        {
            if (rng == null)
            {
                rng = SingletonRandom.DefaultRNG;
            }

            return(termToEvaluate.GetResult(rng));
        }
Example #3
0
        /// <summary>
        /// Rolls the expression using the RNG given, returning the result.
        /// </summary>
        /// <param name="rng">The RNG to use. If null is specified, the default RNG is used.</param>
        /// <returns>The result obtained by rolling the dice expression.</returns>
        public int Roll(IRandom rng = null)
        {
            if (rng == null)
            {
                rng = SingletonRandom.Instance;
            }

            return(termToEvaluate.GetResult(rng));
        }
Example #4
0
        /// <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(IGenerator rng)
        {
            int keepVal = keep.GetResult(rng);

            if (keepVal < 0)
            {
                throw new Exceptions.InvalidChooseException();
            }

            diceTerm.GetResult(rng); // Roll so we can check chooses

            if (keepVal > diceTerm.LastMultiplicity)
            {
                throw new Exceptions.InvalidChooseException();
            }

            return(diceTerm.DiceResults.OrderByDescending(value => value).Take(keepVal).Sum());
        }
Example #5
0
        /// <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());
        }
Example #6
0
 /// <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);
Example #7
0
 /// <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));