fen to fen functions

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Uri Blass
Posts: 10279
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: 10279
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 (&#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++;
	//castling rights
	if &#40;i<l&#41;
	&#123;
		if &#40;fen&#91;i&#93;=='-')
		&#123;
			fen1&#91;strlen&#40;fen1&#41;&#93;='-';
			i++;
		&#125;
		else
			return fen1;//should never happen
	&#125;
	//now we need enpassent pawn
	fen1&#91;strlen&#40;fen1&#41;&#93;=' ';
	while (&#40;i<l&#41;&&&#40;fen&#91;i&#93;==' '))
		i++;
	if &#40;i==l&#41;
		return fen1;//should never happen
	if &#40;fen&#91;i&#93;!='-')
	&#123;
		fen1&#91;strlen&#40;fen1&#41;&#93;='h'+'a'-fen&#91;i&#93;;//correct file for the enpassent pawn
		i++;
	&#125;
	while &#40;i<l&#41;
	&#123;
		fen1&#91;strlen&#40;fen1&#41;&#93;=fen&#91;i&#93;;
		i++;
	&#125;
	 
	return fen1;
	
	
&#125;
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 &#58; Prints FEN string of horizontally mirrored input position, castling rights clobbered
@_ = @ARGV;
$_&#91;0&#93; = reverse join '/', reverse split /\//, $_&#91;0&#93;;
$_&#91;2&#93; = "-";
$_&#91;3&#93; =~ tr/a-h/hgfedcba/;
print join ' ', @_;

Code: Select all

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