code to get FRC fen from id number

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Uri Blass
Posts: 10297
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

code to get FRC fen from id number

Post by Uri Blass »

seeking frc fen from id number

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 &#40;j<5&#41;
	&#123;
		if &#40;numfil&#91;i&#93;==0&#41;
		&#123;
			emptyfiles&#91;j&#93;=i;
			j++;
		&#125;
		i++;
	&#125;
	while &#40;numfil&#91;i&#93;>0&#41;
		i++;
	int knightcode=id/96;
	if &#40;knightcode<=3&#41;
	&#123;
		numfil&#91;emptyfiles&#91;0&#93;&#93;='n';
		numfil&#91;emptyfiles&#91;1+knightcode&#93;&#93;='n';
		if &#40;knightcode==0&#41;
		&#123;
			numfil&#91;emptyfiles&#91;2&#93;&#93;='r';
			numfil&#91;emptyfiles&#91;3&#93;&#93;='k';
			numfil&#91;emptyfiles&#91;4&#93;&#93;='r';
		&#125;
		else
		&#123;
			numfil&#91;emptyfiles&#91;1&#93;&#93;='r';
			if &#40;knightcode==1&#41;
			&#123;
					numfil&#91;emptyfiles&#91;3&#93;&#93;='k';
					numfil&#91;emptyfiles&#91;4&#93;&#93;='r';
			&#125;
			else
			&#123;
				numfil&#91;emptyfiles&#91;2&#93;&#93;='k';
				numfil&#91;emptyfiles&#91;6-knightcode&#93;&#93;='r';
			&#125;
		&#125;
	&#125;
	else
	&#123;
		numfil&#91;emptyfiles&#91;0&#93;&#93;='r';
		if &#40;knightcode<=6&#41;
		&#123;
			numfil&#91;emptyfiles&#91;1&#93;&#93;='n';
			numfil&#91;emptyfiles&#91;knightcode-2&#93;&#93;='n';
			if &#40;knightcode==4&#41;
			&#123;
				numfil&#91;emptyfiles&#91;3&#93;&#93;='k';
				numfil&#91;emptyfiles&#91;4&#93;&#93;='r';
			&#125;
			else
			&#123;
				numfil&#91;emptyfiles&#91;2&#93;&#93;='k';
				numfil&#91;emptyfiles&#91;9-knightcode&#93;&#93;='r';
			&#125;
		&#125;
		else
		&#123;
			numfil&#91;emptyfiles&#91;1&#93;&#93;='k';
			numfil&#91;emptyfiles&#91;2&#93;&#93;='n';
			numfil&#91;emptyfiles&#91;3&#93;&#93;='n';
			numfil&#91;emptyfiles&#91;4&#93;&#93;='n';
			//delete one knight
			numfil&#91;emptyfiles&#91;11-knightcode&#93;&#93;='r';
		&#125;

	&#125;
	char position1&#91;8&#93;;
	for &#40;i=0;i<8;i++)
	&#123;
		position&#91;i&#93;=numfil&#91;i&#93;;
		position1&#91;i&#93;=numfil&#91;i&#93;+'A'-'a';
		position&#91;8&#93;=0;
		position1&#91;8&#93;=0;
	&#125;

	strncat&#40;position,"/pppppppp/8/8/8/8/PPPPPPPP/",99&#41;;
	strncat&#40;position,position1,12&#41;;
	strncat&#40;position," w KQkq - 0 1",15&#41;;
	setup&#40;position&#41;;
&#125;
Alessandro Scotti

Re: code to get FRC fen from id number

Post by Alessandro Scotti »

Hi Uri,
very nice, thanks for sharing this!
pijl

Re: code to get FRC fen from id number

Post by pijl »

my alternative. It fills an array with all possible FRC starting positions. It is a table based approach, putting position 1 in startFEN[1] etc. IIRC it is much like the method in Reinhard's book.

Code: Select all

