fen to fen functions

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Uri Blass
Posts: 8598
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 4:51 pm

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: 8598
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 8:13 pm

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: Sat Mar 11, 2006 11:08 pm
Location: Midlands, England

Re: fen to fen functions

Post by steffan » Sat Jun 02, 2007 5:05 pm

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 ' ', @_;

Post Reply