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
Post new topic    TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions Flat
View previous topic :: View next topic  
Author Message
Uri Blass



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

PostPost subject: Re: fen to fen functions    Posted: Thu May 24, 2007 12:52 pm Reply to topic Reply with quote

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! Wink
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:
char fen1[1024];
char shortfen[10];
char * opp_line(char *rankfen)
{
   strcpy(shortfen,rankfen);
   unsigned int i;
   for (i=0;i<strlen(shortfen);i++)
//   if (shortfen[i]>='a'&&shortfen[i]<='z')
//      shortfen[i]+='A'-'a';
//   else
//      if (shortfen[i]>='A'&&shortfen[i]<='Z')
//      shortfen[i]+='a'-'A';
if (isalpha(shortfen[i]))
   shortfen[i]^=0x20;
   return shortfen;
}
char *tranpose_line(char *rankfen)
{
   memset(shortfen,0,10);
   for( char * e = rankfen+strlen(rankfen) - 1; e >= rankfen; e-- )
        shortfen[strlen(shortfen)]=*e;
   return shortfen;

}
enum {shortcastlewhite=1,longcastlewhite=2,shortcastleblack=4,longcastleblack=8};

char * translate_fen_1(char *fen)
{
   memset(fen1,0,1024);
   char tempfen[10];
   char tempfen2[10][10];
   unsigned int l=strlen(fen);
   unsigned int i=0;
   int i0;
   int j=0;
   int cast=0;
   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+1);
      strcpy(tempfen2[j],opp_line(tempfen));
       if (fen[i]=='/')
      i++;
      j++;
   }
   j--;
   strncat(fen1,tempfen2[j],strlen(tempfen2[j]));
   fen1[strlen(fen1)-1]='/';

   while (j>0)
   {
      j--;
      strncat(fen1,tempfen2[j],strlen(tempfen2[j]));
   }
   fen1[strlen(fen1)-1]=' ';
   i++;
   while (fen[i]==' ')
      i++;
   if ((fen[i]=='w')||(fen[i]=='W'))
   fen1[strlen(fen1)]='b';
   else
      fen1[strlen(fen1)]='w';
   fen1[strlen(fen1)]=' ';
   i++;
   while (fen[i]==' ')
      i++;
   if (fen[i]=='-')
   {
      fen1[strlen(fen1)]='-';
      i++;
   }
   else
   {
      while (fen[i]!=' ')
      {
         if (fen[i]=='k')
            cast|=shortcastlewhite;
         if (fen[i]=='q')
            cast|=longcastlewhite;
         if (fen[i]=='K')
            cast|=shortcastleblack;
         if (fen[i]=='Q')
            cast|=longcastleblack;
         i++;
      }
      if (cast&shortcastlewhite)
         fen1[strlen(fen1)]='K';
      if (cast&longcastlewhite)
         fen1[strlen(fen1)]='Q';
      if (cast&shortcastleblack)
         fen1[strlen(fen1)]='k';
      if (cast&longcastleblack)
         fen1[strlen(fen1)]='q';
   }
   fen1[strlen(fen1)]=' ';
   while (fen[i]==' ')
      i++;
   if (fen[i]=='-')
      fen1[strlen(fen1)]='-';
   else
   {
      fen1[strlen(fen1)]=fen[i];
      i++;
      fen1[strlen(fen1)]=9-fen[i];
   }
   i++;
   while (i!=l)
   {
      fen1[strlen(fen1)]=fen[i];
      i++;
   }

   return fen1;
}
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++;
   if (i<l)
   if (fen[i]=='-')
   {
      while (i<l)
      {
         fen1[strlen(fen1)]=fen[i];
         i++;
      }
   }
   else
   {
      //castling rights
      
      while ((fen[i]!=' ')&&(i<l))
      {
         if (fen[i]=='k')
            cast|=longcastleblack;
         if (fen[i]=='q')
            cast|=shortcastleblack;
         if (fen[i]=='K')
            cast|=longcastlewhite;
         if (fen[i]=='Q')
            cast|=shortcastlewhite;
         i++;
      }
      if (cast&shortcastlewhite)
         fen1[strlen(fen1)]='K';
      if (cast&longcastlewhite)
         fen1[strlen(fen1)]='Q';
      if (cast&shortcastleblack)
         fen1[strlen(fen1)]='k';
      if (cast&longcastleblack)
         fen1[strlen(fen1)]='q';
      while (i<l)
      {
         fen1[strlen(fen1)]=fen[i];
         i++;
      }

   }
   return fen1;
   
   
}
Back to top
View user's profile Send private message
Display posts from previous:   
Subject Author Date/Time
fen to fen functions Uri Blass Mon May 21, 2007 10:17 am
      Re: fen to fen functions Alessandro Scotti Mon May 21, 2007 3:20 pm
      Re: fen to fen functions Reinhard Scharnagl Mon May 21, 2007 5:10 pm
            Re: fen to fen functions Uri Blass Tue May 22, 2007 1:21 pm
                  Re: fen to fen functions Alessandro Scotti Tue May 22, 2007 3:46 pm
                        Re: fen to fen functions Uri Blass Tue May 22, 2007 4:24 pm
                              Re: fen to fen functions Alessandro Scotti Tue May 22, 2007 4:46 pm
                                    Re: fen to fen functions Uri Blass Thu May 24, 2007 12:52 pm
                                          Re: fen to fen functions Uri Blass Thu May 24, 2007 4:51 pm
                                                Re: fen to fen functions Uri Blass Thu May 24, 2007 8:13 pm
                                    Re: fen to fen functions Steffan Westcott Sat Jun 02, 2007 5:05 pm
                              Re: fen to fen functions Dann Corbit Tue May 22, 2007 6:33 pm
      Re: fen to fen functions Dann Corbit Mon May 21, 2007 5:48 pm
Post new topic    TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions

 
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