前言

梅花易数是中国传统预测术数,可以从声音、方位、时间、动静、地理、天时、人物、颜色、动植物等自然界或人类社会中的一切感知事物异相,作为预测其发展趋势的方法。作者已无从追究,但由宋朝易学家邵康节先生的观梅占后得名为梅花易数。

爻与卦:

《易经》:“易有太极,始生两仪,两仪生四象,四象生八卦。世间万事万物都存在阴阳两种属性,用符号表示则称“—”为阳爻、称“- -”为阴爻,而卦则由三个爻组成,例如乾卦☰由三个阳爻组成,坤卦☷由三个阴爻组成,离卦☲由两个阳爻和一个阴爻组成,两个爻这样组合一共能生成八个卦,称作八卦。八卦又分为先天八卦和后天八卦,梅花是以先天八卦的卦序起卦(先天八卦:乾一、兑二、离三、震四、巽五、坎六、艮七、坤八) 先天八卦图:

本卦互卦变卦:

本卦是最先得到的卦由上卦和下卦组成,两个卦上下组合共六个爻,从下到上分别为初爻,二爻,三爻,四爻,五爻,上爻 互卦是由本卦的三四五爻组合作为互卦的上卦,二三四爻组合作为互卦的下卦 变卦是得到动爻后,本卦该爻阴变阳,阳变阴后得到的卦 例:

起卦法

梅花易数起卦的方法很多,主要分为两大类,先天卦和后天卦。后天卦一般以目之所见,耳之所闻加之时辰直接成卦,而先天卦则需要取数或拆字笔画数成卦,以得到的数字按先天八卦的卦序取卦得到本卦以及变卦,平时取数排盘相对繁琐尤其是古法时间排盘,于是打算用python实现常用的三种起卦方式,下面是三种起卦方式原理:

古法时间起卦

以农历之年月日总和除以八,以余数为卦数求上卦;以年月日时总和除以八,以余数为卦数求下卦,再以年月日时总和除以六,以余数为动爻。

例:当前是农历癸卯年正月甘四日戌时起卦:卯年4数,戌时11数。

上卦为:(年+月+日)÷8,取余数。 即:(4+1+24)÷8,余数5为巽卦。

下卦为:(年+月+日+时)÷8,取余数。 即:(4+1+24+11)÷8 此处无余数为坤卦。

动爻数为:(年+月+日+时)÷6,取余数。 即:(4+1+24+11)除以6,余数为4,4爻动。

得卦为:风地观之天地否

两位数起卦

报数起卦多用于群友射覆,原理是以第一个数对8取余的结果作为上卦,第二个数对8取余的结果作为下卦,时辰对6取余作为变爻 例:当前是农历癸卯年正月甘四日戌时起卦,报数92 上卦为:9%8=1 取乾卦 下卦为:2%8=2 取兑卦 动爻数为:11%6=5  5爻动 得卦为:天泽履之火泽睽

三位数起卦

三位数起卦原理于两位数相似,但由于是奇数,要遵循“天轻地重”的原则取数,第一个数对8取余作为上卦,第二三个数相加对8取余作为下卦,三位数相加对6取余作为动爻 例:当前是农历癸卯年正月甘四日戌时起卦,报数369 上卦为:3%8=3 取离卦 下卦为:(6+9)%8=7 取艮卦 动爻数为:(3+6+9)%6=0  6爻动 得卦为:火山旅之雷山小过

代码编写思路

首先要将数字和卦联系起来起来的,用1表示阳爻,0表示阴爻,一个列表代表一个卦,例如[1,1,1]代表乾卦,[1,0,1]代表离卦,再将符号和八卦名称对应起来

BaGua = ([1, 1, 1],[1, 1, 0],[1, 0, 1],[1, 0, 0],[0, 1, 1],[0, 1, 0],[0, 0, 1],[0, 0, 0]) ZhongWen = (‘☰乾’, ‘☱兑’, ‘☲离’, ‘☳震’, ‘☴巽’, ‘☵坎’, ‘☶艮’, ‘☷坤’)

然后我们写一个对上下卦进行处理的函数,首先根据传来的两个数字得到对应卦的列表,然后合并通过切片组合成互卦

bengua=[] num_one = num_one - 1 num_two = num_two - 1 #确定上下卦开始 bengua_shang =BaGua[num_one] bengua_xia =BaGua[num_two] #确定上下卦结束 hugua_xia = bengua_xia + bengua_shang #合并两个列表 hugua_xia = hugua_xia[1:4] hugua_shang= bengua_xia + bengua_shang hugua_shang = hugua_shang[2:5] # hugua_xia[1:4]下互卦,hugua_shang[2:5]为上互卦 BenGuaShangGua = bengua_shang BenGuaXiaGua = bengua_xia HuGuaShangGua = hugua_shang HuGuaXiaGua = hugua_xia

这样得到了本卦和互卦,再去取变卦,变卦无非是某一爻0和1的转换,但要注意的是传过来的大于3数且小于6的数要定位到上卦对应的列表,如果为0的话则能确定是最上面的爻 至于如何获取时间起卦时的地支年份以及12小时制的时辰,此处用python不难获取现代时间,重点在于干支纪年法取数的年份以及古代12小时制的时辰取数,定义一个函数将24小时转化为地支对应的时辰,并导入sxtwl模块将时间转化为农历减去一个甲子年并对12取余就得到了地支年份。

