public void DiceTerm_CalculateResultsExplodingAndChooseTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(10, 12, choose: 8, exploding: 9);

            // run test
            IReadOnlyList <TermResult> results = term.CalculateResults(new RandomDieRoller());

            // validate results
            Assert.IsNotNull(results);
            int included = 0;

            foreach (TermResult r in results)
            {
                Assert.IsNotNull(r);
                Assert.AreEqual(1, r.Scalar);
                AssertHelpers.IsWithinRangeInclusive(1, 12, r.Value);
                Assert.AreEqual("DiceTerm.d12", r.Type);
                if (r.AppliesToResultCalculation)
                {
                    included++;
                }
            }
            Assert.AreEqual(8, included);
        }
示例#2
0
        public void Constructor_ChooseMoreDiceThanRolled_ThrowsInvalidChooseException()
        {
            const int multiplicity = 1;
            const int choose       = multiplicity + 1;

            var chooseMoreDiceThanRolled = new DiceTerm(multiplicity, 6, choose, 1);
        }
        /// <summary>
        /// Handles the dice operator and its sub-expressions, and returns the result of the
        /// dice rolls in the results list and token value.
        /// </summary>
        /// <param name="results">List of term results</param>
        /// <param name="tokens">String expression to parse</param>
        /// <param name="op">current operator</param>
        /// <param name="dieRoller">Die roller to use</param>
        private void HandleDieOperator(List <TermResult> results, List <string> tokens, string op, IDieRoller dieRoller)
        {
            if (tokens.IndexOf("f") >= 0)
            {
                throw new FormatException("Fudge dice and regular dice cannot be used in the same expression");
            }

            // find the previous and next numbers in the token list
            int opPosition = tokens.IndexOf(op);
            int sides = 0, length = 0;

            int numDice = int.Parse(tokens[opPosition - 1]);

            // allow default value for dice (if not digit is specified)
            if (opPosition + 1 < tokens.Count && char.IsDigit(tokens[opPosition + 1], 0))
            {
                sides   = int.Parse(tokens[opPosition + 1]);
                length += 2;
            }
            else
            {
                sides = this.config.DefaultDieSides;
                length++;
            }

            // look-ahead to find other dice operators (like the choose-keep/drop operators)
            int?choose  = this.ChooseLookAhead(tokens, opPosition, numDice, ref length);
            int?explode = this.ExplodeLookAhead(tokens, opPosition, sides, ref length);

            // create a dice term based on the values
            DiceTerm term = new DiceTerm(numDice, sides, 1, choose, explode);

            // then evaluate the dice term to roll dice and get the result
            this.EvaluateDiceTerm(results, tokens, dieRoller, opPosition, length, term);
        }
示例#4
0
        public void Constructor_ChooseLessThanAnyDice_ThrowsInvalidChooseException()
        {
            const int multiplicity = 1;
            const int choose       = -1;

            var chooseLessThanAnyDice = new DiceTerm(multiplicity, 6, choose, 1);
        }
        public void DiceTerm_CalculateResultsExplodingLowerThanMaxTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(10, 12, exploding: 9);

            // run test
            IReadOnlyList <TermResult> results = term.CalculateResults(new RandomDieRoller());

            // validate results
            Assert.IsNotNull(results);
            int count = 10;

            foreach (TermResult r in results)
            {
                Assert.IsNotNull(r);
                Assert.AreEqual(1, r.Scalar);
                AssertHelpers.IsWithinRangeInclusive(1, 12, r.Value);
                if (r.Value >= 9)
                {
                    count++;
                }
                Assert.AreEqual("DiceTerm.d12", r.Type);
            }
            Assert.AreEqual(count, results.Count);
        }
        public void DiceTerm_CalculateResultsChooseDiceTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(5, 6, choose: 3);

            // run test
            IReadOnlyList <TermResult> results = term.CalculateResults(constantRoller);

            // validate results
            Assert.IsNotNull(results);
            Assert.AreEqual(5, results.Count);
            int included = 0;

            foreach (TermResult r in results)
            {
                Assert.IsNotNull(r);
                Assert.AreEqual(1, r.Scalar);
                Assert.AreEqual(1, r.Value);
                Assert.AreEqual("DiceTerm.d6", r.Type);
                if (r.AppliesToResultCalculation)
                {
                    included++;
                }
            }
            Assert.AreEqual(3, included);
        }
示例#7
0
        public void ToString_ChooseThree_RepresentationIsCorrect()
        {
            var diceTerm = new DiceTerm(4, 6, 3, 1);

            string stringRepresentation = diceTerm.ToString();

            Assert.AreEqual("4d6k3", stringRepresentation);
        }
示例#8
0
        public void ToString_ScalarOfOne_RepresentationIsCorrect()
        {
            const int multiplicity = 3;
            const int sides        = 6;
            var       diceTerm     = new DiceTerm(multiplicity, sides, 1);

            string stringRepresentation = diceTerm.ToString();

            Assert.AreEqual("3d6", stringRepresentation);
        }
        public void DiceTerm_CalculateResultsErrorMaxRerollsTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(10, 12, exploding: 9);

            // run test
            Assert.ThrowsException <OverflowException>(() => term.CalculateResults(new ConstantDieRoller(10)));

            // validate results
        }
        public void DiceTerm_CalculateResultsNullDieRollerTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(1, 10);

            // run test
            Assert.ThrowsException <ArgumentNullException>(() => term.CalculateResults(null));

            // validate results
        }
