-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitboard.go
74 lines (66 loc) · 1.6 KB
/
bitboard.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
package octad
import (
"math/bits"
"strconv"
"strings"
)
// bitboard is an Octad board representation encoded in an unsigned 16-bit
// integer. The 16 board positions begin with A1 as the most significant bit and
// D4 as the least significant.
type bitboard uint16
func newBitboard(m map[Square]bool) bitboard {
s := ""
for sq := 0; sq < squaresOnBoard; sq++ {
if m[Square(sq)] {
s += "1"
} else {
s += "0"
}
}
bb, err := strconv.ParseUint(s, 2, 16)
if err != nil {
panic(err)
}
return bitboard(bb)
}
func (b bitboard) Mapping() map[Square]bool {
m := map[Square]bool{}
for sq := 0; sq < squaresOnBoard; sq++ {
if b&bbForSquare(Square(sq)) > 0 {
m[Square(sq)] = true
}
}
return m
}
// String returns a 64 character string of 1s and 0s starting with the most
// significant bit.
func (b bitboard) String() string {
s := strconv.FormatUint(uint64(b), 2)
return strings.Repeat("0", squaresOnBoard-len(s)) + s
}
// Draw returns visual representation of the bitboard useful for debugging.
func (b bitboard) Draw() string {
s := "\n A B C D\n"
for r := 3; r >= 0; r-- {
s += Rank(r).String()
for f := 0; f < squaresInRow; f++ {
sq := getSquare(File(f), Rank(r))
if b.Occupied(sq) {
s += "1"
} else {
s += "0"
}
s += " "
}
s += "\n"
}
return s
}
// Reverse returns a bitboard where the bit order is reversed.
func (b bitboard) Reverse() bitboard {
return bitboard(bits.Reverse16(uint16(b)))
}
// Occupied returns true if the square's bitboard position is 1.
func (b bitboard) Occupied(sq Square) bool {
return (bits.RotateLeft16(uint16(b), int(sq)+1) & 1) == 1
}