home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 3 / cd.iso / os2 / pmgnuchs / util.c < prev    next >
Text File  |  1994-02-23  |  7KB  |  264 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 <os2.h>
  24. #include <strings.h>
  25. #include "gnuchess.h"
  26. extern unsigned int TTadd;
  27. short int ISZERO = 1, *TrP;
  28. extern char mvstr[4][6];
  29. #ifdef CACHE
  30. extern struct etable etab[2][ETABLE];
  31. #endif
  32.  
  33. int
  34. parse (FILE * fd, short unsigned int *mv, short int side, char *opening)
  35. {
  36.   register int c, i, r1, r2, c1, c2;
  37.   char s[128];
  38.   char *p;
  39.  
  40.   while ((c = getc (fd)) == ' ' || c == '\n') ;
  41.   i = 0;
  42.   s[0] = (char) c;
  43.   if (c == '!')
  44.     {
  45.       p = opening;
  46.       do
  47.     {
  48.       *p++ = c;
  49.       c = getc (fd);
  50.       if (c == '\n' || c == EOF)
  51.         {
  52.           *p = '\0';
  53.           return 0;
  54.         }
  55.       } while (true);
  56.     }
  57.   while (c != '?' && c != ' ' && c != '\t' && c != '\n' && c != EOF)
  58.     s[++i] = (char) (c = getc (fd));
  59.   s[++i] = '\0';
  60.   if (c == EOF)
  61.     return (-1);
  62.   if (s[0] == '!' || s[0] == ';' || i < 3)
  63.     {
  64.       while (c != '\n' && c != EOF)
  65.     c = getc (fd);
  66.       return (0);
  67.     }
  68.   if (s[4] == 'o')
  69.     *mv = ((side == black) ? LONGBLACKCASTLE : LONGWHITECASTLE);
  70.   else if (s[0] == 'o')
  71.     *mv = ((side == black) ? BLACKCASTLE : WHITECASTLE);
  72.   else
  73.     {
  74.       c1 = s[0] - 'a';
  75.       r1 = s[1] - '1';
  76.       c2 = s[2] - 'a';
  77.       r2 = s[3] - '1';
  78.       *mv = (locn (r1, c1) << 8) | locn (r2, c2);
  79.     }
  80.   if (c == '?')
  81.     {                /* Bad move, not for the program to play */
  82.       *mv |= 0x8000;        /* Flag it ! */
  83.       c = getc (fd);
  84.     }
  85.   return (1);
  86. }
  87. #ifdef ttblsz
  88. static struct hashentry *ttagew, *ttageb;
  89.  
  90. void
  91. ZeroTTable (void)
  92. {
  93. bzero(ttable[white],(unsigned)(ttblsize+rehash));
  94. bzero(ttable[black],(unsigned)(ttblsize+rehash));
  95.     TTadd = 0; 
  96. }
  97.  
  98. #ifdef HASHFILE
  99. int Fbdcmp(char *a,char *b)
  100. {
  101.     register int i;
  102.     for(i = 0;i<32;i++)
  103.         if(a[i] != b[i])return false;
  104.     return true;
  105. }
  106.  
  107. #define CB(i) (unsigned char) ((color[2 * (i)] ? 0x80 : 0)\
  108.                | (board[2 * (i)] << 4)\
  109.                | (color[2 * (i) + 1] ? 0x8 : 0)\
  110.                | (board[2 * (i) + 1]))
  111.  
  112. int
  113. ProbeFTable (short int side,
  114.          short int depth,
  115.          short int ply,
  116.          short int *alpha,
  117.          short int *beta,
  118.          short int *score)
  119.  
  120. /*
  121.  * Look for the current board position in the persistent transposition table.
  122.  */
  123.  
  124. {
  125.   register short int i;
  126.   register unsigned long hashix;
  127.   struct fileentry new, t;
  128.  
  129.   hashix = ((side == white) ? (hashkey & 0xFFFFFFFE) : (hashkey | 1)) % filesz;
  130.  
  131.   for (i = 0; i < 32; i++)
  132.     new.bd[i] = CB (i);
  133.   new.flags = 0;
  134.   if (Mvboard[kingP[side]] == 0)
  135.     {
  136.       if (Mvboard[qrook[side]] == 0)
  137.     new.flags |= queencastle;
  138.       if (Mvboard[krook[side]] == 0)
  139.     new.flags |= kingcastle;
  140.     }
  141.   for (i = 0; i < frehash; i++)
  142.     {
  143.       fseek (hashfile,
  144.          sizeof (struct fileentry) * ((hashix + 2 * i) % (filesz)),
  145.          SEEK_SET);
  146.       fread (&t, sizeof (struct fileentry), 1, hashfile);
  147.       if (!t.depth) break;
  148.        if(!Fbdcmp(t.bd, new.bd)) continue;
  149.       if (((short int) t.depth >= depth) 
  150.       && (new.flags == (unsigned short)(t.flags & (kingcastle | queencastle))))
  151.     {
  152. #ifndef BAREBONES
  153.       FHashCnt++;
  154. #endif
  155.       PV = (t.f << 8) | t.t;
  156.       *score = (t.sh << 8) | t.sl;
  157.       /* adjust *score so moves to mate is from root */
  158.       if (*score > 9000)
  159.         *score -= ply;
  160.       else if (*score < -9000)
  161.         *score += ply;
  162.       if (t.flags & truescore)
  163.         {
  164.           *beta = -20000;
  165.         }
  166.       else if (t.flags & lowerbound)
  167.         {
  168.           if (*score > *alpha)
  169.         *alpha = *score - 1;
  170.         }
  171.       else if (t.flags & upperbound)
  172.         {
  173.           if (*score < *beta)
  174.         *beta = *score + 1;
  175.         }
  176.       return (true);
  177.     }
  178.     }
  179.   return (false);
  180. }
  181.  
  182. void
  183. PutInFTable (short int side,
  184.          short int score,
  185.          short int depth,
  186.          short int ply,
  187.          short int alpha,
  188.          short int beta,
  189.          short unsigned int f,
  190.          short unsigned int t)
  191.  
  192. /*
  193.  * Store the current board position in the persistent transposition table.
  194.  */
  195.  
  196. {
  197.   register unsigned short i;
  198.   register unsigned long hashix;
  199.   struct fileentry new, tmp;
  200.  
  201.   hashix = ((side == white) ? (hashkey & 0xFFFFFFFE) : (hashkey | 1)) % filesz;
  202.   for (i = 0; i < 32; i++) new.bd[i] = CB (i);
  203.   new.f = (unsigned char) f;
  204.   new.t = (unsigned char) t;
  205.   if (score < alpha)
  206.     new.flags = upperbound;
  207.   else
  208.     new.flags = ((score > beta) ? lowerbound : truescore);
  209.   if (Mvboard[kingP[side]] == 0)
  210.     {
  211.       if (Mvboard[qrook[side]] == 0)
  212.     new.flags |= queencastle;
  213.       if (Mvboard[krook[side]] == 0)
  214.     new.flags |= kingcastle;
  215.     }
  216.   new.depth = (unsigned char) depth;
  217.   /* adjust *score so moves to mate is from root */
  218.   if (score > 9000)
  219.     score += ply;
  220.   else if (score < -9000)
  221.     score -= ply;
  222.  
  223.  
  224.   new.sh = (unsigned char) (score >> 8);
  225.   new.sl = (unsigned char) (score & 0xFF);
  226.  
  227.   for (i = 0; i < frehash; i++)
  228.     {
  229.       fseek (hashfile,
  230.          sizeof (struct fileentry) * ((hashix + 2 * i) % (filesz)),
  231.          SEEK_SET);
  232.       if(fread (&tmp, sizeof (struct fileentry), 1, hashfile) == 0){perror("hashfile");exit(1);}
  233.       if (tmp.depth && !Fbdcmp(tmp.bd,new.bd))continue;
  234.       if(tmp.depth == depth)break;
  235.       if (!tmp.depth || (short) tmp.depth < depth)
  236.     {
  237.       fseek (hashfile,
  238.          sizeof (struct fileentry) * ((hashix + 2 * i) % (filesz)),
  239.          SEEK_SET);
  240.       fwrite (&new, sizeof (struct fileentry), 1, hashfile);
  241. #ifndef BAREBONES
  242.           FHashAdd++;
  243. #endif
  244.       break;
  245.     }
  246.     }
  247. }
  248.  
  249. #endif /* HASHFILE */
  250. #endif /* ttblsz */
  251.  
  252. void
  253. ZeroRPT (void)
  254. {
  255. #ifdef NOMEMSET
  256.   register int side, i;
  257.   for (side = white; side <= black; side++)
  258.     for (i = 0; i < 256;)
  259.       rpthash[side][i++] = 0;
  260. #else
  261.    if(ISZERO){memset ((char *) rpthash, 0, sizeof (rpthash));ISZERO=0;}
  262. #endif
  263. }
  264.