-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitboards.go
64 lines (56 loc) · 1.42 KB
/
bitboards.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
package main
import (
"fmt"
"strconv"
"strings"
)
// Bitboard is the number whose 64 bit positions is to be printed
type Bitboard uint64
// Print is to represent presence of each piece using bitwise operations
func (bit Bitboard) Print() {
var shiftMe uint64
shiftMe = 1
fmt.Println()
for rank := Rank8; rank >= Rank1; rank-- {
for file := FileA; file <= FileH; file++ {
sq := FR2SQ(file, rank) //120 based
sq64 := SQ64(sq) //64 based
expr := (shiftMe << uint64(sq64)) & uint64(bit)
if expr != 0 {
fmt.Printf("X")
} else {
fmt.Printf("-")
}
}
fmt.Println()
}
fmt.Println()
}
func reverse(s string) (result string) {
for _, v := range s {
result = string(v) + result
}
return
}
// Pop returns position of first piece from MSB side of a particular pieces' bitboard
func (bit *Bitboard) Pop() (i int) {
bs := strconv.FormatUint(uint64(*bit), 2)
rev := reverse(bs)
i = strings.Index(rev, "1")
mask := ^(uint64(1) << uint64(i))
*bit &= Bitboard(mask)
return
}
// Count counts number of pieces of a particular piece
func (bit Bitboard) Count() int {
bs := strconv.FormatUint(uint64(bit), 2)
return strings.Count(bs, "1")
}
// Clear clears the bit of a sq in a particular pieces' bitboard
func (bit *Bitboard) Clear(sq int) {
*bit &= Bitboard(ClearMask[sq])
}
// Set sets the bit of a sq in a particular pieces' bitboard
func (bit *Bitboard) Set(sq int) {
*bit |= Bitboard(SetMask[sq])
}