public void DiceChainShouldBeValidlySerialized()
 {
     deserializedD2d20mr3t6p40 = (DiceChain)diceChainSerializer.ReadObject(serializedD2d20mr3t6p40);
     serializedD2d20mr3t6p40.Close();
     Assert.AreEqual(d2d20mr3t6p40.Maximum, deserializedD2d20mr3t6p40.Maximum, "Serialized die maximum is wrong");
     Assert.AreEqual(d2d20mr3t6p40.Minimum, deserializedD2d20mr3t6p40.Minimum, "Serialized die minimum is wrong");
     Assert.AreEqual(d2d20mr3t6p40.ToString(), deserializedD2d20mr3t6p40.ToString(), "Serialized die repr is wrong");
 }
Esempio n. 2
0
        /// <summary> Converts the string representation of a dice chain to an actual dice chain. </summary>
        /// <param name="representation">A string that contains a dice chain to parse.</param>
        /// <returns> Parsed dice chain or null, if string does not represent a known die type. </returns>
        /// <exception cref="InvalidOperationException">Can't parse node.</exception>
        public static DiceChain Parse(string representation)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(representation));

            string cleaned = representation.Trim();
            int    opIndex = cleaned.IndexOfAny(diceOps);

            if (opIndex < 0)
            {
                var singleNode = ToNode(cleaned);
                if (singleNode == null)
                {
                    throw new InvalidOperationException("Can't parse node " + cleaned);
                }
                return(new DiceChain(singleNode));
            }

            string firstPart = cleaned.Substring(0, opIndex);

            firstPart = firstPart.Remove("(").Remove(")");
            var firstNode = ToNode(firstPart);

            if (firstNode == null)
            {
                throw new InvalidOperationException("Can't parse node " + firstPart);
            }

            var result = new DiceChain(firstNode);

            while (opIndex > -1)
            {
                // Determine whether there is a next dice chain node or not:
                int    nextOpIndex        = cleaned.IndexOfAny(diceOps, opIndex + 1);
                int    nextNodePartLength = nextOpIndex - opIndex - 1;
                string nodePart           = (nextNodePartLength > 0) // If there is a next dice chain node, extract text before it.
                    ? cleaned.Substring(opIndex + 1, nextNodePartLength)
                    : cleaned.Substring(opIndex + 1);                // Otherwise just get all remaining text.
                nodePart = nodePart.Remove("(").Remove(")");
                var node = ToNode(nodePart);
                if (node == null)
                {
                    throw new InvalidOperationException("Can't parse node " + nodePart);
                }

                result.Append(DiceOperationTools.FromNodeString(cleaned, opIndex), node);
                opIndex = nextOpIndex;
            }

            return(result);
        }
 public void CreatedDiceChainD2D20Mr3T6P40()
 {
     d2d20mr3t6p40 = Dice.Take(Dice.D2).D(20).MultiplyTake(1).I(3, 6).Plus(40);
 }
 public void CreatedD2D20()
 {
     d2d20mr3t6p40 = Dice.Take(Dice.D2).D(20).MultiplyTake(Dice.D4).I(3, 6).Plus(40);
 }
Esempio n. 5
0
        public void EntryLevel()
        {
            // Roll a standard cubic die one time:
            var d6 = new Die(6);
            var roll = d6.Roll();

            // Roll an interval die from 10 to 20 four times:
            var interval10t20 = new Die(10, 20);
            roll = interval10t20.Roll(4).Sum();

            // There is also a fixed die, which always returns the same value.
            // It's useful when something expects IDie as value provider and you want to pass a constant value:
            var fixed50 = new FixedDie(50);

            // Roll two d10's:
            var d2d10 =new SeveralDice(Dice.D10, 2);
            roll = d2d10.Roll(); // Compared to simple Dice.D10.Roll(2), SeveralDice provides statistics and serialization
            Console.WriteLine("Possible roll value range: " + d2d10.Minimum + " to " + d2d10.Maximum);

            // Roll a d4d10 / d2 * 3:
            var d4d10divD2mul3 = Dice.Take(Dice.D4).D(10).Divide(Dice.D2).Multiply(3);  // This is a dice chain.
            // Math operations in dice chains are always executed from left to right, so this chain translates to:
            // take a 1-4 of d10 dice and divide their roll value by 2 with 50 % chance, then multiply resulting roll value by 3.
            roll = d4d10divD2mul3.Roll();
            // Dice chains also provide statistics and serialization.

            // This chain was created via fluent interface, but it is also possible to create such chain in a more explicit way:
            d4d10divD2mul3 = new DiceChain(
                new SeveralDice(new Die(10), new Die(4)))               // d4d10
                .Append(DiceOperation.Divide, new SeveralDice(2))       // / 2
                .Append(DiceOperation.Multiply, new SeveralDice(3));    // * 3

            // Oh, and where is a random number generator? Built-in dice get it from thread-local `Dice.Random`.
        }
 public void CreatedD10D100Em50P10()
 {
     d10d100em50p10 = Dice.Take(Dice.D10).D(100).EachMinus(50).Plus(10);
 }
Esempio n. 7
0
 public void D4D6MinusInterval2to5()
 {
     cd4d6mr2t5 = Dice.Take(Dice.D4).D(6).Minus(new Die(2, 5));
 }
Esempio n. 8
0
 public void D3D8WithPlus3ToEach()
 {
     c3d8ep3 = Dice.Take(3).D(8).EachPlus(3);
 }
Esempio n. 9
0
 public void D2D10DivideByD2MultiplyBy2()
 {
     d2d10div2mul2 = Dice.Take(Dice.D2).D(10).Divide(Dice.D2).Multiply(2);
 }
Esempio n. 10
0
 public void DieWith6FacesShowingADifferentIntegralNumberFrom1To6()
 {
     var c2d6 = new DiceChain(new SeveralDice(new Die(6), 2));
     c2d6p3 = c2d6.Append(DiceOperation.Plus, new SeveralDice(3));
 }