-
Notifications
You must be signed in to change notification settings - Fork 9
/
zhouyi.go
330 lines (291 loc) · 5.3 KB
/
zhouyi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package yi
import (
"log"
"math/bits"
"strconv"
"strings"
"time"
)
const TimeFormat = "2006-01-02 15:04"
const (
// BenGua 本卦
BenGua = iota
// BianGua 变卦
BianGua
// HuGua 互卦
HuGua
// CuoGua 错卦
CuoGua
// ZongGua 综卦
ZongGua
// GuaMax 卦最大值
GuaMax
)
type BaGua = int
const (
// QianGua 卦象:乾(0)
QianGua BaGua = 0b000
// DuiGua 卦象:兑(1)
DuiGua BaGua = 0b001
// LiGua 卦象:离(2)
LiGua BaGua = 0b010
// ZhenGua 卦象:震(3)
ZhenGua BaGua = 0b011
// XunGua 卦象:巽(4)
XunGua BaGua = 0b100
// KanGua 卦象:坎(5)
KanGua BaGua = 0b101
// GenGua 卦象:艮(6)
GenGua BaGua = 0b110
// KunGua 卦象:坤(7)
KunGua BaGua = 0b111
)
//符号
var fu map[int]string
///卦象
var gua map[int]string
//Yi 周易卦象
type Yi struct {
gua [GuaMax]*GuaXiang
bianShu []int
}
//Get 取卦
func (y *Yi) Get(m int) *GuaXiang {
if m < 0 && m >= GuaMax {
return nil
}
return y.gua[m]
}
func init() {
fu = make(map[int]string)
gua = make(map[int]string)
file8gua, err := DataFiles.Open("data/8gua.csv")
if err != nil {
panic(err)
}
records, err := readData(file8gua)
if err != nil {
log.Fatal(err)
}
for _, record := range records {
guashu, err := strconv.ParseInt(record[1], 10, bits.UintSize)
if err != nil {
if len(record[1]) == 0 {
continue
}
panic(err)
}
fu[int(guashu)] = record[2]
gua[int(guashu)] = record[0]
}
}
//QiGua 起卦
func QiGua(xia, shang int) *Yi {
return NumberQiGua(shang, xia)
}
//TimeQiGua 按时间起卦
func TimeQiGua(xia int, shang int, t time.Time) *Yi {
bs := timeToBian(t)
return NumberQiGua(xia, shang, bs)
}
func getBianShu(bs ...int) int {
bNum := 0
if bs != nil {
bNum = bs[0]
}
return bNum
}
//NumberQiGua 按数起卦
func NumberQiGua(xia int, shang int, bs ...int) *Yi {
ben := benGua(shang, xia)
bian := bianGua(ben, bs...)
hu := ben
if ben.ShangShu == KunGua && ben.XiaShu == KunGua ||
ben.ShangShu == QianGua && ben.XiaShu == QianGua {
hu = bian
}
hu = huGua(hu)
cuo := cuoGua(ben)
zong := zongGua(ben)
return &Yi{
gua: [GuaMax]*GuaXiang{
BenGua: ben,
BianGua: bian,
HuGua: hu,
CuoGua: cuo,
ZongGua: zong,
},
bianShu: bs,
}
}
//StringToTime trans string to time
func StringToTime(s string) time.Time {
t, err := time.ParseInLocation(TimeFormat, s, time.Local)
if err != nil {
return time.Time{}
}
log.Println(t.String(), t.Unix())
return t
}
func timeToBian(t time.Time) int {
t, _ = time.ParseInLocation(TimeFormat, t.Format(TimeFormat), t.Location())
bnum := t.Unix()
if bnum != 0 {
return int(bnum % 6)
}
return 0
}
//BianYao 变卦,爻
func (y *Yi) BianYao() int {
return bianYao(y.bianShu...)
}
func bianYao(bs ...int) int {
s := getBianShu(bs...)
return getYao(s)
}
//由数取卦
func getGua(i int) string {
if i = i % 8; i != 0 {
return gua[i-1]
}
return gua[7]
}
// GetGua3Num 三数取卦数(1~8)
func GetGua3Num(shang, zhong, xia int) int {
shangYao := shang % 2
zhongYao := zhong % 2
xiaYao := xia % 2
return shangYao<<2 + zhongYao<<1 + xiaYao + 1
}
//取爻
func getYao(i int) int {
if i = i % 6; i != 0 {
return i - 1
}
return 5
}
//本卦
func benGua(shang, xia int) *GuaXiang {
bg := strings.Join([]string{getGua(shang), getGua(xia)}, "")
gx := getGuaXiangs()
if v, b := gx[bg]; b {
return v
}
return nil
}
//变卦
func bianGua(ben *GuaXiang, b ...int) *GuaXiang {
gx := getGuaXiangs()
bz := bianYao(b...)
sg := gua[ben.ShangShu]
xg := gua[ben.XiaShu]
if bz > 2 {
sg = gua[bian(ben.ShangShu, bz-3)]
} else {
xg = gua[bian(ben.XiaShu, bz)]
}
gua := strings.Join([]string{sg, xg}, "")
if gx[gua] == nil {
panic(gua)
}
return gx[gua]
}
//变
func bian(gua, bian int) int {
idx := 1 << (2 - uint(bian))
if gua&idx == 0 {
gua = gua | idx
} else {
gua = gua ^ idx
}
return gua
}
//互
func hu(shang, xia int) int {
huXia := 0
er := 1 << 1
san := 1 << 0
si := 1 << 2
if xia&er > 0 {
huXia |= 1 << 2
}
if xia&san > 0 {
huXia |= 1 << 1
}
if shang&si > 0 {
huXia |= 1 << 0
}
return huXia
}
//交
func jiao(shang, xia int) int {
jiaoShang := 0
san := 1 << 0
si := 1 << 2
wu := 1 << 1
if xia&san > 0 {
//位移2
jiaoShang |= 1 << 2
}
if shang&si > 0 {
//位移1
jiaoShang |= 1 << 1
}
if shang&wu > 0 {
//位不动
jiaoShang |= 1 << 0
}
return jiaoShang
}
//错
func cuo(gua int) int {
gua ^= 0x7
return gua
}
//错卦
func cuoGua(ben *GuaXiang) *GuaXiang {
gx := getGuaXiangs()
sg := gua[cuo(ben.ShangShu)]
xg := gua[cuo(ben.XiaShu)]
newGua := strings.Join([]string{sg, xg}, "")
return gx[newGua]
}
//综
func zong(shang, xia int) (int, int) {
zShang := 0
zXia := 0
if (shang & (1 << 2)) > 0 {
zXia |= 1 << 0
}
if (shang & (1 << 1)) > 0 {
zXia |= 1 << 1
}
if (shang & (1 << 0)) > 0 {
zXia |= 1 << 2
}
if (xia & (1 << 2)) > 0 {
zShang |= 1 << 0
}
if (xia & (1 << 1)) > 0 {
zShang |= 1 << 1
}
if (xia & (1 << 0)) > 0 {
zShang |= 1 << 2
}
return zShang, zXia
}
//综卦
func zongGua(ben *GuaXiang) *GuaXiang {
sg, xg := zong(ben.ShangShu, ben.XiaShu)
newGua := strings.Join([]string{gua[sg], gua[xg]}, "")
return gx[newGua]
}
//互卦
func huGua(ben *GuaXiang) *GuaXiang {
bg := strings.Join([]string{getGua(jiao(ben.ShangShu, ben.XiaShu)), getGua(hu(ben.ShangShu, ben.XiaShu))}, "")
gx := getGuaXiangs()
if v, b := gx[bg]; b {
return v
}
return nil
}