Example #1
0
void BookPGNReadFromFile (const char *file)
/****************************************************************************
 *
 *  To read a game from a PGN file and store out the hash entries to book.
 *
 ****************************************************************************/
{
   FILE *fp;
   char s[100], wmv[8], bmv[8];
   int c;
   unsigned int i;
   char header[2000];
   int moveno, result, ngames = 0;
   leaf *p;
   time_t t1, t2;
   double et;
   int examinecolor, playerfound[2];

/* Only players in the table below are permitted into the opening book 
   from the PGN files. Please expand the table as needed. Generally,
   I would recommend only acknowledged GM's and IM's and oneself, but
   because of the self-changing nature of the book, anything inferior
   will eventually be eliminated through automatic play as long as
   you feed the games the program plays back to itself with "book add pgnfile"
*/
/* XXX: Is it really a good idea to have a list like this hardcoded? */

const char *const player[] = {
  "Alekhine",
  "Adams",
  "Anand",
  "Anderssen",
  "Andersson",
  "Aronin",
  "Averbakh",
  "Balashov",
  "Beliavsky",
  "Benko",
  "Bernstein",
  "Bird",
  "Bogoljubow",
  "Bolbochan",
  "Boleslavsky",
  "Byrne",
  "Botvinnik",
  "Bronstein",
  "Browne",
  "Capablanca",
  "Chigorin",
  "Christiansen",
  "De Firmian",
  "Deep Blue",
  "Deep Thought",
  "Donner",
  "Dreev",
  "Duras",
  "Euwe",
  "Evans",
  "Filip",
  "Fine",
  "Fischer",
  "Flohr",
  "Furman",
  "Gelfand",
  "Geller",
  "Gereben",
  "Glek",
  "Gligoric",
  "GNU",
  "Golombek",
  "Gruenfeld",
  "Guimard",
  "Hodgson",
  "Ivanchuk",
  "Ivkov",
  "Janowsky",
  "Kamsky",
  "Kan",
  "Karpov",
  "Kasparov",
  "Keres",
  "Korchnoi",
  "Kortschnoj",
  "Kotov",
  "Kramnik",
  "Kupreich",
  "Lasker",
  "Lautier",
  "Letelier",
  "Lilienthal",
  "Ljubojevic",
  "Marshall",
  "Maroczy",
  "Mieses",
  "Miles",
  "Morphy",
  "Mueller",     /* Every other German has this name... */
  "Nimzowitsch",
  "Nunn",
  "Opocensky",
  "Pachman",
  "Petrosian",
  "Piket",
  "Pilnik",
  "Pirc",
  "Polgar",
  "Portisch",
  "Psakhis",
  "Ragozin",
  "Reshevsky",
  "Reti",
  "Romanish",
  "Rubinstein",
  "Saemisch",
  "Seirawan",
  "Shirov",
  "Short",
  "Silman",
  "Smyslov",
  "Sokolsky",
  "Spassky",
  "Sveshnikov",
  "Stahlberg",
  "Steinitz",
  "Tal",
  "Tarjan",
  "Tartakower",
  "Timman",
  "Topalov",
  "Torre",
  "Vidmar"

  };

   et = 0.0;
   t1 = time(NULL);
   result = -1;
   fp = fopen (file, "r");
   if (fp == NULL)
   {
     fprintf(stderr, "Cannot open file %s: %s\n", 
	     file, strerror(errno));
     return;
   }

   /* Maybe add some more clever error handling later */
   if (BookBuilderOpen() != BOOK_SUCCESS)
     return;
   newpos = existpos = 0;


 nextgame:

   header[0] = 0;
   InitVars ();
   NewPosition ();
   CLEAR (flags, MANUAL);
   CLEAR (flags, THINK);
   myrating = opprating = 0;

   playerfound[black] = playerfound[white] = 0;

   /* Skip all the tags */
   /*
    * XXX: This has two problems: 1) Leading whitespace leads to
    * undefined stuff in s, and completely confuses the program.
    * 2) If there is a game between two players in the list, only
    * the moves of the white player will be added.
    */

   /* Skip whitespace */
   while ((c=fgetc(fp)) != EOF) {
     if (c != ' ' && c != '\t' && c != '\n') {
       ungetc(c, fp);
       break;
     }
   }
   
   while ((c=fgetc(fp)) == '[') {
     ungetc(c, fp);
     fgets(s, 100, fp);
     strcat(header,s);
     if (strncmp(s+1, "White ",6) == 0) {
       examinecolor = white;
       ngames++;
     } else if (strncmp(s+1, "Black ",6) == 0) {
       examinecolor = black;
     } else if (strncmp(s+1, "Result", 6) == 0) {
       if (strncmp(s+7," \"1-0",5) == 0) {
	 result = R_WHITE_WINS;
       } else if (strncmp(s+7," \"0-1",5) == 0) {
	 result = R_BLACK_WINS;
       } else if (strncmp(s+7," \"1/2-1/2",9) == 0) {
	 result = R_DRAW;
       } else {
	 result = R_NORESULT;
       }
       continue;
     } else continue;
     /*
      * We get only here if White or Black matched, it is ugly but it 
      * works. Attention: If at some point we want to put this array
      * of authorized players in some external file, we have to keep
      * track of the size of that array in some other way.
      */
     for (i = 0; i < (sizeof(player) / sizeof(*player)); i++) {
       if (strstr(s+7, player[i]) != NULL) {
	 playerfound[examinecolor] = 1;
       }
     }
   }
   ungetc(c, fp);
   while (1) {
     if (fscanf(fp, "%d. %7s ", &moveno, wmv) < 2) break;

     if (wmv[0] == '1' || wmv[0] == '[' || wmv[0] == '*'
	 || strcmp(wmv, "0-1") == 0) break;
     
     p = ValidateMove (wmv);
     if (!p) {
       puts(header);
       ShowBoard();
       printf ("Illegal move %d. %s\n", moveno, wmv);
       break;
     }
     MakeMove (white, &p->move);
     if (playerfound[white]) {
       if (BookBuilder (result, white) != BOOK_SUCCESS) break;
     }
     strcpy (Game[GameCnt].SANmv, wmv);
     
     if (fscanf(fp, "%7s ", bmv) < 1) break;
     if (bmv[0] == '1' || bmv[0] == '[' || bmv[0] == '*'
	 || strcmp(bmv, "0-1")  == 0) break;

     p = ValidateMove (bmv);
     if (!p) {
       puts (header);
       ShowBoard();
       printf ("Illegal move %d. ... %s\n", moveno, bmv);
       break;
     }
     MakeMove (black, &p->move);
     if (playerfound[black] ) {
       if (BookBuilder (result, black) != BOOK_SUCCESS) break;
     }
     strcpy (Game[GameCnt].SANmv, bmv);
   }

   /* Read to end of game but don't parse */
   while (!feof(fp)) {
     fgets(s,100,fp);
     if (s[0] == '\n') break;
   }
   /* printf ("%d(%d)\n", GameCnt,BOOKDEPTH); */
   if (!feof(fp)) {
     if (ngames % 10 == 0) printf("%d\r",ngames);
     fflush(stdout);
     goto nextgame;
   }

   fclose (fp);
   if (BookBuilderClose() != BOOK_SUCCESS) {
     perror("Error writing opening book");
   }

   /* Reset the board otherwise we leave the last position in the book
      on the board. */
   header[0] = 0;
   InitVars ();
   NewPosition ();
   CLEAR (flags, MANUAL);
   CLEAR (flags, THINK);
   myrating = opprating = 0;

   t2 = time(NULL);
   et += difftime(t2, t1);
   putchar('\n');

   /* Handle divide-by-zero problem */
   if (et < 0.5) { et = 1.0; };

   printf("Time = %.0f seconds\n", et);
   printf("Games compiled: %d\n",ngames);
   printf("Games per second: %f\n",ngames/et);
   printf("Positions scanned: %d\n",newpos+existpos);
   printf("Positions per second: %f\n",(newpos+existpos)/et);
   printf("New & unique added: %d positions\n",newpos);
   printf("Duplicates not added: %d positions\n",existpos);
}
Example #2
0
void BookPGNReadFromFile (const char *file)
/****************************************************************************
 *
 *  To read a game from a PGN file and store out the hash entries to book.
 *
 ****************************************************************************/
{
   FILE *fp;
   int moveno, ngames = 0;
   time_t t1, t2;
   double et;
   int error;

   /* TODO: Fix reading from file */

   et = 0.0;
   t1 = time(NULL);

   fp = fopen (file, "r");
   if (fp == NULL)
   {
     fprintf(stderr, "Cannot open file %s: %s\n", 
	     file, strerror(errno));
     return;
   }
   yyin = fp;

   /* Maybe add some more clever error handling later */
   if (BookBuilderOpen() != BOOK_SUCCESS)
     return;
   newpos = existpos = 0;
   data_dest = DEST_BOOK;

   while(1) {
     InitVars ();
     NewPosition ();
     CLEAR (flags, MANUAL);
     CLEAR (flags, THINK);
     myrating = opprating = 0;

     error = yylex();
     if (error) break;

     ngames++;
     if (ngames % 10 == 0) printf("Games processed: %d\r",ngames);
     fflush(stdout);
   }

   fclose (fp);
   if (BookBuilderClose() != BOOK_SUCCESS) {
     perror("Error writing opening book during BookBuilderClose");
   }

   /* Reset the board otherwise we leave the last position in the book
      on the board. */
   InitVars ();
   NewPosition ();
   CLEAR (flags, MANUAL);
   CLEAR (flags, THINK);
   myrating = opprating = 0;

   t2 = time(NULL);
   et += difftime(t2, t1);
   putchar('\n');

   /* Handle divide-by-zero problem */
   if (et < 0.5) { et = 1.0; };

   printf("Time = %.0f seconds\n", et);
   printf("Games compiled: %d\n",ngames);
   printf("Games per second: %f\n",ngames/et);
   printf("Positions scanned: %d\n",newpos+existpos);
   printf("Positions per second: %f\n",(newpos+existpos)/et);
   printf("New & unique added: %d positions\n",newpos);
   printf("Duplicates not added: %d positions\n",existpos);
}