• 六爻排盘神机


    选修课留了3000字的论文......确实,削微有那么一点小困难……
    但是,倘若我拿出已经占了6419个字符的 “六爻排盘神机” ,阁下…应该…不会…骂我吧


    且看,六爻排盘神机!

    1. import random
    2. import datetime
    3. from lunarcalendar import Converter, Solar
    4. def get_user_input_time():
    5. user_input = input("请输入起卦时间 (格式如2024.05.29-21:56): ")
    6. return user_input
    7. def parse_input(user_input):
    8. try:
    9. date_part, time_part = user_input.split('-')
    10. year, month, day = map(int, date_part.split('.'))
    11. hour, minute = map(int, time_part.split(':'))
    12. return datetime.datetime(year, month, day, hour, minute)
    13. except ValueError as e:
    14. print(f"输入格式无效: {e}")
    15. return None
    16. def get_chinese_hour(hour):
    17. chinese_hours = [
    18. "子时", "丑时", "丑时", "寅时", "寅时", "卯时", "卯时", "辰时", "辰时", "巳时", "巳时",
    19. "午时", "午时", "未时", "未时", "申时", "申时", "酉时", "酉时", "戌时", "戌时", "亥时", "亥时"
    20. ]
    21. return chinese_hours[hour]
    22. def convert_to_lunar(solar_date):
    23. try:
    24. solar = Solar(solar_date.year, solar_date.month, solar_date.day)
    25. lunar = Converter.Solar2Lunar(solar)
    26. return lunar
    27. except ValueError as e:
    28. print(f"转换为阴历时出错: {e}")
    29. return None
    30. def get_ganzhi_year(year):
    31. # 天干
    32. tiangan = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
    33. # 地支
    34. dizhi = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
    35. # 甲子年(公元4年)为基准
    36. base_year = 4
    37. diff_years = year - base_year
    38. gan = tiangan[diff_years % 10]
    39. zhi = dizhi[diff_years % 12]
    40. return f"{gan}{zhi}年"
    41. def num_to_chinese(num):
    42. chinese_nums = "零一二三四五六七八九"
    43. return "".join(chinese_nums[int(digit)] for digit in str(num))
    44. def get_chinese_day(day):
    45. chinese_days = [
    46. "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
    47. "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
    48. "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"
    49. ]
    50. return chinese_days[day - 1]
    51. def get_chinese_month(month, is_leap):
    52. chinese_months = [
    53. "正月", "二月", "三月", "四月", "五月", "六月",
    54. "七月", "八月", "九月", "十月", "冬月", "腊月"
    55. ]
    56. return ("闰" if is_leap else "") + chinese_months[month - 1]
    57. def print_lunar_date(lunar_date, solar_date):
    58. if lunar_date:
    59. chinese_hour = get_chinese_hour(solar_date.hour)
    60. chinese_year = get_ganzhi_year(lunar_date.year)
    61. chinese_month = get_chinese_month(lunar_date.month, lunar_date.isleap)
    62. chinese_day = get_chinese_day(lunar_date.day)
    63. print(f"\n起卦时间: {chinese_year} {chinese_month} {chinese_day} {chinese_hour}\n")
    64. def coin_toss():
    65. # 模拟投掷三枚硬币,0表示正面朝上(阴),1表示反面朝上(阳)
    66. tosses = [random.choice([0, 1]) for _ in range(3)] # 生成三次硬币投掷的结果
    67. # print("投掷结果:", tosses)
    68. return tosses.count(1) # 统计投掷结果中反面(阳面)朝上的硬币个数
    69. # 定义64个卦的名字
    70. hexagram_names = {
    71. '111111': '⚠乾为天',
    72. '111110': '天风姤',
    73. '111100': '天山遁',
    74. '111000': '天地否',
    75. '110000': '风地观',
    76. '100000': '山地剥',
    77. '101000': '火地晋',
    78. '101111': '火天大有',
    79. '011011': '⚠兑为泽',
    80. '011010': '泽水困',
    81. '011000': '泽地萃',
    82. '011100': '泽山咸',
    83. '010100': '水山蹇',
    84. '000100': '地山谦',
    85. '001100': '雷山小过',
    86. '001011': '雷泽归妹',
    87. '101101': '⚠离为火',
    88. '101100': '火山旅',
    89. '101110': '火风鼎',
    90. '101010': '火水未济',
    91. '100010': '山水蒙',
    92. '110010': '风水换',
    93. '111010': '天水讼',
    94. '111101': '天火同人',
    95. '001001': '⚠震为雷',
    96. '001000': '雷地豫',
    97. '001010': '雷水解',
    98. '001110': '雷风恒',
    99. '000110': '地风升',
    100. '010110': '水风井',
    101. '011110': '泽风大过',
    102. '011001': '泽雷随',
    103. '110110': '⚠巽为风',
    104. '110111': '风天小畜',
    105. '110101': '风火家人',
    106. '110001': '风雷益',
    107. '111001': '天雷无妄',
    108. '101001': '火雷噬嗑',
    109. '100001': '山雷颐',
    110. '100110': '山风蛊',
    111. '010010': '⚠坎为水',
    112. '010011': '水泽节',
    113. '010001': '水雷屯',
    114. '010101': '水火既济',
    115. '011101': '泽火革',
    116. '001101': '雷火丰',
    117. '000101': '地火明夷',
    118. '000010': '地水师',
    119. '100100': '⚠艮为山',
    120. '100101': '山火贲',
    121. '100111': '山天大畜',
    122. '100011': '山泽损',
    123. '101011': '火泽睽',
    124. '111011': '天泽履',
    125. '110011': '风泽中孚',
    126. '110100': '风山渐',
    127. '000000': '⚠坤为地',
    128. '000001': '地雷复',
    129. '000011': '地泽临',
    130. '000111': '地天泰',
    131. '001111': '雷天大壮',
    132. '011111': '泽天夬',
    133. '010111': '天水需',
    134. '010000': '水地比'
    135. }
    136. def get_hexagram_name(hexagram):
    137. hexagram_code = ''.join(['1' if '▄▄▄▄▄▄▄▄▄▄' in yao else '0' for yao in hexagram])
    138. # 翻转hexagram_code
    139. reversed_hexagram_code = hexagram_code[::-1]
    140. return hexagram_names.get(reversed_hexagram_code, '出错了!')
    141. def coin_toss():
    142. # 模拟投掷三枚硬币,0表示正面朝上(阴),1表示反面朝上(阳)
    143. tosses = [random.choice([0, 1]) for _ in range(3)] # 生成三次硬币投掷的结果
    144. return tosses.count(1) # 统计投掷结果中反面(阳面)朝上的硬币个数
    145. def interpret_toss(toss_result):
    146. # 解释投掷结果
    147. if toss_result == 1: # 1个阳面
    148. return '▄▄▄▄▄▄▄▄▄▄', False # 少阳,不变
    149. elif toss_result == 2: # 2个阳面 (1个阴面)
    150. return '▄▄▄▄ ▄▄▄▄', False # 少阴,不变
    151. elif toss_result == 0: # 0个阳面 (3个阴面)
    152. return '▄▄▄▄ ▄▄▄▄ x', True # 老阴,有变
    153. else: # toss_result == 3
    154. return '▄▄▄▄▄▄▄▄▄▄ x', True # 老阳,有变
    155. def generate_hexagram():
    156. hexagram = []
    157. mutations = []
    158. for i in range(6):
    159. toss_result = coin_toss()
    160. yao, is_mutation = interpret_toss(toss_result)
    161. hexagram.append(yao)
    162. if is_mutation:
    163. mutations.append((len(hexagram) - 1, yao))
    164. return hexagram, mutations
    165. def generate_mutations(hexagram, mutations):
    166. new_hexagram = hexagram[:]
    167. for yao_idx, yao in mutations:
    168. if yao == '▄▄▄▄ ▄▄▄▄ x': # 老阴变老阳
    169. new_hexagram[yao_idx] = '▄▄▄▄▄▄▄▄▄▄'
    170. elif yao == '▄▄▄▄▄▄▄▄▄▄ x': # 老阳变老阴
    171. new_hexagram[yao_idx] = '▄▄▄▄ ▄▄▄▄'
    172. return new_hexagram
    173. def print_hexagram(hexagram):
    174. yao_labels = ["六爻", "五爻", "四爻", "三爻", "二爻", "初爻"]
    175. for i, yao in enumerate(hexagram[::-1]):
    176. print(f"{yao_labels[i]}: {yao}")
    177. def main():
    178. print("-+"*10 + " 六爻排盘神机 " + "+-"*10+"\n")
    179. input("请写下占问事宜:")
    180. user_input_time = get_user_input_time()
    181. solar_date = parse_input(user_input_time)
    182. if solar_date:
    183. lunar_date = convert_to_lunar(solar_date)
    184. print_lunar_date(lunar_date, solar_date)
    185. hexagram, mutations = generate_hexagram()
    186. hexagram_name = get_hexagram_name(hexagram)
    187. print(f"主卦: {hexagram_name}")
    188. print_hexagram(hexagram)
    189. if mutations:
    190. new_hexagram = generate_mutations(hexagram, mutations)
    191. new_hexagram_name = get_hexagram_name(new_hexagram)
    192. print(f"\n变卦: {new_hexagram_name}")
    193. print_hexagram(new_hexagram)
    194. print("\n" + "-+" * 10 + " 六爻排盘神机 " + "+-" * 10)
    195. if __name__ == "__main__":
    196. main()

  • 相关阅读:
    在访问一个网页时弹出的浏览器窗口,如何用selenium 网页自动化解决?
    WebSocket实战之六心跳重连机制
    “飞桨+辨影相机”成为AI界的“预制菜”,工业AI质检落地更简单
    Python批量采集美女内容并把音频数据和画面内容合并保存
    Android 沉浸式状态栏
    接口测试,
    Android 协程 异常捕获 异常的传播特性
    双向电平转换电路
    数据特征选择 | Lasso特征选择(Python)
    CCPC威海 J(博弈),C(计算几何) CF1442A(差分,DP) CF847C
  • 原文地址:https://blog.csdn.net/2301_79933084/article/details/139841537