Note that there is a bug in my code and I simply should assume no symmetry position when there is castling rights
Here is one example from wac
8
7
6
5
4
3
2
1
a
b
c
d
e
f
g
h
r1b1k2r/1pp1q2p/p1n3p1/3QPp2/8/1BP3B1/P5PP/3R1RK1 w kq - bm Bh4; id "WAC.133";
black cannot castle in case that it is black to move but in case that
I use the symmetric position black is allowed to castle when I switch the side to move
8
7
6
5
4
3
2
1
a
b
c
d
e
f
g
h
r2k1b1r/p2q1pp1/1p3n1p/2pPQ3/8/1B3PB1/PP5P/1KR1R3 w kq -
char *translate_fen_2(char *fen)
{
unsigned int l=strlen(fen);
unsigned int i=0;
char tempfen[10];
int i0;
int cast=0;
memset(fen1,0,1024);
while ((i<l)&&(fen[i]==' '))
i++;
while (fen[i]!=' ')
{
i0=i;
while ((fen[i]!='/')&&(fen[i]!=' '))
i++;
memset(tempfen,0,10);
memcpy(tempfen,fen+i0,i-i0);
strcat(fen1,tranpose_line(tempfen));
fen1[strlen(fen1)]=fen[i];
if (fen[i]=='/')
i++;
}
while ((fen[i]==' ')&&(i<l))
i++;
//correct color
if (i<l)
fen1[strlen(fen1)]=fen[i];
//correct space after color
fen1[strlen(fen1)]=' ';
i++;
if (fen[i]==' ')
i++;
//castling rights
if (i<l)
{
if (fen[i]=='-')
{
fen1[strlen(fen1)]='-';
i++;
}
else
return fen1;//should never happen
}
//now we need enpassent pawn
fen1[strlen(fen1)]=' ';
while ((i<l)&&(fen[i]==' '))
i++;
if (i==l)
return fen1;//should never happen
if (fen[i]!='-')
{
fen1[strlen(fen1)]='h'+'a'-fen[i];//correct file for the enpassent pawn
i++;
}
while (i<l)
{
fen1[strlen(fen1)]=fen[i];
i++;
}
return fen1;
}
Alessandro Scotti wrote:I think we could run an informal contest for the:
1) shortest code (but clean and elegant);
2) shortest and trickiest code (anything goes).
Anyone interested?
Here's my effort for your entertainment, 2 Perl programs for mirror and flip operations. They both handle all FEN fields, including side-to-move, en passant square and castling rights: