#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#author:Wos
#阴阳八卦的计算脚本
#给出本卦和动爻后,脚本会计算出 互卦,变卦,综卦,错卦
import readline
import os
import sys
basic8 = ['天天','地地','水水','火火','雷雷','风风','泽泽','山山']
gx = {
        '天天':'乾为天',
        '地地':'坤为地',
        '水雷':'屯',
        '山水':'蒙',
        '水天':'需',
        '天水':'讼',
        '地水':'师',
        '水地':'比',
        '风天':'小蓄',
        '天泽':'履',
        '地天':'泰',
        '天地':'否',
        '天火':'同人',
        '火天':'大有',
        '地山':'谦',
        '雷地':'豫',
        '泽雷':'随',
        '山风':'蛊',
        '地泽':'临',
        '风地':'观',
        '火雷':'噬嗑',
        '山火':'贲',
        '山地':'剥',
        '地雷':'复',
        '天雷':'无妄',
        '山天':'大蓄',
        '山雷':'颐',
        '泽风':'大过',
        '水水':'坎为水',
        '火火':'离为火',
        '泽山':'咸',
        '雷风':'恒',
        '天山':'遁',
        '雷天':'大壮',
        '火地':'晋',
        '地火':'明夷',
        '风火':'家人',
        '火泽':'睽',
        '水山':'蹇',
        '雷水':'解',
        '山泽':'损',
        '风雷':'益',
        '泽天':'夬',
        '天风':'姤',
        '泽地':'萃',
        '地风':'升',
        '泽水':'困',
        '水风':'井',
        '泽火':'革',
        '火风':'鼎',
        '雷雷':'震为雷',
        '山山':'艮为山',
        '山风':'渐',
        '雷泽':'归妹',
        '雷火':'丰',
        '火山':'旅',
        '风风':'巽为风',
        '泽泽':'兑为泽',
        '风水':'涣',
        '水泽':'节',
        '风泽':'中孚',
        '雷山':'小过',
        '水火':'既济',
        '火水':'未济'
     }

if __name__ == "__main__":
    #从左到右,数字顺序为由上到下,按照读卦的顺序,0为阴,1为阳
    #     天     泽    火    雷     风    水    山   地     
    a = {'天':'111','水':'010','山':'100','雷':'001','风':'110','火':'101','地':'000','泽':'011'}
    b = {}
    gx_k = []
    gx_v = []
    for k,v in a.items():
        for k2,v2 in a.items():
            b[str(k)+str(k2)]=str(v)+str(v2)
    for k,v in gx.items():
        gx_k.append(k)
        gx_v.append(v)
    while True:
        os.system('clear')
        print("八个基本卦名称略有不同:比如天天==乾为天,风风==巽为风")
        print("如果不输入动爻,则变卦不显示,动爻编号1~6,1为初爻,6为上爻")            
        try:
            s = input("输入卦名和动爻(eg.地山 2):")
        except KeyboardInterrupt:
            os.system('clear')
            sys.exit()
        bg = "" #本卦
        bgn = "" #本卦名称
        for k,v in b.items():
            if str(s).split(" ")[0].strip() == str(k):
                bg = str(v)
                bgn = str(k)
                for k2,v2 in gx.items():
                    if str(k) == str(k2):
                        if str(k) not in basic8:
                            print("\n本卦(目前的情况):"+str(k2)+str(v2))
                        else:
                            print("\n本卦(目前的情况):"+str(v2))
        bg_ = list(bg)
        try:
            if str(s).split(" ")[-1].strip() != "":
                dy = int(str(s).split(" ")[-1])
                if int(dy) > 0 and int(dy) < 7:
                    if bg_[6-int(dy)] == '1':
                        bg_[6-int(dy)] = '0'
                    else:
                        bg_[6-int(dy)] = '1'
                    bg__ = ''.join(bg_)
                    for k,v in b.items():
                        if str(bg__) == str(v):
                            for k2,v2 in gx.items():
                                if str(k) == str(k2):
                                    if str(k) not in basic8:
                                        print("\n变卦(结果):"+str(k2)+str(v2))
                                    else:
                                        print("\n变卦(结果):"+str(v2))
        except:
            pass
        print("\n序卦(3个):",end="")
        for k in gx_k:
            if str(k) == str(bgn):
                id_ = gx_k.index(k)+1
                if int(id_)+3 > 64:
                    gx_v_ = gx_v[id_:]+gx_v[0:(3-len(gx_v[id_:]))]
                else:
                    gx_v_ = gx_v[id_:id_+3]
                for i in gx_v_:
                    for k2,v2 in gx.items():
                        if str(i) == str(v2):
                            if str(k2) not in basic8:
                                print(str(k2)+str(v2),end=",")
                            else:
                                print(str(v2),end=",")

        hg = str(bg[1:4])+str(bg[2:5])
        for k,v in b.items():
            if str(hg) == str(v):
                for k2,v2 in gx.items():
                    if str(k) == str(k2):
                        if str(k) not in basic8:
                            print("\n\n互卦(过程):"+str(k2)+str(v2))
                        else:
                            print("\n\n互卦(过程):"+str(v2))
        zg = bg[::-1]
        for k,v in b.items():
            if str(zg) == str(v): 
                for k2,v2 in gx.items():
                    if str(k) == str(k2):
                        if str(k) not in basic8:
                            print("\n综卦(不同角度看):"+str(k2)+str(v2))
                        else:
                            print("\n综卦(不同角度看):"+str(v2))

        cg = ""
        for i in str(bg):
            if str(i) == '1':
                cg += '0'
            else:
                cg += '1'
        for k,v in b.items():
            if str(cg) == str(v):
                for k2,v2 in gx.items():
                    if str(k) == str(k2):
                        if str(k) not in basic8:
                            print("\n错卦(相反的角度):"+str(k2)+str(v2))
                        else:
                            print("\n错卦(相反的角度):"+str(v2))

        s = input("\n按任意键继续!")