const char kingTable&#91;61&#93;&#91;8&#93;= &#123;
	"QNNRKR", "NQNRKR", "NNQRKR", "NNRQKR",
	"NNRKQR", "NNRKRQ", "QNRNKR", "NQRNKR",
	"NRQNKR", "NRNQKR", "NRNKQR", "NRNKRQ",
	"QNRKNR", "NQRKNR", "NRQKNR", "NRKQNR",
	"NRKNQR", "NRKNRQ", "QNRKRN", "NQRKRN",
	"NRQKRN", "NRKQRN", "NRKRQN", "NRKRNQ",
	"QRNNKR", "RQNNKR", "RNQNKR", "RNNQKR",
	"RNNKQR", "RNNKRQ", "QRNKNR", "RQNKNR",
	"RNQKNR", "RNKQNR", "RNKNQR", "RNKNRQ",
	"QRNKRN", "RQNKRN", "RNQKRN", "RNKQRN",
	"RNKRQN", "RNKRNQ", "QRKNNR", "RQKNNR",
	"RKQNNR", "RKNQNR", "RKNNQR", "RKNNRQ",
	"QRKNRN", "RQKNRN", "RKQNRN", "RKNQRN",
	"RKNRQN", "RKNRNQ", "QRKRNN", "RQKRNN",
	"RKQRNN", "RKRQNN", "RKRNQN", "RKRNNQ",
	"QNNRKR"
&#125;;
const int bishopTable&#91;16&#93;= &#123;
	0xc0, 0x90, 0x84, 0x81, 0x60, 0x30, 0x24, 0x21, 
	0x48, 0x18, 0x0c, 0x09, 0x42, 0x12, 0x06, 0x03
&#125;;
	// Calculate Starting positions
	for &#40;n=0; n<=960; n++) &#123;
		int i,j,k;
		int b;
		const char *s;
		char *f;

		i=n>>4;
		j=bishopTable&#91;n&0x0000000f&#93;;
		b=128;
		s=kingTable&#91;i&#93;;
		f=startFEN&#91;n&#93;;
		for &#40;k=1; k<=8; k++) &#123;
			if &#40;b&j&#41; &#123;
				*&#40;f++)='b';
				b>>=1;
				continue;
			&#125;
			*&#40;f++)=tolower&#40;*&#40;s++));
			b>>=1;
		&#125;

		strcpy&#40;f,"/pppppppp/8/8/8/8/PPPPPPPP/");

		i=n>>4;
		j=bishopTable&#91;n&0x0000000f&#93;;
		b=128;
		s=kingTable&#91;i&#93;;
		f=startFEN&#91;n&#93;+strlen&#40;startFEN&#91;n&#93;);
		for &#40;k=1; k<=8; k++) &#123;
			if &#40;b&j&#41; &#123;
				*&#40;f++)='B';
				b>>=1;
				continue;
			&#125;
			*&#40;f++)=toupper&#40;*&#40;s++));
			b>>=1;
		&#125;

		strcpy&#40;f," w KQkq -");
	&#125;
User avatar
smrf
Posts: 484
Joined: Mon Mar 13, 2006 11:08 am
Location: Klein-Gerau, Germany

Re: code to get FRC fen from id number

Post by smrf »

Hi Richard,

after I wrote my book on Chess960 I simplified the method for generating FRC positions to finally fit upon one single page. The Chess Tigers have published this approach on their site as a PDF document:

http://www.chesstigers.de/download/chess960_regeln.pdf

Moreover one could find this flat two table model even at Wikipedia.

If someone would also be interested in 10x8 CRC positions, there is a combined C++ source covering FRC and CRC positions by a unified routine.

Regards, Reinhard.

P.S.: There are routines generating those starting positions in compatible X-FEN, maybe Shredder needs some modifications here, which is not recommended by myself.
Uri Blass
Posts: 10297
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: code to get FRC fen from id number

Post by Uri Blass »

pijl wrote:my alternative. It fills an array with all possible FRC starting positions. It is a table based approach, putting position 1 in startFEN[1] etc. IIRC it is much like the method in Reinhard's book.

Code: Select all