示例#11
0
        public void GetResults_NoChooseSpecified_ReturnsAllDice()
        {
            const int multiplicity = 6;
            const int sides        = 6;
            var       diceTerm     = new DiceTerm(multiplicity, sides, 1);
            var       maxRandom    = new MaxRandom();

            IEnumerable <TermResult> results = diceTerm.GetResults(maxRandom);

            Assert.AreEqual(multiplicity, results.Count());
        }
示例#12
0
        public void Constructor_GoodValues_SetsAllValues()
        {
            const int multiplicity = 3;
            const int sides        = 4;
            const int scalar       = 5;

            var diceTerm = new DiceTerm(multiplicity, sides, scalar);

            Assert.AreEqual(multiplicity, diceTerm.Multiplicity);
            Assert.AreEqual(sides, diceTerm.Sides);
            Assert.AreEqual(scalar, diceTerm.Scalar);
        }
        public void DiceTerm_ConstructorTest()
        {
            // setup test

            // run test
            IExpressionTerm term = new DiceTerm(1, 20);

            // validate results
            Assert.IsNotNull(term);
            Assert.IsInstanceOfType(term, typeof(IExpressionTerm));
            Assert.IsInstanceOfType(term, typeof(DiceTerm));
        }
示例#14
0
        public void StringRepresentationIsCorrectForScalarOfTwo()
        {
            const int multiplicity = 3;
            const int sides        = 6;
            const int scalar       = 2;

            var diceTerm = new DiceTerm(multiplicity, sides, scalar);

            string stringRepresentation = diceTerm.ToString();

            Assert.AreEqual("2*3d6", stringRepresentation);
        }
        public void DiceTerm_ToStringExplodingLowerThanMaxTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(10, 12, exploding: 9);

            // run test
            string result = term.ToString();

            // validate results
            Assert.IsFalse(string.IsNullOrEmpty(result));
            Assert.AreEqual("10d12!9", result);
        }
        public void DiceTerm_ToStringAllTermsTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(4, 6, 10, 3, 6);

            // run test
            string result = term.ToString();

            // validate results
            Assert.IsFalse(string.IsNullOrEmpty(result));
            Assert.AreEqual("4d6k3!6x10", result);
        }
        public void DiceTerm_ToStringMultiplierTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(2, 8, 10);

            // run test
            string result = term.ToString();

            // validate results
            Assert.IsFalse(string.IsNullOrEmpty(result));
            Assert.AreEqual("2d8x10", result);
        }
        public void DiceTerm_ToStringExplodingNoneDiceTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(5, 6, exploding: 6);

            // run test
            string result = term.ToString();

            // validate results
            Assert.IsFalse(string.IsNullOrEmpty(result));
            Assert.AreEqual("5d6!6", result);
        }
        public void DiceTerm_ToStringChooseTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(5, 6, choose: 3);

            // run test
            string result = term.ToString();

            // validate results
            Assert.IsFalse(string.IsNullOrEmpty(result));
            Assert.AreEqual("5d6k3", result);
        }
示例#20
0
        public void ReturnsMultiplicityResultsWhenNoChooseSpecified()
        {
            const int multiplicity = 6;
            const int sides        = 6;

            var diceTerm = new DiceTerm(multiplicity, sides, 1);

            var dieRoller = Substitute.For <IDieRoller>();

            dieRoller.RollDie(sides).Returns(sides);

            IEnumerable <TermResult> results = diceTerm.GetResults(dieRoller);

            Assert.AreEqual(multiplicity, results.Count());
        }
        public void DiceTerm_CalculateResultsTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(1, 20);

            // run test
            IReadOnlyList <TermResult> results = term.CalculateResults(dieRoller);

            // validate results
            Assert.IsNotNull(results);
            Assert.AreEqual(1, results.Count);
            TermResult r = results.FirstOrDefault();

            Assert.IsNotNull(r);
            Assert.AreEqual(1, r.Scalar);
            AssertHelpers.IsWithinRangeInclusive(1, 20, r.Value);
            Assert.AreEqual("DiceTerm.d20", r.Type);
        }
        public void DiceTerm_CalculateResultsMultiplierDiceTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(2, 8, 10);

            // run test
            IReadOnlyList <TermResult> results = term.CalculateResults(constantRoller);

            // validate results
            Assert.IsNotNull(results);
            Assert.AreEqual(2, results.Count);
            foreach (TermResult r in results)
            {
                Assert.IsNotNull(r);
                Assert.AreEqual(10, r.Scalar);
                Assert.AreEqual(1, r.Value);
                Assert.AreEqual("DiceTerm.d8", r.Type);
            }
        }
        public void DiceTerm_CalculateResultsExplodingNoneDiceTest()
        {
            // setup test
            IExpressionTerm term = new DiceTerm(5, 6, exploding: 6);

            // run test
            IReadOnlyList <TermResult> results = term.CalculateResults(constantRoller);

            // validate results
            Assert.IsNotNull(results);
            Assert.AreEqual(5, results.Count);
            foreach (TermResult r in results)
            {
                Assert.IsNotNull(r);
                Assert.AreEqual(1, r.Scalar);
                Assert.AreEqual(1, r.Value);
                Assert.AreEqual("DiceTerm.d6", r.Type);
            }
        }
示例#24
0
        public void Constructor_InvalidNumberOfSides_ThrowsImpossibleDieException()
        {
            const int invalidNumberOfSides = -1;

            var dieWithInvalidSides = new DiceTerm(1, invalidNumberOfSides, 1);
        }
示例#25
0
        public void Constructor_LessThanZeroDice_ThrowsInvalidMultiplicityException()
        {
            const int invalidMultiplicity = -1;

            var lessThanNoDice = new DiceTerm(invalidMultiplicity, 6, 1);
        }