Пример #1
0
        /// <summary>
        /// Copy the values of a DieRoll in this object.
        /// </summary>
        /// <param name="roll">The original DieRoll.</param>
        private void CopyFrom(DieRoll roll)
        {
            if (roll == null)
            {
                this.Count = 0;
                this.Die   = 0;
                this.Mod   = 0;
            }
            else
            {
                this.Count = roll.Count;
                this.Die   = roll.Die;
                this.Mod   = roll.Mod;

                if (roll.ExtraRolls != null)
                {
                    this.ExtraRolls = new List <DieRollElement>();

                    foreach (DieRollElement element in roll.ExtraRolls)
                    {
                        this.ExtraRolls.Add(new DieRollElement(element.Count, element.Die));
                    }
                }
                else
                {
                    this.ExtraRolls = null;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Determina si el objeto especificado es igual al objeto actual.
        /// </summary>
        /// <param name="obj">El objeto a comparar.</param>
        /// <returns>True si los objetos son iguales.</returns>
        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(DieRoll))
            {
                return(false);
            }

            DieRoll roll = (DieRoll)obj;

            if (roll.ExtraRolls == null ^ this.ExtraRolls == null)
            {
                return(false);
            }

            if (this.ExtraRolls != null)
            {
                if (roll.ExtraRolls.Count != this.ExtraRolls.Count)
                {
                    return(false);
                }

                for (int i = 0; i < this.ExtraRolls.Count; i++)
                {
                    if (roll.ExtraRolls[i] != this.ExtraRolls[i])
                    {
                        return(false);
                    }
                }
            }

            return(roll.Count == this.Count && roll.Die == this.Die && roll.Mod == this.Mod);
        }
Пример #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);
        }
Пример #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DieRoll"/> class.
 /// </summary>
 /// <param name="roll">The DieRoll to copy from.</param>
 public DieRoll(DieRoll roll)
 {
     this.CopyFrom(roll);
 }
Пример #5
0
        /// <summary>
        /// Copy the values of a DieRoll in this object.
        /// </summary>
        /// <param name="roll">The original DieRoll.</param>
        private void CopyFrom(DieRoll roll)
        {
            if (roll == null)
            {
                this.Count = 0;
                this.Die = 0;
                this.Mod = 0;
            }
            else
            {
                this.Count = roll.Count;
                this.Die = roll.Die;
                this.Mod = roll.Mod;

                if (roll.ExtraRolls != null)
                {
                    this.ExtraRolls = new List<DieRollElement>();

                    foreach (DieRollElement element in roll.ExtraRolls)
                    {
                        this.ExtraRolls.Add(new DieRollElement(element.Count, element.Die));
                    }
                }
                else
                {
                    this.ExtraRolls = null;
                }
            }
        }
Пример #6
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;
        }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DieRoll"/> class.
 /// </summary>
 /// <param name="roll">The DieRoll to copy from.</param>
 public DieRoll(DieRoll roll)
 {
     this.CopyFrom(roll);
 }
Пример #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DieRollElement"/> class.
 /// </summary>
 /// <param name="roll">A DieRoll object</param>
 public DieRollElement(DieRoll roll)
 {
     this.Count = roll.Count;
     this.Die = roll.Die;
 }
Пример #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DieRollElement"/> class.
 /// </summary>
 /// <param name="roll">A DieRoll object</param>
 public DieRollElement(DieRoll roll)
 {
     this.Count = roll.Count;
     this.Die   = roll.Die;
 }