# -*- coding: utf-8 -*-
"""
Book of Changes (I Ching)
"""
from random import choice, randint
thetrigrams = {
7:("☰","天","qián","Heaven"),
6:("☱","兌","duì","Lake"),
5:("☲","離","lí","Fire"),
4:("☳","震","zhèn","Thunder"),
3:("☴","巽","duì","Wind"),
2:("☵","坎","kǎn","Water"),
1:("☶","艮","gèn","Mountain"),
0:("☷","坤","kūn","Earth")}
"""
The King Wen Sequence of Hexagrams
(trigram, trigram):(index,UCS4 code point, Chinese name, translation)
http://www.unicode.org/charts/PDF/Unicode-4.0/U40-4DC0.pdf
Unicode
In Unicode, monograms cover code points U+268A to U+268B,
digrams cover code points U+268C to U+268F,
trigrams cover code points U+2630 to U+2637,
hexagram symbols cover code points U+4DC0 to U+4DFF
(19904 – 19967).
Tai Xuan Jing(太玄) digrams cover code points
U+1D301 to U+1D305, tetragrams cover code points
U+1D306 to U+1D356.
The monograms cover code points U+1D300 (earth),
U+268A (yang), U+268B (yin).
"""
thehexagrams = {
("☰","☰"):(1,'4DC0',"qián","乾","Creative Power"),
("☷","☷"):(2,'4DC1',"kūn","坤","Natural Response"),
("☵","☳"):(3,'4DC2',"chún","屯","Difficult Beginnings"),
("☶","☵"):(4,'4DC3',"meng2","蒙","Inexperience"),
("☵","☰"):(5,'4DC4',"xu1","需","Calculated Waiting"),
("☰","☵"):(6,'4DC5',"song4","訟","Conflict"),
("☷","☵"):(7,'4DC6',"shil","師","Collective Force"),
("☵","☷"):(8,'4DC7',"bi3","比","Unity"),
("☴","☰"):(9,'4DC8',"xiao3 chu4","小畜","Restrained"),
("☰","☱"):(10,'4DC9',"lu3","履","Conduct"),
("☷","☰"):(11,'4DCA',"tai4","泰","Prospering"),
("☰","☷"):(12,'4DCB',"pi3","否","Stagnation"),
("☰","☲"):(13,'4DCC',"tong2 ren2","同人","Community"),
("☲","☰"):(14,'4DCD',"da4 you3","大有","Sovereignty"),
("☷","☶"):(15,'4DCE',"qianl","謙","Moderation"),
("☳","☷"):(16,'4DCF',"yu4","豫","Harmonize"),
("☱","☳"):(17,'4DD0',"sui2","隨","Adapting"),
("☶","☴"):(18,'4DD1',"gu3","蠱","Repair"),
("☷","☱"):(19,'4DD2',"lin2","臨","Promotion"),
("☴","☷"):(20,'4DD3',"guanl","觀","Contemplating"),
("☲","☳"):(21,'4DD4',"shi4 ke4","噬嗑","Reform"),
("☶","☲"):(22,'4DD5',"bi4","賁","Grace"),
("☶","☷"):(23,'4DD6',"bol","剝","Deterioration"),
("☷","☳"):(24,'4DD7',"fù","復","Returning"),
("☰","☳"):(25,'4DD8',"wu2 wang4","無妄","Innocence"),
("☶","☰"):(26,'4DD9',"da4 chu4","大畜","Potential Energy"),
("☶","☳"):(27,'4DDA',"yi2","頤","Nourishing"),
("☱","☴"):(28,'4DDB',"da4 guo4","大過","Critical Mass"),
("☵","☵"):(29,'4DDC',"kan3","坎","Danger"),
("☲","☲"):(30,'4DDD',"li2","離","Synergy"),
("☱","☶"):(31,'4DDE',"xian2","咸","Attraction"),
("☳","☴"):(32,'4DDF',"heng2","恆","Continuing"),
("☰","☶"):(33,'4DE0',"dun4","遯","Retreat"),
("☳","☰"):(34,'4DE1',"da4 zhuang4","大壯","Great Power"),
("☲","☷"):(35,'4DE2',"jin4","晉","Progress"),
("☷","☲"):(36,'4DE3',"míng yí","明夷","Censorship"),
("☴","☲"):(37,'4DE4',"jial ren2","家人","Family"),
("☲","☱"):(38,'4DE5',"kui2","睽","Contradiction"),
("☵","☶"):(39,'4DE6',"jian3","蹇","Obstacles"),
("☳","☵"):(40,'4DE7',"xie4","解","Liberation"),
("☶","☱"):(41,'4DE8',"sun3","損","Decline"),
("☴","☳"):(42,'4DE9',"yi4","益","Benefit"),
("☱","☰"):(43,'4DEA',"guai4","夬","Resolution"),
("☰","☴"):(44,'4DEB',"gou4","姤","Temptation"),
("☱","☷"):(45,'4DEC',"cui4","萃","Assembling"),
("☷","☴"):(46,'4DED',"sheng1","升","Advancement"),
("☱","☵"):(47,'4DEE',"kun4","困","Adversity"),
("☵","☴"):(48,'4DEF',"jing3","井","The Source"),
("☱","☲"):(49,'4DF0',"ge2","革","Revolution"),
("☲","☴"):(50,'4DF1',"ding3","鼎","Cosmic Order"),
("☳","☳"):(51,'4DF2',"zhen4","震","Shocking"),
("☶","☶"):(52,'4DF3',"gèn","艮","Meditation"),
("☴","☶"):(53,'4DF4',"jian4","漸","Developing"),
("☳","☱"):(54,'4DF5',"gui1 mei4","歸妹","Subordinate"),
("☳","☲"):(55,'4DF6',"feng1","豐","Zenith"),
("☲","☶"):(56,'4DF7',"lu3","旅","Traveling"),
("☴","☴"):(57,'4DF8',"xun4","巽","Penetrating Influence"),
("☱","☱"):(58,'4DF9',"dui4","兌","Encouraging"),
("☴","☵"):(59,'4DFA',"huan4","渙","Reuniting"),
("☵","☱"):(60,'4DFB',"jie2","節","Limitations"),
("☴","☱"):(61,'4DFC',"zhong1 fu2","中孚","Insight"),
("☳","☶"):(62,'4DFD',"xiao3 guo4","小過","Conscientiousness"),
("☵","☲"):(63,'4DFE',"ji4 ji4","既濟","After the End"),
("☲","☵"):(64,'4DFF',"wei4 ji4","未濟","Before the End")}
def coin():
"""
2 = yin (even) - -
3 = yang (odd) ---
"""
while True:
yield choice([2, 3])
coin0 = coin(); coin1 = coin(); coin2 = coin()
def toss():
result = (next(coin0), next(coin1), next(coin2))
thesum = sum(result)
if thesum % 2 == 0: # even
if thesum == 6: # old yin
return "-x-" # goes to "---"
return "- -"
else: # odd
if thesum == 9: # old yang
return "-o-" # goes to "- -"
return "---"
def trigram():
lines = ""
for i in range(3):
lines = toss() + "\n" + lines
return lines
def hexagram():
lower = trigram()
upper = trigram()
return upper + lower
def thishex(hx):
return hx.replace("-x-","- -").replace("-o-","---")
def change(hx):
return hx.replace("-x-","---").replace("-o-","- -")
def lookup(hx):
hexcodes = []
bindigits = '0b'
elems = hx.split("\n")[:-1]
for x in range(3):
if elems[x] == "---":
bindigits += "1"
else:
bindigits += "0"
hexcodes.append(thetrigrams[eval(bindigits)][0])
bindigits = '0b'
for x in range(3,6):
if elems[x] == "---":
bindigits += "1"
else:
bindigits += "0"
hexcodes.append(thetrigrams[eval(bindigits)][0])
return tuple(hexcodes)
def prnhex(hxcode):
print(hxcode[0])
print(hxcode[1])
def throw():
hx = hexagram()
nowhx = thishex(hx)
newhx = change(hx)
nowcode = thehexagrams[lookup(nowhx)]
newcode = thehexagrams[lookup(newhx)]
print("---------")
prnhex(lookup(nowhx))
print(str(nowcode[0])+": ("+nowcode[3] + ' '+ nowcode[4]+")")
if nowcode != newcode:
prnhex(lookup(newhx))
print(str(newcode[0])+": ("+newcode[3] + ' '+ newcode[4]+")")
print("---------")
def test():
for i in range(30):
throw()