/
board.go
318 lines (295 loc) · 7.41 KB
/
board.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
package main
import (
"fmt"
"strconv"
"strings"
)
/*
+----+----+----+----+----+----+----+----+
8 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 8th rank
+----+----+----+----+----+----+----+----+
7 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 7th rank
+----+----+----+----+----+----+----+----+
6 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 6th rank
+----+----+----+----+----+----+----+----+
5 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 5th rank
+----+----+----+----+----+----+----+----+
4 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 4th rank
+----+----+----+----+----+----+----+----+
3 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 3rd rank
+----+----+----+----+----+----+----+----+
2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 2nd rank
+----+----+----+----+----+----+----+----+
1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1st rank
+----+----+----+----+----+----+----+----+
A B C D E F G H - file(s)
*/
var COORD_MAP = [BOARD_SQUARES]string{
"A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1",
"A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2",
"A3", "B3", "C3", "D3", "E3", "F3", "G3", "H3",
"A4", "B4", "C4", "D4", "E4", "F4", "G4", "H4",
"A5", "B5", "C5", "D5", "E5", "F5", "G5", "H5",
"A6", "B6", "C6", "D6", "E6", "F6", "G6", "H6",
"A7", "B7", "C7", "D7", "E7", "F7", "G7", "H7",
"A8", "B8", "C8", "D8", "E8", "F8", "G8", "H8",
}
var SQUARE_MAP = [SQUARE_TYPES]string{
"EMPTY", "WHITE_PAWN", "WHITE_KNIGHT", "WHITE_BISHOP",
"WHITE_ROOK", "WHITE_QUEEN", "WHITE_KING", "BLACK_PAWN",
"BLACK_KNIGHT", "BLACK_BISHOP", "BLACK_ROOK", "BLACK_QUEEN",
"BLACK_KING",
}
type Board struct {
squares []Square
bbWP Bitboard
bbWN Bitboard
bbWB Bitboard
bbWR Bitboard
bbWQ Bitboard
bbWK Bitboard
bbBP Bitboard
bbBN Bitboard
bbBB Bitboard
bbBR Bitboard
bbBQ Bitboard
bbBK Bitboard
bbWhitePieces Bitboard
bbBlackPieces Bitboard
bbAllPieces Bitboard
kingCoords []Coord
sideToMove Color
castleRights CastleRights
epCoord Coord
halfMove HalfMove
fullMove int
hash Hash
undoIndex int
undos []Undo
}
func NewBoard(fen string) (*Board, error) {
InitBitboards()
fenParts := strings.Split(fen, " ")
if len(fenParts) != 6 {
return nil, fmt.Errorf("FEN parts: %v != 6", len(fenParts))
}
b := Board{}
b.squares = make([]Square, BOARD_SQUARES)
b.undos = make([]Undo, MAX_GAME_MOVES)
b.kingCoords = make([]Coord, PLAYERS)
ranks := strings.Split(fenParts[0], "/")
if len(ranks) != 8 {
return nil, fmt.Errorf("Ranks: %v != 8", len(ranks))
}
coord := Coord(BOARD_SQUARES - 1)
for _, rank := range ranks {
runes := []rune(rank)
for r := len(runes) - 1; r >= 0; r-- {
char := string(runes[r])
switch char {
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "P":
b.squares[coord] = WHITE_PAWN
b.bbWP.SetBit(coord)
case "N":
b.squares[coord] = WHITE_KNIGHT
b.bbWN.SetBit(coord)
case "B":
b.squares[coord] = WHITE_BISHOP
b.bbWB.SetBit(coord)
case "R":
b.squares[coord] = WHITE_ROOK
b.bbWR.SetBit(coord)
case "Q":
b.squares[coord] = WHITE_QUEEN
b.bbWQ.SetBit(coord)
case "K":
b.squares[coord] = WHITE_KING
b.bbWK.SetBit(coord)
b.kingCoords[WHITE] = coord
case "p":
b.squares[coord] = BLACK_PAWN
b.bbBP.SetBit(coord)
case "n":
b.squares[coord] = BLACK_KNIGHT
b.bbBN.SetBit(coord)
case "b":
b.squares[coord] = BLACK_BISHOP
b.bbBB.SetBit(coord)
case "r":
b.squares[coord] = BLACK_ROOK
b.bbBR.SetBit(coord)
case "q":
b.squares[coord] = BLACK_QUEEN
b.bbBQ.SetBit(coord)
case "k":
b.squares[coord] = BLACK_KING
b.bbBK.SetBit(coord)
b.kingCoords[BLACK] = coord
default:
return nil, fmt.Errorf("Invalid piece/digit in fen string: %v", char)
}
count, err := strconv.Atoi(char)
if err != nil {
coord--
} else {
for i := 0; i < count; i++ {
b.squares[coord] = EMPTY
coord--
}
}
}
}
b.UpdateUnionBitboards()
sideToMove := fenParts[1]
if sideToMove == "w" {
b.sideToMove = WHITE
} else if sideToMove == "b" {
b.sideToMove = BLACK
} else {
return nil, fmt.Errorf("Invalid side to move in fen string: %v", sideToMove)
}
castlingRights := fenParts[2]
for _, char := range []rune(castlingRights) {
switch string(char) {
case "-":
case "K":
b.castleRights |= CASTLING_RIGHTS_WHITE_KING_MASK
case "Q":
b.castleRights |= CASTLING_RIGHTS_WHITE_QUEEN_MASK
case "k":
b.castleRights |= CASTLING_RIGHTS_BLACK_KING_MASK
case "q":
b.castleRights |= CASTLING_RIGHTS_BLACK_QUEEN_MASK
default:
return nil, fmt.Errorf("Invalid castling rights in fen string: %v", string(char))
}
}
var err error
if fenParts[3] != "-" {
b.epCoord, err = StringToCoord(fenParts[3])
if err != nil {
return nil, err
}
}
halfMove, err := strconv.Atoi(fenParts[4])
if err != nil {
return nil, err
}
b.halfMove = HalfMove(halfMove)
b.fullMove, err = strconv.Atoi(fenParts[5])
if err != nil {
return nil, err
}
b.GenerateBoardHash()
return &b, nil
}
func (b *Board) UpdateUnionBitboards() {
b.bbWhitePieces = b.bbWP | b.bbWN | b.bbWB | b.bbWR | b.bbWQ | b.bbWK
b.bbBlackPieces = b.bbBP | b.bbBN | b.bbBB | b.bbBR | b.bbBQ | b.bbBK
b.bbAllPieces = b.bbWhitePieces | b.bbBlackPieces
}
func (b Board) CopyBoard() Board {
squares := make([]Square, len(b.squares))
copy(squares, b.squares)
undos := make([]Undo, len(b.undos))
copy(undos, b.undos)
kingCoords := make([]Coord, len(b.kingCoords))
copy(kingCoords, b.kingCoords)
cb := Board{
squares: squares,
bbWP: b.bbWP,
bbWN: b.bbWN,
bbWB: b.bbWB,
bbWR: b.bbWR,
bbWQ: b.bbWQ,
bbWK: b.bbWK,
bbBP: b.bbBP,
bbBN: b.bbBN,
bbBB: b.bbBB,
bbBR: b.bbBR,
bbBQ: b.bbBQ,
bbBK: b.bbBK,
bbWhitePieces: b.bbWhitePieces,
bbBlackPieces: b.bbBlackPieces,
bbAllPieces: b.bbAllPieces,
kingCoords: kingCoords,
sideToMove: b.sideToMove,
castleRights: b.castleRights,
epCoord: b.epCoord,
halfMove: b.halfMove,
fullMove: b.fullMove,
hash: b.hash,
undoIndex: b.undoIndex,
undos: undos,
}
return cb
}
var PRINT_MAP = [64]Coord{
A8, B8, C8, D8, E8, F8, G8, H8,
A7, B7, C7, D7, E7, F7, G7, H7,
A6, B6, C6, D6, E6, F6, G6, H6,
A5, B5, C5, D5, E5, F5, G5, H5,
A4, B4, C4, D4, E4, F4, G4, H4,
A3, B3, C3, D3, E3, F3, G3, H3,
A2, B2, C2, D2, E2, F2, G2, H2,
A1, B1, C1, D1, E1, F1, G1, H1,
}
func (b Board) ToString() string {
s := ""
sep := "\n_________________________\n"
s += sep
rank := 8
for i, coord := range PRINT_MAP {
if i != 0 && i%8 == 0 {
s += "| "
s += strconv.Itoa(rank)
rank--
s += sep
}
s += "|"
square := b.squares[coord]
switch square {
case EMPTY:
s += " "
case WHITE_PAWN:
s += "♟"
case WHITE_KNIGHT:
s += "♞"
case WHITE_BISHOP:
s += "♝"
case WHITE_ROOK:
s += "♜"
case WHITE_QUEEN:
s += "♛"
case WHITE_KING:
s += "♚"
case BLACK_PAWN:
s += "♙"
case BLACK_KNIGHT:
s += "♘"
case BLACK_BISHOP:
s += "♗"
case BLACK_ROOK:
s += "♖"
case BLACK_QUEEN:
s += "♕"
case BLACK_KING:
s += "♔"
default:
s += "♠"
}
s += " "
}
s += "| 1"
s += sep
s += " A B C D E F G H"
return s
}