#时间转换 now = str(datetime.datetime.today()) zhdate = sxtwl.fromSolar(datetime.datetime.now().year,datetime.datetime.now().month,datetime.datetime.now().day) year = zhdate.getLunarYear() month = zhdate.getLunarMonth() day = zhdate.getLunarDay()time = int(now[11:13]) #天干地支计算 re_biangua=[] #print(num_three) biangua_shang = bengua_shang biangua_xia = bengua_xia if 4 <= num_three <= 5: num_three = num_three - 4 if biangua_shang[num_three] == 0: biangua_shang[num_three] = 1 else: biangua_shang[num_three] = 0 elif num_three == 0: num_three = num_three + 2 if biangua_shang[num_three] == 0: biangua_shang[num_three] = 1 else: biangua_shang[num_three] = 0 else: num_three = num_three - 1 if biangua_xia[num_three] == 0: biangua_xia[num_three] = 1 else: biangua_xia[num_three] = 0 def format_time(): tiangan = (year - 1984 ) % 10 dizhi = (year - 2019 ) % 12 if time >= 23 or time < 1: hour = 1 elif 1 <= time < 3: hour = 2 elif 3 <= time < 5: hour = 3 elif 5 <= time < 7: hour = 4 elif 7 <= time < 9: hour = 5 elif 9 <= time < 11: hour = 6 elif 11 <= time < 13: hour = 7 elif 13 <= time < 15: hour = 8 elif 15 <= time < 17: hour = 9 elif 17 <= time < 19: hour = 10 elif 19 <= time < 21: hour = 11 else: hour = 12 return tiangan,dizhi,hour

这里的减2019其实不是太恰当因为2019年以前的时间就没法起卦了,不过很少有人用梅花去测过去的事情,所以无伤大雅。那么到此为止核心内容就已经写完了,后面还需要封装一些函数以及三种起卦方法的判断,文章末尾有完整代码,感兴趣的可自行对接qq微信机器人等。这里先用命令行演示一下: 两位数起卦: 三位数起卦: 时间起卦:   完整代码:

import sys import copy import datetime import sxtwl BaGua = ([1, 1, 1],[1, 1, 0],[1, 0, 1],[1, 0, 0],[0, 1, 1],[0, 1, 0],[0, 0, 1],[0, 0, 0]) ZhongWen = (‘☰乾’, ‘☱兑’, ‘☲离’, ‘☳震’, ‘☴巽’, ‘☵坎’, ‘☶艮’, ‘☷坤’) # 获取当前时间 now = str(datetime.datetime.today()) zhdate = sxtwl.fromSolar(datetime.datetime.now().year,datetime.datetime.now().month,datetime.datetime.now().day) year = zhdate.getLunarYear() month = zhdate.getLunarMonth() day = zhdate.getLunarDay() #year = int(now[:4]) #month = int(now[5:7]) #day = int(now[8:10]) time = int(now[11:13]) #求上下卦和互卦 def Num_One(self, num_one, num_two): bengua=[] num_one = num_one - 1 num_two = num_two - 1 #确定上下卦开始 bengua_shang =BaGua[num_one] bengua_xia =BaGua[num_two] #确定上下卦结束 hugua_xia = bengua_xia + bengua_shang #合并两个列表 hugua_xia = hugua_xia[1:4] hugua_shang= bengua_xia + bengua_shang hugua_shang = hugua_shang[2:5] # hugua_xia[1:4]下互卦,hugua_shang[2:5]为上互卦 BenGuaShangGua = bengua_shang BenGuaXiaGua = bengua_xia HuGuaShangGua = hugua_shang HuGuaXiaGua = hugua_xia bengua.append(BenGuaShangGua) bengua.append(BenGuaXiaGua) bengua.append(HuGuaShangGua) bengua.append(HuGuaXiaGua) return bengua #求变卦 def Bian(self,num_three,bengua_shang,bengua_xia): re_biangua=[] #print(num_three) biangua_shang = bengua_shang biangua_xia = bengua_xia if 4 <= num_three <= 5: num_three = num_three - 4 if biangua_shang[num_three] == 0: biangua_shang[num_three] = 1 else: biangua_shang[num_three] = 0 elif num_three == 0: num_three = num_three + 2 if biangua_shang[num_three] == 0: biangua_shang[num_three] = 1 else: biangua_shang[num_three] = 0 else: num_three = num_three - 1 if biangua_xia[num_three] == 0: biangua_xia[num_three] = 1 else: biangua_xia[num_three] = 0 re_biangua.append(biangua_shang) re_biangua.append(biangua_xia) return re_biangua #分离三位数 def get_nums(num): arr = [] arr.append(int(num)//100) arr.append(int(num)//10%10) arr.append(int(num)%10) return arr #分离两位数 def get_nums_two(num): arr = [] arr.append(int(num)//10) arr.append(int(num)%10) return arr # 天干,地支,时辰 def format_time(): tiangan = (year - 1984 ) % 10 dizhi = (year - 2019 ) % 12 if time >= 23 or time < 1: hour = 1 elif 1 <= time < 3: hour = 2 elif 3 <= time < 5: hour = 3 elif 5 <= time < 7: hour = 4 elif 7 <= time < 9: hour = 5 elif 9 <= time < 11: hour = 6 elif 11 <= time < 13: hour = 7 elif 13 <= time < 15: hour = 8 elif 15 <= time < 17: hour = 9 elif 17 <= time < 19: hour = 10 elif 19 <= time < 21: hour = 11 else: hour = 12 return tiangan,dizhi,hour # 根据时间获取卦 def time_to_gua(): tiangan,dizhi,hour=format_time() time_gua = [] #print(hour) up = (dizhi + month + day) % 8 if (dizhi + month + day) % 8 else 8 down = (dizhi + month + day + hour) % 8 if (dizhi + month + day + hour) % 8 else 8 move = (dizhi + month + day + hour) % 6 if (dizhi + month + day + hour) % 6 else 0 time_gua.append(up) time_gua.append(down) time_gua.append(move) return time_gua # 时间起卦 def time_gua(): time_gua=time_to_gua() array_ben=Num_One(Num_One,int(time_gua[0]),int(time_gua[1])) Ben_up=array_ben[0] Ben_down=array_ben[1] Hu_up=array_ben[2] Hu_down=array_ben[3] num_bian=time_gua[2] #print(num_bian) bengua_shang=copy.deepcopy(Ben_up) bengua_down=copy.deepcopy(Ben_down) array_bian=Bian(Bian,num_bian,bengua_shang,bengua_down) Bian_up=array_bian[0] Bian_down=array_bian[1] print(“本卦”+’ ‘+”互卦”+’ ‘+”变卦”) print(ZhongWen[BaGua.index(Ben_up)]+’ ‘+ZhongWen[BaGua.index(Hu_up)]+’ ‘+ZhongWen[BaGua.index(Bian_up)]) print(‘\n’) print(ZhongWen[BaGua.index(Ben_down)]+’ ‘+ZhongWen[BaGua.index(Hu_down)]+’           ‘+ZhongWen[BaGua.index(Bian_down)]) #三位数起卦 def baoshuqigua(num): array_num=get_nums(num) num_up=array_num[0] % 8 num_down=(array_num[1]+array_num[2]) % 8 num_bian=(array_num[0]+array_num[1]+array_num[2]) % 6 array_ben=Num_One(Num_One,num_up,num_down) Ben_up=array_ben[0] Ben_down=array_ben[1] Hu_up=array_ben[2] Hu_down=array_ben[3] bengua_shang=copy.deepcopy(Ben_up) bengua_down=copy.deepcopy(Ben_down) array_bian=Bian(Bian,num_bian,bengua_shang,bengua_down) Bian_up=array_bian[0] Bian_down=array_bian[1] print(“本卦”+’ ‘+”互卦”+’ ‘+”变卦”) print(ZhongWen[BaGua.index(Ben_up)]+’ ‘+ZhongWen[BaGua.index(Hu_up)]+’ ‘+ZhongWen[BaGua.index(Bian_up)]) print(‘\n’) print(ZhongWen[BaGua.index(Ben_down)]+’ ‘+ZhongWen[BaGua.index(Hu_down)]+’ ‘+ZhongWen[BaGua.index(Bian_down)]) #两数加时辰起卦 def liangshu(num): tiangan,dizhi,hour=format_time() array_num=get_nums_two(num) num_up=array_num[0] % 8 num_down=array_num[1] % 8 num_bian=hour % 6 array_ben=Num_One(Num_One,num_up,num_down) Ben_up=array_ben[0] Ben_down=array_ben[1] Hu_up=array_ben[2] Hu_down=array_ben[3] bengua_shang=copy.deepcopy(Ben_up) bengua_down=copy.deepcopy(Ben_down) array_bian=Bian(Bian,num_bian,bengua_shang,bengua_down) Bian_up=array_bian[0] Bian_down=array_bian[1] print(“本卦”+’ ‘+”互卦”+’ ‘+”变卦”) print(ZhongWen[BaGua.index(Ben_up)]+’ ‘+ZhongWen[BaGua.index(Hu_up)]+’ ‘+ZhongWen[BaGua.index(Bian_up)]) print(‘\n’) print(ZhongWen[BaGua.index(Ben_down)]+’ ‘+ZhongWen[BaGua.index(Hu_down)]+’ ‘+ZhongWen[BaGua.index(Bian_down)]) #判断起卦方式 if str(sys.argv[1]) == “time”: print(‘时间起卦’) time_gua() elif str(sys.argv[1]).isnumeric() and len(sys.argv[1]) == 3: print(‘三位数起卦’) num=sys.argv[1] baoshuqigua(num) elif str(sys.argv[1]).isnumeric() and len(sys.argv[1]) == 2: print(‘两位数起卦’) num=sys.argv[1] liangshu(num) else: print(‘暂不支持此种起卦方式!’)