fen to fen functions

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Uri Blass
Posts: 8349
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

fen to fen functions

Post by Uri Blass » Mon May 21, 2007 10:17 am

I wonder if there is a free program with source that finds symmetric positions of a specific position.

The program simply should get a fen and parameter (same side to move in case that there is no problem because of castling rights or different side to move) and return a symmetric fen

Example:

The program should get the following fen

[D]4k3/8/8/pppppppp/8/8/PPPPPPPP/4K3 w - - 0 1

it can return the following fens(when the first is for the same side to move and the second is for different side to move)

[D]3k4/8/8/pppppppp/8/8/PPPPPPPP/3K4 w - - 0 1
[D]4k3/pppppppp/8/8/PPPPPPPP/8/8/4K3 b - - 0 1

Alessandro Scotti

Re: fen to fen functions

Post by Alessandro Scotti » Mon May 21, 2007 3:20 pm

There is some code in Kiwi that does something very similar to what you need, it's in engine.cxx function runEvalSuiteEPD.

User avatar
smrf
Posts: 484
Joined: Mon Mar 13, 2006 10:08 am
Location: Klein-Gerau, Germany
Contact:

Re: fen to fen functions

Post by smrf » Mon May 21, 2007 5:10 pm

Hi Uri,

SMIRF has such an edit option, but its source is not free. It changes

[D]4k3/8/8/pppppppp/8/8/PPPPPPPP/4K3 w - - 0 1

to

[D]4k3/pppppppp/8/8/PPPPPPPP/8/8/4K3 b - - 0 1

And this "switch positions" is working also for Chess960 and 10x8 CRC.

Regards, Reinhard.

Dann Corbit
Posts: 9425
Joined: Wed Mar 08, 2006 7:57 pm
Location: Redmond, WA USA
Contact:

Re: fen to fen functions

Post by Dann Corbit » Mon May 21, 2007 5:48 pm

Uri Blass wrote:I wonder if there is a free program with source that finds symmetric positions of a specific position.

The program simply should get a fen and parameter (same side to move in case that there is no problem because of castling rights or different side to move) and return a symmetric fen

Example:

The program should get the following fen

[D]4k3/8/8/pppppppp/8/8/PPPPPPPP/4K3 w - - 0 1

it can return the following fens(when the first is for the same side to move and the second is for different side to move)

[D]3k4/8/8/pppppppp/8/8/PPPPPPPP/3K4 w - - 0 1
[D]4k3/pppppppp/8/8/PPPPPPPP/8/8/4K3 b - - 0 1
There is a visual basic utility written by Les Fernandez that does exactly that.

I can send you a copy if you want, including the source code.

Uri Blass
Posts: 8349
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

Re: fen to fen functions

Post by Uri Blass » Tue May 22, 2007 1:21 pm

smrf wrote:Hi Uri,

SMIRF has such an edit option, but its source is not free. It changes

[D]4k3/8/8/pppppppp/8/8/PPPPPPPP/4K3 w - - 0 1

to

[D]4k3/pppppppp/8/8/PPPPPPPP/8/8/4K3 b - - 0 1

And this "switch positions" is working also for Chess960 and 10x8 CRC.

Regards, Reinhard.
Note that the function that I asked about is not function that is dependent on the move generator and the same function may be useful for many engines.

It is not an hard task to do it and I will write in C a function that does it by myself.

Uri

Alessandro Scotti

Re: fen to fen functions

Post by Alessandro Scotti » Tue May 22, 2007 3:46 pm

Just a wild shot:

Code: Select all

void fen2fen( char * fen, char * buf )
{
    for( char * e = strchr(fen,' ') - 1; e >= fen; e-- ) {
        *buf++ = *e ^ (isalpha(*e) ? 0x20 : 0x00);
    }
    strcpy( buf, strchr(fen,' ') );
}

Uri Blass
Posts: 8349
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

Re: fen to fen functions

Post by Uri Blass » Tue May 22, 2007 4:24 pm

Alessandro Scotti wrote:Just a wild shot:

Code: Select all

