Skip to content

Commit bef728e

Browse files
Daniel WhiteDaniel White
authored andcommitted
Now support en-passent captures.
1 parent 576490a commit bef728e

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

iota.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,52 @@
66

77
char c[Q], *C,
88
*T="#6*.683234#15BD#24#24$3#(.)$?).(/.2##$"; // Piece move steps, board setup data and promotion XORs (subtract '#'=35).
9-
B[Q], S, W, X;
9+
B[Q], R, S, W, X;
1010

11-
F(U, V, p) {
12-
int f=0,t, i, d, P, u;
11+
F(U, V, p, r) {
12+
int f=0,t, i, d, P, u, q;
1313
for(;f<128;f=f+9&~8) { // Loop over squares looking for pieces.
1414
for(i=T[(P=B[f])&7]-35,d=0;d<0 || (d=T[i++]-35);d=-d) { // Loop over move steps for this piece.
1515
for(t=f+d;P&S;t+=d) { // Loop over destination squares in this direction.
1616
// Invalid square or friendly capture?
1717
if ((t&136) || ((u=B[t])&S))
1818
break;
1919

20-
// Check for correct pawn movement.
21-
if (P%8==2 && ((d^(u>0))%2 || ((d>0)^(S==32))))
22-
break;
20+
// Special pawn logic.
21+
q=128;
22+
if (P%8==2)
23+
{
24+
if (((d>0)^(S==32)) || // Bad direction?
25+
(!u && d%2 && t!=r) || // Diagonal without capture?
26+
(!(d%2) && u>0)) // Straight with capture?
27+
break;
28+
29+
q=(t==r ? 16 : q); // Is this an en-passent capture?
30+
}
2331

2432
// Other side left in check?
2533
if (U==8 && (u&16))
2634
return 0;
35+
2736
// Make move.
2837
B[t]=P;
38+
B[f]=0;
39+
B[t^q]=0; // If en-passent capture, remove victim pawn.
2940
if (P%8==2 && (t<8 || t>103))
3041
B[t]^=T[p%7+32]-35; // Promotion.
31-
B[f]=0;
3242
S^=96;
3343
if ((P&16) && t!=f+d)
3444
B[(f+t)/2]=B[t+(d>0?1:-2)],B[t+(d>0?1:-2)]=0; // If castling also move rook.
3545

3646
// Looking to make a move? (if our own move, make sure does not leave us in check)
3747
if ((t==V && f==U) || (U==Q && F(8,0,0,Q))) {
3848
W=f,X=t;
49+
R=((P%8==2 && t!=f+d) ? (t+f)/2 : 9); // Set ep-target square if double pawn move.
3950
return B[t]==P; // Indicate if promotion has NOT occured.
4051
}
4152

4253
// Undo move.
54+
B[t^q]=10|S; // If en-passent capture, replace victim pawn.
4355
S^=96;
4456
B[f]=P;
4557
B[t]=u;
@@ -68,14 +80,14 @@ main(i, p) {
6880
Z"q",1)) break; // Quit command, exit.
6981
Z"p",1)) { // Parse 'position' command.
7082
for(i=0;i<8;++i) // Reset board to start position.
71-
B[i+16]=42,B[i+96]=74, // Pawns.
83+
B[i+16]=R=42,B[i+96]=74, // Pawns.
7284
B[i+112]=(B[i]=T[i+24]-3)+(S=32), // Pieces.
7385
B[i+S]=B[i+48]=B[i+64]=B[i+80]=0; // Empty rows.
7486
for(;C=strtok(0," ");)
75-
F(*C+C[1]*16-881, C[2]+C[3]*16-881, C[4]);
87+
F(*C+C[1]*16-881, C[2]+C[3]*16-881, C[4], R);
7688
}
7789
Z"g",1)) // Go command. Make a move and print it.
78-
p=F(Q, 0, 0),
90+
p=F(Q, 0, 0, R),
7991
printf("bestmove %c%i%c%i%c\n", W%16+97, W/16+1, X%16+97, X/16+1, p?32:98);
8092
}
8193
}

0 commit comments

Comments
 (0)