Files
meihua-yishu/calculator/dist/hexagrams.js
T
小钰 4ff76822ad feat: 梅花易数 + 梅花心易 算法层 v1.0
- 八卦基础数据(先天数/五行/方位/生克关系)
- 六十四卦完整数据(卦名/卦辞/象辞/吉凶/关键词)
- 体用分析引擎(动爻→体用→五行生克→旺衰判定)
- 变卦/互卦计算
- 三种起卦方式:数字法、时间法、外应法(梅花心易)
- CLI入口:JSON/文本双格式输出
- 断卦分析 prompt + 梅花心易 prompt + 海报版 prompt
- HTML海报模板(古风暗色系)
2026-08-03 07:12:25 +08:00

146 lines
15 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 八卦符号映射
const SYMBOLS = {
1: '☰', 2: '☱', 3: '☲', 4: '☳', 5: '☴', 6: '☵', 7: '☶', 8: '☷'
};
// 上卦×下卦 → 卦名
const HEXAGRAM_NAMES = {
'1,1': { name: '乾', fullName: '乾为天', judgment: '元亨利贞。', image: '天行健,君子以自强不息。', nature: '大吉', keywords: ['刚健', '进取', '领导'] },
'1,2': { name: '履', fullName: '天泽履', judgment: '履虎尾,不咥人,亨。', image: '上天下泽,履。君子以辩上下,定民志。', nature: '平', keywords: ['谨慎', '礼仪', '践履'] },
'1,3': { name: '同人', fullName: '天火同人', judgment: '同人于野,亨。', image: '天与火,同人。君子以类族辨物。', nature: '吉', keywords: ['团结', '合作', '同心'] },
'1,4': { name: '无妄', fullName: '天雷无妄', judgment: '元亨利贞。', image: '天下雷行,物与无妄。', nature: '吉', keywords: ['真实', '无妄', '正道'] },
'1,5': { name: '姤', fullName: '天风姤', judgment: '女壮,勿用取女。', image: '天下有风,姤。后以施命诰四方。', nature: '凶', keywords: ['遭遇', '诱惑', '警醒'] },
'1,6': { name: '讼', fullName: '天水讼', judgment: '有孚窒惕,中吉。', image: '天与水违行,讼。君子以作事谋始。', nature: '凶', keywords: ['争讼', '矛盾', '慎始'] },
'1,7': { name: '遁', fullName: '天山遁', judgment: '亨。小利贞。', image: '天下有山,遁。君子以远小人,不恶而严。', nature: '平', keywords: ['退避', '隐遁', '守正'] },
'1,8': { name: '否', fullName: '天地否', judgment: '否之匪人,不利君子贞。', image: '天地不交,否。君子以俭德辟难。', nature: '凶', keywords: ['闭塞', '不通', '等待'] },
'2,1': { name: '夬', fullName: '泽天夬', judgment: '扬于王庭。孚号有厉。', image: '泽上于天,夬。君子以施禄及下。', nature: '吉', keywords: ['决断', '果断', '刚健'] },
'2,2': { name: '兑', fullName: '兑为泽', judgment: '亨,利贞。', image: '丽泽,兑。君子以朋友讲习。', nature: '吉', keywords: ['喜悦', '交流', '和谐'] },
'2,3': { name: '革', fullName: '泽火革', judgment: '己日乃孚。元亨利贞。', image: '泽中有火,革。君子以治历明时。', nature: '吉', keywords: ['变革', '更新', '时机'] },
'2,4': { name: '随', fullName: '泽雷随', judgment: '元亨利贞,无咎。', image: '泽中有雷,随。君子以向晦入宴息。', nature: '吉', keywords: ['随从', '适应', '顺势'] },
'2,5': { name: '大过', fullName: '泽风大过', judgment: '栋桡。利有攸往。', image: '泽灭木,大过。君子以独立不惧。', nature: '凶', keywords: ['过度', '非常', '独立'] },
'2,6': { name: '困', fullName: '泽水困', judgment: '亨。贞大人吉。', image: '泽无水,困。君子以致命遂志。', nature: '凶', keywords: ['困境', '坚持', '志向'] },
'2,7': { name: '咸', fullName: '泽山咸', judgment: '亨,利贞。取女吉。', image: '山上有泽,咸。君子以虚受人。', nature: '大吉', keywords: ['感应', '婚恋', '虚心'] },
'2,8': { name: '萃', fullName: '泽地萃', judgment: '亨。王假有庙。', image: '泽上于地,萃。君子以除戎器,戒不虞。', nature: '吉', keywords: ['聚集', '荟萃', '防患'] },
'3,1': { name: '大有', fullName: '火天大有', judgment: '元亨。', image: '火在天上,大有。君子以遏恶扬善。', nature: '大吉', keywords: ['丰收', '大有', '光明'] },
'3,2': { name: '睽', fullName: '火泽睽', judgment: '小事吉。', image: '上火下泽,睽。君子以同而异。', nature: '平', keywords: ['乖离', '差异', '求同'] },
'3,3': { name: '离', fullName: '离为火', judgment: '利贞,亨。畜牝牛吉。', image: '明两作,离。大人以继明照于四方。', nature: '吉', keywords: ['光明', '依附', '文明'] },
'3,4': { name: '噬嗑', fullName: '火雷噬嗑', judgment: '亨。利用狱。', image: '雷电噬嗑。先王以明罚敕法。', nature: '平', keywords: ['决断', '刑罚', '障碍'] },
'3,5': { name: '鼎', fullName: '火风鼎', judgment: '元吉,亨。', image: '木上有火,鼎。君子以正位凝命。', nature: '大吉', keywords: ['鼎新', '变革', '端正'] },
'3,6': { name: '未济', fullName: '火水未济', judgment: '亨。小狐汔济。', image: '火在水上,未济。君子以慎辨物居方。', nature: '平', keywords: ['未完成', '谨慎', '过渡'] },
'3,7': { name: '旅', fullName: '火山旅', judgment: '小亨。旅贞吉。', image: '山上有火,旅。君子以明慎用刑。', nature: '平', keywords: ['旅行', '在外', '谨慎'] },
'3,8': { name: '晋', fullName: '火地晋', judgment: '康侯用锡马蕃庶。', image: '明出地上,晋。君子以自昭明德。', nature: '大吉', keywords: ['晋升', '光明', '进取'] },
'4,1': { name: '大壮', fullName: '雷天大壮', judgment: '利贞。', image: '雷在天上,大壮。君子以非礼弗履。', nature: '吉', keywords: ['强壮', '刚健', '守正'] },
'4,2': { name: '归妹', fullName: '雷泽归妹', judgment: '征凶,无攸利。', image: '泽上有雷,归妹。君子以永终知敝。', nature: '凶', keywords: ['归宿', '婚嫁', '终结'] },
'4,3': { name: '丰', fullName: '雷火丰', judgment: '亨。王假之。', image: '雷电皆至,丰。君子以折狱致刑。', nature: '大吉', keywords: ['丰盛', '光明', '盛大'] },
'4,4': { name: '震', fullName: '震为雷', judgment: '亨。震来虩虩,笑言哑哑。', image: '洊雷,震。君子以恐惧修省。', nature: '平', keywords: ['震动', '惊恐', '反省'] },
'4,5': { name: '恒', fullName: '雷风恒', judgment: '亨,无咎,利贞。', image: '雷风,恒。君子以立不易方。', nature: '大吉', keywords: ['恒久', '坚守', '持久'] },
'4,6': { name: '解', fullName: '雷水解', judgment: '利西南。', image: '雷雨作,解。君子以赦过宥罪。', nature: '吉', keywords: ['解除', '宽恕', '化解'] },
'4,7': { name: '小过', fullName: '雷山小过', judgment: '亨,利贞。可小事,不可大事。', image: '山上有雷,小过。君子以行过乎恭。', nature: '平', keywords: ['小过', '谦虚', '小事'] },
'4,8': { name: '豫', fullName: '雷地豫', judgment: '利建侯行师。', image: '雷出地奋,豫。先王以作乐崇德。', nature: '大吉', keywords: ['欢乐', '预备', '安乐'] },
'5,1': { name: '小畜', fullName: '风天小畜', judgment: '亨。密云不雨。', image: '风行天上,小畜。君子以懿文德。', nature: '平', keywords: ['积蓄', '小成', '文德'] },
'5,2': { name: '中孚', fullName: '风泽中孚', judgment: '豚鱼吉。', image: '泽上有风,中孚。君子以议狱缓死。', nature: '大吉', keywords: ['诚信', '信任', '中正'] },
'5,3': { name: '家人', fullName: '风火家人', judgment: '利女贞。', image: '风自火出,家人。君子以言有物而行有恒。', nature: '吉', keywords: ['家庭', '秩序', '言行'] },
'5,4': { name: '益', fullName: '风雷益', judgment: '利有攸往。', image: '风雷,益。君子以见善则迁,有过则改。', nature: '大吉', keywords: ['增益', '改善', '利他'] },
'5,5': { name: '巽', fullName: '巽为风', judgment: '小亨。利有攸往。', image: '随风,巽。君子以申命行事。', nature: '平', keywords: ['顺从', '谦逊', '渗透'] },
'5,6': { name: '涣', fullName: '风水涣', judgment: '亨。王假有庙。', image: '风行水上,涣。先王以享于帝立庙。', nature: '平', keywords: ['涣散', '离散', '聚合'] },
'5,7': { name: '渐', fullName: '风山渐', judgment: '女归吉。', image: '山上有木,渐。君子以居贤德善俗。', nature: '大吉', keywords: ['渐进', '循序', '修养'] },
'5,8': { name: '观', fullName: '风地观', judgment: '盥而不荐。', image: '风行地上,观。先王以省方观民设教。', nature: '平', keywords: ['观察', '审视', '教化'] },
'6,1': { name: '需', fullName: '水天需', judgment: '有孚,光亨。', image: '云上于天,需。君子以饮食宴乐。', nature: '吉', keywords: ['等待', '信心', '滋养'] },
'6,2': { name: '节', fullName: '水泽节', judgment: '亨。苦节不可贞。', image: '泽上有水,节。君子以制数度,议德行。', nature: '平', keywords: ['节制', '适度', '制度'] },
'6,3': { name: '既济', fullName: '水火既济', judgment: '亨小。利贞。', image: '水在火上,既济。君子以思患而豫防之。', nature: '吉', keywords: ['成功', '完成', '防患'] },
'6,4': { name: '屯', fullName: '水雷屯', judgment: '元亨利贞。', image: '云雷屯。君子以经纶。', nature: '平', keywords: ['初创', '艰难', '经营'] },
'6,5': { name: '井', fullName: '水风井', judgment: '改邑不改井。', image: '木上有水,井。君子以劳民劝相。', nature: '平', keywords: ['不变', '滋养', '劳作'] },
'6,6': { name: '坎', fullName: '坎为水', judgment: '习坎,有孚。', image: '水洊至,习坎。君子以常德行,习教事。', nature: '凶', keywords: ['险难', '陷阱', '坚持'] },
'6,7': { name: '蹇', fullName: '水山蹇', judgment: '利西南。', image: '山上有水,蹇。君子以反身修德。', nature: '凶', keywords: ['艰难', '反省', '修德'] },
'6,8': { name: '比', fullName: '水地比', judgment: '吉。原筮元永贞。', image: '地上有水,比。先王以建万国,亲诸侯。', nature: '大吉', keywords: ['亲比', '辅助', '亲近'] },
'7,1': { name: '大畜', fullName: '山天大畜', judgment: '利贞。', image: '天在山中,大畜。君子以多识前言往行。', nature: '大吉', keywords: ['积蓄', '大成', '博学'] },
'7,2': { name: '损', fullName: '山泽损', judgment: '有孚,元吉。', image: '山下有泽,损。君子以惩忿窒欲。', nature: '平', keywords: ['减损', '克制', '奉献'] },
'7,3': { name: '贲', fullName: '山火贲', judgment: '亨。小利有攸往。', image: '山下有火,贲。君子以明庶政,无敢折狱。', nature: '平', keywords: ['装饰', '文采', '外表'] },
'7,4': { name: '颐', fullName: '山雷颐', judgment: '贞吉。观颐。', image: '山下有雷,颐。君子以慎言语,节饮食。', nature: '吉', keywords: ['修养', '养生', '谨慎'] },
'7,5': { name: '蛊', fullName: '山风蛊', judgment: '元亨。利涉大川。', image: '山下有风,蛊。君子以振民育德。', nature: '凶', keywords: ['腐败', '整顿', '革新'] },
'7,6': { name: '蒙', fullName: '山水蒙', judgment: '亨。匪我求童蒙。', image: '山下出泉,蒙。君子以果行育德。', nature: '平', keywords: ['启蒙', '教育', '幼稚'] },
'7,7': { name: '艮', fullName: '艮为山', judgment: '艮其背,不获其身。', image: '兼山,艮。君子以思不出其位。', nature: '平', keywords: ['停止', '静止', '安分'] },
'7,8': { name: '剥', fullName: '山地剥', judgment: '不利有攸往。', image: '山附于地,剥。上以厚下安宅。', nature: '凶', keywords: ['剥落', '衰退', '基础'] },
'8,1': { name: '泰', fullName: '地天泰', judgment: '小往大来。吉,亨。', image: '天地交,泰。后以财成天地之道。', nature: '大吉', keywords: ['通泰', '顺利', '和谐'] },
'8,2': { name: '临', fullName: '地泽临', judgment: '元亨利贞。', image: '泽上有地,临。君子以教思无穷。', nature: '大吉', keywords: ['临近', '治理', '教化'] },
'8,3': { name: '明夷', fullName: '地火明夷', judgment: '利艰贞。', image: '明入地中,明夷。君子以莅众用晦而明。', nature: '凶', keywords: ['光明受损', '韬光', '艰难'] },
'8,4': { name: '复', fullName: '地雷复', judgment: '亨。出入无疾。', image: '雷在地中,复。先王以至日闭关。', nature: '大吉', keywords: ['回复', '回归', '一阳来复'] },
'8,5': { name: '升', fullName: '地风升', judgment: '元亨。', image: '地中生木,升。君子以顺德积小以高大。', nature: '大吉', keywords: ['上升', '成长', '积累'] },
'8,6': { name: '师', fullName: '地水师', judgment: '贞丈人吉。', image: '地中有水,师。君子以容民畜众。', nature: '平', keywords: ['军队', '统帅', '纪律'] },
'8,7': { name: '谦', fullName: '地山谦', judgment: '亨。君子有终。', image: '地中有山,谦。君子以裒多益寡。', nature: '大吉', keywords: ['谦虚', '美德', '平衡'] },
'8,8': { name: '坤', fullName: '坤为地', judgment: '元亨,利牝马之贞。', image: '地势坤。君子以厚德载物。', nature: '大吉', keywords: ['柔顺', '承载', '包容'] },
};
// 获取六十四卦
export function getHexagram(upperNum, lowerNum) {
const key = `${upperNum},${lowerNum}`;
const data = HEXAGRAM_NAMES[key];
if (!data) {
throw new Error(`Invalid hexagram key: ${key}`);
}
return {
upper: upperNum,
lower: lowerNum,
...data,
symbol: SYMBOLS[upperNum] + SYMBOLS[lowerNum],
};
}
// 获取变卦(动爻变化后的卦)
export function getChangedHexagram(upperNum, lowerNum, changingLine) {
// changingLine: 1-6, 1=初爻(最下), 6=上爻(最上)
// 1-3 属下卦, 4-6 属上卦
let newUpper = upperNum;
let newLower = lowerNum;
if (changingLine <= 3) {
// 下卦变:阴阳互换(通过上下卦互换模拟变卦效果)
// 梅花易数简化处理:动爻所在卦变为对应互卦
newLower = getOppositeTrigram(lowerNum);
}
else {
newUpper = getOppositeTrigram(upperNum);
}
return getHexagram(newUpper, newLower);
}
// 获取互卦(取2/3/4爻为下卦,3/4/5爻为上卦)
export function getMutualHexagram(upperNum, lowerNum) {
// 将上下卦展开为6爻(每卦3爻,阳=1阴=0)
const upperBits = trigramToBits(upperNum);
const lowerBits = trigramToBits(lowerNum);
// 6爻从下到上: lower[0], lower[1], lower[2], upper[0], upper[1], upper[2]
const lines = [...lowerBits, ...upperBits];
// 互卦:取2,3,4爻为下卦;3,4,5爻为上卦
const mutualLower = bitsToTrigram([lines[1], lines[2], lines[3]]);
const mutualUpper = bitsToTrigram([lines[2], lines[3], lines[4]]);
return getHexagram(mutualUpper, mutualLower);
}
// 八卦转三爻二进制(乾=111, 兑=110, 离=101, 震=100, 巽=011, 坎=010, 艮=001, 坤=000)
function trigramToBits(num) {
const map = {
1: [1, 1, 1], // 乾
2: [1, 1, 0], // 兑
3: [1, 0, 1], // 离
4: [1, 0, 0], // 震
5: [0, 1, 1], // 巽
6: [0, 1, 0], // 坎
7: [0, 0, 1], // 艮
8: [0, 0, 0], // 坤
};
return map[num];
}
// 三爻二进制转八卦数
function bitsToTrigram(bits) {
const map = {
'111': 1, '110': 2, '101': 3, '100': 4,
'011': 5, '010': 6, '001': 7, '000': 8,
};
return map[bits.join('')];
}
// 获取相反卦(阴阳互换)
function getOppositeTrigram(num) {
const map = {
1: 8, 8: 1, // 乾↔坤
2: 7, 7: 2, // 兑↔艮
3: 6, 6: 3, // 离↔坎
4: 5, 5: 4, // 震↔巽
};
return map[num];
}