feat: 梅花易数 + 梅花心易 算法层 v1.0

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

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
export interface HuaInput {
method: 'number' | 'time' | 'observation';
numbers?: number[];
question?: string;
observation?: string;
observedTrigrams?: number[];
}
export interface HuaResult {
mainHexagram: {
upper: any;
lower: any;
hexagram: any;
};
tiGua: {
trigram: any;
position: 'upper' | 'lower';
};
yongGua: {
trigram: any;
position: 'upper' | 'lower';
};
dongYao: number;
changedHexagram: any;
mutualHexagram: any;
wuxingAnalysis: {
tiElement: string;
yongElement: string;
relation: string;
verdict: string;
};
wangShuai: {
season: string;
tiWang: boolean;
yongWang: boolean;
analysis: string;
};
meta: {
method: string;
inputNumbers: number[];
question?: string;
timestamp: string;
};
}
export declare function calculateHua(input: HuaInput): HuaResult;
+117
View File
@@ -0,0 +1,117 @@
import { getTrigramByNumber, getRelation, WUXING_SHENG, WUXING_KE } from './trigrams.js';
import { getHexagram, getChangedHexagram, getMutualHexagram } from './hexagrams.js';
// 获取当前节气(用于判断旺衰)
function getCurrentJieqi() {
const now = new Date();
const month = now.getMonth() + 1;
// 简化版:按月份判断大致季节
if (month >= 3 && month <= 4)
return '春(寅卯月)';
if (month >= 5 && month <= 6)
return '夏(巳午月)';
if (month >= 7 && month <= 8)
return '夏末(未申月)';
if (month >= 9 && month <= 10)
return '秋(酉戌月)';
if (month >= 11 || month <= 1)
return '冬(亥子月)';
return '冬末(丑寅月)';
}
// 判断五行在当季旺衰
function isElementWang(element, season) {
if (season.includes('春') && element === '木')
return true;
if (season.includes('夏') && element === '火')
return true;
if (season.includes('秋') && element === '金')
return true;
if (season.includes('冬') && element === '水')
return true;
if (season.includes('末') && element === '土')
return true;
return false;
}
// 核心排盘
export function calculateHua(input) {
let upperNum, lowerNum, dongYao;
if (input.method === 'number' && input.numbers) {
// 数字起卦
const nums = input.numbers;
upperNum = nums[0] % 8 || 8;
lowerNum = nums[1] % 8 || 8;
dongYao = nums.length >= 3 ? (nums[0] + nums[1] + nums[2]) % 6 || 6 : (nums[0] + nums[1]) % 6 || 6;
}
else if (input.method === 'time') {
// 时间起卦:年月日时
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
const hour = now.getHours();
const minute = now.getMinutes();
upperNum = (year + month + day) % 8 || 8;
lowerNum = (year + month + day + hour) % 8 || 8;
dongYao = (year + month + day + hour + minute) % 6 || 6;
}
else if (input.method === 'observation' && input.observedTrigrams) {
// 梅花心易:从外应推导
const nums = input.observedTrigrams;
upperNum = nums[0] % 8 || 8;
lowerNum = nums[1] % 8 || 8;
dongYao = nums.length >= 3 ? nums[2] % 6 || 6 : (nums[0] + nums[1]) % 6 || 6;
}
else {
throw new Error('无效的起卦参数');
}
const upperTrigram = getTrigramByNumber(upperNum);
const lowerTrigram = getTrigramByNumber(lowerNum);
const mainHex = getHexagram(upperNum, lowerNum);
// 体用判断:动爻在下卦(1-3)则下卦为用、上卦为体;动爻在上卦(4-6)则上卦为用、下卦为体
const tiPosition = dongYao <= 3 ? 'upper' : 'lower';
const yongPosition = dongYao <= 3 ? 'lower' : 'upper';
const tiTrigram = tiPosition === 'upper' ? upperTrigram : lowerTrigram;
const yongTrigram = yongPosition === 'upper' ? upperTrigram : lowerTrigram;
// 变卦
const changedHex = getChangedHexagram(upperNum, lowerNum, dongYao);
// 互卦
const mutualHex = getMutualHexagram(upperNum, lowerNum);
// 五行分析
const relation = getRelation(tiTrigram.element, yongTrigram.element);
let verdict = '';
if (WUXING_SHENG[yongTrigram.element] === tiTrigram.element) {
verdict = '用生体:外来助力,事易成,有人帮助。';
}
else if (WUXING_SHENG[tiTrigram.element] === yongTrigram.element) {
verdict = '体生用:我泄气于外,耗损之象,需付出较多。';
}
else if (WUXING_KE[yongTrigram.element] === tiTrigram.element) {
verdict = '用克体:外来压力大,受制于人,事难成。';
}
else if (WUXING_KE[tiTrigram.element] === yongTrigram.element) {
verdict = '体克用:我能制外,事可成但需努力。';
}
else {
verdict = '体用比和:势均力敌,事可成,和合之象。';
}
// 旺衰
const season = getCurrentJieqi();
const tiWang = isElementWang(tiTrigram.element, season);
const yongWang = isElementWang(yongTrigram.element, season);
const wangAnalysis = `当令${season}。体卦${tiTrigram.element}${tiWang ? '旺' : '衰'},用卦${yongTrigram.element}${yongWang ? '旺' : '衰'}。`;
return {
mainHexagram: { upper: upperTrigram, lower: lowerTrigram, hexagram: mainHex },
tiGua: { trigram: tiTrigram, position: tiPosition },
yongGua: { trigram: yongTrigram, position: yongPosition },
dongYao,
changedHexagram: changedHex,
mutualHexagram: mutualHex,
wuxingAnalysis: { tiElement: tiTrigram.element, yongElement: yongTrigram.element, relation, verdict },
wangShuai: { season, tiWang, yongWang, analysis: wangAnalysis },
meta: {
method: input.method,
inputNumbers: input.numbers || [],
question: input.question,
timestamp: new Date().toISOString(),
},
};
}
+14
View File
@@ -0,0 +1,14 @@
export interface Hexagram {
upper: number;
lower: number;
name: string;
fullName: string;
symbol: string;
judgment: string;
image: string;
nature: string;
keywords: string[];
}
export declare function getHexagram(upperNum: number, lowerNum: number): Hexagram;
export declare function getChangedHexagram(upperNum: number, lowerNum: number, changingLine: number): Hexagram;
export declare function getMutualHexagram(upperNum: number, lowerNum: number): Hexagram;
+145
View File
@@ -0,0 +1,145 @@
// 八卦符号映射
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];
}
+1
View File
@@ -0,0 +1 @@
export {};
+89
View File
@@ -0,0 +1,89 @@
import { calculateHua } from './analysis.js';
// CLI 入口
const args = process.argv.slice(2);
function getArg(name) {
const found = args.find(a => a.startsWith(`--${name}=`));
return found ? found.split('=')[1] : undefined;
}
const method = (getArg('method') || 'time');
const question = getArg('question') || undefined;
const numbersStr = getArg('numbers');
const observation = getArg('observation') || undefined;
const trigramsStr = getArg('trigrams');
const outputFormat = getArg('output') || 'json'; // json | text
const input = { method, question };
if (method === 'number' && numbersStr) {
input.numbers = numbersStr.split(',').map(Number);
}
else if (method === 'observation') {
input.observation = observation;
if (trigramsStr) {
input.observedTrigrams = trigramsStr.split(',').map(Number);
}
}
try {
const result = calculateHua(input);
if (outputFormat === 'text') {
// 文本输出(供 LLM 阅读)
console.log(formatAsText(result));
}
else {
console.log(JSON.stringify(result, null, 2));
}
}
catch (e) {
console.error('排盘错误:', e.message);
process.exit(1);
}
function formatAsText(r) {
const main = r.mainHexagram;
const changed = r.changedHexagram;
const mutual = r.mutualHexagram;
const ti = r.tiGua;
const yong = r.yongGua;
let text = `═══════════════════════════════════
梅花易数 排盘结果
═══════════════════════════════════
【起卦方式】${r.meta.method === 'number' ? '数字起卦' : r.meta.method === 'time' ? '时间起卦' : '梅花心易(外应)'}
${r.meta.question ? `【问事】${r.meta.question}` : ''}
【起卦时间】${r.meta.timestamp}
───────────────────────────────────
主 卦
───────────────────────────────────
${main.hexagram.fullName} (${main.hexagram.symbol})
上卦:${main.upper.name}(${main.upper.element}) 下卦:${main.lower.name}(${main.lower.element})
卦辞:${main.hexagram.judgment}
象辞:${main.hexagram.image}
吉凶:${main.hexagram.nature}
───────────────────────────────────
体 用 分 析
───────────────────────────────────
动爻:第${r.dongYao}爻
体卦:${ti.trigram.name}(${ti.trigram.element}) — ${ti.position === 'upper' ? '上卦' : '下卦'}(不动之卦)
用卦:${yong.trigram.name}(${yong.trigram.element}) — ${yong.position === 'upper' ? '上卦' : '下卦'}(动爻所在)
五行关系:${r.wuxingAnalysis.relation}
判定:${r.wuxingAnalysis.verdict}
───────────────────────────────────
旺 衰
───────────────────────────────────
${r.wangShuai.analysis}
───────────────────────────────────
变 卦(趋势走向)
───────────────────────────────────
${changed.fullName} (${changed.symbol})
卦辞:${changed.judgment}
象辞:${changed.image}
───────────────────────────────────
互 卦(内在因素)
───────────────────────────────────
${mutual.fullName} (${mutual.symbol})
卦辞:${mutual.judgment}
`;
return text;
}
+16
View File
@@ -0,0 +1,16 @@
export interface Trigram {
number: number;
name: string;
symbol: string;
element: string;
direction: string;
attribute: string;
bodyPart: string;
family: string;
nature: string;
}
export declare const TRIGRAMS: Record<number, Trigram>;
export declare const WUXING_SHENG: Record<string, string>;
export declare const WUXING_KE: Record<string, string>;
export declare function getRelation(a: string, b: string): string;
export declare function getTrigramByNumber(n: number): Trigram;
+37
View File
@@ -0,0 +1,37 @@
// 先天八卦数:乾1 兑2 离3 震4 巽5 坎6 艮7 坤8
export const TRIGRAMS = {
1: { number: 1, name: '乾', symbol: '☰', element: '金', direction: '西北', attribute: '天', bodyPart: '头/骨/肺', family: '父', nature: '刚健' },
2: { number: 2, name: '兑', symbol: '☱', element: '金', direction: '西', attribute: '泽', bodyPart: '口/肺', family: '少女', nature: '喜悦' },
3: { number: 3, name: '离', symbol: '☲', element: '火', direction: '南', attribute: '火', bodyPart: '目/心', family: '中女', nature: '文明' },
4: { number: 4, name: '震', symbol: '☳', element: '木', direction: '东', attribute: '雷', bodyPart: '足/肝', family: '长男', nature: '动' },
5: { number: 5, name: '巽', symbol: '☴', element: '木', direction: '东南', attribute: '风', bodyPart: '股/胆', family: '长女', nature: '入' },
6: { number: 6, name: '坎', symbol: '☵', element: '水', direction: '北', attribute: '水', bodyPart: '耳/肾', family: '中男', nature: '陷' },
7: { number: 7, name: '艮', symbol: '☶', element: '土', direction: '东北', attribute: '山', bodyPart: '手/脾', family: '少男', nature: '止' },
8: { number: 8, name: '坤', symbol: '☷', element: '土', direction: '西南', attribute: '地', bodyPart: '腹/胃', family: '母', nature: '顺' },
};
// 五行生克
export const WUXING_SHENG = {
'木': '火', '火': '土', '土': '金', '金': '水', '水': '木'
};
export const WUXING_KE = {
'木': '土', '土': '水', '水': '火', '火': '金', '金': '木'
};
// 判断生克关系
export function getRelation(a, b) {
if (WUXING_SHENG[a] === b)
return `${a}生${b}(我生)`;
if (WUXING_SHENG[b] === a)
return `${b}生${a}(生我)`;
if (WUXING_KE[a] === b)
return `${a}克${b}(我克)`;
if (WUXING_KE[b] === a)
return `${b}克${a}(克我)`;
return `${a}与${b}比和`;
}
// 获取卦象(上卦+下卦 → 卦名)
export function getTrigramByNumber(n) {
// 数字取卦:除8取余,整除取8(坤)
const remainder = n % 8;
const key = remainder === 0 ? 8 : remainder;
return TRIGRAMS[key];
}