ChessUSA.com TalkChess.com
Hosted by Your Move Chess & Games
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

fen to fen functions
Goto page Previous  1, 2
 
Post new topic       TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions Threaded
View previous topic :: View next topic  
Author Message
Uri Blass



Joined: 08 Mar 2006
Posts: 5956
Location: Tel-Aviv Israel

PostPosted: Thu May 24, 2007 4:51 pm    Post subject: Re: fen to fen functions Reply to topic Reply with quote

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";
BR :: BB :: BK ::    BR
:: BP BP    BQ    :: BP
BP :: BN ::    :: BP ::
::    :: WQ WP BP ::    
   ::    ::    ::    ::
:: WB WP    ::    WB    
WP ::    ::    :: WP WP
::    :: WR :: WR WK    


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 -
BR ::    BK    BB    BR
BP    :: BQ :: BP BP    
   BP    ::    BN    BP
::    BP WP WQ    ::    
   ::    ::    ::    ::
:: WB ::    :: WP WB    
WP WP    ::    ::    WP
:: WK WR    WR    ::    
Back to top
View user's profile Send private message
Uri Blass



Joined: 08 Mar 2006
Posts: 5956
Location: Tel-Aviv Israel

PostPosted: Thu May 24, 2007 8:13 pm    Post subject: Re: fen to fen functions Reply to topic Reply with quote

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:
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;
   
   
}
Back to top
View user's profile Send private message
Steffan Westcott



Joined: 11 Mar 2006
Posts: 28
Location: Midlands, England

PostPosted: Sat Jun 02, 2007 5:05 pm    Post subject: Re: fen to fen functions Reply to topic Reply with quote

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:
# 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:
# 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 ' ', @_;
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic       TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions All times are GMT
Goto page Previous  1, 2
Threaded
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Powered by phpBB © 2001, 2005 phpBB Group
Enhanced with Moby Threads