Scientific divination based on PYTHON

background

For a long time, Chinese divination is based on the manual implementation of fortune-tellers, and the procedures are cumbersome (often need to bathe, calculate the weather, meditate, etc.). The preparation work is complicated (usually requires copper coins and other props), and the calculation method is complicated. It needs to manually calculate the binary and convert it into the final hexagram. In order to solve this problem, the author has implemented a set of scientific fortune-telling tools based on python for rapid divination
. The fortune-telling method is implemented by gossip + Zhouyi + Plum Blossom Yishu, and the script is developed based on python3.9.0. My research on the Five Elements of Zhouyi is relatively shallow, please forgive me if there are any omissions. The
final effect is as shown in the figure. After running the program, it will automatically obtain the hexagrams of what you are thinking of according to the current fortune (this hexagram, mutual hexagram, and change hexagram).
insert image description here

Pre-knowledge

Fundamentals

First of all, we need to understand some basic divination knowledge. At present, several mainstream divination methods in my country are basically based on easy evolution. In general, different hexagrams are obtained according to certain phenomena, and different hexagrams will eventually represent the beginning, development and result of the divination.

Tai Chi gives birth to two instruments, two instruments give birth to four images, and four images give birth to gossip

I believe that everyone has heard of this sentence in many film and television works, but few people know its true meaning. This sentence actually summarizes the process of hexagram generation.

  • Tai Chi: It represents a state of absolute chaos. It is a philosophical concept. It must be applied to our objective world. It can be understood as the state of the universe before the big bang. All physical laws do not take effect. this state.

  • Liangyi: It is also a philosophical concept, representing the two opposite states of a thing. When applied to the objective world, it can be "life-death", "black-white", "clear-turbid". In the process of divination, we usually There will be two states of "yin and yang". For the convenience of recording, the ancients invented two symbols to represent these two states. When divination, we call one such state a yao (yao).
    two instruments

  • Four images: When we combine yin and yang in pairs, we can get four different combinations. The ancients called them four images. Note that the four images here are also philosophical. , it can also be the four directions of east, west, northwest. When divination, we usually use "Taiyin", "Shaoyin", "Sun" and "Shaoyang" to call these four signs
    four elephants

  • Eight Trigrams: When we add one line to the four images, that is, when three yin and yang are combined, we can get eight combinations. The ancients believed that these eight combinations can represent eight types of things in nature (the eight states) , that is, for gossip
    gossip
    . Of course, the eight states are not enough to represent the development direction of things, so the ancients combined gossip (individual gossip called Jinggua) in pairs, resulting in 64 different other trigrams, This is how the sixty-four hexagrams in the Book of Changes came into being.
    dry for heaven
    At present, the mainstream divination in China basically obtains different hexagrams through different numbers, and finally judges the direction of things. In fact, for programmers, two meters can be regarded as a one-digit binary number, with two states of 0 and 1. Sixiang is a two-digit binary number with four states of 00, 01, 10, and 11. Gossip is a three-digit binary number with four states: 000, 001, 010, 011, 100, 101, 110, and 111

How to generate hexagrams

Now we know how the hexagrams evolve, but we have not been able to get the way of the hexagrams. In fact, in the process of divination, the biggest difference between different divination methods is the different ways of starting the hexagrams. way of hexagrams

The Plum Blossom Easy Counting Gua Method (only two kinds of Gua methods are intercepted here)
1. Year, month, and date:
divide the sum of the year, month, and day of the lunar calendar by eight, and use the remainder as the number of hexagrams to find the upper hexagram; Divide the sum of the time by eight, use the remainder as the number of hexagrams to find the next hexagram, then divide the sum of the year, month, day and hour by six, and use the remainder as the moving line.
Example: The hexagram starts on the 11th day of the fourth month of the Renshen year in the lunar calendar: 9 counts in the Shen year and 6 counts at the si hour.
The previous hexagram is: (year + month + day) ÷ 8, take the remainder. That is: (9+4+11)÷8, there is no remainder here.
The next hexagram is: (year + month + day + hour) ÷ 8, take the remainder. That is: (9+4+11+6) ÷ 8, the remainder is 6, which is Kan Gua.
The number of moving lines is: (year + month + day + hour) ÷ 6, take the remainder. That is: (9+4+11+6) divided by 6, there is no remainder here.
This hexagram is: the upper hexagram is Kun, the lower hexagram is Kan, and the moving line is the upper line.
2. Counting the hexagrams directly
This is a simple and highly accurate method of starting the hexagrams. When someone asks to measure something, you can ask the person to say two numbers at will, the first number is taken as the upper hexagram, the second number is taken as the lower hexagram, the sum of the two numbers is divided by 6, and the remainder is the moving line. Or you can use other methods that can get two numbers to start hexagrams, such as flipping books, calendars, etc.

develop

Let's summarize the process of Plum Blossom Yishu in the words of programmers. The process is as follows

  1. Get a random number (we use the current timestamp here) modulo 8 and use it as a hang (a three-digit binary number)
  2. Get another random number, take the modulo of eight, and use it as a hang (a three-digit binary number)
  3. Combine the above two random numbers to get a six-digit binary number
  4. Convert the six-digit binary number into a decimal number and look up the table to get this hexagram
  5. Take a random number, take modulo 6, change the 0 of the corresponding digits of the above six-digit binary number to 1, and 1 to 0, then convert it into a decimal number and look up the table to get the change.
  6. Check the table according to the original hexagram and the changed hexagram to get the divination result
import json
import random
import time

#别挂配置数据
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()

source code

https://github.com/MrFengGG/ToChange/tree/master

Guess you like

Origin blog.csdn.net/qq_35488769/article/details/123996567
Recommended