home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / bbs / libdisks / d700t799 / disk790.lha / UChess / UChessSrc.lha / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-17  |  9.7 KB  |  425 lines

  1. /*
  2.  * util.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include "gnuchess.h"
  24. unsigned int TTadd = 0;
  25. short int recycle;
  26. extern char mvstr[4][6];
  27. #ifdef CACHE
  28. extern struct etable __far etab[2][ETABLE];
  29. #endif
  30.  
  31. int
  32. parse (FILE * fd, short unsigned int *mv, short int side, char *opening)
  33. {
  34.   register int c, i, r1, r2, c1, c2;
  35.   char s[128];
  36.   char *p;
  37.  
  38.   while ((c = getc (fd)) == ' ' || c == '\n') ;
  39.   i = 0;
  40.   s[0] = (char) c;
  41.   if (c == '!')
  42.     {
  43.       p = opening;
  44.       do
  45.     {
  46.       *p++ = c;
  47.       c = getc (fd);
  48.       if (c == '\n' || c == EOF)
  49.         {
  50.           *p = '\0';
  51.           return 0;
  52.         }
  53.       } while (true);
  54.     }
  55.   while (c != '?' && c != ' ' && c != '\t' && c != '\n' && c != EOF)
  56.     s[++i] = (char) (c = getc (fd));
  57.   s[++i] = '\0';
  58.   if (c == EOF)
  59.     return (-1);
  60.   if (s[0] == '!' || s[0] == ';' || i < 3)
  61.     {
  62.       while (c != '\n' && c != EOF)
  63.     c = getc (fd);
  64.       return (0);
  65.     }
  66.   if (s[4] == 'o')
  67.     *mv = ((side == black) ? LONGBLACKCASTLE : LONGWHITECASTLE);
  68.   else if (s[0] == 'o')
  69.     *mv = ((side == black) ? BLACKCASTLE : WHITECASTLE);
  70.   else
  71.     {
  72.       c1 = s[0] - 'a';
  73.       r1 = s[1] - '1';
  74.       c2 = s[2] - 'a';
  75.       r2 = s[3] - '1';
  76.       *mv = (locn (r1, c1) << 8) | locn (r2, c2);
  77.     }
  78.   if (c == '?')
  79.     {                /* Bad move, not for the program to play */
  80.       *mv |= 0x8000;        /* Flag it ! */
  81.       c = getc (fd);
  82.     }
  83.   return (1);
  84. }
  85.  
  86.  
  87. #if ttblsz
  88.  
  89. #define CB(i) (unsigned char) ((color[2 * (i)] ? 0x80 : 0)\
  90.            | (board[2 * (i)] << 4)\
  91.            | (color[2 * (i) + 1] ? 0x8 : 0)\
  92.            | (board[2 * (i) + 1]))
  93.  
  94. int
  95. ProbeTTable (short int side,
  96.          short int depth,
  97.          short int ply,
  98.          short int *alpha,
  99.          short int *beta,
  100.          short int *score)
  101.  
  102. /*
  103.  * Look for the current board position in the transposition table.
  104.  */
  105.  
  106. {
  107.   register struct hashentry *ptbl;
  108.   register /*unsigned*/ short i;  /*to match new type of rehash --tpm*/
  109.  
  110.   ptbl = &ttable[side][hashkey & (ttblsize - 1)];
  111.  
  112.   /* rehash max rehash times */
  113.   for (i = 0; (ptbl->depth) && (ptbl->hashbd != hashbd) && (i < rehash); i++) ptbl++;
  114.   if ((ptbl->depth) && (ptbl->hashbd == hashbd))
  115. /*     ^^^^^^^^^^^^   we can use some informations of the entry even
  116.                       the depth is not large enough */
  117.     {
  118. #ifdef HASHTEST
  119.       for (i = 0; i < 32; i++)
  120.     {
  121.       if (ptbl->bd[i] != CB (i))
  122.         {
  123. #if !defined CHESSTOOL && !defined XBOARD
  124.           HashCol++;
  125.           ShowMessage (CP[199]);    /*ttable collision detected*/
  126. #endif
  127.           break;
  128.         }
  129.     }
  130. #endif /* HASHTEST */
  131.  
  132.  
  133.       PV = SwagHt = ptbl->mv;
  134.       if ((short) ptbl->depth >= depth)
  135.     {
  136. #if !defined CHESSTOOL && !defined XBOARD
  137.           HashCnt++;
  138. #endif
  139.       if (ptbl->flags & truescore)
  140.         {
  141.           *score = ptbl->score;
  142.           /* adjust *score so moves to mate is from root */
  143.           if (*score > 9000)
  144.         *score -= ply;
  145.           else if (*score < -9000)
  146.         *score += ply;
  147.           *beta = -20000;
  148.         }
  149. #ifdef notdef            /* Never happens! see search */
  150.       else if (ptbl->flags & upperbound)
  151.         {
  152.           if (ptbl->score < *beta)
  153.         *beta = ptbl->score + 1;
  154.         }
  155. #endif
  156.       else if (ptbl->flags & lowerbound)
  157.         {
  158.           if (ptbl->score > *alpha)
  159.         *alpha = ptbl->score - 1;
  160.         }
  161.       return (true);
  162.     }
  163.     }
  164.   return (false);
  165. }
  166.  
  167.  
  168. int
  169. PutInTTable (short int side,
  170.          short int score,
  171.          short int depth,
  172.          short int ply,
  173.          short int alpha,
  174.          short int beta,
  175.          short unsigned int mv)
  176.  
  177. /*
  178.  * Store the current board position in the transposition table.
  179.  */
  180.  
  181. {
  182.   register struct hashentry *ptbl;
  183.   register /*unsigned*/ short i;  /*to match new type of rehash --tpm*/
  184.  
  185.   ptbl = &ttable[side][hashkey & (ttblsize - 1)];
  186.  
  187.   /* rehash max rehash times */
  188.   for (i = 0; ptbl->depth && ptbl->hashbd != hashbd && i < rehash; i++)
  189.     ptbl++;
  190.   if (i == rehash) {
  191. #if !defined CHESSTOOL && !defined XBOARD
  192.     THashCol++;
  193. #endif
  194.     ptbl -= recycle;}
  195.   if (depth >= (short)ptbl->depth || ptbl->hashbd != hashbd)
  196.     {
  197. #if !defined CHESSTOOL && !defined XBOARD
  198.       TTadd++;
  199.       HashAdd++; 
  200. #endif
  201.       ptbl->hashbd = hashbd;
  202.       ptbl->depth = (unsigned char) depth;
  203.       /* adjust score so moves to mate is from this ply */
  204.       if (score > 9000)
  205.     score += ply;
  206.       else if (score < -9000)
  207.     score -= ply;
  208.       ptbl->score = score;
  209.       ptbl->mv = mv;
  210. #ifdef DEBUG4
  211.       if (debuglevel & 32)
  212.     {
  213.       algbr (mv >> 8, mv & 0xff, 0);
  214.       printf ("-add-> d=%d s=%d p=%d a=%d b=%d %s\n", depth, score, ply, alpha, beta, mvstr);
  215.     }
  216. #endif
  217. /*#ifdef notdef
  218.       if (score < alpha)
  219.     ptbl->flags = upperbound;
  220.       else
  221. /*#endif /* 0 */
  222.       if (score > beta)
  223.     {
  224.       ptbl->flags = lowerbound;
  225.       score = beta + 1;
  226.     }
  227.       else
  228.     ptbl->flags = truescore;
  229.  
  230. #ifdef HASHTEST
  231.       for (i = 0; i < 32; i++)
  232.     {
  233.       ptbl->bd[i] = CB (i);
  234.     }
  235. #endif /* HASHTEST */
  236.       return true;
  237.     }
  238.   return false;
  239. }
  240.  
  241.    
  242. static struct hashentry *ttagew, *ttageb;
  243.  
  244. void
  245. ZeroTTable (void)
  246. {
  247.    register struct hashentry *w, *b;
  248.   for(w=ttable[white],b=ttable[black];w<&ttable[white][ttblsize];w++,b++)
  249.    { w->depth = 0; b->depth = 0;}
  250.     ttagew = ttable[white]; ttageb = ttable[black];
  251. #ifdef CACHE
  252.    memset ((char *) etab, 0, sizeof (etab));
  253. #endif
  254. #ifdef notdef
  255.   register unsigned int a;
  256.   for (a = 0; a < ttblsize + (unsigned int)rehash; a++)
  257.     {
  258.       ttable[white][a].depth = 0;
  259.       ttable[black][a].depth = 0;
  260.     }
  261. #endif
  262.     TTadd = 0; 
  263. }
  264.  
  265. #ifdef HASHFILE
  266. int Fbdcmp(char *a,char *b)
  267. {
  268.     register int i;
  269.     for(i = 0;i<32;i++)
  270.         if(a[i] != b[i])return false;
  271.     return true;
  272. }
  273. int
  274. ProbeFTable (short int side,
  275.          short int depth,
  276.          short int ply,
  277.          short int *alpha,
  278.          short int *beta,
  279.          short int *score)
  280.  
  281. /*
  282.  * Look for the current board position in the persistent transposition table.
  283.  */
  284.  
  285. {
  286.   register short int i;
  287.   register unsigned long hashix;
  288.   struct fileentry new, t;
  289.  
  290.   hashix = ((side == white) ? (hashkey & 0xFFFFFFFE) : (hashkey | 1)) & filesz;
  291.  
  292.   for (i = 0; i < 32; i++)
  293.     new.bd[i] = CB (i);
  294.   new.flags = 0;
  295.   if (Mvboard[kingP[side]] == 0)
  296.     {
  297.       if (Mvboard[qrook[side]] == 0)
  298.     new.flags |= queencastle;
  299.       if (Mvboard[krook[side]] == 0)
  300.     new.flags |= kingcastle;
  301.     }
  302.   for (i = 0; i < frehash; i++)
  303.     {
  304.       fseek (hashfile,
  305.          sizeof (struct fileentry) * ((hashix + 2 * i) & (filesz)),
  306.          SEEK_SET);
  307.       fread (&t, sizeof (struct fileentry), 1, hashfile);
  308.       if (!t.depth) break;
  309.        if(!Fbdcmp(t.bd, new.bd)) continue;
  310.       if (((short int) t.depth >= depth) 
  311.       && (new.flags == (unsigned short)(t.flags & (kingcastle | queencastle))))
  312.     {
  313. #if !defined CHESSTOOL && !defined XBOARD
  314.       FHashCnt++;
  315. #endif
  316.       PV = (t.f << 8) | t.t;
  317.       *score = (t.sh << 8) | t.sl;
  318.       /* adjust *score so moves to mate is from root */
  319.       if (*score > 9000)
  320.         *score -= ply;
  321.       else if (*score < -9000)
  322.         *score += ply;
  323.       if (t.flags & truescore)
  324.         {
  325.           *beta = -20000;
  326.         }
  327.       else if (t.flags & lowerbound)
  328.         {
  329.           if (*score > *alpha)
  330.         *alpha = *score - 1;
  331.         }
  332.       else if (t.flags & upperbound)
  333.         {
  334.           if (*score < *beta)
  335.         *beta = *score + 1;
  336.         }
  337.       return (true);
  338.     }
  339.     }
  340.   return (false);
  341. }
  342.  
  343. void
  344. PutInFTable (short int side,
  345.          short int score,
  346.          short int depth,
  347.          short int ply,
  348.          short int alpha,
  349.          short int beta,
  350.          short unsigned int f,
  351.          short unsigned int t)
  352.  
  353. /*
  354.  * Store the current board position in the persistent transposition table.
  355.  */
  356.  
  357. {
  358.   register unsigned short i;
  359.   register unsigned long hashix;
  360.   struct fileentry new, tmp;
  361.  
  362.   hashix = ((side == white) ? (hashkey & 0xFFFFFFFE) : (hashkey | 1)) & filesz;
  363.   for (i = 0; i < 32; i++) new.bd[i] = CB (i);
  364.   new.f = (unsigned char) f;
  365.   new.t = (unsigned char) t;
  366.   if (score < alpha)
  367.     new.flags = upperbound;
  368.   else
  369.     new.flags = ((score > beta) ? lowerbound : truescore);
  370.   if (Mvboard[kingP[side]] == 0)
  371.     {
  372.       if (Mvboard[qrook[side]] == 0)
  373.     new.flags |= queencastle;
  374.       if (Mvboard[krook[side]] == 0)
  375.     new.flags |= kingcastle;
  376.     }
  377.   new.depth = (unsigned char) depth;
  378.   /* adjust *score so moves to mate is from root */
  379.   if (score > 9000)
  380.     score += ply;
  381.   else if (score < -9000)
  382.     score -= ply;
  383.  
  384.  
  385.   new.sh = (unsigned char) (score >> 8);
  386.   new.sl = (unsigned char) (score & 0xFF);
  387.  
  388.   for (i = 0; i < frehash; i++)
  389.     {
  390.       fseek (hashfile,
  391.          sizeof (struct fileentry) * ((hashix + 2 * i) & (filesz)),
  392.          SEEK_SET);
  393.       if(fread (&tmp, sizeof (struct fileentry), 1, hashfile) == NULL){perror("hashfile");exit(1);}
  394.       if (tmp.depth && !Fbdcmp(tmp.bd,new.bd))continue;
  395.       if(tmp.depth == depth)break;
  396.       if (!tmp.depth || (short) tmp.depth < depth)
  397.     {
  398.       fseek (hashfile,
  399.          sizeof (struct fileentry) * ((hashix + 2 * i) & (filesz)),
  400.          SEEK_SET);
  401.       fwrite (&new, sizeof (struct fileentry), 1, hashfile);
  402. #if !defined CHESSTOOL && !defined XBOARD
  403.           FHashAdd++;
  404. #endif
  405.       break;
  406.     }
  407.     }
  408. }
  409.  
  410. #endif /* HASHFILE */
  411. #endif /* ttblsz */
  412.  
  413. void
  414. ZeroRPT (void)
  415. {
  416. #ifdef NOMEMSET
  417.   register int side, i;
  418.   for (side = white; side <= black; side++)
  419.     for (i = 0; i < 256;)
  420.       rpthash[side][i++] = 0;
  421. #else
  422.    memset ((char *) rpthash, 0, sizeof (rpthash));
  423. #endif
  424. }
  425.