void fen2fen( char * fen, char * buf )
{
    for( char * e = strchr(fen,' ') - 1; e >= fen; e-- ) {
        *buf++ = *e ^ (isalpha(*e) ? 0x20 : 0x00);
    }
    strcpy( buf, strchr(fen,' ') );
}
If I understand correctly you try both to replace the side to move and replace the files of the pieces so a1 goes to h8 in this code.

This code also does not replace the side to move that is part of the fen
and it also is not correct for castling because there are order rules
and you need to write KQkq in this order and kqKQ is not correct fen.

This code is clearly shorter than my code.
I did not know the trick of xoring with 0x20 to make big letters small letters and the opposite.

I wrote so far code only for the case of changing side that is always possible and not for the case of left-right symmetry that may be impossible in case of castling rights(unless you use FRC and not normal chess and in that case left-right symmetry is always possible)

I did not go from the end to the beginning of the string and simply saved an array of strings for every rank of the board when later I used strncat to make from them one string(I have a special function to change one line when I do not need to change the order but only change big letters to small letters and the opposite).

I did not check that your code works but
thanks for sharing your code

I can make my code shorter based on the idea that you gave here but I do not think that it is important.

Uri

Alessandro Scotti

Re: fen to fen functions

Post by Alessandro Scotti » Tue May 22, 2007 4:46 pm

Hi Uri,
yes my code only tries to address the easiest scenario and it simply copies anything that follows the board representation. After all I'm not H. G. and there is a limit to what I can do in four lines of code! ;-)
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?

Dann Corbit
Posts: 9425
Joined: Wed Mar 08, 2006 7:57 pm
Location: Redmond, WA USA
Contact:

Re: fen to fen functions

Post by Dann Corbit » Tue May 22, 2007 6:33 pm

Uri Blass wrote:
Alessandro Scotti wrote:Just a wild shot:

Code: Select all

void fen2fen( char * fen, char * buf )
{
    for( char * e = strchr(fen,' ') - 1; e >= fen; e-- ) {
        *buf++ = *e ^ (isalpha(*e) ? 0x20 : 0x00);
    }
    strcpy( buf, strchr(fen,' ') );
}
If I understand correctly you try both to replace the side to move and replace the files of the pieces so a1 goes to h8 in this code.

This code also does not replace the side to move that is part of the fen
and it also is not correct for castling because there are order rules
and you need to write KQkq in this order and kqKQ is not correct fen.

This code is clearly shorter than my code.
I did not know the trick of xoring with 0x20 to make big letters small letters and the opposite.

I wrote so far code only for the case of changing side that is always possible and not for the case of left-right symmetry that may be impossible in case of castling rights(unless you use FRC and not normal chess and in that case left-right symmetry is always possible)

I did not go from the end to the beginning of the string and simply saved an array of strings for every rank of the board when later I used strncat to make from them one string(I have a special function to change one line when I do not need to change the order but only change big letters to small letters and the opposite).

I did not check that your code works but
thanks for sharing your code

I can make my code shorter based on the idea that you gave here but I do not think that it is important.

Uri
The xor trick relies upon the ASCII character set where 'A' is 0x41 and 'a' is 0x61 up to 'Z' is 0x5A and 'z' is 0x7A.

You can make reversing case more plain with:
foo = isupper(foo) ? tolower(foo) : toupper(foo);

Note that the toupper() and tolower() macros/functions do nothing if the input integer is not an alphabetic letter.

Uri Blass
Posts: 8349
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

Re: fen to fen functions

Post by Uri Blass » Thu May 24, 2007 12:52 pm

Alessandro Scotti wrote:Hi Uri,
yes my code only tries to address the easiest scenario and it simply copies anything that follows the board representation. After all I'm not H. G. and there is a limit to what I can do in four lines of code! ;-)
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?
No chance in competition
I simply tries to write code that works and I am not a good programmer like you but here is my code.

Note that it does not translate move to make in epd files but only the string of the position

Note that I used your trick of xoring with 0x20 to go from small letters to big letters and the opposite otherwise my code could be longer.

I have a different function to check if the string is legal fen so this certainly can crash on illegal fen.

Code: Select all

