-
Notifications
You must be signed in to change notification settings - Fork 0
/
movegen.go
333 lines (268 loc) · 9.02 KB
/
movegen.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
package main
var rookDistances [64][]int
var bishopDistances [64][]int
var mvvlvaTable [16][16]int
var mvvlvaVictimScores = []int{0, 100, 400, 200, 300, 500, 600, 0, 0, 100, 400, 200, 300, 500, 600}
// MVVLVA constants
const (
MVVLVA_PADDING = 1000000
KILLER_PRIMARY = 900000
KILLER_SECONDARY = 800000
)
const (
MOVEGEN_DIR_N = 0
MOVEGEN_DIR_E = 1
MOVEGEN_DIR_S = 2
MOVEGEN_DIR_W = 3
)
const piecesToChars = " PRNBQK prnbqk"
func initMoveGen() {
var order = []int{WHITE_PAWN, WHITE_KNIGHT, WHITE_BISHOP, WHITE_ROOK, WHITE_QUEEN, WHITE_KING, BLACK_PAWN, BLACK_KNIGHT, BLACK_BISHOP, BLACK_ROOK, BLACK_QUEEN, BLACK_KING}
for i := 0; i < len(order); i++ {
for j := 0; j < len(order); j++ {
attacker := order[i]
victim := order[j]
mvvlvaTable[victim][attacker] = (mvvlvaVictimScores[victim] + 6 - (mvvlvaVictimScores[attacker] / 100))
}
}
}
func generateMoves(pos Position, onlyCaptures bool) ([MAX_MOVES_IN_POS]Move, int) {
var moves [MAX_MOVES_IN_POS]Move
var index int = 0
generatePawnMoves(pos, &moves, &index, onlyCaptures)
generateSlidingMoves(pos, &moves, &index, SLIDING_GEN_TYPE_ROOK, onlyCaptures)
generateKnightMoves(pos, &moves, &index, onlyCaptures)
generateSlidingMoves(pos, &moves, &index, SLIDING_GEN_TYPE_BISHOP, onlyCaptures)
generateSlidingMoves(pos, &moves, &index, SLIDING_GEN_TYPE_QUEEN, onlyCaptures)
generateKingMoves(pos, &moves, &index, onlyCaptures)
return moves, index
}
func generateCaptures(pos Position) ([MAX_MOVES_IN_POS]Move, int) {
var moves [MAX_MOVES_IN_POS]Move
var index int = 0
generatePawnMoves(pos, &moves, &index, true)
generateSlidingMoves(pos, &moves, &index, SLIDING_GEN_TYPE_ROOK, true)
generateKnightMoves(pos, &moves, &index, true)
generateSlidingMoves(pos, &moves, &index, SLIDING_GEN_TYPE_BISHOP, true)
generateSlidingMoves(pos, &moves, &index, SLIDING_GEN_TYPE_QUEEN, true)
generateKingMoves(pos, &moves, &index, true)
return moves, index
}
func generatePawnMoves(pos Position, moves *[MAX_MOVES_IN_POS]Move, index *int, onlyCaptures bool) {
piece := createPiece(pos.turn, PIECE_PAWN)
pieceCount := pos.getPieceCount(piece)
opponentSide := pos.getOpponentSide()
for i := 0; i < pieceCount; i++ {
origSquare := pos.pieceList[piece][i]
rank := getRankOf(origSquare)
file := getFileOf(origSquare)
isPromotion := (rank == RANK_7 && pos.turn == SIDE_WHITE) || (rank == RANK_2 && pos.turn == SIDE_BLACK)
// Captures and EP
pawnCaptureDir1 := DIR_NE
pawnCaptureDir2 := DIR_NW
if pos.turn == SIDE_BLACK {
pawnCaptureDir1 = DIR_SE
pawnCaptureDir2 = DIR_SW
}
if file < FILE_H {
pawnCapture1Square := origSquare + pawnCaptureDir1
pawnCapture1Piece := pos.getPieceOn(pawnCapture1Square)
if (pawnCapture1Square == pos.state.epSquare) || (pawnCapture1Piece != NO_PIECE && getColorOf(pawnCapture1Piece) == opponentSide) {
if isPromotion {
for promotePiece := PROMOTION_PIECE_ROOK; promotePiece <= PROMOTION_PIECE_QUEEN; promotePiece++ {
move := Move{}
move.createWithPromotion(origSquare, pawnCapture1Square, promotePiece)
move.score = (mvvlvaVictimScores[promotePiece+2] - mvvlvaVictimScores[PIECE_PAWN]) + mvvlvaTable[pawnCapture1Piece][PIECE_PAWN]
moves[*index] = move
*index++
}
} else {
pawnCapture1Move := Move{}
pawnCapture1Move.score = mvvlvaTable[pawnCapture1Piece][PIECE_PAWN]
if pawnCapture1Square == pos.state.epSquare {
pawnCapture1Move.createWithEnPassant(origSquare, pawnCapture1Square)
} else {
pawnCapture1Move.create(origSquare, pawnCapture1Square)
}
moves[*index] = pawnCapture1Move
*index++
}
}
}
if file > FILE_A {
pawnCapture2Square := origSquare + pawnCaptureDir2
pawnCapture2Piece := pos.getPieceOn(pawnCapture2Square)
if (pawnCapture2Square == pos.state.epSquare) || (pawnCapture2Piece != NO_PIECE && getColorOf(pawnCapture2Piece) == opponentSide) {
if isPromotion {
for promotePiece := PROMOTION_PIECE_ROOK; promotePiece <= PROMOTION_PIECE_QUEEN; promotePiece++ {
move := Move{}
move.createWithPromotion(origSquare, pawnCapture2Square, promotePiece)
move.score = (mvvlvaVictimScores[promotePiece+2] - mvvlvaVictimScores[PIECE_PAWN]) + mvvlvaTable[pawnCapture2Piece][PIECE_PAWN]
moves[*index] = move
*index++
}
} else {
pawnCapture2Move := Move{}
pawnCapture2Move.score = mvvlvaTable[pawnCapture2Piece][PIECE_PAWN]
if pawnCapture2Square == pos.state.epSquare {
pawnCapture2Move.createWithEnPassant(origSquare, pawnCapture2Square)
} else {
pawnCapture2Move.create(origSquare, pawnCapture2Square)
}
moves[*index] = pawnCapture2Move
*index++
}
}
}
if onlyCaptures {
continue
}
// Pawn pushes
pawnPushDir := DIR_N
if pos.turn == SIDE_BLACK {
pawnPushDir = DIR_S
}
// Single push
singlePushSquare := origSquare + pawnPushDir
if pos.getPieceOn(singlePushSquare) == NO_PIECE {
if isPromotion {
for promotePiece := PROMOTION_PIECE_ROOK; promotePiece <= PROMOTION_PIECE_QUEEN; promotePiece++ {
move := Move{}
move.createWithPromotion(origSquare, singlePushSquare, promotePiece)
moves[*index] = move
*index++
}
} else {
singlePushMove := Move{}
singlePushMove.create(origSquare, singlePushSquare)
moves[*index] = singlePushMove
*index++
}
// Double push
if (rank == RANK_2 && pos.turn == SIDE_WHITE) || (rank == RANK_7 && pos.turn == SIDE_BLACK) {
doublePushSquare := origSquare + (pawnPushDir * 2)
if pos.getPieceOn(doublePushSquare) == NO_PIECE {
doublePushMove := Move{}
doublePushMove.create(origSquare, doublePushSquare)
moves[*index] = doublePushMove
*index++
}
}
}
}
}
const (
SLIDING_GEN_TYPE_BISHOP = 0
SLIDING_GEN_TYPE_ROOK = 1
SLIDING_GEN_TYPE_QUEEN = 2
)
func generateSlidingMoves(pos Position, moves *[MAX_MOVES_IN_POS]Move, index *int, genType int, onlyCaptures bool) {
piece := PIECE_BISHOP
if genType == SLIDING_GEN_TYPE_ROOK {
piece = PIECE_ROOK
} else if genType == SLIDING_GEN_TYPE_QUEEN {
piece = PIECE_QUEEN
}
piece = createPiece(pos.turn, piece)
pieceCount := pos.getPieceCount(piece)
for i := 0; i < pieceCount; i++ {
origSquare := pos.pieceList[piece][i]
var attacks uint64 = 0
occupancy := pos.getAllPieces()
// Get attacks bitboard
if genType == SLIDING_GEN_TYPE_BISHOP {
attacks |= getBishopAttacks(origSquare, occupancy)
} else if genType == SLIDING_GEN_TYPE_ROOK {
attacks |= getRookAttacks(origSquare, occupancy)
} else {
attacks |= (getBishopAttacks(origSquare, occupancy) | getRookAttacks(origSquare, occupancy))
}
// Only use relevant square & pieces
if onlyCaptures {
attacks &= pos.getPiecesByColor(pos.getOpponentSide())
} else {
attacks &= ^pos.getPiecesByColor(pos.turn)
}
// Go through the attack moves
for attacks != 0 {
square := bbPopLSB(&attacks)
move := Move{}
capPiece := pos.getPieceOn(square)
if capPiece != NO_PIECE {
move.score = mvvlvaTable[capPiece][piece]
}
move.create(origSquare, square)
moves[*index] = move
*index++
}
}
}
func generateKnightMoves(pos Position, moves *[MAX_MOVES_IN_POS]Move, index *int, onlyCaptures bool) {
piece := createPiece(pos.turn, PIECE_KNIGHT)
pieceCount := pos.getPieceCount(piece)
for i := 0; i < pieceCount; i++ {
origSquare := pos.pieceList[piece][i]
attacks := getKnightAttacks(origSquare)
if onlyCaptures {
attacks &= pos.getPiecesByColor(pos.getOpponentSide())
} else {
attacks &= ^pos.getPiecesByColor(pos.turn)
}
for attacks != 0 {
square := bbPopLSB(&attacks)
move := Move{}
capPiece := pos.getPieceOn(square)
if capPiece != NO_PIECE {
move.score = mvvlvaTable[capPiece][piece]
}
move.create(origSquare, square)
moves[*index] = move
*index++
}
}
}
func generateKingMoves(pos Position, moves *[MAX_MOVES_IN_POS]Move, index *int, onlyCaptures bool) {
piece := createPiece(pos.turn, PIECE_KING)
pieceCount := pos.getPieceCount(piece)
for i := 0; i < pieceCount; i++ {
origSquare := pos.pieceList[piece][i]
// Regular moves
attacks := getKingAttacks(origSquare)
if onlyCaptures {
attacks &= pos.getPiecesByColor(pos.getOpponentSide())
} else {
attacks &= ^pos.getPiecesByColor(pos.turn)
}
for attacks != 0 {
square := bbPopLSB(&attacks)
move := Move{}
capPiece := pos.getPieceOn(square)
if capPiece != NO_PIECE {
move.score = mvvlvaTable[capPiece][piece]
}
move.create(origSquare, square)
moves[*index] = move
*index++
}
if onlyCaptures {
continue
}
// Castling
if pos.canCastleShort(pos.turn) {
if pos.getPieceOn(origSquare+1) == NO_PIECE && pos.getPieceOn(origSquare+2) == NO_PIECE {
move := Move{}
move.createWithCastling(origSquare, origSquare+2)
moves[*index] = move
*index++
}
}
if pos.canCastleLong(pos.turn) {
if pos.getPieceOn(origSquare-1) == NO_PIECE && pos.getPieceOn(origSquare-2) == NO_PIECE && pos.getPieceOn(origSquare-3) == NO_PIECE {
move := Move{}
move.createWithCastling(origSquare, origSquare-2)
moves[*index] = move
*index++
}
}
}
}