利用PYTHON实现科学占卜

一直以来,中式占卜都是基于算命先生手工实现,程序繁琐(往往需要沐浴、计算天时、静心等等流程)。准备工作复杂(通常需要铜钱等道具),计算方法复杂,需要纯手工计算二进制并转换为最终的卦象,为了解决这个问题,笔者基于python实现了一套科学算命工具,用于快速进行占卜。

本文的算命方式采用八卦 + 周易+ 梅花易数实现,脚本基于python3.9.0开发。本人对于周易五行研究较浅,如有疏漏请见谅。

最终效果如图所示。运行程序后,你会根据当前的运气(原来的卦、互卦、变心)自动获得你心中的卦像。

[En]

The final effect is as shown in the picture. After running the program, you will automatically get the hexagram image of what you have in mind according to the current luck (original hexagram, mutual hexagram, change of mind).

前置知识

基础原理

首先,我们需要了解一些最基本的占卜知识。目前,《中国》中的几种主流占卜方式,基本上都是建立在容易演变的基础上的。一般说来,不同的卦像是根据一定的现象得到的,不同的卦像最终会代表占卜的开始、发展和结果。

[En]

First of all, we need to understand some of the most basic knowledge of divination. At present, several mainstream ways of divination in China are basically based on easy evolution. Generally speaking, different hexagram images are obtained according to certain phenomena, and different hexagram images will eventually represent the beginning, development and result of divination.

太极生二器,二器生四象,四象生八卦

[En]

Taiji gives birth to two instruments, two instruments give birth to four images, and four images give birth to gossip

我相信你在很多影视作品中都听说过这句话,但很少有人知道它的真正含义,这句话实际上概括了卦像制作的过程。

[En]

I believe you have heard this sentence in many film and television works, but few people know its true meaning, this sentence actually summarizes the process of the production of hexagram images.

  • 太极:代表一种绝对混乱的状态,是一个哲学概念,如果非要应用我们的客观世界,它可以理解为大爆炸之前的宇宙状态,所有的物理定律都不起作用。在我们开始八卦之前,我们就处于这种状态。
    [En]

    Tai Chi: represents a state of absolute chaos, is a philosophical concept, if we have to apply our objective world, it can be understood as the state of the universe before the Big Bang, and all the laws of physics are not in effect. We are in this state before we start to gossip.*

  • 两仪:同样是一个哲学观念,代表一个事物的两个对立状态,套用到客观世界可以是”生-死”、”黑-白”、”清-浊”,在占卜的过程中,我们通常会有”阴-阳”两个状态,为了方便记录,古人发明了两个符号代表这两个状态,在占卜的时候,一个这样的状态我们称之为一爻(yao)

  • 四象:当我们结合阴阳时,我们可以得到四种不同的组合。古人称它们为四象。请注意,这里的四个图像也是哲学的。它可以是“蔚蓝龙、白虎、朱雀和黑乌龟”。也可以是东、南、西、东四个方向。在占卜中,我们通常把这四个意象称为“太阴”、“韶音”、“太阳”和“邵阳”。

    [En]

    four Images: when we combine Yin and Yang, we can get four different combinations. The ancients called them four Images. Note that the four Images here are also philosophical. It can be “Azure Dragon, White Tiger, Vermilion Bird and Black Tortoise”. It can also be the four directions of east, south, west and east. In divination, we usually call these four images “Taiyin”, “Shaoyin”, “sun” and “Shaoyang”.*

  • 八卦:四象加一易,也就是三阴三阳的组合,就是八个组合。古人认为,这八种组合可以代表自然界的八种事物(八种中学状态),即八卦。

    [En]

    gossip: when we add one Yi to the four images, that is, the combination of three yin and yang, we can get eight combinations. The ancients thought that these eight combinations could represent eight kinds of things in nature (eight middle school states), that is, gossip.*

当然,八种状态代表事物的方向是不够的,古人就把八卦(分开的称为儒卦)结合在一起,得到了64种不同的卦。《周易》中的六十四卦就是这样产生的。

[En]

Of course, it is not enough for eight states to represent the direction of things, so the ancients combined the eight diagrams (separate ones called Confucian diagrams) and got 64 different hexagrams. This is how the 64 hexagrams in the Book of changes came into being.

目前,国内主流的占卜基本上是通过不同的方式得到不同的卦,最终判断事物的走向。事实上,对于程序员来说,可以将这两个仪器视为具有0和1两种状态的一位二进制数。这四个大象是两位二进制数,有四种状态:000001j01j0pc11。八卦是一个三位二进制数,有四种状态:000,001,010,011,100,101,110,111。

[En]

At present, the domestic mainstream divination is basically through different ways to get different hexagrams, and finally judge the direction of things. In fact, for programmers, it is possible to treat the two instruments as an one-digit binary number with two states of 0 and 1. The four elephants are two-digit binary numbers, which have four states: 000001j01j0pc11. Gossip is a three-digit binary number with four states: 000, 001, 010, 011, 100, 101, 110, 111.

