/// <summary>
        /// 比较卦画。
        /// Compare the paintings.
        /// </summary>
        /// <param name="basis">
        /// 作为比较标准的卦画。
        /// The painting to be the comparing basis.
        /// </param>
        /// <param name="obj">
        /// 要比较的卦画。
        /// The painting to be compared.
        /// </param>
        /// <returns>
        /// 变换结果。
        /// The derived painting.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="basis"/> 或 <paramref name="obj"/> 是 <c>null</c>.
        /// <paramref name="basis"/> or <paramref name="obj"/> is <c>null</c>.
        /// </exception>
        public int[] Compare(Core.Painting basis, Core.Painting obj)
        {
            if (basis is null)
            {
                throw new ArgumentNullException(nameof(basis));
            }
            if (obj is null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var        min    = Math.Min(basis.Count, obj.Count);
            var        max    = Math.Max(basis.Count, obj.Count);
            List <int> result = new List <int>(max);

            for (int i = 0; i < min; i++)
            {
                if (basis[i] != obj[i])
                {
                    result.Add(i);
                }
            }
            for (int n = min; n < max; n++)
            {
                result.Add(n);
            }
            return(result.ToArray());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 比较卦画。
        /// Compare the paintings.
        /// </summary>
        /// <param name="basis">
        /// 作为比较标准的卦画。
        /// The painting to be the comparing basis.
        /// </param>
        /// <param name="obj">
        /// 要比较的卦画。
        /// The painting to be compared.
        /// </param>
        /// <returns>
        /// 变换结果。
        /// The derived painting.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="basis"/> 或 <paramref name="obj"/> 是 <c>null</c>.
        /// <paramref name="basis"/> or <paramref name="obj"/> is <c>null</c>.
        /// </exception>
        public bool Compare(Core.Painting basis, Core.Painting obj)
        {
            if (basis is null)
            {
                throw new ArgumentNullException(nameof(basis));
            }
            if (obj is null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (basis.Count != obj.Count)
            {
                return(false);
            }

            for (int i = 0; i < basis.Count; i++)
            {
                if (basis[i] == obj[i])
                {
                    return(false);
                }
            }
            return(true);
        }
 /// <summary>
 /// 变换指定的卦画,产生新卦画。
 /// Derive a new painting from the given one.
 /// </summary>
 /// <param name="from">
 /// 要变换的卦画。
 /// The painting to derive from.
 /// </param>
 /// <returns>
 /// 变换结果。
 /// The derived painting.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="from"/> 是 <c>null</c>.
 /// <paramref name="from"/> is <c>null</c>.
 /// </exception>
 public Core.Painting Derive(Core.Painting from)
 {
     if (from is null)
     {
         throw new ArgumentNullException(nameof(from));
     }
     return(new Core.Painting(this.GetDerivedLines(from)));
 }
 /// <summary>
 /// 变换指定的卦画,产生新卦画。
 /// Derive a new painting from the given one.
 /// </summary>
 /// <param name="from">
 /// 要变换的卦画。
 /// The painting to derive from.
 /// </param>
 /// <returns>
 /// 变换结果。
 /// The derived painting.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="from"/> 是 <c>null</c>.
 /// <paramref name="from"/> is <c>null</c>.
 /// </exception>
 public Core.Painting Derive(Core.Painting from)
 {
     if (from is null)
     {
         throw new ArgumentNullException(nameof(from));
     }
     return(new Core.Painting(from.Reverse()));
 }
Exemplo n.º 5
0
 /// <summary>
 /// 获取综卦卦画。
 /// Get the overturned painting.
 /// </summary>
 /// <param name="from">
 /// 要变换的卦画。
 /// The painting to derive from.
 /// </param>
 /// <returns>
 /// 变换结果。
 /// The derived painting.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="from"/> 是 <c>null</c>.
 /// <paramref name="from"/> is <c>null</c>.
 /// </exception>
 public static Core.Painting ToOverturned(
     this Core.Painting from)
 {
     if (from is null)
     {
         throw new ArgumentNullException(nameof(from));
     }
     return(overturningDerivation.Derive(from));
 }
Exemplo n.º 6
0
 /// <summary>
 /// 获取错卦卦画。
 /// Get the laterally linked painting.
 /// </summary>
 /// <param name="from">
 /// 要变换的卦画。
 /// The painting to derive from.
 /// </param>
 /// <returns>
 /// 变换结果。
 /// The derived painting.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="from"/> 是 <c>null</c>.
 /// <paramref name="from"/> is <c>null</c>.
 /// </exception>
 public static Core.Painting ToLaterallyLinked(
     this Core.Painting from)
 {
     if (from is null)
     {
         throw new ArgumentNullException(nameof(from));
     }
     return(laterallyLinkingDerivation.Derive(from));
 }
Exemplo n.º 7
0
 /// <summary>
 /// 复制卦画。
 /// Copy the painting.
 /// </summary>
 /// <param name="from">
 /// 要变换的卦画。
 /// The painting to derive from.
 /// </param>
 /// <returns>
 /// 变换结果。
 /// The derived painting.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="from"/> 是 <c>null</c>.
 /// <paramref name="from"/> is <c>null</c>.
 /// </exception>
 public static Core.Painting Copy(
     this Core.Painting from)
 {
     if (from is null)
     {
         throw new ArgumentNullException(nameof(from));
     }
     return(copyingDerivation.Derive(from));
 }
        private IEnumerable <Core.YinYang> GetDerivedLines(Core.Painting from)
        {
            int i = 0;

            foreach (var line in from)
            {
                yield return(new Core.YinYang(
                                 line.IsYang ^ this.indexesOfTheChangingLines.Contains(i++)));
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 进行转换。
        /// Convert a painting.
        /// </summary>
        /// <param name="painting">
        /// 要转换的卦画。
        /// The painting to convert.
        /// </param>
        /// <returns>
        /// 转换结果。
        /// The result.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="painting"/> 是 <c>null</c> 。
        /// <paramref name="painting"/> is <c>null</c>.
        /// </exception>
        public string ConvertTo(Core.Painting painting)
        {
            if (painting is null)
            {
                throw new ArgumentNullException(nameof(painting));
            }
            var linq = from line in painting.Reverse()
                       select line.IsYang ? this.yang : this.yin;

            return(string.Join(this.separator, linq));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 变换指定的卦画,产生新卦画。
        /// Derive a new painting from the given one.
        /// </summary>
        /// <param name="from">
        /// 要变换的卦画。
        /// The painting to derive from.
        /// </param>
        /// <returns>
        /// 变换结果。
        /// The derived painting.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="from"/> 是 <c>null</c>.
        /// <paramref name="from"/> is <c>null</c>.
        /// </exception>
        public Core.Painting Derive(Core.Painting from)
        {
            if (from is null)
            {
                throw new ArgumentNullException(nameof(from));
            }
            var result = from line in @from
                         select !line;

            return(new Core.Painting(result));
        }
 /// <summary>
 /// 比较卦画。
 /// Compare the paintings.
 /// </summary>
 /// <param name="basis">
 /// 作为比较标准的卦画。
 /// The painting to be the comparing basis.
 /// </param>
 /// <param name="obj">
 /// 要比较的卦画。
 /// The painting to be compared.
 /// </param>
 /// <returns>
 /// 变换结果。
 /// The derived painting.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="basis"/> 或 <paramref name="obj"/> 是 <c>null</c>.
 /// <paramref name="basis"/> or <paramref name="obj"/> is <c>null</c>.
 /// </exception>
 public bool Compare(Core.Painting basis, Core.Painting obj)
 {
     if (basis is null)
     {
         throw new ArgumentNullException(nameof(basis));
     }
     if (obj is null)
     {
         throw new ArgumentNullException(nameof(obj));
     }
     return(basis.Equals(obj));
 }
Exemplo n.º 12
0
 /// <summary>
 /// 判断两个卦是否相同。
 /// Judge whether the two paintings are the same.
 /// </summary>
 /// <param name="obj">
 /// 要判断的目标卦画。
 /// The object painting to judge.
 /// </param>
 /// <param name="basis">
 /// 作为判断基础的卦画。
 /// The paintings to be used as the comparing basis.
 /// </param>
 /// <returns>
 /// 判断结果。
 /// The result.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="basis"/> 或 <paramref name="obj"/> 是 <c>null</c> 。
 /// <paramref name="basis"/> or <paramref name="obj"/> is <c>null</c>.
 /// </exception>
 public static bool IsTheSameAs(
     this Core.Painting obj, Core.Painting basis)
 {
     if (obj is null)
     {
         throw new ArgumentNullException(nameof(obj));
     }
     if (basis is null)
     {
         throw new ArgumentNullException(nameof(basis));
     }
     return(copyingComparer.Compare(basis, obj));
 }
Exemplo n.º 13
0
 /// <summary>
 /// 判断目标卦是否是基础卦的错卦。
 /// Judge whether the object painting is laterally linked with the basis.
 /// </summary>
 /// <param name="obj">
 /// 要判断的目标卦画。
 /// The object painting to judge.
 /// </param>
 /// <param name="basis">
 /// 作为判断基础的卦画。
 /// The paintings to be used as the comparing basis.
 /// </param>
 /// <returns>
 /// 判断结果。
 /// The result.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="basis"/> 或 <paramref name="obj"/> 是 <c>null</c> 。
 /// <paramref name="basis"/> or <paramref name="obj"/> is <c>null</c>.
 /// </exception>
 public static bool IsLaterallyLinkedWith(
     this Core.Painting obj, Core.Painting basis)
 {
     if (obj is null)
     {
         throw new ArgumentNullException(nameof(obj));
     }
     if (basis is null)
     {
         throw new ArgumentNullException(nameof(basis));
     }
     return(laterallyLinkingComparer.Compare(basis, obj));
 }
Exemplo n.º 14
0
 /// <summary>
 /// 判断目标卦是否是基础卦的综卦。
 /// Judge whether the object painting is overturned from the basis.
 /// </summary>
 /// <param name="obj">
 /// 要判断的目标卦画。
 /// The object painting to judge.
 /// </param>
 /// <param name="basis">
 /// 作为判断基础的卦画。
 /// The paintings to be used as the comparing basis.
 /// </param>
 /// <returns>
 /// 判断结果。
 /// The result.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="basis"/> 或 <paramref name="obj"/> 是 <c>null</c> 。
 /// <paramref name="basis"/> or <paramref name="obj"/> is <c>null</c>.
 /// </exception>
 public static bool IsOverturnedFrom(
     this Core.Painting obj, Core.Painting basis)
 {
     if (obj is null)
     {
         throw new ArgumentNullException(nameof(obj));
     }
     if (basis is null)
     {
         throw new ArgumentNullException(nameof(basis));
     }
     return(overturningComparer.Compare(basis, obj));
 }
Exemplo n.º 15
0
 /// <summary>
 /// 应用指定变换。
 /// Apply the given derivation.
 /// </summary>
 /// <param name="from">
 /// 要变换的卦画。
 /// The painting to derive from.
 /// </param>
 /// <param name="derivation">
 /// 变换。
 /// The derivation.
 /// </param>
 /// <returns>
 /// 变换结果。
 /// The derived painting.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="from"/> 或 <paramref name="derivation"/> 是 <c>null</c>.
 /// <paramref name="from"/> or <paramref name="derivation"/> is <c>null</c>.
 /// </exception>
 /// <exception cref="PaintingDerivationException">
 /// 变换失败。这通常表示此变换过程不适用于指定的卦画。
 /// Derivation failed. This often means that the derivation doesn't fit the given painting.
 /// </exception>
 public static Core.Painting ApplyDerivation(
     this Core.Painting from, IDerivation derivation)
 {
     if (from is null)
     {
         throw new ArgumentNullException(nameof(from));
     }
     if (derivation is null)
     {
         throw new ArgumentNullException(nameof(derivation));
     }
     return(derivation.Derive(from));
 }
Exemplo n.º 16
0
 /// <summary>
 /// 获取变爻的序号。
 /// Get the indexes of the changed lines.
 /// </summary>
 /// <param name="obj">
 /// 要判断的目标卦画。
 /// The object painting to judge.
 /// </param>
 /// <param name="basis">
 /// 作为判断基础的卦画。
 /// The paintings to be used as the comparing basis.
 /// </param>
 /// <returns>
 /// 判断结果。
 /// The result.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="basis"/> 或 <paramref name="obj"/> 是 <c>null</c> 。
 /// <paramref name="basis"/> or <paramref name="obj"/> is <c>null</c>.
 /// </exception>
 public static int[] GetDifferentLinesBetween(
     this Core.Painting obj, Core.Painting basis)
 {
     if (obj is null)
     {
         throw new ArgumentNullException(nameof(obj));
     }
     if (basis is null)
     {
         throw new ArgumentNullException(nameof(basis));
     }
     return(changingComparer.Compare(basis, obj));
 }
        public void GetHashCodeTest()
        {
            Random r = new Random();

            for (int i = 0; i < 1000000;)
            {
                Core.Painting painting1 = new Core.Painting(
                    (Core.YinYang)r.Next(0, 2),
                    (Core.YinYang)r.Next(0, 2),
                    (Core.YinYang)r.Next(0, 2),
                    (Core.YinYang)r.Next(0, 2),
                    (Core.YinYang)r.Next(0, 2),
                    (Core.YinYang)r.Next(0, 2));
                Core.Painting painting2 = new Core.Painting(
                    (Core.YinYang)r.Next(0, 2),
                    (Core.YinYang)r.Next(0, 2),
                    (Core.YinYang)r.Next(0, 2),
                    (Core.YinYang)r.Next(0, 2),
                    (Core.YinYang)r.Next(0, 2),
                    (Core.YinYang)r.Next(0, 2));
                if (r.Next(0, 2) == 0)
                {
                    var line1 = this.yijing.GetHexagram(painting1).GetLine(r.Next(1, 7));
                    var line2 = this.yijing.GetHexagram(painting2).GetLine(r.Next(1, 7));
                    if (line1.GetHashCode() != line2.GetHashCode())
                    {
                        i++;
                        Assert.IsFalse(line1.Equals(line2));
                    }
                    else
                    {
                        Assert.IsTrue(line1.Equals(line2));
                    }
                }
                else
                {
                    var line1 = this.yijing.GetHexagram(painting1).GetLine(r.Next(1, 7));
                    var line2 = this.yijing.GetHexagram(painting1).GetLine(r.Next(1, 7));
                    if (line1.GetHashCode() != line2.GetHashCode())
                    {
                        i++;
                        Assert.AreNotEqual(line1, line2);
                    }
                    else
                    {
                        Assert.AreEqual(line1, line2);
                    }
                }
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// 获取变卦卦画。
        /// Get the changed painting.
        /// </summary>
        /// <param name="from">
        /// 要变换的卦画。
        /// The painting to derive from.
        /// </param>
        /// <param name="indexesOfTheChangingLines">
        /// 变爻的序号。从零开始计。
        /// The indexes of the changing lines, starting from zero.
        /// </param>
        /// <returns>
        /// 变换结果。
        /// The derived painting.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="from"/> 或 <paramref name="indexesOfTheChangingLines"/> 是 <c>null</c>.
        /// <paramref name="from"/> or <paramref name="indexesOfTheChangingLines"/> is <c>null</c>.
        /// </exception>
        public static Core.Painting ChangeLines(
            this Core.Painting from, IEnumerable <int> indexesOfTheChangingLines)
        {
            if (from is null)
            {
                throw new ArgumentNullException(nameof(from));
            }
            if (indexesOfTheChangingLines is null)
            {
                throw new ArgumentNullException(nameof(indexesOfTheChangingLines));
            }
            var derivation = new ChangingDerivation(indexesOfTheChangingLines);

            return(derivation.Derive(from));
        }
Exemplo n.º 19
0
        /// <summary>
        /// 应用指定变换函数。
        /// Apply the given derivation function.
        /// </summary>
        /// <param name="from">
        /// 要变换的卦画。
        /// The painting to derive from.
        /// </param>
        /// <param name="derivationFunction">
        /// 变换函数。
        /// The derivation function.
        /// </param>
        /// <returns>
        /// 变换结果。
        /// The derived painting.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="from"/> 或 <paramref name="derivationFunction"/> 是 <c>null</c>.
        /// <paramref name="from"/> or <paramref name="derivationFunction"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="PaintingDerivationException">
        /// 变换失败。这通常表示此变换过程不适用于指定的卦画。
        /// Derivation failed. This often means that the derivation doesn't fit the given painting.
        /// </exception>
        public static Core.Painting ApplyDerivation(
            this Core.Painting from, DerivationFunc derivationFunction)
        {
            if (from is null)
            {
                throw new ArgumentNullException(nameof(from));
            }
            if (derivationFunction is null)
            {
                throw new ArgumentNullException(nameof(derivationFunction));
            }
            var derivation = new FunctionDerivation(derivationFunction);

            return(derivation.Derive(from));
        }
        /// <summary>
        /// 变换指定的卦画,产生新卦画。
        /// Derive a new painting from the given one.
        /// </summary>
        /// <param name="from">
        /// 要变换的卦画。
        /// The painting to derive from.
        /// </param>
        /// <returns>
        /// 变换结果。
        /// The derived painting.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="from"/> 是 <c>null</c>.
        /// <paramref name="from"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="PaintingDerivationException">
        /// 传入的卦不是六爻卦。
        /// The given painting isn't a hexagram.
        /// </exception>
        public Core.Painting Derive(Core.Painting from)
        {
            if (from is null)
            {
                throw new ArgumentNullException(nameof(from));
            }
            if (from.Count != 6)
            {
                throw new PaintingDerivationException("This derivation can only be applied to hexagrams.");
            }

            return(new Core.Painting(
                       from[2 - 1], from[3 - 1], from[4 - 1],
                       from[3 - 1], from[4 - 1], from[5 - 1]));
        }
Exemplo n.º 21
0
        /// <summary>
        /// 判断一个卦画是否由另一个卦画,通过指定的方式变换而来。
        /// Judge whether the object painting is derived from the basis painting,
        /// through the given derivation function.
        /// </summary>
        /// <param name="obj">
        /// 要判断的目标卦画。
        /// The object painting to judge.
        /// </param>
        /// <param name="basis">
        /// 作为判断基础的卦画。
        /// The paintings to be used as the comparing basis.
        /// </param>
        /// <param name="derivationFunction">
        /// 变换过程函数。
        /// The derivation function.
        /// </param>
        /// <returns>
        /// 判断结果。
        /// The result.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="basis"/> 或 <paramref name="derivationFunction"/> 或 <paramref name="obj"/> 是 <c>null</c> 。
        /// <paramref name="basis"/> or <paramref name="derivationFunction"/> or <paramref name="obj"/> is <c>null</c>.
        /// </exception>
        public static bool IsDerivedFrom(
            this Core.Painting obj, Core.Painting basis, DerivationFunc derivationFunction)
        {
            if (obj is null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            if (basis is null)
            {
                throw new ArgumentNullException(nameof(basis));
            }
            if (derivationFunction is null)
            {
                throw new ArgumentNullException(nameof(derivationFunction));
            }
            var functionComparer = new FunctionComparer(derivationFunction);

            return(functionComparer.Compare(basis, obj));
        }
Exemplo n.º 22
0
 /// <summary>
 /// 比较卦画。
 /// Compare the paintings.
 /// </summary>
 /// <param name="basis">
 /// 作为比较标准的卦画。
 /// The painting to be the comparing basis.
 /// </param>
 /// <param name="obj">
 /// 要比较的卦画。
 /// The painting to be compared.
 /// </param>
 /// <returns>
 /// 变换结果。
 /// The derived painting.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="basis"/> 或 <paramref name="obj"/> 是 <c>null</c>.
 /// <paramref name="basis"/> or <paramref name="obj"/> is <c>null</c>.
 /// </exception>
 public bool Compare(Core.Painting basis, Core.Painting obj)
 {
     if (basis is null)
     {
         throw new ArgumentNullException(nameof(basis));
     }
     if (obj is null)
     {
         throw new ArgumentNullException(nameof(obj));
     }
     try
     {
         return(this.function(basis).Equals(obj));
     }
     catch
     {
         return(false);
     }
 }
        /// <summary>
        /// 比较卦画。
        /// Compare the paintings.
        /// </summary>
        /// <param name="basis">
        /// 作为比较标准的卦画。
        /// The painting to be the comparing basis.
        /// </param>
        /// <param name="obj">
        /// 要比较的卦画。
        /// The painting to be compared.
        /// </param>
        /// <returns>
        /// 变换结果。
        /// The derived painting.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="basis"/> 或 <paramref name="obj"/> 是 <c>null</c>.
        /// <paramref name="basis"/> or <paramref name="obj"/> is <c>null</c>.
        /// </exception>
        public bool Compare(Core.Painting basis, Core.Painting obj)
        {
            if (basis is null)
            {
                throw new ArgumentNullException(nameof(basis));
            }
            if (obj is null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (basis.Count != 6)
            {
                return(false);
            }

            return(new Core.Painting(
                       basis[2 - 1], basis[3 - 1], basis[4 - 1],
                       basis[3 - 1], basis[4 - 1], basis[5 - 1]).Equals(obj));
        }
Exemplo n.º 24
0
        /// <summary>
        /// 进行转换。
        /// Convert a painting.
        /// </summary>
        /// <param name="painting">
        /// 要转换的卦画。
        /// The painting to convert.
        /// </param>
        /// <returns>
        /// 转换结果。
        /// The result.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="painting"/> 是 <c>null</c> 。
        /// <paramref name="painting"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="PaintingConversionException">
        /// <paramref name="painting"/> 不是一爻、二爻、三爻或六爻卦。
        /// <paramref name="painting"/> isn't a painting with 1, 2, 3 or 6 lines.
        /// </exception>
        public char ConvertTo(Core.Painting painting)
        {
            if (painting.Count == 1)
            {
                return(painting[0].IsYang ? '\u268A' : '\u268B');
            }

            if (painting.Count == 2)
            {
                if (painting[0].IsYang)
                {
                    return(painting[1].IsYang ? '\u268C' : '\u268D');
                }
                return(painting[1].IsYang ? '\u268E' : '\u268F');
            }

            if (painting.Count == 3)
            {
                int value = '\u2630';
                int d     = 4;
                foreach (var line in painting)
                {
                    if (!line.IsYang)
                    {
                        value += d;
                    }
                    d = d / 2;
                }
                return((char)value);
            }

            if (painting.Count == 6)
            {
                int value = '\u4DC0';
                return((char)(value + Array.IndexOf(hexagramTable, painting.ToBytes()[0])));
            }

            throw new PaintingConversionException("The painting should have 1, 2, 3 or 6 lines.");
        }
Exemplo n.º 25
0
        /// <summary>
        /// 通过卦画获取别卦。
        /// Get a hexagram by its painting.
        /// </summary>
        /// <param name="painting">
        /// 卦画。
        /// The painting.
        /// </param>
        /// <returns>
        /// 结果。
        /// The result.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="painting"/> 是 <c>null</c> 。
        /// <paramref name="painting"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="painting"/> 不表示一个别卦。
        /// <paramref name="painting"/> can't represent a hexagram.
        /// </exception>
        public ZhouyiHexagram GetHexagram(Core.Painting painting)
        {
            if (painting.Count != 6)
            {
                throw new ArgumentException(
                          $"{nameof(painting)} should represent a hexagram.",
                          nameof(painting));
            }
            var l     = this.GetTrigram(new Core.Painting(painting[0], painting[1], painting[2]));
            var u     = this.GetTrigram(new Core.Painting(painting[3], painting[4], painting[5]));
            var index = this.hexagramsTrigramsMap.GetHexagram(l.Index, u.Index);
            var t     = this.trigramsAndHexagrams.GetHexagram(index);

            return(new ZhouyiHexagram(this.patternsAndNumbers,
                                      index,
                                      t.Name,
                                      t.Text,
                                      t.ApplyNinesOrSixes,
                                      l,
                                      u,
                                      t.Lines));
        }
Exemplo n.º 26
0
        /// <summary>
        /// 通过卦画获取经卦。
        /// Get a trigram by its painting.
        /// </summary>
        /// <param name="painting">
        /// 卦画。
        /// The painting.
        /// </param>
        /// <returns>
        /// 结果。
        /// The result.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="painting"/> 是 <c>null</c> 。
        /// <paramref name="painting"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="painting"/> 不表示一个经卦。
        /// <paramref name="painting"/> can't represent a trigram.
        /// </exception>
        public ZhouyiTrigram GetTrigram(Core.Painting painting)
        {
            if (painting is null)
            {
                throw new ArgumentNullException(nameof(painting));
            }
            if (painting.Count != 3)
            {
                throw new ArgumentException(
                          $"{nameof(painting)} should represent a trigram.",
                          nameof(painting));
            }

            int d = 0;

            for (int i = 0; i < 3; i++)
            {
                d  = d << 1;
                d += (int)!painting[i];
            }
            return(this.trigramsAndHexagrams.GetTrigram(d + 1));
        }
Exemplo n.º 27
0
        /// <summary>
        /// 尝试从某个值进行转换。
        /// Try converting from.
        /// </summary>
        /// <param name="value">
        /// 要转换的值。
        /// The value to convert from.
        /// </param>
        /// <param name="result">
        /// 转换结果。
        /// The result.
        /// </param>
        /// <returns>
        /// 指示转换是否成功的值。
        /// A value indicates whether the conversion succeeded or not.
        /// </returns>
        public bool TryConvertFrom(char value, [MaybeNullWhen(false)] out Core.Painting result)
        {
            switch (value)
            {
            case '\u268A':
                result = new Core.Painting(Core.YinYang.Yang);
                return(true);

            case '\u268B':
                result = new Core.Painting(Core.YinYang.Yin);
                return(true);

            case '\u268C':
                result = new Core.Painting(Core.YinYang.Yang, Core.YinYang.Yang);
                return(true);

            case '\u268D':
                result = new Core.Painting(Core.YinYang.Yang, Core.YinYang.Yin);
                return(true);

            case '\u268E':
                result = new Core.Painting(Core.YinYang.Yin, Core.YinYang.Yang);
                return(true);

            case '\u268F':
                result = new Core.Painting(Core.YinYang.Yin, Core.YinYang.Yin);
                return(true);

            default:
                break;
            }

            {
                var difference = value - '\u2630';
                if (difference is >= 0 and < 8)
                {
                    var first = Core.YinYang.Yang;
                    if (difference >= 4)
                    {
                        first       = Core.YinYang.Yin;
                        difference -= 4;
                    }

                    var second = Core.YinYang.Yang;
                    if (difference >= 2)
                    {
                        second      = Core.YinYang.Yin;
                        difference -= 2;
                    }

                    var third = Core.YinYang.Yang;
                    if (difference >= 1)
                    {
                        third = Core.YinYang.Yin;
                    }

                    result = new Core.Painting(first, second, third);
                    return(true);
                }
            }

            {
                var difference = value - '\u4DC0';
                if (difference is >= 0 and < 64)
                {
                    result = Core.Painting.FromBytes(hexagramTable[difference]);
                    return(true);
                }
            }

            result = null;
            return(false);
        }