/// <summary>
        /// 解释单个骰点命令内容并返回骰点结果。形如1d3或2等
        /// </summary>
        /// <param name="cmd">骰点命令内容</param>
        private static List <int> TranslateSingleRollingCommand(string cmd)
        {
            RollingManager rollingManager = RollingManager.GetInstance();
            List <int>     results        = new List <int>();
            // 存储初始骰点范围,避免产生预期外的影响
            uint initRollingRange = rollingManager.RollingRange;

            try
            {
                int index = cmd.IndexOf('d');
                if (index > -1)
                {
                    String[] numsStr = cmd.Split('d');
                    if (numsStr.Length != 2 || numsStr[0] == "" || numsStr[1] == "")
                    {
                        // 分割字符串不为2个,或有值为空时抛出异常
                        throw new FormatException();
                    }

                    // 第一个值为骰点个数
                    int num = Convert.ToInt32(numsStr[0]);
                    // 第二个值为骰点范围
                    uint range = Convert.ToUInt32(numsStr[1]);

                    // 按照获得值进行骰点
                    rollingManager.RollingRange = range;
                    int[] rollingResults = rollingManager.GetRollingResult(num);

                    results.AddRange(rollingResults);
                }
                else
                {
                    // 当命令行内容为常数时,直接添加结果
                    results.Add(Convert.ToInt32(cmd));
                }
                return(results);
            }
            catch
            {
                // 为可能出现的异常抛格式错误异常
                throw new FormatException("命令行格式错误");
            }
            finally
            {
                // 恢复初始骰点范围
                rollingManager.RollingRange = initRollingRange;
            }
        }
Exemple #2
0
 /// <summary>
 /// 执行一次骰点操作
 /// </summary>
 /// <param name="num">骰点次数</param>
 private int[] Rolling(int num)
 {
     return(rollingManager.GetRollingResult(num));
 }