《梅花易数》用C#写的一系列关于易学的包
《梅花易数》是中国古代占卜法之一。现在的梅花心易就是梅花易数的别称。相传为宋代易学家邵雍所著,梅花易数起源于汉易,是一部以易学中的数学为基础,结合易学中的“象学”进行占卜的书,相传邵雍运用时每卦必中,屡试不爽。
不过前面这段是抄的百度百科,我们不用管它(
C#(读作“See Sharp”)是一种新式编程语言,不仅面向对象,还类型安全。 开发人员利用 C# 能够生成在 .NET 中运行的多种安全可靠的应用程序。 C# 源于 C 语言系列,C、C++、Java 和 程序员很快就可以上手使用。
上面这段又是抄的官方文档,我们还是不用管它(
是……
这个抄不到了,自己也写不来一点,还是别管了吧(逃
总之, 是用 C# 写的一系列关于易学的包,目前包括:
现在我们就可以用程序来生成一个梅花易数时间卦了,先给出 using :
using System.Diagnostics;
using System.Net.Http.Json;
using YiJingFramework.Annotating.Zhouyi;
using YiJingFramework.Annotating.Zhouyi.Entities;
using YiJingFramework.EntityRelations.GuaDerivations.Extensions;
using YiJingFramework.Nongli.Extensions;
using YiJingFramework.Nongli.Lunar;
using YiJingFramework.PrimitiveTypes;
using YiJingFramework.PrimitiveTypes.GuaWithFixedCount;
梅花易数时间卦,首先要获取年月日时数。一般取农历阴历日期,年数为年地支的序数,月数即为此年中月的序数,日数为此月中日的序数,时数为时地支的序数。代码如下:
var dateTime = DateTime.Now;
Console.WriteLine(dateTime.ToString("yyyy/MM/dd HH:mm"));
#region 获取年月日时数 Get the Shus (numbers) of Nian (year), Yue (month), Ri (day) and Shi (hour)
var lunar = LunarDateTime.FromGregorian(dateTime);
Console.WriteLine($"{lunar.Nian:C}年{lunar.YueInChinese()}月{lunar.RiInChinese()}{lunar.Shi:C}时");
// Console.WriteLine($"Nian:{lunar.Nian} Yue:{lunar.Yue} Ri:{lunar.Ri} Shi:{lunar.Shi}");
Console.WriteLine();
// 取农历年月日时。
// Get the date and time of Nongli.
var nianshu = lunar.Nian.Dizhi.Index;
// 《梅花易数》:如子年一数丑年二数直至亥年十二数
// The Nianshu will be 1 if it's the Nian of Zi, 2 if Chou, ..., 12 if Hai.
var yueshu = lunar.Yue;
// 《梅花易数》:月如正月一数直至十二月亦作十二数
// The Yueshu is the 1-based index of the Yue.
var rishu = lunar.Ri;
// 《梅花易数》:日数如初一一数直至三十日为三十数
// The Rishu is the 1-based index of the Ri.
var shishu = lunar.Shi.Index;
// 《梅花易数》:时如子时一数直至亥时为十二数
// The Shishu will be 1 if it's the Shi of Zi, 2 if Chou, ..., 12 if Hai.
#endregion
随后我们要通过年月日时数算出卦数。一般以年月日三数相加为上卦卦数,年月日时四数相加为下卦卦数和全卦卦数,也即动爻数。代码如下:
#region 算卦数 Calculate the Guashus (numbers of the Guas)
var upperGuashu = nianshu + yueshu + rishu;
var upperGuaIndex = upperGuashu % 8;
upperGuaIndex = upperGuaIndex == 0 ? 8 : upperGuaIndex;
// 《梅花易数》:年月日共计几数以八除之以零数作上卦
// just do as the above three lines to get the upper Guashu and the index of the upper Gua (trigram)
var lowerGuashu = nianshu + yueshu + rishu + shishu;
var lowerGuaIndex = lowerGuashu % 8;
lowerGuaIndex = lowerGuaIndex == 0 ? 8 : lowerGuaIndex;
// 《梅花易数》:年月日数加时之数总计几数以八除之零数作下卦
// just do as the above three lines to get the lower Guashu and the index of the lower Gua (trigram)
var guashu = lowerGuashu;
var dongyaoIndex = guashu % 6;
dongyaoIndex = dongyaoIndex == 0 ? 6 : dongyaoIndex;
// 《梅花易数》:就以除六数作动爻
// just do as the above three lines to get the total Guashu and the index of the Dongyao (changing line)
#endregion
接下来,我们可以根据乾一、兑二、离三、震四、巽五、坎六、艮七、坤八得出上卦和下卦,然后拼出本卦:
#region 取本卦卦画 Get the Bengua (the original hexagram)
static GuaTrigram GetTrigram(int xiantanIndex)
{
xiantanIndex--;
Debug.Assert(xiantanIndex is >= 0b000 and <= 0b111);
return new GuaTrigram(
new Yinyang((xiantanIndex & 0b100) is 0),
new Yinyang((xiantanIndex & 0b010) is 0),
new Yinyang((xiantanIndex & 0b001) is 0));
}
// 这是通过先天八卦数获取画卦的数学方法。
// 也可以直接查表:1->☰ 2->☱ 3->☲ 4->☳ 5->☴ 6->☵ 7->☶ 8->☷
// This is a mathematical method to get the painting through the Xiantian indexes.
// It can also be done directly by mapping:1->☰ 2->☱ 3->☲ 4->☳ 5->☴ 6->☵ 7->☶ 8->☷
var upperGua = GetTrigram(upperGuaIndex);
var lowerGua = GetTrigram(lowerGuaIndex);
// 获取上卦和下卦的卦画。
// Get the paintings of the upper and the lower Guas (trigrams).
var linesOfBengua = lowerGua.Concat(upperGua);
var bengua = new GuaHexagram(linesOfBengua);
// 将上卦下卦放在一起得到一个六爻卦,这就是本卦。
// Put the two Guas (trigrams) together to get a Gua (hexagram), which is called Bengua (the original hexagram).
#endregion
随后根据动爻序数《梅花易数》用C#写的一系列关于易学的包,改变本卦指定位置爻的阴阳属性,得到变卦。然后取本卦或变卦的互卦梅花易数,并将前面得到的本互变三卦打印出来:
#region 取变卦卦画 Get the Biangua (the changed hexagram)
var biangua = bengua.ChangeYaos(dongyaoIndex - 1);
// 这里使用了 YiJingFramework.EntityRelations 提供的拓展方法,
// 把对应的爻阴阳性质改变,返回新的卦,即变卦。
// Here we use the extension method provided by the YiJingFramework.EntityRelations.
// The specific line's Yinyang attribute has been changed and a new Gua (hexgram) has been returned, which is called Biangua (the changed hexagram).
#endregion
#region 取互卦卦画 Get the Hugua (the overlapping hexagram)
var hugua = bengua.Hugua();
// 仍是 YiJingFramework.EntityRelations 包提供的拓展方法,
// 二三四爻作下卦,三四五爻作上卦产生新的卦,这就是互卦。
// It's also an extension method provided by the YiJingFramework.EntityRelations package.
// It returns a new hexagram which will be made up of --
// the second line, the third line, the fourth line,
// then the third line again, the fourth line again and the fifth line
// -- of the original hexagram.
// This new hexagram is the so-called Hugua (the overlapping hexagram).
if (hugua == bengua)
hugua = biangua.Hugua();
// 《梅花易数》:乾坤无互互其变卦
// If the Bengua is Qian or Kun, which does not have a Hugua, we should use the Biangua's Hugua instead.
#endregion
#region 将三个卦打印出来 Print the three Guas (hexagrams)
static void PrintGua(GuaHexagram gua)
{
for (int i = 5; i >= 0; i--)
Console.WriteLine(gua[i].IsYang ? "-----" : "-- --");
}
Console.WriteLine("本卦 BENGUA");
PrintGua(bengua);
Console.WriteLine();
Console.WriteLine("互卦 HUGUA");
PrintGua(hugua);
Console.WriteLine();
Console.WriteLine("变卦 BIANGUA");
PrintGua(biangua);
Console.WriteLine();
#endregion
接下来,我们可以查询《周易》梅花易数,得到一个初步的断语:
#region 查询《周易》及《易传》 Looking it up in Zhouyi and Yizhuan
var storeUri = "https://yueyinqiu.github.io/my-yijing-annotation-stores/975345ca/2023-08-02-1.json";
using var client = new HttpClient();
var zhouyi = await client.GetFromJsonAsync<ZhouyiStore>(storeUri);
Debug.Assert(zhouyi is not null);
// 初始化 ZhouyiStore 。
// 这里是从网上下载了一个注解仓库,当然也可以使用本地文件之类。
// Initialize ZhouyiStore.
// Here the annotation store is downloaded from the internet.
// You can also load it from elsewhere such as from a local file.
var benguaInZhouyi = zhouyi.GetHexagram(bengua);
var dongyao = benguaInZhouyi.EnumerateYaos().ElementAt(dongyaoIndex - 1);
var bianguaInZhouyi = zhouyi.GetHexagram(biangua);
var huguaInZhouyi = zhouyi.GetHexagram(hugua);
Console.Write($"{benguaInZhouyi.Name}之{bianguaInZhouyi.Name},");
// Console.Write($"It's {benguaInZhouyi.Name} changing to {bianguaInZhouyi.Name}, ");
(ZhouyiTrigram huguaUpper, ZhouyiTrigram huguaLower) = huguaInZhouyi.SplitToTrigrams(zhouyi);
if (huguaUpper.Painting == huguaLower.Painting)
{
Console.WriteLine($"互重{huguaUpper.Name}。");
// Console.WriteLine($"and doubled {huguaUpper.Name} as the Hugua.");
}
else
{
Console.WriteLine($"互{huguaUpper.Name}{huguaLower.Name}。");
// Console.WriteLine($"and {huguaUpper.Name} with {huguaLower.Name} as the Hugua.");
}
Console.WriteLine($"易曰:{dongyao.YaoText}");
// Console.WriteLine($"Zhouyi: {dongyao.YaoText}");
Console.WriteLine($"象曰:{dongyao.Xiang}");
// Console.WriteLine($"And Xiang: {dongyao.Xiang}");
#endregion
再往后,应当再无程序化的行为可循,需要人自主进行决断。整个生成梅花时间卦的过程就到此为止了,可能的生成结果如下:
2023/08/31 11:46
癸卯年七月十六午时
本卦 BENGUA
-----
-- --
-----
-- --
-----
-----
互卦 HUGUA
-- --
-----
-- --
-----
-- --
-----
变卦 BIANGUA
-----
-- --
-- --
-- --
-----
-----
睽之损,互坎离。
易曰:睽孤遇元夫交孚厉无咎
象曰:交孚无咎志行也
以上内容,只是按照个人理解所就,水平有限,如有偏差,敬请斧正。
其中,关于梅花易数时间卦更详细的内容,可以参考其他一些文章,比如:
关于 可以参考官方文档《梅花易数》用C#写的一系列关于易学的包,遇到问题也欢迎在各项目的 中提出意见或建议,更欢迎大佬们一起参与到项目中。
关于此篇文章使用的代码,可以在这里找到:
此外,本文以及整个 项目的创作和发布梅花易数,只为弘扬传统文化,坚决拒绝封建迷信。