Esempio n. 1
0
void WriteBranchList ( LList<BranchPtr> &blist, IntegerSink &isink )
{
   unsigned long n = blist.numItems();

   isink.writeDec ( n );          // number of branches in node
   for ( unsigned i=0; i < n; i++ )
   {
      blist[i]->write ( isink );
   }
}
Esempio n. 2
0
unsigned long Branch::querySize() const
{
   unsigned long size = 3;   // up front overhead: size + move + reply count

   for ( unsigned i=0; i < replies.numItems(); i++ )
   {
      BranchPtr r = replies[i];
      size += r->querySize();   // count + branchlist size
   }

   return size;
}
Esempio n. 3
0
void ReadData ( const char *inFilename,
                FILE *infile,
                LList<BranchPtr> &library )
{
   printf ( "Reading from file '%s'\n", inFilename );

   char line [128];
   LList<BranchPtr> *current = 0;

   while ( fgets ( line, sizeof(line), infile ) )
   {
      if ( StripComments ( line ) )
      {
         if ( line[0] == '[' )
         {
            // We have found the start of a new opening continuation

            current = &library;
            printf ( "processing %s\n", line );
         }
         else
         {
            // We have found the next move in the current continuation

            if ( !current )
            {
               fprintf ( stderr,
                         "Error:  move '%s' is not in a continuation!\n",
                         line );

               exit(1);
            }

            int len = (int) strlen(line);
            int isValid = 0;

            if ( len == 4 || (len == 5 && line[4] == '?') )
            {
               int x1 = line[0] - 'a';
               int y1 = line[1] - '1';
               int x2 = line[2] - 'a';
               int y2 = line[3] - '1';

               if ( x1 >= 0 && x1 <= 7 &&
                    y1 >= 0 && y1 <= 7 &&
                    x2 >= 0 && x2 <= 7 &&
                    y2 >= 0 && y2 <= 7 )
               {
                  isValid = 1;
                  unsigned short source = (x1+2) + (y1+2)*12;
                  unsigned short dest   = (x2+2) + (y2+2)*12;
                  unsigned short move   = (source << 8) | dest;

                  // First, see if this move is already there...
                  Branch *b = 0;

                  for ( unsigned j=0; j < current->numItems(); j++ )
                  {
                     Branch *x = (*current)[j];
                     if ( x->move == move )
                     {
                        b = x;
                        break;
                     }
                  }

                  if ( !b )
                  {
                     // This branch wasn't found, so create it!

                     b = new Branch;
                     if ( !b )
                     {
                        fprintf ( stderr, "Error: out of memory!\n" );
                        exit(1);
                     }

                     b->isBad = (line[4] == '?');
                     b->move  = move;

                     if ( current->addToBack ( b ) != DDC_SUCCESS )
                     {
                        fprintf ( stderr, "Error adding branch to node!\n" );
                        exit(1);
                     }
                  }

                  current = &(b->replies);
               }
            }

            if ( !isValid )
            {
               fprintf ( stderr,
                         "Error:  invalid move syntax: '%s'\n",
                         line );
               exit(1);
            }
         }
      }
   }
}