char fen1[1024];
char shortfen[10];
char * opp_line(char *rankfen)
{
	strcpy(shortfen,rankfen);
	unsigned int i;
	for &#40;i=0;i<strlen&#40;shortfen&#41;;i++)
//	if &#40;shortfen&#91;i&#93;>='a'&&shortfen&#91;i&#93;<='z')
//		shortfen&#91;i&#93;+='A'-'a';
//	else
//		if &#40;shortfen&#91;i&#93;>='A'&&shortfen&#91;i&#93;<='Z')
//		shortfen&#91;i&#93;+='a'-'A';
if &#40;isalpha&#40;shortfen&#91;i&#93;))
	shortfen&#91;i&#93;^=0x20;
	return shortfen;
&#125;
char *tranpose_line&#40;char *rankfen&#41;
&#123;
	memset&#40;shortfen,0,10&#41;;
	for&#40; char * e = rankfen+strlen&#40;rankfen&#41; - 1; e >= rankfen; e-- ) 
        shortfen&#91;strlen&#40;shortfen&#41;&#93;=*e;
	return shortfen;

&#125;
enum &#123;shortcastlewhite=1,longcastlewhite=2,shortcastleblack=4,longcastleblack=8&#125;;

char * translate_fen_1&#40;char *fen&#41;
&#123;
	memset&#40;fen1,0,1024&#41;;
	char tempfen&#91;10&#93;;
	char tempfen2&#91;10&#93;&#91;10&#93;;
	unsigned int l=strlen&#40;fen&#41;;
	unsigned int i=0;
	int i0;
	int j=0;
	int cast=0;
	while (&#40;i<l&#41;&&&#40;fen&#91;i&#93;==' '))
		i++;
	while &#40;fen&#91;i&#93;!=' ')
	&#123;
		i0=i;
		while (&#40;fen&#91;i&#93;!='/')&&&#40;fen&#91;i&#93;!=' '))
		i++;
		memset&#40;tempfen,0,10&#41;;
		memcpy&#40;tempfen,fen+i0,i-i0+1&#41;;
		strcpy&#40;tempfen2&#91;j&#93;,opp_line&#40;tempfen&#41;);
	 	if &#40;fen&#91;i&#93;=='/')
		i++;
		j++;
	&#125;
	j--;
	strncat&#40;fen1,tempfen2&#91;j&#93;,strlen&#40;tempfen2&#91;j&#93;));
	fen1&#91;strlen&#40;fen1&#41;-1&#93;='/';

	while &#40;j>0&#41;
	&#123;
		j--;
		strncat&#40;fen1,tempfen2&#91;j&#93;,strlen&#40;tempfen2&#91;j&#93;));
	&#125;
	fen1&#91;strlen&#40;fen1&#41;-1&#93;=' ';
	i++;
	while &#40;fen&#91;i&#93;==' ')
		i++;
	if (&#40;fen&#91;i&#93;=='w')||&#40;fen&#91;i&#93;=='W'))
	fen1&#91;strlen&#40;fen1&#41;&#93;='b';
	else
		fen1&#91;strlen&#40;fen1&#41;&#93;='w';
	fen1&#91;strlen&#40;fen1&#41;&#93;=' ';
	i++;
	while &#40;fen&#91;i&#93;==' ')
		i++;
	if &#40;fen&#91;i&#93;=='-')
	&#123;
		fen1&#91;strlen&#40;fen1&#41;&#93;='-';
		i++;
	&#125;
	else
	&#123;
		while &#40;fen&#91;i&#93;!=' ')
		&#123;
			if &#40;fen&#91;i&#93;=='k')
				cast|=shortcastlewhite;
			if &#40;fen&#91;i&#93;=='q')
				cast|=longcastlewhite;
			if &#40;fen&#91;i&#93;=='K')
				cast|=shortcastleblack;
			if &#40;fen&#91;i&#93;=='Q')
				cast|=longcastleblack;
			i++;
		&#125;
		if &#40;cast&shortcastlewhite&#41;
			fen1&#91;strlen&#40;fen1&#41;&#93;='K';
		if &#40;cast&longcastlewhite&#41;
			fen1&#91;strlen&#40;fen1&#41;&#93;='Q';
		if &#40;cast&shortcastleblack&#41;
			fen1&#91;strlen&#40;fen1&#41;&#93;='k';
		if &#40;cast&longcastleblack&#41;
			fen1&#91;strlen&#40;fen1&#41;&#93;='q';
	&#125;
	fen1&#91;strlen&#40;fen1&#41;&#93;=' ';
	while &#40;fen&#91;i&#93;==' ')
		i++;
	if &#40;fen&#91;i&#93;=='-')
		fen1&#91;strlen&#40;fen1&#41;&#93;='-';
	else
	&#123;
		fen1&#91;strlen&#40;fen1&#41;&#93;=fen&#91;i&#93;;
		i++;
		fen1&#91;strlen&#40;fen1&#41;&#93;=9-fen&#91;i&#93;;
	&#125;
	i++;
	while &#40;i!=l&#41;
	&#123;
		fen1&#91;strlen&#40;fen1&#41;&#93;=fen&#91;i&#93;;
		i++;
	&#125;

	return fen1;
&#125;
char *translate_fen_2&#40;char *fen&#41;
&#123;
	unsigned int l=strlen&#40;fen&#41;;
	unsigned int i=0;
	char tempfen&#91;10&#93;;
	int i0;
	int cast=0;
	memset&#40;fen1,0,1024&#41;;
	while (&#40;i<l&#41;&&&#40;fen&#91;i&#93;==' '))
		i++;
	while &#40;fen&#91;i&#93;!=' ')
	&#123;
		i0=i;
		while (&#40;fen&#91;i&#93;!='/')&&&#40;fen&#91;i&#93;!=' '))
		i++;
		memset&#40;tempfen,0,10&#41;;
		memcpy&#40;tempfen,fen+i0,i-i0&#41;;
		strcat&#40;fen1,tranpose_line&#40;tempfen&#41;);
		fen1&#91;strlen&#40;fen1&#41;&#93;=fen&#91;i&#93;;
		if &#40;fen&#91;i&#93;=='/')
			i++;
	&#125;
	while (&#40;fen&#91;i&#93;==' ')&&&#40;i<l&#41;)
		i++;
	//correct color
	if &#40;i<l&#41;
		fen1&#91;strlen&#40;fen1&#41;&#93;=fen&#91;i&#93;;
	//correct space after color
	fen1&#91;strlen&#40;fen1&#41;&#93;=' ';
	i++;
	if &#40;fen&#91;i&#93;==' ')
		i++;
	if &#40;i<l&#41;
	if &#40;fen&#91;i&#93;=='-')
	&#123;
		while &#40;i<l&#41;
		&#123;
			fen1&#91;strlen&#40;fen1&#41;&#93;=fen&#91;i&#93;;
			i++;
		&#125;
	&#125;
	else
	&#123;
		//castling rights
		
		while (&#40;fen&#91;i&#93;!=' ')&&&#40;i<l&#41;)
		&#123;
			if &#40;fen&#91;i&#93;=='k')
				cast|=longcastleblack;
			if &#40;fen&#91;i&#93;=='q')
				cast|=shortcastleblack;
			if &#40;fen&#91;i&#93;=='K')
				cast|=longcastlewhite;
			if &#40;fen&#91;i&#93;=='Q')
				cast|=shortcastlewhite;
			i++;
		&#125;
		if &#40;cast&shortcastlewhite&#41;
			fen1&#91;strlen&#40;fen1&#41;&#93;='K';
		if &#40;cast&longcastlewhite&#41;
			fen1&#91;strlen&#40;fen1&#41;&#93;='Q';
		if &#40;cast&shortcastleblack&#41;
			fen1&#91;strlen&#40;fen1&#41;&#93;='k';
		if &#40;cast&longcastleblack&#41;
			fen1&#91;strlen&#40;fen1&#41;&#93;='q';
		while &#40;i<l&#41;
		&#123;
			fen1&#91;strlen&#40;fen1&#41;&#93;=fen&#91;i&#93;;
			i++;
		&#125;

	&#125;
	return fen1;
	
	
&#125;

Post Reply