fen to fen functions

Discussion of chess software programming and technical issues.

Moderator: Ras

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

Re: fen to fen functions

Post by Uri Blass »

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

[d]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
[d]r2k1b1r/p2q1pp1/1p3n1p/2pPQ3/8/1B3PB1/PP5P/1KR1R3 w kq -
Uri Blass
Posts: 10788
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: fen to fen functions

Post by Uri Blass »

Here is a new code for translate_fen_2 that was not correct also because it had no code to change the file of enpassent pawn in the fen

Code: Select all

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;
	
	
}
steffan
Posts: 28
Joined: Sun Mar 12, 2006 12:08 am
Location: Midlands, England

Re: fen to fen functions

Post by steffan »

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:

Code: Select all

# mirror.pl : Prints FEN string of horizontally mirrored input position, castling rights clobbered
@_ = @ARGV;
$_[0] = reverse join '/', reverse split /\//, $_[0];
$_[2] = "-";
$_[3] =~ tr/a-h/hgfedcba/;
print join ' ', @_;

Code: Select all

# flip.pl : Prints FEN string of vertically flipped input position, colour swapped
@_ = @ARGV;
$_[0] = join '/', reverse split /\//, $_[0];
$_[0] =~ tr/a-zA-Z/A-Za-z/;
$_[1] =~ tr/wb/bw/;
$_[2] =~ tr/KQkq/kqKQ/;
$_[2] = join '', sort split //, $_[2];
$_[3] =~ tr/36/63/;
print join ' ', @_;