-
Notifications
You must be signed in to change notification settings - Fork 3
/
position.go
451 lines (416 loc) · 10.8 KB
/
position.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package engine
import (
"fmt"
"log"
"github.com/bits-and-blooms/bitset"
)
// Position 位置信息.
type Position struct {
Pawns *bitset.BitSet
Cannons *bitset.BitSet
Rooks *bitset.BitSet
Knights *bitset.BitSet
Bishops *bitset.BitSet
Advisors *bitset.BitSet
Kings *bitset.BitSet
Red *bitset.BitSet
Black *bitset.BitSet
redStrengthVal int // 红方子力价值
blackStrengthVal int // 黑方子力价值
redPstVal int // 红方位置价值
blackPstVal int // 黑方位置价值
// PiecesSq [256]int // 存放每个位置的棋子
CntRed uint
CntBlack uint
IsRedMove bool
// Key 当前局面哈希
Key uint64
}
func (p *Position) addPiece(sq uint, pieceTyp int, isRed bool) {
switch pieceTyp {
case Pawn:
p.Pawns.Set(sq)
case Cannon:
p.Cannons.Set(sq)
case Rook:
p.Rooks.Set(sq)
case Knight:
p.Knights.Set(sq)
case Bishop:
p.Bishops.Set(sq)
case Advisor:
p.Advisors.Set(sq)
case King:
p.Kings.Set(sq)
default:
log.Fatalf("bad pieceTyp: %d, sq: %x, isRed: %v\n", pieceTyp, sq, isRed)
}
if isRed {
p.Red.Set(sq)
} else {
p.Black.Set(sq)
}
}
func (p *Position) GetKey() uint64 {
return p.Key
}
func (p *Position) IsMaximizingPlayerTurn() bool {
return p.IsRedMove
}
// WhatPiece 返回 sq 位置的棋子类型.
// XXX 可用一个 256 数组 PiecesSq 存储每个位置的棋子类型提高速度.
func (p *Position) WhatPiece(sq uint) int {
if !p.Red.Test(sq) && !p.Black.Test(sq) {
return Empty
}
if p.Pawns.Test(sq) {
return Pawn
}
if p.Rooks.Test(sq) {
return Rook
}
if p.Cannons.Test(sq) {
return Cannon
}
if p.Knights.Test(sq) {
return Knight
}
if p.Bishops.Test(sq) {
return Bishop
}
if p.Advisors.Test(sq) {
return Advisor
}
if p.Kings.Test(sq) {
return King
}
panic(fmt.Errorf("wrong piece on 0x%x", sq))
}
// isKingCheck 返回将帅是否照面.
func (p *Position) isKingCheck() bool {
// 是否同一列
redKing, found := p.Kings.NextSet(0)
if !found {
log.Panic("not found red king")
}
blackKing, found := p.Kings.NextSet(redKing + 1)
if !found {
log.Panicf("not found black king, redKing: 0x%x", redKing)
}
if File(int(redKing)) != File(int(blackKing)) {
return false
}
return !p.IsAnyPieceBetweenFile(int(redKing), int(blackKing))
}
// IsOnePieceBetweenFile 判断同一列的两个棋子(sq1, sq2)之间是否有且仅有一个棋子.
// sq1, sq2 必须是同一列的,即 File(sq1) == File(sq2).
func (p *Position) IsOnePieceBetweenFile(sq1, sq2 int) bool {
file := File(sq1)
if File(sq2) != file {
log.Fatalf("sq1(%d) and sq2(%d) is not in same file", sq1, sq2)
}
min, max := sq1, sq2
if sq1 > sq2 {
min, max = sq2, sq1
}
gb := p.Black.Union(p.Red)
fileMask := gb.Intersection(FileMasks[file])
next, _ := fileMask.NextSet(uint(min + 1))
if int(next) >= max {
return false
}
next, _ = fileMask.NextSet(next + 1)
return int(next) == max
}
// IsOnePieceBetweenRank 判断同一行的两个棋子(sq1, sq2)之间是否有且仅有一个棋子.
// sq1, sq2 必须是同一行的,即 Rank(sq1) == Rank(sq2).
func (p *Position) IsOnePieceBetweenRank(sq1, sq2 int) bool {
rank := Rank(sq1)
if Rank(sq2) != rank {
log.Fatalf("sq1(0x%x) and sq2(0x%x) is not in same rank", sq1, sq2)
}
min, max := sq1, sq2
if sq1 > sq2 {
min, max = sq2, sq1
}
gb := p.Black.Union(p.Red)
rankMask := gb.Intersection(RankMasks[rank])
next, _ := rankMask.NextSet(uint(min + 1))
if int(next) >= max {
return false
}
next, _ = rankMask.NextSet(next + 1)
return int(next) == max
}
// IsAnyPieceBetweenFile 判断同一列的两个棋子(sq1, sq2)之间是否还有其他棋子.
// sq1, sq2 必须是同一列的,即 File(sq1) == File(sq2).
func (p *Position) IsAnyPieceBetweenFile(sq1, sq2 int) bool {
file := File(sq1)
if File(sq2) != file {
log.Fatalf("sq1(%d) and sq2(%d) is not in same file", sq1, sq2)
}
min, max := sq1, sq2
if sq1 > sq2 {
min, max = sq2, sq1
}
gb := p.Black.Union(p.Red)
fileMask := gb.Intersection(FileMasks[file])
next, _ := fileMask.NextSet(uint(min + 1))
return int(next) < max
}
// IsAnyPieceBetweenRank 判断同一行的两个棋子(sq1, sq2)之间是否还有其他棋子.
// sq1, sq2 必须是同一行的,即 Rank(sq1) == Rank(sq2).
func (p *Position) IsAnyPieceBetweenRank(sq1, sq2 int) bool {
rank := Rank(sq1)
if Rank(sq2) != rank {
log.Fatalf("sq1(%d) and sq2(%d) is not in same rank", sq1, sq2)
}
min, max := sq1, sq2
if sq1 > sq2 {
min, max = sq2, sq1
}
gb := p.Black.Union(p.Red)
rankMask := gb.Intersection(RankMasks[rank])
next, _ := rankMask.NextSet(uint(min + 1))
return int(next) < max
}
// knightAttacks 返回马位于 sq 位置时的攻击点.
func (p *Position) knightAttacks(sq uint) *bitset.BitSet {
mask := bitset.New(256)
gb := p.Black.Union(p.Red)
if gb.Test(sq + 1) {
mask.Set(sq + 0x10 + 2)
mask.Set(sq - 0x10 + 2)
}
if gb.Test(sq - 1) {
mask.Set(sq + 0x10 - 2)
mask.Set(sq - 0x10 - 2)
}
if gb.Test(sq + 0x10) {
mask.Set(sq + 0x20 + 1)
mask.Set(sq + 0x20 - 1)
}
if gb.Test(sq - 0x10) {
mask.Set(sq - 0x20 + 1)
mask.Set(sq - 0x20 - 1)
}
return KnightAttacks[int(sq)].Difference(mask)
}
func (p *Position) knightAttacksNg(sq uint) *bitset.BitSet {
atts := bitset.New(256)
gb := p.Black.Union(p.Red)
if !gb.Test(sq + 1) {
atts.Set(sq + 0x10 + 2)
atts.Set(sq - 0x10 + 2)
}
if !gb.Test(sq - 1) {
atts.Set(sq + 0x10 - 2)
atts.Set(sq - 0x10 - 2)
}
if !gb.Test(sq + 0x10) {
atts.Set(sq + 0x20 + 1)
atts.Set(sq + 0x20 - 1)
}
if !gb.Test(sq - 0x10) {
atts.Set(sq - 0x20 + 1)
atts.Set(sq - 0x20 - 1)
}
atts.InPlaceIntersection(BoardMask)
return atts
}
// isKnightCheck 返回己方马是否在将.
func (p *Position) isKnightCheck(isRedCheck bool) bool {
var (
kingSq uint
selfPs *bitset.BitSet
sidePs *bitset.BitSet
)
if isRedCheck {
selfPs, sidePs = p.Black, p.Red
} else {
selfPs, sidePs = p.Red, p.Black
}
kingSq, _ = p.Kings.Intersection(sidePs).NextSet(0)
knightAttacks := KnightAttacks[int(kingSq)]
selfKnights := p.Knights.Intersection(selfPs)
// 先判断将附近的八个马位是否有己方的马
if !knightAttacks.Intersection(selfKnights).Any() {
return false
}
for k, e := selfKnights.NextSet(0); e; k, e = selfKnights.NextSet(k + 1) {
if p.knightAttacksNg(k).Test(kingSq) {
return true
}
}
return false
}
// isPawnCheck 返回己方兵是否在将.
func (p *Position) isPawnCheck(isRedCheck bool) bool {
var (
kingSq uint
selfPs *bitset.BitSet
sidePs *bitset.BitSet
)
if isRedCheck {
selfPs, sidePs = p.Black, p.Red
} else {
selfPs, sidePs = p.Red, p.Black
}
kingSq, _ = p.Kings.Intersection(sidePs).NextSet(0)
pawnAttacks := AttackKingPawnSqs[int(kingSq)]
selfPawns := p.Pawns.Intersection(selfPs)
return pawnAttacks.Intersection(selfPawns).Any()
}
// isCannonCheck 返回己方炮是否在将.
func (p *Position) isCannonCheck(isRedCheck bool) bool {
var (
kingSq uint
selfPs *bitset.BitSet
sidePs *bitset.BitSet
)
if isRedCheck {
selfPs, sidePs = p.Black, p.Red
} else {
selfPs, sidePs = p.Red, p.Black
}
kingSq, _ = p.Kings.Intersection(sidePs).NextSet(0)
rookAttacks := RookAttacks[int(kingSq)]
selfCannons := p.Cannons.Intersection(selfPs)
// 先判断是否对方帅同一行及同一列有没有己方炮
if !rookAttacks.Intersection(selfCannons).Any() {
return false
}
for c, e := selfCannons.NextSet(0); e; c, e = selfCannons.NextSet(c + 1) {
if File(int(c)) == File(int(kingSq)) { // 炮将同一列
if p.IsOnePieceBetweenFile(int(c), int(kingSq)) { // 中间一子隔挡
return true
}
} else if Rank(int(c)) == Rank(int(kingSq)) { // 同一行
if p.IsOnePieceBetweenRank(int(c), int(kingSq)) {
return true
}
}
}
return false
}
// isRookCheck 返回车是否在将.
// isRedCheck = true: 返回红帅是否被黑车将.
// isRedCheck = false: 返回黑将是否被红车将.
func (p *Position) isRookCheck(isRedCheck bool) bool {
var (
kingSq uint
selfPs *bitset.BitSet
sidePs *bitset.BitSet
)
if isRedCheck {
selfPs, sidePs = p.Black, p.Red
} else {
selfPs, sidePs = p.Red, p.Black
}
kingSq, _ = p.Kings.Intersection(sidePs).NextSet(0)
rookAttacks := RookAttacks[int(kingSq)]
selfRooks := p.Rooks.Intersection(selfPs)
// 先判断是否对方帅同一行及同一列有没有己方车
if !rookAttacks.Intersection(selfRooks).Any() {
return false
}
for r, e := selfRooks.NextSet(0); e; r, e = selfRooks.NextSet(r + 1) {
if File(int(r)) == File(int(kingSq)) { // 车将同一列
if !p.IsAnyPieceBetweenFile(int(r), int(kingSq)) { // 中间无子隔挡
return true
}
} else if Rank(int(r)) == Rank(int(kingSq)) { // 同一行
if !p.IsAnyPieceBetweenRank(int(r), int(kingSq)) {
return true
}
}
}
return false
}
// IsCheck 返回是否将.
// isRedCheck = true: 返回红帅是否被将.
// isRedCheck = false: 返回黑将是否被将.
func (p *Position) IsCheck(isRedCheck bool) bool {
if p.isKingCheck() {
return true
}
if p.isRookCheck(isRedCheck) {
return true
}
if p.isCannonCheck(isRedCheck) {
return true
}
if p.isKnightCheck(isRedCheck) {
return true
}
if p.isPawnCheck(isRedCheck) {
return true
}
return false
}
// AllPieces 返回所有棋子.
func (p *Position) AllPieces() *bitset.BitSet {
return p.Red.Union(p.Black)
}
// LegalBishopMvs 返回 sq 这个位置象的合法着法位置.
func (p *Position) LegalBishopMvs(sq uint) *bitset.BitSet {
allPieces := p.AllPieces()
mvsBs := bitset.New(256)
if sq < 0x2f { // 底相
if !allPieces.Test(sq + 0x10 + 0x01) {
mvsBs.Set(sq + 0x20 + 0x02)
}
if !allPieces.Test(sq + 0x10 - 0x01) {
mvsBs.Set(sq + 0x20 - 0x02)
}
} else if sq < 0x50 { // 中相
if !allPieces.Test(sq + 0x10 + 0x01) {
mvsBs.Set(sq + 0x20 + 0x02)
}
if !allPieces.Test(sq + 0x10 - 0x01) {
mvsBs.Set(sq + 0x20 - 0x02)
}
if !allPieces.Test(sq - 0x10 + 0x01) {
mvsBs.Set(sq - 0x20 + 0x02)
}
if !allPieces.Test(sq - 0x10 - 0x01) {
mvsBs.Set(sq - 0x20 - 0x02)
}
} else if sq < 0x6f { // 高相不能过河
if !allPieces.Test(sq - 0x10 + 0x01) {
mvsBs.Set(sq - 0x20 + 0x02)
}
if !allPieces.Test(sq - 0x10 - 0x01) {
mvsBs.Set(sq - 0x20 - 0x02)
}
} else if sq < 0x80 { // 高象不能过河
if !allPieces.Test(sq + 0x10 + 0x01) {
mvsBs.Set(sq + 0x20 + 0x02)
}
if !allPieces.Test(sq + 0x10 - 0x01) {
mvsBs.Set(sq + 0x20 - 0x02)
}
} else if sq < 0xaf { // 中象
if !allPieces.Test(sq + 0x10 + 0x01) {
mvsBs.Set(sq + 0x20 + 0x02)
}
if !allPieces.Test(sq + 0x10 - 0x01) {
mvsBs.Set(sq + 0x20 - 0x02)
}
if !allPieces.Test(sq - 0x10 + 0x01) {
mvsBs.Set(sq - 0x20 + 0x02)
}
if !allPieces.Test(sq - 0x10 - 0x01) {
mvsBs.Set(sq - 0x20 - 0x02)
}
} else { // 底象
if !allPieces.Test(sq - 0x10 + 0x01) {
mvsBs.Set(sq - 0x20 + 0x02)
}
if !allPieces.Test(sq - 0x10 - 0x01) {
mvsBs.Set(sq - 0x20 - 0x02)
}
}
mvsBs.InPlaceIntersection(BoardMask)
return mvsBs
}