-
Notifications
You must be signed in to change notification settings - Fork 0
/
makemove.c
348 lines (264 loc) · 7.54 KB
/
makemove.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "defs.h"
#include "stdio.h"
#define HASH_PCE(pce,sq) (pos->posKey ^= (PieceKeys[(pce)][(sq)]))
#define HASH_CA (pos->posKey ^= (CastleKeys[(pos->castlePerm)]))
#define HASH_SIDE (pos->posKey ^= (SideKey))
#define HASH_EP (pos->posKey ^= (PieceKeys[EMPTY][(pos->enPas)]))
const int CastlePerm[120] = {
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 13, 15, 15, 15, 12, 15, 15, 14, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 7, 15, 15, 15, 3, 15, 15, 11, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15
};
static void ClearPiece(const int sq, S_BOARD *pos) {
ASSERT(SqOnBoard(sq));
int pce = pos->pieces[sq];
ASSERT(PieceValid(pce));
int col = PieceCol[pce];
int index = 0;
int t_pceNum = -1;
HASH_PCE(pce,sq);
pos->pieces[sq] = EMPTY;
pos->material[col] -= PieceVal[pce];
if(PieceBig[pce]) {
pos->bigPce[col]--;
if(PieceMaj[pce]) {
pos->majPce[col]--;
} else {
pos->minPce[col]--;
}
} else {
CLRBIT(pos->pawns[col],SQ64(sq));
CLRBIT(pos->pawns[BOTH],SQ64(sq));
}
for(index = 0; index < pos->pceNum[pce]; ++index) {
if(pos->pList[pce][index] == sq) {
t_pceNum = index;
break;
}
}
ASSERT(t_pceNum != -1);
pos->pceNum[pce]--;
pos->pList[pce][t_pceNum] = pos->pList[pce][pos->pceNum[pce]];
}
static void AddPiece(const int sq, S_BOARD *pos, const int pce) {
ASSERT(PieceValid(pce));
ASSERT(SqOnBoard(sq));
int col = PieceCol[pce];
HASH_PCE(pce,sq);
pos->pieces[sq] = pce;
if(PieceBig[pce]) {
pos->bigPce[col]++;
if(PieceMaj[pce]) {
pos->majPce[col]++;
} else {
pos->minPce[col]++;
}
} else {
SETBIT(pos->pawns[col],SQ64(sq));
SETBIT(pos->pawns[BOTH],SQ64(sq));
}
pos->material[col] += PieceVal[pce];
pos->pList[pce][pos->pceNum[pce]++] = sq;
}
static void MovePiece(const int from, const int to, S_BOARD *pos) {
ASSERT(SqOnBoard(from));
ASSERT(SqOnBoard(to));
int index = 0;
int pce = pos->pieces[from];
int col = PieceCol[pce];
#ifdef DEBUG
int t_PieceNum = FALSE;
#endif
HASH_PCE(pce,from);
pos->pieces[from] = EMPTY;
HASH_PCE(pce,to);
pos->pieces[to] = pce;
if(!PieceBig[pce]) {
CLRBIT(pos->pawns[col],SQ64(from));
CLRBIT(pos->pawns[BOTH],SQ64(from));
SETBIT(pos->pawns[col],SQ64(to));
SETBIT(pos->pawns[BOTH],SQ64(to));
}
for(index = 0; index < pos->pceNum[pce]; ++index) {
if(pos->pList[pce][index] == from) {
pos->pList[pce][index] = to;
#ifdef DEBUG
t_PieceNum = TRUE;
#endif
break;
}
}
ASSERT(t_PieceNum);
}
int MakeMove(S_BOARD *pos, int move) {
ASSERT(CheckBoard(pos));
int from = FROMSQ(move);
int to = TOSQ(move);
int side = pos->side;
ASSERT(SqOnBoard(from));
ASSERT(SqOnBoard(to));
ASSERT(SideValid(side));
ASSERT(PieceValid(pos->pieces[from]));
pos->history[pos->hisPly].posKey = pos->posKey;
if(move & MFLAGEP) {
if(side == WHITE) {
ClearPiece(to-10,pos);
} else {
ClearPiece(to+10,pos);
}
} else if (move & MFLAGCA) {
switch(to) {
case C1:
MovePiece(A1, D1, pos);
break;
case C8:
MovePiece(A8, D8, pos);
break;
case G1:
MovePiece(H1, F1, pos);
break;
case G8:
MovePiece(H8, F8, pos);
break;
default: ASSERT(FALSE); break;
}
}
if(pos->enPas != NO_SQ) HASH_EP;
HASH_CA;
pos->history[pos->hisPly].move = move;
pos->history[pos->hisPly].fiftyMove = pos->fiftyMove;
pos->history[pos->hisPly].enPas = pos->enPas;
pos->history[pos->hisPly].castlePerm = pos->castlePerm;
pos->castlePerm &= CastlePerm[from];
pos->castlePerm &= CastlePerm[to];
pos->enPas = NO_SQ;
HASH_CA;
int captured = CAPTURED(move);
pos->fiftyMove++;
if(captured != EMPTY) {
ASSERT(PieceValid(captured));
ClearPiece(to, pos);
pos->fiftyMove = 0;
}
pos->hisPly++;
pos->ply++;
if(PiecePawn[pos->pieces[from]]) {
pos->fiftyMove = 0;
if(move & MFLAGPS) {
if(side==WHITE) {
pos->enPas=from+10;
ASSERT(RanksBrd[pos->enPas]==RANK_3);
} else {
pos->enPas=from-10;
ASSERT(RanksBrd[pos->enPas]==RANK_6);
}
HASH_EP;
}
}
MovePiece(from, to, pos);
int prPce = PROMOTED(move);
if(prPce != EMPTY) {
ASSERT(PieceValid(prPce) && !PiecePawn[prPce]);
ClearPiece(to, pos);
AddPiece(to, pos, prPce);
}
if(PieceKing[pos->pieces[to]]) {
pos->KingSq[pos->side] = to;
}
pos->side ^= 1;
HASH_SIDE;
ASSERT(CheckBoard(pos));
if(SqAttacked(pos->KingSq[side],pos->side,pos)) {
TakeMove(pos);
return FALSE;
}
return TRUE;
}
void TakeMove(S_BOARD *pos) {
ASSERT(CheckBoard(pos));
pos->hisPly--;
pos->ply--;
int move = pos->history[pos->hisPly].move;
int from = FROMSQ(move);
int to = TOSQ(move);
ASSERT(SqOnBoard(from));
ASSERT(SqOnBoard(to));
if(pos->enPas != NO_SQ) HASH_EP;
HASH_CA;
pos->castlePerm = pos->history[pos->hisPly].castlePerm;
pos->fiftyMove = pos->history[pos->hisPly].fiftyMove;
pos->enPas = pos->history[pos->hisPly].enPas;
if(pos->enPas != NO_SQ) HASH_EP;
HASH_CA;
pos->side ^= 1;
HASH_SIDE;
if(MFLAGEP & move) {
if(pos->side == WHITE) {
AddPiece(to-10, pos, bP);
} else {
AddPiece(to+10, pos, wP);
}
} else if(MFLAGCA & move) {
switch(to) {
case C1: MovePiece(D1, A1, pos); break;
case C8: MovePiece(D8, A8, pos); break;
case G1: MovePiece(F1, H1, pos); break;
case G8: MovePiece(F8, H8, pos); break;
default: ASSERT(FALSE); break;
}
}
MovePiece(to, from, pos);
if(PieceKing[pos->pieces[from]]) {
pos->KingSq[pos->side] = from;
}
int captured = CAPTURED(move);
if(captured != EMPTY) {
ASSERT(PieceValid(captured));
AddPiece(to, pos, captured);
}
if(PROMOTED(move) != EMPTY) {
ASSERT(PieceValid(PROMOTED(move)) && !PiecePawn[PROMOTED(move)]);
ClearPiece(from, pos);
AddPiece(from, pos, (PieceCol[PROMOTED(move)] == WHITE ? wP : bP));
}
ASSERT(CheckBoard(pos));
}
void MakeNullMove(S_BOARD *pos) {
ASSERT(CheckBoard(pos));
ASSERT(!SqAttacked(pos->KingSq[pos->side],pos->side^1,pos));
pos->ply++;
pos->history[pos->hisPly].posKey = pos->posKey;
if(pos->enPas != NO_SQ) HASH_EP;
pos->history[pos->hisPly].move = NOMOVE;
pos->history[pos->hisPly].fiftyMove = pos->fiftyMove;
pos->history[pos->hisPly].enPas = pos->enPas;
pos->history[pos->hisPly].castlePerm = pos->castlePerm;
pos->enPas = NO_SQ;
pos->side ^= 1;
pos->hisPly++;
HASH_SIDE;
ASSERT(CheckBoard(pos));
return;
} // MakeNullMove
void TakeNullMove(S_BOARD *pos) {
ASSERT(CheckBoard(pos));
pos->hisPly--;
pos->ply--;
if(pos->enPas != NO_SQ) HASH_EP;
pos->castlePerm = pos->history[pos->hisPly].castlePerm;
pos->fiftyMove = pos->history[pos->hisPly].fiftyMove;
pos->enPas = pos->history[pos->hisPly].enPas;
if(pos->enPas != NO_SQ) HASH_EP;
pos->side ^= 1;
HASH_SIDE;
ASSERT(CheckBoard(pos));
}