如何产生卦象

现在我们知道卦像是如何演变的,但我们还不能得到卦像的方法。事实上,在占卜的过程中,不同的占卜方式最大的区别就是占卜的方式不同。我们在这里用梅花开始卦。

[En]

Now we know how the hexagram image evolved, but we have not yet been able to get the way of the hexagram image. in fact, in the process of divination, the biggest difference between different ways of divination is that the way of divination is different. We use plum blossoms to start hexagrams here.

梅花很容易数卦(这里只截取两种卦)

[En]

Plum blossom is easy to count hexagram (here only two ways of hexagram are intercepted)

一、年月日时起卦
也就是说,农历年月日之和除以八,余数作为卦,年月日之和除以八,余数作为卦,年、月、日之和除以六,余数作为动图。

[En]

That is, the sum of the year, month and day of the lunar calendar is divided by eight, the remainder is used as the hexagram, the sum of the year, month and day is divided by eight, the remainder is used as the hexagram, and then the sum of years, months and days is divided by six, and the remainder is used as the moving figure.

例:农历四月十一日,卦始于零时:申请年号为9,时数为6。

[En]

Ex.: on April 11 of the Lunar New year, the hexagram begins at 00 o’clock: the number of the year of application is 9 and the number of hours is 6.

上卦为:(年+月+日)÷8,取余数。 即:(9+4+11)÷8,此处无余数。
下卦为:(年+月+日+时)÷8,取余数。 即:(9+4+11+6)÷8,余数为6为坎卦。
动爻数为:(年+月+日+时)÷6,取余数。 即:(9+4+11+6)除以6,此处无余数。
这个卦是这样的:上卦是坤,下卦是脊,动是上咒。

[En]

This hexagram is as follows: the upper hexagram is the Kun, the lower hexagram is the ridge, and the movement is the upper curse.

二、直接以数起卦
这是一种简单而准确的开始六卦的方法。当有人要求测试什么东西时,你可以让参观者随意说出两个数字,第一个数字是上卦,第二个数字是下卦,两个数字的和除以6,其余的是动态的,或者你可以用其他方式得到两个数字,比如翻书、日历等。

[En]

This is a simple and accurate way to start a hexagram. When someone asks to test something, you can ask the visitor to say two numbers at will, the first number is the upper hexagram, the second number is the lower hexagram, the sum of the two numbers is divided by 6, and the rest is dynamic, or you can use other ways to get two numbers, such as flipping books, calendars, and so on.

开发

我们会用容易数梅花的方式来总结这个过程,用程序员的话来说,过程如下。

[En]

We will be easy to count the plum blossom way to sum up the process in the words of programmers, the process is as follows.

获取一个随机数(我们在这里使用当前时间戳),将模8作为一个三位二进制数。

[En]

Get a random number (we use the current timestamp here) to take the module 8 as a three-digit binary number.

然后取一个随机数,取模数为8,作为吊数(三位二进制数)。

[En]

Then get a random number, take the module for eight, as hanging (a three-digit binary number).

将上述两个随机数组合得到一个六位二进制数。

[En]

The above two random numbers are combined to get a six-digit binary number.

将六个二进制数转换成十进制数,然后查表就可以得到这个六角图。

[En]

Convert six binary numbers to decimal numbers and look up the table to get this hexagram.

取一个随机数,对6取模,将上述六位二进制数对应位数的0变为1,1变为0,然后转化为十进制数并查表,得到变卦。
根据这个卦的表和变化的卦,得到占卜的结果。

[En]

According to the table of this hexagram and changing hexagram, the result of divination is obtained.

import json
import random
import time

#别挂配置数据
# 源代码,Python学习资料都在这个群啦 924040232
gua_data_path = "data.json"

#别卦数据
gua_data_map = {

}
fake_delay = 10

#读取别卦数据
def init_gua_data(json_path):
    with open(gua_data_path,'r',encoding='utf8')as fp:
        global gua_data_map
        gua_data_map = json.load(fp)
#爻图标映射
yao_icon_map = {
    0:"- -",
    1:"---"
}

#经卦名
base_gua_name_map = {
    0:"",1:"",2:"",3:"",4:"",5:"",6:"",7:""
}

#数字转化为二进制数组
def base_gua_to_yao(gua, yao_length=3):
    result = []
    while gua >= 1:
        level = 0 if gua % 2 == 0 else 1
        gua //= 2
        result.append(level)
    while len(result) < yao_length:
        result.append(0)
    return result

#二进制数组转化为数字
def base_yao_to_gua(array):
    array = array[:]
    while len(array) > 0 and array[-1] == 0:
        array.pop()
    result = 0
    for i in range(len(array)):
        if array[i] == 0:
            continue
        result += pow(2, i)

    return result

