-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakemove.c
More file actions
242 lines (199 loc) · 6.74 KB
/
Copy pathmakemove.c
File metadata and controls
242 lines (199 loc) · 6.74 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
#include "stdio.h"
#include "defs.h"
/* Incremental hash update helpers */
#define HASH_PCE(pce, sq) (pos->posKey ^= (PieceKeys[(pce)][(sq)]))
#define HASH_CA (pos->posKey ^= (CastleKeys[(pos->castlePerm)]))
#define HASH_SIDE (pos->posKey ^= SideKeys)
#define HASH_EP (pos->posKey ^= (PieceKeys[EMPTY][(pos->enPas)]))
/* Remove castling rights when king or rook moves off their start square.
AND pos->castlePerm with this table entry for from-sq and to-sq. */
static 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, /* rank 1: a1=21→13, e1=25→12, h1=28→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, 7,15,15,15, 3,15,15,11,15, /* rank 8: a8=91→7, e8=95→3, h8=98→11 */
15,15,15,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,15
};
/* ---- Internal helpers ---- */
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;
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]--;
if (PieceMin[pce]) pos->minPce[col]--;
if (PiecePawn[pce]) {
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;
pos->material[col] += PieceVal[pce];
if (PieceBig[pce]) pos->bigPce[col]++;
if (PieceMaj[pce]) pos->majPce[col]++;
if (PieceMin[pce]) pos->minPce[col]++;
if (PiecePawn[pce]) {
SETBIT(pos->pawns[col], SQ64(sq));
SETBIT(pos->pawns[BOTH], SQ64(sq));
}
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 pce = pos->pieces[from];
int col = PieceCol[pce];
int index;
HASH_PCE(pce, from);
pos->pieces[from] = EMPTY;
HASH_PCE(pce, to);
pos->pieces[to] = pce;
if (PiecePawn[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));
}
if (PieceKing[pce]) pos->KingSq[col] = to;
for (index = 0; index < pos->pceNum[pce]; ++index) {
if (pos->pList[pce][index] == from) { pos->pList[pce][index] = to; break; }
}
}
/* ---- Public API ---- */
int MakeMove(S_BOARD *pos, int move) {
ASSERT(SideValid(pos->side));
int from = FROMSQ(move);
int to = TOSQ(move);
int side = pos->side;
ASSERT(SqOnBoard(from));
ASSERT(SqOnBoard(to));
ASSERT(PieceValid(pos->pieces[from]));
/* Save state for undo */
pos->history[pos->hisply].posKey = pos->posKey;
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;
/* Special moves */
if (move & MFLAGEP) {
if (side == WHITE) ClearPiece(to - 10, pos);
else ClearPiece(to + 10, pos);
} else if (move & MFLAGCA) {
switch (to) {
case G1: MovePiece(H1, F1, pos); break;
case C1: MovePiece(A1, D1, pos); break;
case G8: MovePiece(H8, F8, pos); break;
case C8: MovePiece(A8, D8, pos); break;
default: ASSERT(FALSE);
}
}
/* Remove old hash contributions */
if (pos->enPas != NO_SQ) HASH_EP;
HASH_CA;
pos->castlePerm &= CastlePerm[from];
pos->castlePerm &= CastlePerm[to];
pos->enPas = NO_SQ;
pos->fiftyMove++;
/* Re-hash castle rights */
HASH_CA;
/* Capture */
int captured = CAPTURED(move);
if (captured != EMPTY) {
ASSERT(PieceValidEmpty(captured));
ClearPiece(to, pos);
pos->fiftyMove = 0;
}
pos->hisply++;
pos->ply++;
/* Pawn moves */
if (PiecePawn[pos->pieces[from]]) {
pos->fiftyMove = 0;
if (move & MFLAGPS) {
if (side == WHITE) pos->enPas = from + 10;
else pos->enPas = from - 10;
HASH_EP;
}
}
MovePiece(from, to, pos);
/* Promotion */
int prPce = PROMOTED(move);
if (prPce != EMPTY) {
ASSERT(PieceValidEmpty(prPce) && !PiecePawn[prPce]);
ClearPiece(to, pos);
AddPiece(to, pos, prPce);
}
pos->side ^= 1;
HASH_SIDE;
/* Legality: is our king in check after the move? */
if (SqAttacked(pos->KingSq[side], pos->side, pos)) {
TakeMove(pos);
return FALSE;
}
return TRUE;
}
void TakeMove(S_BOARD *pos) {
ASSERT(SideValid(pos->side));
ASSERT(pos->hisply > 0);
pos->hisply--;
pos->ply--;
int move = pos->history[pos->hisply].move;
int from = FROMSQ(move);
int to = TOSQ(move);
/* Restore hash for ep/castle before overwriting */
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;
/* Undo special moves */
if (move & MFLAGEP) {
if (pos->side == WHITE) AddPiece(to - 10, pos, bP);
else AddPiece(to + 10, pos, wP);
} else if (move & MFLAGCA) {
switch (to) {
case G1: MovePiece(F1, H1, pos); break;
case C1: MovePiece(D1, A1, pos); break;
case G8: MovePiece(F8, H8, pos); break;
case C8: MovePiece(D8, A8, pos); break;
default: ASSERT(FALSE);
}
}
MovePiece(to, from, pos);
/* Restore captured piece */
int captured = CAPTURED(move);
if (captured != EMPTY) AddPiece(to, pos, captured);
/* Undo promotion */
int prPce = PROMOTED(move);
if (prPce != EMPTY) {
ClearPiece(from, pos);
AddPiece(from, pos, (PieceCol[prPce] == WHITE) ? wP : bP);
}
}