-
Notifications
You must be signed in to change notification settings - Fork 0
/
makemove.c
381 lines (290 loc) · 8.45 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "defs.h"
#include "stdio.h"
#define HASH_CA (pos->posKey ^= (CastleKeys[(pos->castlePerm)]))
#define HASH_PCE(piece,sq) (pos->posKey ^= (PieceKeys[(piece)][(sq)]))
#define HASH_SIDE (pos->posKey ^= (SideKey))
#define HASH_EP (pos->posKey ^= (PieceKeys[EMPTY][(pos->enPassantSQ)]))
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 piece = pos->pieces[sq];
ASSERT(PieceValid(piece));
int color = PieceColor[piece];
int i = 0;
int targetPceNum = -1;
//remove piece from posKey
HASH_PCE(piece,sq);
pos->pieces[sq] = EMPTY;
pos->material[color] -= PieceVal[piece];
//reduces the number of the various arrays the piece was indexed in
if(PieceBig[piece]) {
pos->bigPce[color]--;
if(PieceMaj[piece]) {
pos->majPce[color]--;
}
else {
pos->minPce[color]--;
}
//if it is a pawn, removes it from the bitboard
} else {
CLRBIT(pos->pawns[color], SQ64(sq));
CLRBIT(pos->pawns[BOTH], SQ64(sq));
}
//finds the piece in the pList
for (i = 0; i < pos->pceNum[piece]; i++) {
if(pos->pList[piece][i] == sq) {
targetPceNum = i;
break;
}
}
ASSERT(targetPceNum != -1);
//reduces the pceNum for the piece we're removing
pos->pceNum[piece]--;
//overwrites the piece to be removed with the current last piece in the pList
pos->pList[piece][targetPceNum] = pos->pList[piece][pos->pceNum[piece]];
}
static void AddPiece(const int sq, S_BOARD *pos, const int piece) {
ASSERT(PieceValid(piece))
ASSERT(SqOnBoard(sq));
int color = PieceColor[piece];
//adds piece to posKey
HASH_PCE(piece,sq);
pos->pieces[sq] = piece;
pos->material[color] += PieceVal[piece];
//reduces the number of the various arrays the piece was indexed in
if(PieceBig[piece]) {
pos->bigPce[color]++;
if(PieceMaj[piece]) {
pos->majPce[color]++;
}
else {
pos->minPce[color]++;
}
//if it is a pawn, removes it from the bitboard
} else {
SETBIT(pos->pawns[color], SQ64(sq));
SETBIT(pos->pawns[BOTH], SQ64(sq));
}
//adds the piece to the end of pList and increase the number of pieces of this type by 1
pos->pList[piece][pos->pceNum[piece]++] = sq;
}
static void MovePiece(const int from, const int to, S_BOARD *pos) {
ASSERT(SqOnBoard(from));
ASSERT(SqOnBoard(to));
int i = 0;
int piece = pos->pieces[from];
int color = PieceColor[piece];
#ifdef DEBUG
int targetPceNum = FALSE;
#endif
//removes the piece and its old square from the posKey and sets the from-square to empty
HASH_PCE(piece, from);
pos->pieces[from] = EMPTY;
//adds the piece and its new square to the posKey and set the to-square to piece
HASH_PCE(piece, to);
pos->pieces[to] = piece;
//if its a pawn, change the bitboard
if(!PieceBig[piece]) {
CLRBIT(pos->pawns[color], SQ64(from));
CLRBIT(pos->pawns[BOTH], SQ64(from));
SETBIT(pos->pawns[color], SQ64(to));
SETBIT(pos->pawns[BOTH], SQ64(to));
}
for(i = 0; i < pos->pceNum[piece]; i++) {
if(pos->pList[piece][i] == from) {
pos->pList[piece][i] = to;
#ifdef DEBUG
targetPceNum = TRUE;
#endif
break;
}
}
ASSERT(targetPceNum);
}
int MakeMove(S_BOARD *pos, int move) {
ASSERT(checkBoard(pos));
//bitshifts the move to get its values as set up in the macros INT MOVE
int from = FROM(move);
int to = TO(move);
int side = pos->side;
ASSERT(SqOnBoard(from));
ASSERT(SqOnBoard(to));
ASSERT(SideValid(side));
ASSERT(PieceValid(pos->pieces[from]));
//Stores current the poskey in history
pos->history[pos->hisPly].posKey = pos->posKey;
//check enpassant move
if(move & MFLAGEP) {
if(side == WHITE) ClearPiece(to-10,pos);
else ClearPiece(to+10,pos);
}
//check castling move
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;
}
}
//removes the previous enPassantSQ from posKey
if(pos->enPassantSQ != NO_SQ) HASH_EP;
//hash out the current castling state from posKey
HASH_CA;
pos->history[pos->hisPly].move = move;
pos->history[pos->hisPly].fiftyMove = pos->fiftyMove;
pos->history[pos->hisPly].enPassantSQ = pos->enPassantSQ;
pos->history[pos->hisPly].castlePerm = pos->castlePerm;
pos->castlePerm &= CastlePerm[from];
pos->castlePerm &= CastlePerm[to];
pos->enPassantSQ = NO_SQ;
//hash in the new castling state to posKey
HASH_CA;
pos->fiftyMove++;
int captured = CAPTURED(move);
if(captured != EMPTY) {
ASSERT(PieceValid(captured));
ClearPiece(to, pos);
pos->fiftyMove = 0;
}
pos->ply++;
pos->hisPly++;
if(PiecePawn[pos->pieces[from]]) {
pos->fiftyMove = 0;
//if double pawnmove
if(move & MFLAGPS) {
if(side == WHITE) {
pos->enPassantSQ = from + 10;
ASSERT(GetRank[pos->enPassantSQ] == RANK_3);
} else {
pos->enPassantSQ = from - 10;
ASSERT(GetRank[pos->enPassantSQ] == RANK_6);
}
HASH_EP;
}
}
MovePiece(from, to, pos);
//checks after promoted pawn-move
int prPiece = PROMOTED(move);
if(prPiece != EMPTY) {
ASSERT(PieceValid(prPiece) && !PiecePawn[prPiece] && !PieceKing[prPiece]);
ClearPiece(to, pos);
AddPiece(to, pos, prPiece);
}
//checks for kingmove and updates the kingsquare
if(PieceKing[pos->pieces[to]]) {
pos->kingSQ[pos->side] = to;
}
pos->side ^= 1;
HASH_SIDE;
ASSERT(checkBoard(pos));
//checks if the side that moved had their king in check, in that case illegal move
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 = FROM(move);
int to = TO(move);
ASSERT(SqOnBoard(from));
ASSERT(SqOnBoard(to));
if(pos->enPassantSQ != NO_SQ) HASH_EP;
HASH_CA;
pos->fiftyMove = pos->history[pos->hisPly].fiftyMove;
pos->enPassantSQ = pos->history[pos->hisPly].enPassantSQ;
pos->castlePerm = pos->history[pos->hisPly].castlePerm;
if(pos->enPassantSQ != NO_SQ) HASH_EP;
HASH_CA;
pos->side ^= 1;
HASH_SIDE;
//if enpassant capture add in the pieces
if(move & MFLAGEP) {
if(pos->side == WHITE) AddPiece(to-10, pos, bP);
else AddPiece(to+10, pos, wP);
}
//if castling move move the rook back in position
else if(move & MFLAGCA) {
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;
}
}
//redo move before checking for capture
MovePiece(to, from, pos);
//update kingsq
if(PieceKing[pos->pieces[from]]) {
pos->kingSQ[pos->side] = from;
}
int captured = CAPTURED(move);
if(captured != EMPTY) {
ASSERT(PieceValid(captured));
AddPiece(to, pos, captured);
}
int prPiece = PROMOTED(move);
if(prPiece != EMPTY) {
ASSERT(PieceValid(prPiece) && !PiecePawn[prPiece] && !PieceKing[prPiece]);
ClearPiece(from, pos);
AddPiece(from, pos, (PieceColor[prPiece] == WHITE ? wP : bP));
}
ASSERT(checkBoard(pos));
}
void MakeNullMove(S_BOARD *pos) {
ASSERT(checkBoard(pos));
//TODO check incheck
pos->ply++;
pos->history[pos->hisPly].posKey = pos->posKey;
if (pos->enPassantSQ != NO_SQ) HASH_EP;
pos->history[pos->hisPly].move = NOMOVE;
pos->history[pos->hisPly].fiftyMove = pos->fiftyMove;
pos->history[pos->hisPly].enPassantSQ = pos->enPassantSQ;
pos->history[pos->hisPly].castlePerm = pos->castlePerm;
pos->enPassantSQ = NO_SQ;
pos->side ^= 1;
pos->hisPly++;
HASH_SIDE;
ASSERT(checkBoard(pos));
}
void TakeNullMove(S_BOARD *pos) {
ASSERT(checkBoard(pos));
pos->hisPly--;
pos->ply--;
if (pos->enPassantSQ != NO_SQ) HASH_EP;
pos->fiftyMove = pos->history[pos->hisPly].fiftyMove;
pos->enPassantSQ = pos->history[pos->hisPly].enPassantSQ;
pos->castlePerm = pos->history[pos->hisPly].castlePerm;
if (pos->enPassantSQ != NO_SQ) HASH_EP;
pos->side ^= 1;
HASH_SIDE;
ASSERT(checkBoard(pos));
}