You will probably have better code than my code to get frc fen from id number but here is my code
Note that setup is external function that I use for fen so my code does not return a string but simply use it in another function.
Here is my code(I wonder if you can check that I have no bugs)
Code: Select all
static void fenfrc(int id)
{
char position[256];
char numfil[8]={0,0,0,0,0,0,0,0};
int emptyfiles[5]={-1,-1,-1,-1,-1};
int lightbishfil=(id%4)*2+1;
int blackbishfil=((id>>2)%4)*2;
numfil[lightbishfil]='b';
numfil[blackbishfil]='b';
int queenfil=((id>>4)%6);
if (queenfil>=min(lightbishfil,blackbishfil))
queenfil++;
if (queenfil>=max(lightbishfil,blackbishfil))
queenfil++;
numfil[queenfil]='q';
int i=0;
int j=0;
while (j<5)
{
if (numfil[i]==0)
{
emptyfiles[j]=i;
j++;
}
i++;
}
while (numfil[i]>0)
i++;
int knightcode=id/96;
if (knightcode<=3)
{
numfil[emptyfiles[0]]='n';
numfil[emptyfiles[1+knightcode]]='n';
if (knightcode==0)
{
numfil[emptyfiles[2]]='r';
numfil[emptyfiles[3]]='k';
numfil[emptyfiles[4]]='r';
}
else
{
numfil[emptyfiles[1]]='r';
if (knightcode==1)
{
numfil[emptyfiles[3]]='k';
numfil[emptyfiles[4]]='r';
}
else
{
numfil[emptyfiles[2]]='k';
numfil[emptyfiles[6-knightcode]]='r';
}
}
}
else
{
numfil[emptyfiles[0]]='r';
if (knightcode<=6)
{
numfil[emptyfiles[1]]='n';
numfil[emptyfiles[knightcode-2]]='n';
if (knightcode==4)
{
numfil[emptyfiles[3]]='k';
numfil[emptyfiles[4]]='r';
}
else
{
numfil[emptyfiles[2]]='k';
numfil[emptyfiles[9-knightcode]]='r';
}
}
else
{
numfil[emptyfiles[1]]='k';
numfil[emptyfiles[2]]='n';
numfil[emptyfiles[3]]='n';
numfil[emptyfiles[4]]='n';
//delete one knight
numfil[emptyfiles[11-knightcode]]='r';
}
}
char position1[8];
for (i=0;i<8;i++)
{
position[i]=numfil[i];
position1[i]=numfil[i]+'A'-'a';
position[8]=0;
position1[8]=0;
}
strncat(position,"/pppppppp/8/8/8/8/PPPPPPPP/",99);
strncat(position,position1,12);
strncat(position," w KQkq - 0 1",15);
setup(position);
}