-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmove.cpp
More file actions
276 lines (232 loc) · 7.19 KB
/
move.cpp
File metadata and controls
276 lines (232 loc) · 7.19 KB
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
#include "stdafx.h"
#include "transposition.h"
int move_makeNull() {
b.stm = !b.stm;
b.hash ^= zobrist.color;
b.ply ++;
if (b.ep != -1) {
b.hash ^= zobrist.ep[b.ep];
b.ep = -1;
}
return 0;
}
int move_unmakeNull(char ep) {
b.stm = !b.stm;
b.hash ^= zobrist.color;
b.ply --;
if (ep != -1) {
b.hash ^= zobrist.ep[ep];
b.ep = ep;
}
return 0;
}
int move_make(smove move) {
/* switch the side to move */
b.stm = !b.stm;
b.hash ^= zobrist.color;
/* a capture or a pawn move clears b.ply */
b.ply ++;
if ( (move.piece_from == PAWN) || move_iscapt(move) )
b.ply = 0;
/* in case of a capture, the "to" square must be cleared,
else incrementally updated stuff gets blown up */
if ( b.pieces[move.to] != PIECE_EMPTY )
clearSq(move.to);
/* a piece vacates its initial square */
clearSq(move.from);
/* a piece arrives to its destination square */
fillSq( !b.stm, move.piece_to, move.to );
/**************************************************************************
* Reset the castle flags. If either a king or a rook leaves its initial *
* square, the side looses the castling rights. The same happens when *
* a rook on its initial square gets captured. *
**************************************************************************/
switch (move.from) {
case H1:
b.castle &= ~CASTLE_WK;
break;
case E1:
b.castle &= ~(CASTLE_WK|CASTLE_WQ);
break;
case A1:
b.castle &= ~CASTLE_WQ;
break;
case H8:
b.castle &= ~CASTLE_BK;
break;
case E8:
b.castle &= ~(CASTLE_BK|CASTLE_BQ);
break;
case A8:
b.castle &= ~CASTLE_BQ;
break;
}
switch (move.to) {
case H1:
b.castle &= ~CASTLE_WK;
break;
case E1:
b.castle &= ~(CASTLE_WK|CASTLE_WQ);
break;
case A1:
b.castle &= ~CASTLE_WQ;
break;
case H8:
b.castle &= ~CASTLE_BK;
break;
case E8:
b.castle &= ~(CASTLE_BK|CASTLE_BQ);
break;
case A8:
b.castle &= ~CASTLE_BQ;
break;
}
b.hash ^= zobrist.castling[move.castle];
b.hash ^= zobrist.castling[b.castle];
/**************************************************************************
* Finish the castling move. It is represented as the king move (e1g1 *
* = White castles short), which has already been executed above. Now *
* we must move the rook to complete castling. *
**************************************************************************/
if (move.flags & MFLAG_CASTLE) {
if (move.to == G1) {
clearSq(H1);
fillSq(WHITE,ROOK,F1);
}
else if (move.to == C1) {
clearSq(A1);
fillSq(WHITE,ROOK,D1);
}
else if (move.to == G8) {
clearSq(H8);
fillSq(BLACK,ROOK,F8);
}
else if (move.to == C8) {
clearSq(A8);
fillSq(BLACK,ROOK,D8);
}
}
/**************************************************************************
* Erase the current state of the ep-flag, then set it again if a pawn *
* jump that allows such capture has been made. 1.e4 in the initial po- *
* sition will not set the en passant flag, because there are no black *
* pawns on d4 and f4. This soluion helps with opening book and increa- *
* ses the number of transposition table hits. *
**************************************************************************/
if (b.ep != -1) {
b.hash ^= zobrist.ep[b.ep];
b.ep = -1;
}
if ( (move.piece_from == PAWN) && ( abs(move.from - move.to) == 32 )
&& (b.pawn_ctrl[b.stm] [(move.from + move.to) / 2])
) {
b.ep = (move.from + move.to) / 2;
b.hash ^= zobrist.ep[b.ep];
}
/**************************************************************************
* Remove a pawn captured en passant *
**************************************************************************/
if (move.flags & MFLAG_EPCAPTURE) {
if (!b.stm == WHITE) {
clearSq(move.to - 16);
} else {
clearSq(move.to + 16);
}
}
++b.rep_index;
b.rep_stack[b.rep_index] = b.hash;
return 0;
}
int move_unmake(smove move) {
b.stm = !b.stm;
b.hash ^= zobrist.color;
b.ply = move.ply;
/* set en passant square */
if (b.ep != -1)
b.hash ^= zobrist.ep[b.ep];
if (move.ep != -1)
b.hash ^= zobrist.ep[move.ep];
b.ep = move.ep;
/* Move the piece back */
clearSq(move.to);
fillSq(b.stm, move.piece_from, move.from);
/* Un-capture: in case of a capture, put the captured piece back */
if ( move_iscapt(move) )
fillSq(!b.stm, move.piece_cap, move.to );
/* Un-castle: the king has already been moved, now move the rook */
if (move.flags & MFLAG_CASTLE) {
if (move.to == G1) {
clearSq(F1);
fillSq(WHITE,ROOK,H1);
}
else if (move.to == C1) {
clearSq(D1);
fillSq(WHITE,ROOK,A1);
}
else if (move.to == G8) {
clearSq(F8);
fillSq(BLACK,ROOK,H8);
}
else if (move.to == C8) {
clearSq(D8);
fillSq(BLACK,ROOK,A8);
}
}
/* adjust castling flags */
b.hash ^= zobrist.castling[move.castle];
b.hash ^= zobrist.castling[b.castle];
b.castle = move.castle;
/* Put the pawn captured en passant back to its initial square */
if (move.flags & MFLAG_EPCAPTURE) {
if (b.stm == WHITE) {
fillSq(BLACK,PAWN,move.to - 16);
} else {
fillSq(WHITE,PAWN,move.to + 16);
}
}
--b.rep_index;
return 0;
}
int move_iscapt(smove m) {
return (m.piece_cap != PIECE_EMPTY);
}
int move_isprom(smove m) {
return (m.piece_from != m.piece_to);
}
int move_canSimplify(smove m) {
if ( m.piece_cap == PAWN
|| b.piece_material[!b.stm] - e.PIECE_VALUE[m.piece_cap] > e.ENDGAME_MAT )
return 0;
else
return 1;
}
int move_countLegal() {
smove mlist[256];
int mcount = movegen(mlist, 0xFF);
int result = 0;
for (int i = 0; i < mcount; i++) {
/* try a move... */
move_make( mlist[i] );
/* ...then increase the counter if it did not leave us in check */
if ( !isAttacked( b.stm, b.king_loc[!b.stm] ) ) ++result;
move_unmake(mlist[i]);
}
/* return number of legal moves in the current position */
return result;
}
int move_isLegal(smove m) {
smove movelist[256];
int movecount = movegen(movelist, 0xFF);
for (int i = 0; i < movecount; i++) {
if ( movelist[i].from == m.from
&& movelist[i].to == m.to ) {
int result = 1;
/* test if the move in question leaves us in check */
move_make( movelist[i] );
if ( isAttacked( b.stm, b.king_loc[!b.stm] ) ) result = 0;
move_unmake( movelist[i] );
return result;
}
}
return 0;
}