-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.go
372 lines (353 loc) · 9.1 KB
/
util.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package dragontoothmg
import (
"errors"
"fmt"
"log"
"strconv"
"strings"
)
func recomputeBoardHash(b *Board) uint64 {
var hash uint64 = 0
if b.Wtomove {
hash ^= whiteToMoveZobristC
}
if b.whiteCanCastleKingside() {
hash ^= castleRightsZobristC[0]
}
if b.whiteCanCastleQueenside() {
hash ^= castleRightsZobristC[1]
}
if b.blackCanCastleKingside() {
hash ^= castleRightsZobristC[2]
}
if b.blackCanCastleQueenside() {
hash ^= castleRightsZobristC[3]
}
hash ^= uint64(b.enpassant)
for i := uint8(0); i < 64; i++ {
whitePiece, _ := determinePieceType(&(b.White), uint64(1)<<i)
blackPiece, _ := determinePieceType(&(b.Black), uint64(1)<<i)
if whitePiece != Nothing {
hash ^= pieceSquareZobristC[whitePiece-1][i]
}
if blackPiece != Nothing {
hash ^= pieceSquareZobristC[blackPiece+5][i]
}
}
return hash
}
func IsCapture(m Move, b *Board) bool {
toBitboard := (uint64(1) << m.To())
if (toBitboard&b.White.All != 0) || (toBitboard&b.Black.All != 0) {
return true
}
// Is it an en passant capture?
fromBitboard := (uint64(1) << m.From())
originIsPawn := fromBitboard&b.White.Pawns != 0 || fromBitboard&b.Black.Pawns != 0
return originIsPawn && (toBitboard&(uint64(1)<<b.enpassant) != 0)
}
func GetPieceType(square uint8, b *Board) (int, bool) {
squareMask := uint64(1) << square
var isWhite bool = true
if b.White.All&squareMask != 0 {
if b.White.Pawns&squareMask != 0 {
return Pawn, isWhite
} else if b.White.Knights&squareMask != 0 {
return Knight, isWhite
} else if b.White.Bishops&squareMask != 0 {
return Bishop, isWhite
} else if b.White.Rooks&squareMask != 0 {
return Rook, isWhite
} else if b.White.Queens&squareMask != 0 {
return Queen, isWhite
} else if b.White.Kings&squareMask != 0 {
return King, isWhite
}
} else {
isWhite = false
if b.Black.Pawns&squareMask != 0 {
return Pawn, isWhite
} else if b.Black.Knights&squareMask != 0 {
return Knight, isWhite
} else if b.Black.Bishops&squareMask != 0 {
return Bishop, isWhite
} else if b.Black.Rooks&squareMask != 0 {
return Rook, isWhite
} else if b.Black.Queens&squareMask != 0 {
return Queen, isWhite
} else if b.Black.Kings&squareMask != 0 {
return King, isWhite
}
}
return Nothing, isWhite
}
// A testing-use function that ignores the error output
func parseMove(movestr string) Move {
res, _ := ParseMove(movestr)
return res
}
func (b *Bitboards) sanityCheck() {
if b.All != b.Pawns|b.Knights|b.Bishops|b.Rooks|b.Kings|b.Queens {
fmt.Println("Bitboard sanity check problem.")
}
if ((((((b.All ^ b.Pawns) ^ b.Knights) ^ b.Bishops) ^ b.Rooks) ^ b.Kings) ^ b.Queens) != 0 {
fmt.Println("Bitboard sanity check problem.")
}
}
// Some example valid move strings:
// e1e2 b4d6 e7e8q a2a1n
// TODO(dylhunn): Make the parser more forgiving. Eg: 0-0, O-O-O, a2-a3, D3D4
func ParseMove(movestr string) (Move, error) {
if movestr == "0000" {
return 0, nil
}
var mv Move
if len(movestr) < 4 || len(movestr) > 5 {
return mv, errors.New("Invalid move to parse.")
}
from, errf := AlgebraicToIndex(movestr[0:2])
to, errto := AlgebraicToIndex(movestr[2:4])
if errf != nil || errto != nil {
return mv, errors.New("Invalid move to parse.")
}
mv.Setto(Square(to)).Setfrom(Square(from))
if len(movestr) == 5 {
switch movestr[4] {
case 'n':
mv.Setpromote(Knight)
case 'b':
mv.Setpromote(Bishop)
case 'q':
mv.Setpromote(Queen)
case 'r':
mv.Setpromote(Rook)
default:
return mv, errors.New("Invalid promotion symbol in move.")
}
}
return mv, nil
}
func printBitboard(bitboard uint64) {
for i := 63; i >= 0; i-- {
j := (i/8)*8 + (7 - (i % 8))
if bitboard&(uint64(1)<<uint8(j)) == 0 {
fmt.Print("-")
} else {
fmt.Print("X")
}
if i%8 == 0 {
fmt.Println()
}
}
fmt.Println()
}
func printMoves(moves []Move) {
fmt.Println("Moves:")
for _, v := range moves {
fmt.Println(&v)
}
}
// Used for in-place algtoindex parsing where the result is guaranteed to be correct
func algebraicToIndexFatal(alg string) uint8 {
res, err := AlgebraicToIndex(alg)
if err != nil {
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.Fatal("Could not parse algebraic: ", alg)
}
return res
}
// Accepts an algebraic notation chess square, and converts it to a square ID
// as used by Dragontooth (in both the board and move types).
func AlgebraicToIndex(alg string) (uint8, error) {
firstchar := strings.ToLower(alg)[0]
if firstchar < 'a' || firstchar > 'h' || alg[1] < '1' || alg[1] > '8' {
return 64, errors.New("Invalid algebraic " + alg)
}
return (firstchar - 'a') + ((alg[1] - '1') * 8), nil
}
// Accepts a Dragontooth Square ID, and converts it to an algebraic square.
func IndexToAlgebraic(id Square) string {
if id < 0 || id > 63 {
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.Fatal("Could not parse index: ", id)
}
rune := rune((uint8(id) % 8) + 'a')
return fmt.Sprintf("%c", rune) + strconv.Itoa((int(id)/8)+1)
}
// Serializes a board position to a Fen string.
func (b *Board) ToFen() string {
b.White.sanityCheck()
b.Black.sanityCheck()
var position string
var empty int // empty slots
for i := 63; i >= 0; i-- {
// Loop file A to H, within ranks 8 to 1
currIdx := (i/8)*8 + (7 - (i % 8))
var currMask uint64
currMask = 1 << uint64(currIdx)
toprint := ""
if b.White.Pawns&currMask != 0 {
toprint += "P"
} else if b.White.Knights&currMask != 0 {
toprint += "N"
} else if b.White.Bishops&currMask != 0 {
toprint += "B"
} else if b.White.Rooks&currMask != 0 {
toprint += "R"
} else if b.White.Queens&currMask != 0 {
toprint += "Q"
} else if b.White.Kings&currMask != 0 {
toprint += "K"
} else if b.Black.Pawns&currMask != 0 {
toprint += "p"
} else if b.Black.Knights&currMask != 0 {
toprint += "n"
} else if b.Black.Bishops&currMask != 0 {
toprint += "b"
} else if b.Black.Rooks&currMask != 0 {
toprint += "r"
} else if b.Black.Queens&currMask != 0 {
toprint += "q"
} else if b.Black.Kings&currMask != 0 {
toprint += "k"
} else {
empty++
}
if toprint != "" {
if empty != 0 {
position += strconv.Itoa(empty)
empty = 0
}
position += toprint
}
if i%8 == 0 {
if empty != 0 {
position += strconv.Itoa(empty)
empty = 0
}
if i != 0 {
position += "/"
}
}
}
if b.Wtomove {
position += " w"
} else {
position += " b"
}
position += " "
castleCount := 0
if b.whiteCanCastleKingside() {
position += "K"
castleCount++
}
if b.whiteCanCastleQueenside() {
position += "Q"
castleCount++
}
if b.blackCanCastleKingside() {
position += "k"
castleCount++
}
if b.blackCanCastleQueenside() {
position += "q"
castleCount++
}
if castleCount == 0 {
position += "-"
}
position += " "
if b.enpassant != 0 {
position += IndexToAlgebraic(Square(b.enpassant))
} else {
position += "-"
}
position = position + " " + strconv.Itoa(int(b.Halfmoveclock)) + " " + strconv.Itoa(int(b.Fullmoveno))
return position
}
// Parse a board from a FEN string.
func ParseFen(fen string) Board {
// BUG(dylhunn): This FEN parsing implementation doesn't handle malformed inputs.
tokens := strings.Fields(fen)
var b Board
// replace digits with the appropriate number of dashes
for i := 1; i <= 8; i++ {
var replacement string
for j := 0; j < i; j++ {
replacement += "-"
}
tokens[0] = strings.Replace(tokens[0], strconv.Itoa(i), replacement, -1)
}
// reverse the order of the ranks, removing slashes
ranks := strings.Split(tokens[0], "/")
for i := 0; i < len(ranks)/2; i++ {
j := len(ranks) - i - 1
ranks[i], ranks[j] = ranks[j], ranks[i]
}
tokens[0] = ranks[0]
for i := 1; i < len(ranks); i++ {
tokens[0] += ranks[i]
}
// add every piece to the board
for i := uint8(0); i < 64; i++ {
switch tokens[0][i] {
case 'p':
b.Black.Pawns |= 1 << i
case 'n':
b.Black.Knights |= 1 << i
case 'b':
b.Black.Bishops |= 1 << i
case 'r':
b.Black.Rooks |= 1 << i
case 'q':
b.Black.Queens |= 1 << i
case 'k':
b.Black.Kings |= 1 << i
case 'P':
b.White.Pawns |= 1 << i
case 'N':
b.White.Knights |= 1 << i
case 'B':
b.White.Bishops |= 1 << i
case 'R':
b.White.Rooks |= 1 << i
case 'Q':
b.White.Queens |= 1 << i
case 'K':
b.White.Kings |= 1 << i
}
}
b.White.All = b.White.Pawns | b.White.Knights | b.White.Bishops | b.White.Rooks | b.White.Queens | b.White.Kings
b.Black.All = b.Black.Pawns | b.Black.Knights | b.Black.Bishops | b.Black.Rooks | b.Black.Queens | b.Black.Kings
b.Wtomove = tokens[1] == "w" || tokens[1] == "W"
if strings.Contains(tokens[2], "K") {
b.flipWhiteKingsideCastle()
}
if strings.Contains(tokens[2], "Q") {
b.flipWhiteQueensideCastle()
}
if strings.Contains(tokens[2], "k") {
b.flipBlackKingsideCastle()
}
if strings.Contains(tokens[2], "q") {
b.flipBlackQueensideCastle()
}
if tokens[3] != "-" {
res, err := AlgebraicToIndex(tokens[3])
if err != nil {
var b2 Board
return b2 // TODO(dylhunn): return error instead of blank board
}
b.enpassant = res
}
if len(tokens) > 4 {
result, _ := strconv.Atoi(tokens[4])
b.Halfmoveclock = uint8(result)
}
if len(tokens) > 5 {
result, _ := strconv.Atoi(tokens[5])
b.Fullmoveno = uint16(result)
}
b.hash = recomputeBoardHash(&b)
return b
}