Skip to content
This repository was archived by the owner on May 5, 2023. It is now read-only.

Use Cases

yueyinqiu edited this page Apr 30, 2021 · 2 revisions

You can find all the codes in the project Example.

used namespaces

using System;
using System.Diagnostics;
using System.Linq;
using YiJingFramework.Core;

using YiJingFramework.Painting.Deriving;
using YiJingFramework.Painting.Deriving.Derivations;
using YiJingFramework.Painting.Deriving.Comparers;
using YiJingFramework.Painting.Deriving.Extensions;

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

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

Clone this wiki locally