having said that, below you find my san to internal representation converter. I use bitwise instructions to find the possible target squares. Maybe it helps:
Code: Select all
int algeb_san(char * a, U16 * moves) {
int from, to, prom;
int counter = -1;
U64 orig;
for (;;) {
for (;;a++) {
if (!(*a) || (*a == ';')) return counter + 1;
if (strchr("O0KQRBNabcdefgh", a[0])) break;
}
counter++;
if (strstr(a, "O-O-O") || strstr(a, "0-0-0")) {
if (b_global.d.stm == WHITE) moves[counter] = MOVE_SET(E1, C1, 0);
else moves[counter] = MOVE_SET(E8, C8, 0);
continue;
}
else if (strstr(a, "O-O") || strstr(a, "0-0")) {
if (b_global.d.stm == WHITE) moves[counter] = MOVE_SET(E1, G1, 0);
else moves[counter] = MOVE_SET(E8, G8, 0);
continue;
}
from = 0;
to = 0;
prom = 0;
if (strchr("abcdefgh", a[0])) {
if (a[1] == 'x') {
to = algeb_conv_a1_64(a + 2);
from = 7 - a[0] + 'a';
from = bitScanForward(mgen_att_P(!b_global.d.stm, bbSQ[to]) & bbCOL[from]);
if (a[4] == '=') prom = a[5];
a+=3;
} else {
to = algeb_conv_a1_64(a);
if (b_global.d.stm == WHITE) {
if (shift_S (bbSQ[to]) & b_global.bbPC[b_global.d.stm][PIECE_P]) from = to - 8;
else if (shift_SS(bbSQ[to]) & b_global.bbPC[b_global.d.stm][PIECE_P]) from = to - 16;
else return -1;
} else {
if (shift_N (bbSQ[to]) & b_global.bbPC[b_global.d.stm][PIECE_P]) from = to + 8;
else if (shift_NN(bbSQ[to]) & b_global.bbPC[b_global.d.stm][PIECE_P]) from = to + 16;
else return -1;
}
if (a[2] == '=') prom = a[3];
a+=2;
}
switch (prom) {
case 0: moves[counter] = MOVE_SET(from, to, 0); break;
case 'Q': moves[counter] = MOVE_SET(from, to, PIECE_Q); break;
case 'R': moves[counter] = MOVE_SET(from, to, PIECE_R); break;
case 'B': moves[counter] = MOVE_SET(from, to, PIECE_B); break;
case 'N': moves[counter] = MOVE_SET(from, to, PIECE_N); break;
}
if (prom) a+=2;
continue;
}
if (strchr("KQRBN", a[0])) {
int piece = 0;
switch (a[0]) {
case 'K': piece = PIECE_K; break;
case 'Q': piece = PIECE_Q; break;
case 'R': piece = PIECE_R; break;
case 'B': piece = PIECE_B; break;
case 'N': piece = PIECE_N; break;
}
a++;
if (a[0] == 'x') a++;
orig = bbFULL;
if (strchr("12345678", a[0])) {
orig &= bbROW[a[0]-'1'];
a++;
}
if (strchr("abcdefgh", a[0])) {
if (strchr("12345678", a[1])) {
if (a[2] && strchr("abcdefgh", a[2])) {
from = algeb_conv_a1_64(a);
to = algeb_conv_a1_64(a+2);
moves[counter] = MOVE_SET(from, to, 0);
a+=4;
continue;
}
to = algeb_conv_a1_64(a);
a+=2;
}
else if (strchr("abcdefgh", a[1])) {
orig &= bbCOL[7 - a[0] + 'a'];
to = algeb_conv_a1_64(a+1);
a+=3;
}
else return -1;
switch (piece) {
case PIECE_K: orig &= b_global.bbPC[b_global.d.stm][piece] & bbATT[piece][to]; break;
case PIECE_Q: orig &= b_global.bbPC[b_global.d.stm][piece] & mgen_att_Q(to, b_global.bbCO[0] | b_global.bbCO[1]); break;
case PIECE_R: orig &= b_global.bbPC[b_global.d.stm][piece] & mgen_att_R(to, b_global.bbCO[0] | b_global.bbCO[1]); break;
case PIECE_B: orig &= b_global.bbPC[b_global.d.stm][piece] & mgen_att_B(to, b_global.bbCO[0] | b_global.bbCO[1]); break;
case PIECE_N: orig &= b_global.bbPC[b_global.d.stm][piece] & bbATT[piece][to]; break;
}
if (piece == PIECE_K) {
if (!orig) return -1;
from = bitScanForward(orig);
moves[counter] = MOVE_SET(from, to, 0);
continue;
}
while (orig) {
from = bitScanForward(orig);
orig ^= bbSQ[from];
if (mgen_att_R(getKing(&b_global, b_global.d.stm), (b_global.bbCO[0] | b_global.bbCO[1]) ^ bbSQ[from] ^ bbSQ[to]) & (b_global.bbPC[!b_global.d.stm][PIECE_R] | b_global.bbPC[!b_global.d.stm][PIECE_Q])) continue;
if (mgen_att_B(getKing(&b_global, b_global.d.stm), (b_global.bbCO[0] | b_global.bbCO[1]) ^ bbSQ[from] ^ bbSQ[to]) & (b_global.bbPC[!b_global.d.stm][PIECE_B] | b_global.bbPC[!b_global.d.stm][PIECE_Q])) continue;
moves[counter] = MOVE_SET(from, to, 0);
continue;
}
}
}
}
}
