-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitboard.go
134 lines (115 loc) · 2.92 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
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
package godraughts
import (
"github.com/dangnguyendota/godraughts/bit"
c "github.com/dangnguyendota/godraughts/color"
"github.com/dangnguyendota/godraughts/magic"
p "github.com/dangnguyendota/godraughts/piece"
)
type BitBoard struct {
WhitePieces int64
BlackPieces int64
Kings int64
Occupied int64
Empty int64
King [2]int64
Pawn [2]int64
Pieces [2]int64
colorToMove int
}
func NewBitBoard() *BitBoard {
b := &BitBoard{
WhitePieces: 0x3ffffc0000000,
BlackPieces: 0xfffff,
}
b.setupValues()
return b
}
func NewCustomBitBoard(WP, BP, K int64, colorToMove int) *BitBoard {
b := &BitBoard{
WhitePieces: WP,
BlackPieces: BP,
Kings: K,
colorToMove: colorToMove,
}
b.setupValues()
return b
}
func (b *BitBoard) setupValues() {
b.King[c.White] = b.WhitePieces & b.Kings
b.King[c.Black] = b.BlackPieces & b.Kings
b.Pawn[c.White] = b.WhitePieces ^ b.King[c.White]
b.Pawn[c.Black] = b.BlackPieces ^ b.King[c.Black]
b.Occupied = b.WhitePieces | b.BlackPieces
b.Pieces[c.White] = b.WhitePieces
b.Pieces[c.Black] = b.BlackPieces
b.Empty = bit.Reverse(b.Occupied)
}
func (b *BitBoard) doMove(move int64) {
removed := getRemoveMap(move)
end := getEndIndex(move)
nonRemoved := bit.Reverse(removed)
piece := getPiece(move)
color := getColor(move)
b.WhitePieces &= nonRemoved
b.BlackPieces &= nonRemoved
b.Kings &= nonRemoved
b.colorToMove = 1 - b.colorToMove
if piece == p.Pawn {
if color == c.White {
if (magic.MASK[end] & 0x1f) != 0 {
b.Kings |= magic.MASK[end]
}
} else {
if (magic.MASK[end] & 0x3e00000000000) != 0 {
b.Kings |= magic.MASK[end]
}
}
} else if piece == p.King {
b.Kings |= magic.MASK[end]
}
if color == c.White {
b.WhitePieces |= magic.MASK[end]
} else {
b.BlackPieces |= magic.MASK[end]
}
b.setupValues()
}
func (b *BitBoard) isFirstForBlack() bool {
return b.BlackPieces == 0xfffff && b.Kings == 0
}
func (b *BitBoard) isFirstForWhite() bool {
return b.WhitePieces == 0x3ffffc0000000 && b.Kings == 0
}
func (b *BitBoard) isFirst() bool {
return b.isFirstForWhite() || b.isFirstForBlack()
}
func (b *BitBoard) String() string {
wm := b.WhitePieces & b.Kings ^ b.WhitePieces
wk := b.WhitePieces & b.Kings
bm := b.BlackPieces & b.Kings ^ b.BlackPieces
bk := b.BlackPieces & b.Kings
board := "\n +---+---+---+---+---+---+---+---+---+---+\n |"
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
if (i + j) % 2 == 0 {
board += " |"
} else if (1 << magic.Index[i][j] & wm) != 0 {
board += " M |"
} else if (1 << magic.Index[i][j] & wk) != 0 {
board += " K |"
} else if (1 << magic.Index[i][j] & bm) != 0 {
board += " m |"
} else if (1 << magic.Index[i][j] & bk) != 0 {
board += " k |"
} else {
board += "///|"
}
}
if i == 9 {
board += "\n +---+---+---+---+---+---+---+---+---+---+\n"
} else {
board += "\n +---+---+---+---+---+---+---+---+---+---+\n |"
}
}
return board
}