/// <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)); }
static void Main(string[] args) { #region to derive from a painting _ = Painting.TryParse("110000", out Painting painting); Debug.Assert(painting is not null); // Traditionally: IDerivation derivation = new OverturningDerivation(); Console.WriteLine($"110000 -o> {derivation.Derive(painting)}"); Console.WriteLine(); // Output: 110000 -o> 000011 derivation = new ChangingDerivation(3, 4); Console.WriteLine($"110000 -c> {derivation.Derive(painting)}"); Console.WriteLine(); // Output: 110000 -c> 110110 derivation = new FunctionDerivation((painting) => { return(new Painting(painting.Select((_, _) => YinYang.Yang))); }); // This derivation will turn all the lines to yang lines. Console.WriteLine($"110000 -f> {derivation.Derive(painting)}"); Console.WriteLine(); // Output: 110000 -f> 111111 // With extension methods: Console.WriteLine($"110000 -o2> {painting.ToOverlapping()}"); Console.WriteLine(); // Output: 110000 -o2> 100000 Console.WriteLine($"110000 -o2> " + $"{painting.ApplyDerivation((_) => new Painting(YinYang.Yang))}"); Console.WriteLine(); // Output: 110000 -o2> 1 #endregion #region to compare two paintings _ = Painting.TryParse("110000", out Painting painting1); _ = Painting.TryParse("000011", out Painting painting2); _ = Painting.TryParse("111111", out Painting painting3); Debug.Assert(painting1 is not null && painting2 is not null); // Traditionally: IComparer <bool> comparer = new OverlappingComparer(); Console.WriteLine(comparer.Compare(painting1, painting2)); Console.WriteLine(); // Output: False comparer = new OverturningComparer(); Console.WriteLine(comparer.Compare(painting1, painting2)); Console.WriteLine(); // Output: True comparer = new FunctionComparer(derivation); // The current derivation will turn all the lines to yang lines, // and now the compare will check that. Console.WriteLine($"{comparer.Compare(painting1, painting3)} " + $"{comparer.Compare(painting3, painting1)}"); Console.WriteLine(); // Output: True False // With extension methods: Console.WriteLine(painting1.IsOverturnedFrom(painting2)); Console.WriteLine(); // Output: True foreach (var lineIndex in painting1.GetDifferentLinesBetween(painting2)) { Console.Write(lineIndex); } Console.WriteLine(); Console.WriteLine(); // Output: 0145 #endregion }