forked from ulthiel/polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_gen.c
328 lines (234 loc) · 7.8 KB
/
move_gen.c
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
// move_gen.c
// includes
#include "attack.h"
#include "board.h"
#include "colour.h"
#include "list.h"
#include "move.h"
#include "move_gen.h"
#include "move_legal.h"
#include "piece.h"
#include "util.h"
// prototypes
static void add_all_moves (list_t * list, const board_t * board);
static void add_castle_moves (list_t * list, const board_t * board);
static void add_pawn_move (list_t * list, int from, int to);
// functions
// gen_legal_moves()
void gen_legal_moves(list_t * list, const board_t * board) {
ASSERT(list!=NULL);
ASSERT(board_is_ok(board));
gen_moves(list,board);
filter_legal(list,board);
}
// gen_moves()
void gen_moves(list_t * list, const board_t * board) {
ASSERT(list!=NULL);
ASSERT(board_is_ok(board));
list_clear(list);
add_all_moves(list,board);
if (!is_in_check(board,board->turn)) add_castle_moves(list,board);
}
// add_all_moves()
static void add_all_moves(list_t * list, const board_t * board) {
int me, opp;
const uint8 * ptr;
const sint8 * ptr_inc;
int from, to;
int inc;
int piece, capture;
ASSERT(list_is_ok(list));
ASSERT(board_is_ok(board));
me = board->turn;
opp = colour_opp(me);
for (ptr = board->list[me]; (from=*ptr) != SquareNone; ptr++) {
piece = board->square[from];
ASSERT(colour_equal(piece,me));
switch (piece_type(piece)) {
case WhitePawn64:
to = from + 15;
if (to == board->ep_square || colour_equal(board->square[to],opp)) {
add_pawn_move(list,from,to);
}
to = from + 17;
if (to == board->ep_square || colour_equal(board->square[to],opp)) {
add_pawn_move(list,from,to);
}
to = from + 16;
if (board->square[to] == Empty) {
add_pawn_move(list,from,to);
if (square_rank(from) == Rank2) {
to = from + 32;
if (board->square[to] == Empty) {
ASSERT(!square_is_promote(to));
list_add(list,move_make(from,to));
}
}
}
break;
case BlackPawn64:
to = from - 17;
if (to == board->ep_square || colour_equal(board->square[to],opp)) {
add_pawn_move(list,from,to);
}
to = from - 15;
if (to == board->ep_square || colour_equal(board->square[to],opp)) {
add_pawn_move(list,from,to);
}
to = from - 16;
if (board->square[to] == Empty) {
add_pawn_move(list,from,to);
if (square_rank(from) == Rank7) {
to = from - 32;
if (board->square[to] == Empty) {
ASSERT(!square_is_promote(to));
list_add(list,move_make(from,to));
}
}
}
break;
case Knight64:
for (ptr_inc = KnightInc; (inc=*ptr_inc) != IncNone; ptr_inc++) {
to = from + inc;
capture = board->square[to];
if (capture == Empty || colour_equal(capture,opp)) {
list_add(list,move_make(from,to));
}
}
break;
case Bishop64:
for (ptr_inc = BishopInc; (inc=*ptr_inc) != IncNone; ptr_inc++) {
for (to = from+inc; (capture=board->square[to]) == Empty; to += inc) {
list_add(list,move_make(from,to));
}
if (colour_equal(capture,opp)) {
list_add(list,move_make(from,to));
}
}
break;
case Rook64:
for (ptr_inc = RookInc; (inc=*ptr_inc) != IncNone; ptr_inc++) {
for (to = from+inc; (capture=board->square[to]) == Empty; to += inc) {
list_add(list,move_make(from,to));
}
if (colour_equal(capture,opp)) {
list_add(list,move_make(from,to));
}
}
break;
case Queen64:
for (ptr_inc = QueenInc; (inc=*ptr_inc) != IncNone; ptr_inc++) {
for (to = from+inc; (capture=board->square[to]) == Empty; to += inc) {
list_add(list,move_make(from,to));
}
if (colour_equal(capture,opp)) {
list_add(list,move_make(from,to));
}
}
break;
case King64:
for (ptr_inc = KingInc; (inc=*ptr_inc) != IncNone; ptr_inc++) {
to = from + inc;
capture = board->square[to];
if (capture == Empty || colour_equal(capture,opp)) {
list_add(list,move_make(from,to));
}
}
break;
default:
ASSERT(FALSE);
break;
}
}
}
// add_castle_moves()
static void add_castle_moves(list_t * list, const board_t * board) {
int me, opp;
int rank;
int king_from, king_to;
int rook_from, rook_to;
bool legal;
int inc;
int sq;
ASSERT(list_is_ok(list));
ASSERT(board_is_ok(board));
ASSERT(!is_in_check(board,board->turn));
me = board->turn;
opp = colour_opp(me);
rank = colour_is_white(me) ? Rank1 : Rank8;
// h-side castling
if (board->castle[me][SideH] != SquareNone) {
king_from = king_pos(board,me);
king_to = square_make(FileG,rank);
rook_from = board->castle[me][SideH];
rook_to = square_make(FileF,rank);
ASSERT(square_rank(king_from)==rank);
ASSERT(square_rank(rook_from)==rank);
ASSERT(board->square[king_from]==(King64|me)); // HACK
ASSERT(board->square[rook_from]==(Rook64|me)); // HACK
ASSERT(rook_from>king_from);
legal = TRUE;
if (king_to != king_from) {
inc = (king_to > king_from) ? +1 : -1;
for (sq = king_from+inc; TRUE; sq += inc) {
if (sq != rook_from && board->square[sq] != Empty) legal = FALSE;
if (is_attacked(board,sq,opp)) legal = FALSE;
if (sq == king_to) break;
}
}
if (rook_to != rook_from) {
inc = (rook_to > rook_from) ? +1 : -1;
for (sq = rook_from+inc; TRUE; sq += inc) {
if (sq != king_from && board->square[sq] != Empty) legal = FALSE;
if (sq == rook_to) break;
}
}
if (legal) list_add(list,move_make(king_from,rook_from));
}
// a-side castling
if (board->castle[me][SideA] != SquareNone) {
king_from = king_pos(board,me);
king_to = square_make(FileC,rank);
rook_from = board->castle[me][SideA];
rook_to = square_make(FileD,rank);
ASSERT(square_rank(king_from)==rank);
ASSERT(square_rank(rook_from)==rank);
ASSERT(board->square[king_from]==(King64|me)); // HACK
ASSERT(board->square[rook_from]==(Rook64|me)); // HACK
ASSERT(rook_from<king_from);
legal = TRUE;
if (king_to != king_from) {
inc = (king_to > king_from) ? +1 : -1;
for (sq = king_from+inc; TRUE; sq += inc) {
if (sq != rook_from && board->square[sq] != Empty) legal = FALSE;
if (is_attacked(board,sq,opp)) legal = FALSE;
if (sq == king_to) break;
}
}
if (rook_to != rook_from) {
inc = (rook_to > rook_from) ? +1 : -1;
for (sq = rook_from+inc; TRUE; sq += inc) {
if (sq != king_from && board->square[sq] != Empty) legal = FALSE;
if (sq == rook_to) break;
}
}
if (legal) list_add(list,move_make(king_from,rook_from));
}
}
// add_pawn_move()
static void add_pawn_move(list_t * list, int from, int to) {
int move;
ASSERT(list_is_ok(list));
ASSERT(square_is_ok(from));
ASSERT(square_is_ok(to));
move = move_make(from,to);
if (square_is_promote(to)) {
list_add(list,move|MovePromoteKnight);
list_add(list,move|MovePromoteBishop);
list_add(list,move|MovePromoteRook);
list_add(list,move|MovePromoteQueen);
} else {
list_add(list,move);
}
}
// end of move_gen.cpp