/
birboard.go
157 lines (131 loc) · 3.51 KB
/
birboard.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
package butils
/////////////////////////////////////////////////////////////////////
// imports
import (
"math/bits"
)
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// member functions
// RankBb returns a bitboard with all bits on rank set
func RankBb(rank int) Bitboard {
return BbRank1 << uint(8*rank)
}
// FileBb returns a bitboard with all bits on file set
func FileBb(file int) Bitboard {
return BbFileA << uint(file)
}
// North shifts all squares one rank up
func North(bb Bitboard) Bitboard {
return bb << 8
}
// South shifts all squares one rank down
func South(bb Bitboard) Bitboard {
return bb >> 8
}
// meaning of &^ operator
// https://stackoverflow.com/questions/34459450/what-is-the-operator-in-golang
// turn mask bits off
// East shifts all squares one file right
// delete h-file, then shift left
func East(bb Bitboard) Bitboard {
return bb &^ BbFileH << 1
}
// West shifts all squares one file left
// delete a-file, then shift right
func West(bb Bitboard) Bitboard {
return bb &^ BbFileA >> 1
}
// Fill returns a bitboard with all files with squares filled.
func Fill(bb Bitboard) Bitboard {
return NorthFill(bb) | SouthFill(bb)
}
// ForwardSpan computes forward span wrt color.
func ForwardSpan(col Color, bb Bitboard) Bitboard {
if col == White {
return NorthSpan(bb)
}
if col == Black {
return SouthSpan(bb)
}
return bb
}
// ForwardFill computes forward fill wrt color.
func ForwardFill(col Color, bb Bitboard) Bitboard {
if col == White {
return NorthFill(bb)
}
if col == Black {
return SouthFill(bb)
}
return bb
}
// BackwardSpan computes backward span wrt color
func BackwardSpan(col Color, bb Bitboard) Bitboard {
if col == White {
return SouthSpan(bb)
}
if col == Black {
return NorthSpan(bb)
}
return bb
}
// BackwardFill computes forward fill wrt color
func BackwardFill(col Color, bb Bitboard) Bitboard {
if col == White {
return SouthFill(bb)
}
if col == Black {
return NorthFill(bb)
}
return bb
}
// NorthFill returns a bitboard with all north bits set
func NorthFill(bb Bitboard) Bitboard {
bb |= (bb << 8)
bb |= (bb << 16)
bb |= (bb << 32)
return bb
}
// NorthSpan is like NorthFill shifted on up
func NorthSpan(bb Bitboard) Bitboard {
return NorthFill(North(bb))
}
// SouthFill returns a bitboard with all south bits set
func SouthFill(bb Bitboard) Bitboard {
bb |= (bb >> 8)
bb |= (bb >> 16)
bb |= (bb >> 32)
return bb
}
// SouthSpan is like SouthFill shifted on up
func SouthSpan(bb Bitboard) Bitboard {
return SouthFill(South(bb))
}
// Has returns bb if sq is occupied in bitboard
func (bb Bitboard) Has(sq Square) bool {
return bb>>sq&1 != 0
}
// AsSquare returns the occupied square if the bitboard has a single piece
// if the board has more then one piece the result is undefined
// https://golang.org/pkg/math/bits/#TrailingZeros64
func (bb Bitboard) AsSquare() Square {
return Square(bits.TrailingZeros64(uint64(bb)) & 0x3f)
}
// LSB picks a square in the board
// returns empty board for empty board
func (bb Bitboard) LSB() Bitboard {
return bb & (-bb)
}
// count returns the number of squares set in bb
// https://golang.org/pkg/math/bits/#OnesCount64
func (bb Bitboard) Count() int32 {
return int32(bits.OnesCount64(uint64(bb)))
}
// Pop pops a set square from the bitboard
func (bb *Bitboard) Pop() Square {
sq := *bb & (-*bb)
*bb -= sq
return Square(bits.TrailingZeros64(uint64(sq)) & 0x3f)
}
/////////////////////////////////////////////////////////////////////