const char kingTable&#91;61&#93;&#91;8&#93;= &#123;
	"QNNRKR", "NQNRKR", "NNQRKR", "NNRQKR",
	"NNRKQR", "NNRKRQ", "QNRNKR", "NQRNKR",
	"NRQNKR", "NRNQKR", "NRNKQR", "NRNKRQ",
	"QNRKNR", "NQRKNR", "NRQKNR", "NRKQNR",
	"NRKNQR", "NRKNRQ", "QNRKRN", "NQRKRN",
	"NRQKRN", "NRKQRN", "NRKRQN", "NRKRNQ",
	"QRNNKR", "RQNNKR", "RNQNKR", "RNNQKR",
	"RNNKQR", "RNNKRQ", "QRNKNR", "RQNKNR",
	"RNQKNR", "RNKQNR", "RNKNQR", "RNKNRQ",
	"QRNKRN", "RQNKRN", "RNQKRN", "RNKQRN",
	"RNKRQN", "RNKRNQ", "QRKNNR", "RQKNNR",
	"RKQNNR", "RKNQNR", "RKNNQR", "RKNNRQ",
	"QRKNRN", "RQKNRN", "RKQNRN", "RKNQRN",
	"RKNRQN", "RKNRNQ", "QRKRNN", "RQKRNN",
	"RKQRNN", "RKRQNN", "RKRNQN", "RKRNNQ",
	"QNNRKR"
&#125;;
const int bishopTable&#91;16&#93;= &#123;
	0xc0, 0x90, 0x84, 0x81, 0x60, 0x30, 0x24, 0x21, 
	0x48, 0x18, 0x0c, 0x09, 0x42, 0x12, 0x06, 0x03
&#125;;
	// Calculate Starting positions
	for &#40;n=0; n<=960; n++) &#123;
		int i,j,k;
		int b;
		const char *s;
		char *f;

		i=n>>4;
		j=bishopTable&#91;n&0x0000000f&#93;;
		b=128;
		s=kingTable&#91;i&#93;;
		f=startFEN&#91;n&#93;;
		for &#40;k=1; k<=8; k++) &#123;
			if &#40;b&j&#41; &#123;
				*&#40;f++)='b';
				b>>=1;
				continue;
			&#125;
			*&#40;f++)=tolower&#40;*&#40;s++));
			b>>=1;
		&#125;

		strcpy&#40;f,"/pppppppp/8/8/8/8/PPPPPPPP/");

		i=n>>4;
		j=bishopTable&#91;n&0x0000000f&#93;;
		b=128;
		s=kingTable&#91;i&#93;;
		f=startFEN&#91;n&#93;+strlen&#40;startFEN&#91;n&#93;);
		for &#40;k=1; k<=8; k++) &#123;
			if &#40;b&j&#41; &#123;
				*&#40;f++)='B';
				b>>=1;
				continue;
			&#125;
			*&#40;f++)=toupper&#40;*&#40;s++));
			b>>=1;
		&#125;

		strcpy&#40;f," w KQkq -");
	&#125;
It seems that your code is not correct

I used the following link
http://www.dwheeler.com/essays/Fischer_ ... Chess.html

based on that link id=0 means that there are bishops at files a and b and based on your code there are bishops at files g and h.

Note that startFEN is not defined in your code but I added to your code
definition and initialization of this array as follow:

char * startFEN[960];
for (i=0;i<960;i++)
startFEN="";
pijl

Re: code to get FRC fen from id number

Post by pijl »

Uri Blass wrote: based on that link id=0 means that there are bishops at files a and b and based on your code there are bishops at files g and h.

Note that startFEN is not defined in your code but I added to your code
definition and initialization of this array as follow:

char * startFEN[960];
for (i=0;i<960;i++)
startFEN="";

Position 1 is stored in startFEN[1], Position 960 is stored in startFEN[960].
the contents at index 0 are not used.

declaration should be:
char startFEN[961][FENLENGTH];
where I defined FENLENGTH as 80.
Richard.
Uri Blass
Posts: 10297
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: code to get FRC fen from id number

Post by Uri Blass »

Thanks

I wanted to verify no errors.

It seems that now I get the same fen from my code and your code for every 0<=i<=959 when I only modify one line and add 0 1 that I have in my fen

strcpy(f," w KQkq - 0 1");

Uri