#打印一个挂
def print_gua(gua):
    yao_list = base_gua_to_yao(gua, 6)
    up_yao_list = yao_list[0:3]
    up = base_yao_to_gua(up_yao_list)

    print(yao_icon_map[up_yao_list[2]])
    print(yao_icon_map[up_yao_list[1]] + " " + base_gua_name_map[up])
    print(yao_icon_map[up_yao_list[0]])

    print("")

    down_yao_list = yao_list[3:6]
    down = base_yao_to_gua(down_yao_list)
    print(yao_icon_map[down_yao_list[2]])
    print(yao_icon_map[down_yao_list[1]] + " " + base_gua_name_map[down])
    print(yao_icon_map[down_yao_list[0]])

#使用梅花易数
def calculate_with_plum_flower():
    #起上卦
    print("使用梅花易数♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️♣️")
    print_a_wait_animation("卜上卦:", fake_delay)
    up_base_gua = int(round(time.time() * 1000)) % 8
    up_yao_array = base_gua_to_yao(up_base_gua)
    print("上卦获取成功,上卦为:", base_gua_name_map[up_base_gua])
    #起下卦
    print_a_wait_animation("正在获取下卦:", fake_delay)
    down_base_gua = random.randint(0, 999999999999) % 8
    down_yao_array = base_gua_to_yao(down_base_gua)
    print("上卦获取成功,下卦为:", base_gua_name_map[down_base_gua])
    #组成卦象
    print_a_wait_animation("正在组成本卦:", fake_delay)
    print("------------------------------------------------本卦------------------------------------------------")
    yao_list = up_yao_array + down_yao_array
    gua = base_yao_to_gua(yao_list)
    print_gua(gua)
    #读取本卦象信息
    gua_code = str(base_gua_name_map[up_base_gua]) + str(base_gua_name_map[down_base_gua])
    gua_data = gua_data_map[gua_code]
    print("本卦为:", gua_data['name'])
    print("辞:", gua_data['words'],"译:",gua_data['white_words'])
    print("象:", gua_data['picture'],"译:",gua_data['white_picture'])
    print_a_wait_animation("正在组成互卦:", fake_delay)
    print("------------------------------------------------互卦------------------------------------------------")
    #读取互卦象信息
    up_hu_yao_list = [yao_list[4],yao_list[5],yao_list[0]]
    up_hu_gua = base_yao_to_gua(up_hu_yao_list)
    down_hu_yao_list =[yao_list[5],yao_list[0],yao_list[1]]
    down_hu_gua = base_yao_to_gua(down_hu_yao_list)
    hu_yao_list = up_hu_yao_list + down_hu_yao_list
    hu_gua = base_yao_to_gua(hu_yao_list)
    hu_gua_code = str(base_gua_name_map[up_hu_gua]) + str(base_gua_name_map[down_hu_gua])
    hu_gua_data = gua_data_map[hu_gua_code]
    print_gua(hu_gua)
    print("互卦为:", hu_gua_data['name'])
    print("辞:", hu_gua_data['words'],"译:",hu_gua_data['white_words'])
    print("象:", hu_gua_data['picture'],"译:",hu_gua_data['white_picture'])
    print_a_wait_animation("正在组成变卦:", fake_delay)
    print("------------------------------------------------变卦------------------------------------------------")
    change_index = int(round(time.time() * 1000)) % 6
    change_yao_list = yao_list[:]
    change_yao_list[change_index] = 0 if change_yao_list[change_index] == 1 else 1
    up_change_yao_list = change_yao_list[0:3]
    up_change_gua = base_yao_to_gua(up_change_yao_list)
    down_change_yao_list =change_yao_list[3:5]
    down_change_gua = base_yao_to_gua(down_change_yao_list)

    change_gua = base_yao_to_gua(change_yao_list)
    print_gua(change_gua)
    change_gua_code = str(base_gua_name_map[up_change_gua]) + str(base_gua_name_map[down_change_gua])
    change_gua_data = gua_data_map[change_gua_code]
    print("变卦为:", change_gua_data['name'])
    print("辞:", change_gua_data['words'],"译:",change_gua_data['white_words'])
    print("象:", change_gua_data['picture'],"译:",change_gua_data['white_picture'])

def print_a_wait_animation(tips,times):
    animation = "|/-\\"
    idx = 0
    for i in range(times):
        print(tips + animation[idx % len(animation)],animation[idx % len(animation)],animation[idx % len(animation)],animation[idx % len(animation)],animation[idx % len(animation)], end="\r"),
        idx += 1
        time.sleep(0.1)

init_gua_data(gua_data_path)
calculate_with_plum_flower()

源代码私我

Original: https://www.cnblogs.com/hahaa/p/16119925.html
Author: 轻松学Python
Title: 利用PYTHON实现科学占卜

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/500050/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球