选修课留了3000字的论文......确实,削微有那么一点小困难……
但是,倘若我拿出已经占了6419个字符的 “六爻排盘神机” ,阁下…应该…不会…骂我吧
且看,六爻排盘神机!
- import random
- import datetime
- from lunarcalendar import Converter, Solar
-
-
- def get_user_input_time():
- user_input = input("请输入起卦时间 (格式如2024.05.29-21:56): ")
- return user_input
-
-
- def parse_input(user_input):
- try:
- date_part, time_part = user_input.split('-')
- year, month, day = map(int, date_part.split('.'))
- hour, minute = map(int, time_part.split(':'))
- return datetime.datetime(year, month, day, hour, minute)
- except ValueError as e:
- print(f"输入格式无效: {e}")
- return None
-
-
- def get_chinese_hour(hour):
- chinese_hours = [
- "子时", "丑时", "丑时", "寅时", "寅时", "卯时", "卯时", "辰时", "辰时", "巳时", "巳时",
- "午时", "午时", "未时", "未时", "申时", "申时", "酉时", "酉时", "戌时", "戌时", "亥时", "亥时"
- ]
- return chinese_hours[hour]
-
-
- def convert_to_lunar(solar_date):
- try:
- solar = Solar(solar_date.year, solar_date.month, solar_date.day)
- lunar = Converter.Solar2Lunar(solar)
- return lunar
- except ValueError as e:
- print(f"转换为阴历时出错: {e}")
- return None
-
-
- def get_ganzhi_year(year):
- # 天干
- tiangan = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
- # 地支
- dizhi = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
-
- # 甲子年(公元4年)为基准
- base_year = 4
-
- diff_years = year - base_year
- gan = tiangan[diff_years % 10]
- zhi = dizhi[diff_years % 12]
-
- return f"{gan}{zhi}年"
-
-
- def num_to_chinese(num):
- chinese_nums = "零一二三四五六七八九"
- return "".join(chinese_nums[int(digit)] for digit in str(num))
-
-
- def get_chinese_day(day):
- chinese_days = [
- "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
- "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
- "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"
- ]
- return chinese_days[day - 1]
-
-
- def get_chinese_month(month, is_leap):
- chinese_months = [
- "正月", "二月", "三月", "四月", "五月", "六月",
- "七月", "八月", "九月", "十月", "冬月", "腊月"
- ]
- return ("闰" if is_leap else "") + chinese_months[month - 1]
-
-
- def print_lunar_date(lunar_date, solar_date):
- if lunar_date:
- chinese_hour = get_chinese_hour(solar_date.hour)
- chinese_year = get_ganzhi_year(lunar_date.year)
- chinese_month = get_chinese_month(lunar_date.month, lunar_date.isleap)
- chinese_day = get_chinese_day(lunar_date.day)
- print(f"\n起卦时间: {chinese_year} {chinese_month} {chinese_day} {chinese_hour}\n")
-
- def coin_toss():
- # 模拟投掷三枚硬币,0表示正面朝上(阴),1表示反面朝上(阳)
- tosses = [random.choice([0, 1]) for _ in range(3)] # 生成三次硬币投掷的结果
- # print("投掷结果:", tosses)
- return tosses.count(1) # 统计投掷结果中反面(阳面)朝上的硬币个数
-
-
-
- # 定义64个卦的名字
- hexagram_names = {
- '111111': '⚠乾为天',
- '111110': '天风姤',
- '111100': '天山遁',
- '111000': '天地否',
- '110000': '风地观',
- '100000': '山地剥',
- '101000': '火地晋',
- '101111': '火天大有',
- '011011': '⚠兑为泽',
- '011010': '泽水困',
- '011000': '泽地萃',
- '011100': '泽山咸',
- '010100': '水山蹇',
- '000100': '地山谦',
- '001100': '雷山小过',
- '001011': '雷泽归妹',
- '101101': '⚠离为火',
- '101100': '火山旅',
- '101110': '火风鼎',
- '101010': '火水未济',
- '100010': '山水蒙',
- '110010': '风水换',
- '111010': '天水讼',
- '111101': '天火同人',
- '001001': '⚠震为雷',
- '001000': '雷地豫',
- '001010': '雷水解',
- '001110': '雷风恒',
- '000110': '地风升',
- '010110': '水风井',
- '011110': '泽风大过',
- '011001': '泽雷随',
- '110110': '⚠巽为风',
- '110111': '风天小畜',
- '110101': '风火家人',
- '110001': '风雷益',
- '111001': '天雷无妄',
- '101001': '火雷噬嗑',
- '100001': '山雷颐',
- '100110': '山风蛊',
- '010010': '⚠坎为水',
- '010011': '水泽节',
- '010001': '水雷屯',
- '010101': '水火既济',
- '011101': '泽火革',
- '001101': '雷火丰',
- '000101': '地火明夷',
- '000010': '地水师',
- '100100': '⚠艮为山',
- '100101': '山火贲',
- '100111': '山天大畜',
- '100011': '山泽损',
- '101011': '火泽睽',
- '111011': '天泽履',
- '110011': '风泽中孚',
- '110100': '风山渐',
- '000000': '⚠坤为地',
- '000001': '地雷复',
- '000011': '地泽临',
- '000111': '地天泰',
- '001111': '雷天大壮',
- '011111': '泽天夬',
- '010111': '天水需',
- '010000': '水地比'
- }
-
-
- def get_hexagram_name(hexagram):
- hexagram_code = ''.join(['1' if '▄▄▄▄▄▄▄▄▄▄' in yao else '0' for yao in hexagram])
-
- # 翻转hexagram_code
- reversed_hexagram_code = hexagram_code[::-1]
-
- return hexagram_names.get(reversed_hexagram_code, '出错了!')
-
-
- def coin_toss():
- # 模拟投掷三枚硬币,0表示正面朝上(阴),1表示反面朝上(阳)
- tosses = [random.choice([0, 1]) for _ in range(3)] # 生成三次硬币投掷的结果
- return tosses.count(1) # 统计投掷结果中反面(阳面)朝上的硬币个数
-
- def interpret_toss(toss_result):
- # 解释投掷结果
- if toss_result == 1: # 1个阳面
- return '▄▄▄▄▄▄▄▄▄▄', False # 少阳,不变
- elif toss_result == 2: # 2个阳面 (1个阴面)
- return '▄▄▄▄ ▄▄▄▄', False # 少阴,不变
- elif toss_result == 0: # 0个阳面 (3个阴面)
- return '▄▄▄▄ ▄▄▄▄ x', True # 老阴,有变
- else: # toss_result == 3
- return '▄▄▄▄▄▄▄▄▄▄ x', True # 老阳,有变
-
- def generate_hexagram():
- hexagram = []
- mutations = []
- for i in range(6):
- toss_result = coin_toss()
- yao, is_mutation = interpret_toss(toss_result)
- hexagram.append(yao)
- if is_mutation:
- mutations.append((len(hexagram) - 1, yao))
- return hexagram, mutations
-
- def generate_mutations(hexagram, mutations):
- new_hexagram = hexagram[:]
- for yao_idx, yao in mutations:
- if yao == '▄▄▄▄ ▄▄▄▄ x': # 老阴变老阳
- new_hexagram[yao_idx] = '▄▄▄▄▄▄▄▄▄▄'
- elif yao == '▄▄▄▄▄▄▄▄▄▄ x': # 老阳变老阴
- new_hexagram[yao_idx] = '▄▄▄▄ ▄▄▄▄'
- return new_hexagram
-
- def print_hexagram(hexagram):
- yao_labels = ["六爻", "五爻", "四爻", "三爻", "二爻", "初爻"]
- for i, yao in enumerate(hexagram[::-1]):
- print(f"{yao_labels[i]}: {yao}")
-
-
- def main():
- print("-+"*10 + " 六爻排盘神机 " + "+-"*10+"\n")
- input("请写下占问事宜:")
- user_input_time = get_user_input_time()
- solar_date = parse_input(user_input_time)
- if solar_date:
- lunar_date = convert_to_lunar(solar_date)
- print_lunar_date(lunar_date, solar_date)
-
- hexagram, mutations = generate_hexagram()
-
- hexagram_name = get_hexagram_name(hexagram)
- print(f"主卦: {hexagram_name}")
- print_hexagram(hexagram)
-
- if mutations:
- new_hexagram = generate_mutations(hexagram, mutations)
- new_hexagram_name = get_hexagram_name(new_hexagram)
- print(f"\n变卦: {new_hexagram_name}")
- print_hexagram(new_hexagram)
-
- print("\n" + "-+" * 10 + " 六爻排盘神机 " + "+-" * 10)
-
-
- if __name__ == "__main__":
- main()