forked from mprewarski/DiabloChessEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove.c
324 lines (275 loc) · 7.04 KB
/
move.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
/*
Diablo chess engine, Copyright 2005, Marcus Prewarski
This program is free software. It may be copied or modified under the terms
of the GNU General Public License version 2 as published by the Free Software
Foundation. This program is distributed without any warranties whatsoever.
See the file COPYING included with the distribution for details of the GNU general
public license.
*/
#ifndef WIN32
#include <unistd.h>
#include <sys/time.h>
#else
#include <windows.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <assert.h>
#include "diablo.h"
int verify_lists();
int debug_info();
int make_move(int move)
{
Knowledge *know = know_stack + ply;
unsigned int f = FROM(move);
unsigned int t = TO(move);
int oldcastle = board.castle;
int enpassant_cap_sq = 0;
int side = board.side;
int prom_p = 0;
int p;
// assert(f <= H8);
// assert(t <= H8);
know->undo.enpassant = board.enpassant;
know->undo.castle = board.castle;
know->undo.fifty = board.fifty;
know->undo.hash = board.hash;
know->undo.capture = Square(t);
know->move = move;
(know+1)->material = know->material;
p = Square(f);
board.enpassant = 0xc0;
ply++;
if ((move & (CAPTURE | PAWNMOVE)))
board.fifty = 0;
else
board.fifty++;
if (move & CAPTURE) {
if (move & ENPASSANT) {
if (side == WHITE) {
enpassant_cap_sq = t-16;
PL_REMOVE(enpassant_cap_sq);
know->undo.capture = BPAWN;
Square(enpassant_cap_sq) = EMPTY;
}
else {
enpassant_cap_sq = t+16;
PL_REMOVE(enpassant_cap_sq);
know->undo.capture = WPAWN;
Square(enpassant_cap_sq) = EMPTY;
}
}
else {
PL_REMOVE(t);
}
}
if (move & PROMOTION) {
prom_p = promoted[(move >> 16) & 0xf] | (side << 3);
Square(t) = prom_p;
PL_REMOVE(f); PL_INSERT(prom_p, t);
}
else {
Square(t) = p;
PL_MOVE(f, t);
}
Square(f) = EMPTY;
if (move & CASTLE) {
if (t == G1) {
Square(H1) = EMPTY;
Square(F1) = WROOK;
PL_MOVE(H1, F1); }
else if (t == C1) {
Square(A1) = EMPTY;
Square(D1) = WROOK;
PL_MOVE(A1, D1); }
else if (t == G8) {
Square(H8) = EMPTY;
Square(F8) = BROOK;
PL_MOVE(H8, F8); }
else {
Square(A8) = EMPTY;
Square(D8) = BROOK;
PL_MOVE(A8, D8); }
}
if ((Square(t) == WPAWN) && ((f+32) == t))
board.enpassant = f+16;
else if ((Square(t) == BPAWN) && ((f-32) == t))
board.enpassant = f-16;
if (board.castle) {
if (f == E1)
board.castle &= ~(WK_CASTLE | WQ_CASTLE);
else if (f == E8)
board.castle &= ~(BK_CASTLE | BQ_CASTLE);
if ((f == A1) || (t == A1))
board.castle &= ~WQ_CASTLE;
if ((f == H1) || (t == H1))
board.castle &= ~WK_CASTLE;
if ((f == A8) || (t == A8))
board.castle &= ~BQ_CASTLE;
if ((f == H8) || (t == H8))
board.castle &= ~BK_CASTLE;
}
board.hash ^= zobrist_side[0];
board.hash ^= zobrist_board[p][f];
if (move & PROMOTION) {
board.hash ^= zobrist_board[prom_p][t];
}
else {
board.hash ^= zobrist_board[p][t];
}
if (move & CAPTURE) {
if (move & ENPASSANT) {
if (side == WHITE)
board.hash ^= zobrist_board[BPAWN][enpassant_cap_sq];
else
board.hash ^= zobrist_board[WPAWN][enpassant_cap_sq];
}
else {
board.hash ^= zobrist_board[know->undo.capture][t];
}
}
if (oldcastle != board.castle) {
if (move & CASTLE) {
board.hash ^= zobrist_castle[oldcastle];
board.hash ^= zobrist_castle[board.castle];
if (t == G1) {
board.hash ^= zobrist_board[WROOK][H1];
board.hash ^= zobrist_board[WROOK][F1];
}
else if (t == C1) {
board.hash ^= zobrist_board[WROOK][A1];
board.hash ^= zobrist_board[WROOK][D1];
}
else if (t == G8) {
board.hash ^= zobrist_board[BROOK][H8];
board.hash ^= zobrist_board[BROOK][F8];
}
else {
board.hash ^= zobrist_board[BROOK][A8];
board.hash ^= zobrist_board[BROOK][D8];
}
}
else {
board.hash ^= zobrist_castle[oldcastle];
board.hash ^= zobrist_castle[board.castle];
}
}
if ((move & CAPTURE)) {
(know+1)->material -= piece_value[know->undo.capture];
}
if (move & PROMOTION) {
(know+1)->material -= piece_value[PAWN];
(know+1)->material += piece_value[prom_p];
}
board.side ^= 1;
board.xside ^= 1;
board.gameply++;
move_hist[board.gameply].move = move;
move_hist[board.gameply].hash_key = board.hash;
// verify_lists(move, "make");
return 0;
}
int unmake_move(int move)
{
Knowledge *know;
int f, t, p;
ply--;
board.side ^= 1;
board.xside ^= 1;
board.gameply--;
know = know_stack + ply;
board.enpassant = know->undo.enpassant;
board.castle = know->undo.castle;
board.fifty = know->undo.fifty;
board.hash = know->undo.hash;
f = FROM(move); t = TO(move);
p = Square(t);
Square(t) = EMPTY;
if (move & PROMOTION) {
PL_REMOVE(t);
PL_INSERT(PAWN | (board.side << 3), f)
Square(f) = PAWN | (board.side << 3);
}
else {
PL_MOVE(t, f);
Square(f) = p;
}
if (move & CAPTURE) {
if (move & ENPASSANT) {
if (RANK(t) == 5) {
PL_INSERT(BPAWN, t-16);
Square(t-16) = BPAWN; }
else {
PL_INSERT(WPAWN, t+16);
Square(t+16) = WPAWN; }
}
else {
Square(t) = know->undo.capture;
PL_INSERT(know->undo.capture, t);
}
}
if (move & CASTLE) {
if (t == G1) {
Square(F1) = EMPTY;
Square(H1) = WROOK;
PL_MOVE(F1, H1); }
else if (t == C1) {
Square(D1) = EMPTY;
Square(A1) = WROOK;
PL_MOVE(D1, A1); }
else if (t == G8) {
Square(F8) = EMPTY;
Square(H8) = BROOK;
PL_MOVE(F8, H8); }
else {
Square(D8) = EMPTY;
Square(A8) = BROOK;
PL_MOVE(D8, A8); }
}
// verify_lists(move, "unmake");
return 0;
}
int make_null_move()
{
Knowledge *know = know_stack + ply;
know->undo.enpassant = board.enpassant;
know->undo.castle = board.castle;
know->undo.fifty = board.fifty;
know->undo.hash = board.hash;
know->undo.capture = 0;
know++;
know->ms = (know-1)->ms;
know->material = (know-1)->material;
board.hash ^= zobrist_side[0];
board.fifty++;
board.enpassant = 0xc0;
ply++;
board.side ^= 1;
board.xside ^= 1;
board.gameply++;
move_hist[board.gameply].move = 0;
move_hist[board.gameply].hash_key = board.hash;
// verify_lists(0, "make null");
return 0;
}
int unmake_null_move()
{
Knowledge *know;
ply--;
board.gameply--;
board.side ^= 1;
board.xside ^= 1;
know = know_stack + ply;
board.enpassant = know->undo.enpassant;
board.castle = know->undo.castle;
board.fifty = know->undo.fifty;
board.hash = know->undo.hash;
// verify_lists(0, "unmake null");
return 0;
}