Skip to content

Commit d7c6889

Browse files
committed
16 generate Kn and K incl castlings but still need to check for attacks between K and R
1 parent 8ccf10d commit d7c6889

File tree

3 files changed

+126
-65
lines changed

3 files changed

+126
-65
lines changed

castlings.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ const (
1111
longB = uint(0x8) // black can castle short
1212
)
1313

14+
type castlOptions struct {
15+
short uint
16+
long uint
17+
rook int
18+
king int
19+
rookSh uint
20+
rookLong uint
21+
}
22+
23+
var castl = [2]castlOptions{{shortW, longW, wR,E1, H1, A1}, {shortB, longB,bR, E8, H8, A8}}
24+
25+
// only castling privileges (not if it is legal on board)
26+
func (c castlings) canCastle(sd color) bool {
27+
return c.canCastleShort(sd) || c.canCastleLong(sd)
28+
}
29+
func (c castlings) canCastleShort(sd color) bool {
30+
return (castl[sd].short & uint(c)) != 0
31+
}
32+
func (c castlings) canCastleLong(sd color) bool {
33+
return (castl[sd].long & uint(c)) != 0
34+
}
35+
1436
func (c *castlings) on(val uint) {
1537
(*c) |= castlings(val)
1638
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ func main() {
1111
func init(){
1212
initFenSq2Int()
1313
initMagic()
14+
initAtksKings()
15+
initAtksKnights()
1416
}

position.go

Lines changed: 102 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,122 +14,124 @@ const (
1414
BLACK = color(1)
1515
startpos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - "
1616
)
17-
var movesKnight[64] bitBoard
18-
var movesKings[64] bitBoard
17+
18+
var atksKnights [64]bitBoard
19+
var atksKings [64]bitBoard
1920

2021
// initialize all possible knight moves
21-
func initMovesKnights(){
22-
toBB:=bitBoard(0)
23-
for fr:=A1;fr<=H8;fr++{
22+
func initAtksKnights() {
23+
toBB := bitBoard(0)
24+
for fr := A1; fr <= H8; fr++ {
2425
// NNE 2,1
25-
rk:= fr/8
26-
fl:=fr%8
27-
if rk+2<8 && fl+1<8{
28-
to:= uint((rk+2)*8+fl+1)
26+
rk := fr / 8
27+
fl := fr % 8
28+
if rk+2 < 8 && fl+1 < 8 {
29+
to := uint((rk+2)*8 + fl + 1)
2930
toBB.set(to)
3031
}
3132

3233
// ENE 1,2
33-
if rk+1<8 && fl+2<8{
34-
to:= uint((rk+1)*8+fl+2)
34+
if rk+1 < 8 && fl+2 < 8 {
35+
to := uint((rk+1)*8 + fl + 2)
3536
toBB.set(to)
3637
}
3738

3839
// ESE -1,2
39-
if rk-1>=0 && fl+2<8{
40-
to:= uint((rk-1)*8+fl+2)
40+
if rk-1 >= 0 && fl+2 < 8 {
41+
to := uint((rk-1)*8 + fl + 2)
4142
toBB.set(to)
4243
}
4344

4445
// SSE -2,+1
45-
if rk-2>=0 && fl+1<8{
46-
to:= uint((rk-2)*8+fl+1)
46+
if rk-2 >= 0 && fl+1 < 8 {
47+
to := uint((rk-2)*8 + fl + 1)
4748
toBB.set(to)
4849
}
4950

5051
// NNW 2,-1
51-
if rk+2<8 && fl-1>=0{
52-
to:= uint((rk+2)*8+fl-1)
52+
if rk+2 < 8 && fl-1 >= 0 {
53+
to := uint((rk+2)*8 + fl - 1)
5354
toBB.set(to)
5455
}
5556

5657
// WNW 1,-2
57-
if rk+1<8 && fl-2>=0{
58-
to:= uint((rk+1)*8+fl-2)
58+
if rk+1 < 8 && fl-2 >= 0 {
59+
to := uint((rk+1)*8 + fl - 2)
5960
toBB.set(to)
6061
}
61-
62+
6263
// WSW -1,-2
63-
if rk-1>=0 && fl-2>=0{
64-
to:= uint((rk-1)*8+fl-2)
64+
if rk-1 >= 0 && fl-2 >= 0 {
65+
to := uint((rk-1)*8 + fl - 2)
6566
toBB.set(to)
6667
}
6768

6869
// SSW -2,-1
69-
if rk-2>=0 && fl-1>=0{
70-
to:= uint((rk-2)*8+fl-1)
70+
if rk-2 >= 0 && fl-1 >= 0 {
71+
to := uint((rk-2)*8 + fl - 1)
7172
toBB.set(to)
7273
}
73-
movesKnight[fr] = toBB
74+
atksKnights[fr] = toBB
7475
}
7576
}
7677

7778
// initialize all possible King moves
78-
func initMovesKings(){
79-
toBB:=bitBoard(0)
80-
for fr:=A1;fr<=H8;fr++{
79+
func initAtksKings() {
80+
toBB := bitBoard(0)
81+
for fr := A1; fr <= H8; fr++ {
8182
//N 1,0
82-
rk:= fr/8
83-
fl:=fr%8
84-
if rk+1<8{
85-
to:= uint((rk+1)*8+fl)
83+
rk := fr / 8
84+
fl := fr % 8
85+
if rk+1 < 8 {
86+
to := uint((rk+1)*8 + fl)
8687
toBB.set(to)
8788
}
8889

8990
//NE 1,1
90-
if rk+1<8&&fl+1<8 {
91-
to:= uint((rk+1)*8+fl+1)
91+
if rk+1 < 8 && fl+1 < 8 {
92+
to := uint((rk+1)*8 + fl + 1)
9293
toBB.set(to)
9394
}
94-
95+
9596
//E 0,1
96-
if fl+1<8 {
97-
to:= uint((rk)*8+fl+1)
97+
if fl+1 < 8 {
98+
to := uint((rk)*8 + fl + 1)
9899
toBB.set(to)
99100
}
100101

101102
//SE -1,1
102-
if rk-1>=0 && fl+1<8 {
103-
to:= uint((rk-1)*8+fl+1)
103+
if rk-1 >= 0 && fl+1 < 8 {
104+
to := uint((rk-1)*8 + fl + 1)
104105
toBB.set(to)
105106
}
106107

107108
//S -1,0
108-
if rk-1>=0 {
109-
to:= uint((rk-1)*8+fl)
109+
if rk-1 >= 0 {
110+
to := uint((rk-1)*8 + fl)
110111
toBB.set(to)
111112
}
112113

113114
//SW -1,-1
114-
if rk-1>=0 && fl-1>=0 {
115-
to:= uint((rk-1)*8+fl-1)
115+
if rk-1 >= 0 && fl-1 >= 0 {
116+
to := uint((rk-1)*8 + fl - 1)
116117
toBB.set(to)
117118
}
118119

119120
//W 0,-1
120-
if fl-1>=0 {
121-
to:= uint((rk)*8+fl-1)
121+
if fl-1 >= 0 {
122+
to := uint((rk)*8 + fl - 1)
122123
toBB.set(to)
123124
}
124125

125126
//NW 1,-1
126-
if rk+1<8 && fl-1>=0 {
127-
to:= uint((rk+1)*8+fl-1)
127+
if rk+1 < 8 && fl-1 >= 0 {
128+
to := uint((rk+1)*8 + fl - 1)
128129
toBB.set(to)
129130
}
130-
movesKings[fr]=toBB
131+
atksKings[fr] = toBB
131132
}
132133
}
134+
133135
type boardStruct struct {
134136
sq [64]int
135137
wbBB [2]bitBoard
@@ -287,18 +289,16 @@ func (b *boardStruct) newGame() {
287289
}
288290

289291
func (b *boardStruct) genRookMoves(ml *moveList, sd color) {
290-
// TODO: generate rook moves with magic bitBoards - Benchmark
291-
292292
allRBB := b.pieceBB[Rook] & b.wbBB[sd]
293293
p12 := uint(pc2P12(Rook, color(sd)))
294294
ep := uint(b.ep)
295-
castl := uint(b.castlings)
295+
castlings := uint(b.castlings)
296296
var mv move
297297

298298
for fr := allRBB.firstOne(); fr != 64; fr = allRBB.firstOne() {
299299
toBB := mRookTab[fr].atks(b) & (^b.wbBB[sd])
300300
for to := toBB.firstOne(); to != 64; to = toBB.firstOne() {
301-
mv.packMove(uint(fr), uint(to), p12, uint(b.sq[to]), empty, ep, castl)
301+
mv.packMove(uint(fr), uint(to), p12, uint(b.sq[to]), empty, ep, castlings)
302302
ml.add(mv)
303303
}
304304
}
@@ -308,13 +308,12 @@ func (b *boardStruct) genBishopMoves(ml *moveList, sd color) {
308308
allBBB := b.pieceBB[Bishop] & b.wbBB[sd]
309309
p12 := uint(pc2P12(Bishop, color(sd)))
310310
ep := uint(b.ep)
311-
castl := uint(b.castlings)
311+
castlings := uint(b.castlings)
312312
var mv move
313-
314313
for fr := allBBB.firstOne(); fr != 64; fr = allBBB.firstOne() {
315314
toBB := mBishopTab[fr].atks(b) & (^b.wbBB[sd])
316315
for to := toBB.firstOne(); to != 64; to = toBB.firstOne() {
317-
mv.packMove(uint(fr), uint(to), p12, uint(b.sq[to]), empty, ep, castl)
316+
mv.packMove(uint(fr), uint(to), p12, uint(b.sq[to]), empty, ep, castlings)
318317
ml.add(mv)
319318
}
320319
}
@@ -324,24 +323,64 @@ func (b *boardStruct) genQueenMoves(ml *moveList, sd color) {
324323
allQBB := b.pieceBB[Queen] & b.wbBB[sd]
325324
p12 := uint(pc2P12(Queen, color(sd)))
326325
ep := uint(b.ep)
327-
castl := uint(b.castlings)
326+
castlings := uint(b.castlings)
328327
var mv move
329328

330329
for fr := allQBB.firstOne(); fr != 64; fr = allQBB.firstOne() {
331330
toBB := mBishopTab[fr].atks(b) & (^b.wbBB[sd])
332331
toBB |= mRookTab[fr].atks(b) & (^b.wbBB[sd])
333332
for to := toBB.firstOne(); to != 64; to = toBB.firstOne() {
334-
mv.packMove(uint(fr), uint(to), p12, uint(b.sq[to]), empty, ep, castl)
333+
mv.packMove(uint(fr), uint(to), p12, uint(b.sq[to]), empty, ep, castlings)
335334
ml.add(mv)
336335
}
337336
}
338337
}
339338

340339
func (b *boardStruct) genKnightMoves(ml *moveList, sd color) {
340+
allNBB := b.pieceBB[Knight] & b.wbBB[sd]
341+
p12 := uint(pc2P12(Knight, color(sd)))
342+
ep := uint(b.ep)
343+
castlings := uint(b.castlings)
344+
var mv move
341345

346+
for fr := allNBB.firstOne(); fr != 64; fr = allNBB.firstOne() {
347+
toBB := atksKnights[fr] & (^b.wbBB[sd])
348+
for to := toBB.firstOne(); to != 64; to = toBB.firstOne() {
349+
mv.packMove(uint(fr), uint(to), p12, uint(b.sq[to]), empty, ep, castlings)
350+
ml.add(mv)
351+
}
352+
}
342353
}
343354
func (b *boardStruct) genKingMoves(ml *moveList, sd color) {
344-
// castlings!
355+
// 'normal' moves
356+
allKBB := b.pieceBB[King] & b.wbBB[sd]
357+
p12 := uint(pc2P12(King, color(sd)))
358+
ep := uint(b.ep)
359+
castlings := uint(b.castlings)
360+
var mv move
361+
362+
for fr := allKBB.firstOne(); fr != 64; fr = allKBB.firstOne() {
363+
toBB := atksKings[fr] & (^b.wbBB[sd])
364+
for to := toBB.firstOne(); to != 64; to = toBB.firstOne() {
365+
mv.packMove(uint(fr), uint(to), p12, uint(b.sq[to]), empty, ep, castlings)
366+
ml.add(mv)
367+
}
368+
}
369+
370+
if b.King[sd] == castl[sd].king {
371+
// short castling
372+
if b.sq[castl[sd].rookSh] == castl[sd].rook {
373+
// TODO: not in check and between not attacked
374+
mv.packMove(uint(b.King[sd]), uint(b.King[sd]+2), uint(b.sq[b.King[sd]]), empty, empty, uint(b.ep), uint(b.castlings))
375+
ml.add(mv)
376+
}
377+
// long castling
378+
if b.sq[castl[sd].rookLong] == castl[sd].rook {
379+
// TODO: not in check and between not attacked
380+
mv.packMove(uint(b.King[sd]), uint(b.King[sd]-2), uint(b.sq[b.King[sd]]), empty, empty, uint(b.ep), uint(b.castlings))
381+
ml.add(mv)
382+
}
383+
}
345384
}
346385

347386
func (b *boardStruct) genPawnMoves(ml *moveList, sd color) {
@@ -889,12 +928,10 @@ const (
889928

890929
//////////////////////////////// TODO: remove this ////////////////////////////////////////
891930
func (b *boardStruct) genSimpleRookMoves(ml *moveList, sd color) {
892-
// TODO: generate rook moves with magic bitBoards - Benchmark
893-
894931
allRBB := b.pieceBB[Rook] & b.wbBB[sd]
895932
p12 := uint(pc2P12(Rook, color(sd)))
896933
ep := uint(b.ep)
897-
castl := uint(b.castlings)
934+
castlings := uint(b.castlings)
898935
var mv move
899936
for fr := allRBB.firstOne(); fr != 64; fr = allRBB.firstOne() {
900937
rk := fr / 8
@@ -906,7 +943,7 @@ func (b *boardStruct) genSimpleRookMoves(ml *moveList, sd color) {
906943
if cp != empty && p12Color(int(cp)) == sd {
907944
break
908945
}
909-
mv.packMove(uint(fr), to, p12, cp, empty, ep, castl)
946+
mv.packMove(uint(fr), to, p12, cp, empty, ep, castlings)
910947
ml.add(mv)
911948
if cp != empty {
912949
break
@@ -919,7 +956,7 @@ func (b *boardStruct) genSimpleRookMoves(ml *moveList, sd color) {
919956
if cp != empty && p12Color(int(cp)) == sd {
920957
break
921958
}
922-
mv.packMove(uint(fr), to, p12, cp, empty, ep, castl)
959+
mv.packMove(uint(fr), to, p12, cp, empty, ep, castlings)
923960
ml.add(mv)
924961
if cp != empty {
925962
break
@@ -932,7 +969,7 @@ func (b *boardStruct) genSimpleRookMoves(ml *moveList, sd color) {
932969
if cp != empty && p12Color(int(cp)) == sd {
933970
break
934971
}
935-
mv.packMove(uint(fr), to, p12, cp, empty, ep, castl)
972+
mv.packMove(uint(fr), to, p12, cp, empty, ep, castlings)
936973
ml.add(mv)
937974
if cp != empty {
938975
break
@@ -945,7 +982,7 @@ func (b *boardStruct) genSimpleRookMoves(ml *moveList, sd color) {
945982
if cp != empty && p12Color(int(cp)) == sd {
946983
break
947984
}
948-
mv.packMove(uint(fr), to, p12, cp, empty, ep, castl)
985+
mv.packMove(uint(fr), to, p12, cp, empty, ep, castlings)
949986
ml.add(mv)
950987
if cp != empty {
951988
break

0 commit comments

Comments
 (0)