Example #1
0
        /// <summary>
        /// Creates a new instance of DieRoll from its string representation.
        /// </summary>
        /// <param name="text">A string containing the representation for the roll.</param>
        /// <param name="start">The starting position inside text to match the string representation for the roll.</param>
        /// <returns>A new instance of DieRoll matching the string representation.</returns>
        public static DieRoll FromString(string text, int start)
        {
            DieRoll roll = null;

            if (text != null)
            {
                try
                {
                    Regex regRoll = new Regex(SingleDieRollRegexString + ExtraDieRollRegexString + ModDieRollRegexString);

                    Match match = regRoll.Match(text, start);

                    if (match.Success)
                    {
                        roll       = new DieRoll();
                        roll.Count = int.Parse(match.Groups["count"].Value);
                        roll.Die   = int.Parse(match.Groups["die"].Value);

                        if (roll.Die == 0)
                        {
                            throw new FormatException("Invalid Die Roll");
                        }

                        if (match.Groups["extra"].Success)
                        {
                            roll.ExtraRolls = new List <DieRollElement>();

                            Regex regExtra = new Regex(SingleDieRollRegexString);

                            foreach (Match m in regExtra.Matches(match.Groups["extra"].Value))
                            {
                                DieRollElement element = new DieRollElement(int.Parse(m.Groups["extraCount"].Value), int.Parse(m.Groups["extraDie"].Value));
                                if (element.Die == 0)
                                {
                                    throw new FormatException("Invalid Die Roll");
                                }

                                roll.ExtraRolls.Add(element);
                            }
                        }

                        if (match.Groups["mod"].Success)
                        {
                            roll.Mod = int.Parse(match.Groups["mod"].Value);
                        }
                    }
                }
                catch (FormatException)
                {
                    roll = null;
                }
                catch (OverflowException)
                {
                    roll = null;
                }
            }

            return(roll);
        }
        /// <summary>
        /// Compares one object with the DieRollElement.
        /// </summary>
        /// <param name="obj">The object to be compared.</param>
        /// <returns>The object is the same that the DieRollElement.</returns>
        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(DieRollElement))
            {
                return(false);
            }

            DieRollElement element = (DieRollElement)obj;

            return(this.Count == element.Count && this.Die == element.Die);
        }
Example #3
0
        /// <summary>
        /// Creates a new instance of DieRoll from its string representation.
        /// </summary>
        /// <param name="text">A string containing the representation for the roll.</param>
        /// <param name="start">The starting position inside text to match the string representation for the roll.</param>
        /// <returns>A new instance of DieRoll matching the string representation.</returns>
        public static DieRoll FromString(string text, int start)
        {
            DieRoll roll = null;

            if (text != null)
            {
                try
                {
                    Regex regRoll = new Regex(SingleDieRollRegexString + ExtraDieRollRegexString + ModDieRollRegexString);

                    Match match = regRoll.Match(text, start);

                    if (match.Success)
                    {
                        roll = new DieRoll();
                        roll.Count = int.Parse(match.Groups["count"].Value);
                        roll.Die = int.Parse(match.Groups["die"].Value);

                        if (roll.Die == 0)
                        {
                            throw new FormatException("Invalid Die Roll");
                        }

                        if (match.Groups["extra"].Success)
                        {
                            roll.ExtraRolls = new List<DieRollElement>();

                            Regex regExtra = new Regex(SingleDieRollRegexString);

                            foreach (Match m in regExtra.Matches(match.Groups["extra"].Value))
                            {
                                DieRollElement element = new DieRollElement(int.Parse(m.Groups["extraCount"].Value), int.Parse(m.Groups["extraDie"].Value));
                                if (element.Die == 0)
                                {
                                    throw new FormatException("Invalid Die Roll");
                                }

                                roll.ExtraRolls.Add(element);
                            }
                        }

                        if (match.Groups["mod"].Success)
                        {
                            roll.Mod = int.Parse(match.Groups["mod"].Value);
                        }
                    }
                }
                catch (FormatException)
                {
                    roll = null;
                }
                catch (OverflowException)
                {
                    roll = null;
                }
            }

            return roll;
        }