Example #1
0
        private void Init(DiceRoller roller, int num_dice, int dice_size, int bonus)
        {
            if (roller == null)
            {
                throw new ArgumentNullException("roller");
            }
            if (num_dice < 1)
            {
                throw new ArgumentOutOfRangeException("num_dice", "num_dice must be >= 1");
            }
            if ((dice_size < 2) || (dice_size > 255))
            {
                throw new ArgumentOutOfRangeException("dice_size", "dice_size must be in the range [2,255]");
            }

            Value = 0;
            StringBuilder descBuilder = new StringBuilder();

            descBuilder.Append(num_dice);
            descBuilder.Append('d');
            descBuilder.Append(dice_size);
            if (bonus != 0)
            {
                descBuilder.Append((bonus < 0) ? '-' : '+');
                descBuilder.Append(Math.Abs(bonus));
            }

            descBuilder.Append(" ::= ");

            if (bonus != 0)
            {
                descBuilder.Append('(');
            }
            for (int i = 0; i < num_dice; ++i)
            {
                int roll = roller.RollSimple(dice_size);
                Value += roll;
                if (i > 0)
                {
                    descBuilder.Append('+');
                }
                descBuilder.Append(roll);
            }
            if (bonus != 0)
            {
                Value += bonus;
                descBuilder.Append(')');
                descBuilder.Append((bonus < 0) ? '-' : '+');
                descBuilder.Append(Math.Abs(bonus));
            }

            if ((bonus != 0) || (num_dice > 1))
            {
                descBuilder.Append(" = ");
                descBuilder.Append(Value);
            }

            Description = descBuilder.ToString();
        }
Example #2
0
        public DiceRoll(DiceRoller roller, string desc)
        {
            if (desc == null)
            {
                throw new ArgumentNullException("desc");
            }

            Match m = dice_regex.Match(desc);

            if ((m == null) || !m.Success)
            {
                throw new ArgumentException("Not a valid roll spec", desc);
            }

            int num_dice = 1;

            if (m.Groups["nd"].Success)
            {
                num_dice = Convert.ToInt32(m.Groups["nd"].Value);
                if (0 == num_dice)
                {
                    throw new ArgumentException("Not a valid roll spec", desc);
                }
            }

            int dice_size = 0;

            if (m.Groups["ds"].Success)
            {
                dice_size = Convert.ToInt32(m.Groups["ds"].Value);
                if (dice_size < 2)
                {
                    throw new ArgumentException("Not a valid roll spec", desc);
                }
            }
            else
            {
                throw new ArgumentException("Not a valid roll spec", desc);
            }

            int bonus = 0;

            if (m.Groups["bn"].Success)
            {
                bonus = Convert.ToInt32(m.Groups["bn"].Value);
                if (m.Groups["bs"].Success)
                {
                    if (m.Groups["bs"].Value.Equals("-"))
                    {
                        bonus = -bonus;
                    }
                }
            }

            Init(roller, num_dice, dice_size, bonus);
        }
Example #3
0
        public DiceRoll(DiceRoller roller, string desc)
        {
            if (desc == null)
            {
                throw new ArgumentNullException("desc");
            }

            Match m = dice_regex.Match(desc);
            if ((m == null) || !m.Success)
            {
                throw new ArgumentException("Not a valid roll spec", desc);
            }

            int num_dice = 1;
            if (m.Groups["nd"].Success)
            {
                num_dice = Convert.ToInt32(m.Groups["nd"].Value);
                if (0 == num_dice)
                {
                    throw new ArgumentException("Not a valid roll spec", desc);
                }
            }

            int dice_size = 0;
            if (m.Groups["ds"].Success)
            {
                dice_size = Convert.ToInt32(m.Groups["ds"].Value);
                if (dice_size < 2)
                {
                    throw new ArgumentException("Not a valid roll spec", desc);
                }
            }
            else
            {
                throw new ArgumentException("Not a valid roll spec", desc);
            }

            int bonus = 0;
            if (m.Groups["bn"].Success)
            {
                bonus = Convert.ToInt32(m.Groups["bn"].Value);
                if (m.Groups["bs"].Success)
                {
                    if (m.Groups["bs"].Value.Equals("-"))
                    {
                        bonus = -bonus;
                    }
                }
            }

            Init(roller, num_dice, dice_size, bonus);
        }
Example #4
0
 public DiceRoll(DiceRoller roller, int num_dice, int dice_size, int bonus)
 {
     Init(roller, num_dice, dice_size, bonus);
 }
Example #5
0
        private void Init(DiceRoller roller, int num_dice, int dice_size, int bonus)
        {
            if (roller == null)
            {
                throw new ArgumentNullException("roller");
            }
            if (num_dice < 1)
            {
                throw new ArgumentOutOfRangeException("num_dice", "num_dice must be >= 1");
            }
            if ((dice_size < 2) || (dice_size > 255))
            {
                throw new ArgumentOutOfRangeException("dice_size", "dice_size must be in the range [2,255]");
            }

            Value = 0;
            StringBuilder descBuilder = new StringBuilder();

            descBuilder.Append(num_dice);
            descBuilder.Append('d');
            descBuilder.Append(dice_size);
            if (bonus != 0)
            {
                descBuilder.Append((bonus < 0) ? '-' : '+');
                descBuilder.Append(Math.Abs(bonus));
            }

            descBuilder.Append(" ::= ");

            if (bonus != 0)
            {
                descBuilder.Append('(');
            }
            for (int i = 0; i < num_dice; ++i)
            {
                int roll = roller.RollSimple(dice_size);
                Value += roll;
                if (i > 0) { descBuilder.Append('+'); }
                descBuilder.Append(roll);
            }
            if (bonus != 0)
            {
                Value += bonus;
                descBuilder.Append(')');
                descBuilder.Append((bonus < 0) ? '-' : '+');
                descBuilder.Append(Math.Abs(bonus));
            }

            if ((bonus != 0) || (num_dice > 1))
            {
                descBuilder.Append(" = ");
                descBuilder.Append(Value);
            }

            Description = descBuilder.ToString();
        }
Example #6
0
 public DiceRoll(DiceRoller roller, int num_dice, int dice_size, int bonus)
 {
     Init(roller, num_dice, dice_size, bonus);
 }