home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / game / uchess-2.89.lha / UChess / src / util.c < prev    next >
C/C++ Source or Header  |  1994-06-29  |  12KB  |  518 lines

  1. //#define ZEROCACHE 1
  2. /*
  3.  * util.c - C source for GNU CHESS
  4.  *
  5.  * Copyright (c) 1988,1989,1990 John Stanback
  6.  * Copyright (c) 1992 Free Software Foundation
  7.  *
  8.  * This file is part of GNU CHESS.
  9.  *
  10.  * GNU Chess is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2, or (at your option)
  13.  * any later version.
  14.  *
  15.  * GNU Chess is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with GNU Chess; see the file COPYING.  If not, write to
  22.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24. #include "gnuchess.h"
  25. unsigned int __aligned TTadd = 1;
  26. short int __aligned recycle;
  27. short int __aligned ISZERO = 1;
  28. extern char mvstr[8][8];
  29. #ifdef CACHE
  30. extern struct etable __far __aligned 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.  
  88. #ifdef OLDTTABLE
  89.  
  90. #if ttblsz
  91.  
  92. #define CB(i) (unsigned char) ((color[2 * (i)] ? 0x80 : 0)\
  93.            | (board[2 * (i)] << 4)\
  94.            | (color[2 * (i) + 1] ? 0x8 : 0)\
  95.            | (board[2 * (i) + 1]))
  96.  
  97. int
  98. ProbeTTable (short int side,
  99.          short int depth,
  100.          short int ply,
  101.          short int *alpha,
  102.          short int *beta,
  103.          short int *score)
  104.  
  105. /*
  106.  * Look for the current board position in the transposition table.
  107.  */
  108.  
  109. {
  110.   register struct hashentry *ptbl;
  111.   register /*unsigned*/ short i= 0;  /*to match new type of rehash --tpm*/
  112.  
  113.   ptbl = &ttable[side][hashkey % (ttblsize)];
  114.  
  115.   while (true)
  116.     {
  117.       if (ptbl->depth == 0)
  118.     return false;
  119.       if (ptbl->hashbd == hashbd)
  120.     break;
  121.       if (++i > rehash)
  122.     return false;
  123.       ptbl++;
  124.     }
  125.  
  126.   /* rehash max rehash times */
  127.   PV = SwagHt = ptbl->mv; // this is out of loop, in loop was wierd
  128.   if ((ptbl->depth >= (short) depth))
  129.     {
  130. #ifdef HASHTEST
  131.       for (i = 0; i < 32; i++)
  132.     {
  133.       if (ptbl->bd[i] != CB (i))
  134.         {
  135. #ifndef BAREBONES
  136.           HashCol++;
  137.           ShowMessage (CP[199]);    /*ttable collision detected*/
  138. #endif
  139.           break;
  140.         }
  141.     }
  142. #endif /* HASHTEST */
  143.  
  144.  
  145. //      PV = SwagHt = ptbl->mv; // was in loop in 4PL64 moved out for better perf
  146. #ifndef BAREBONES
  147.       HashCnt++;
  148. #endif
  149.       if (ptbl->flags & truescore)
  150.     {
  151.       *score = ptbl->score;
  152.       /* adjust *score so moves to mate is from root */
  153.       if (*score > 9000)
  154.         *score -= ply;
  155.       else if (*score < -9000)
  156.         *score += ply;
  157.       *beta = -20000;
  158.     }
  159.       else if (ptbl->flags & lowerbound)
  160.     {
  161.       if (ptbl->score > *alpha)
  162.         *alpha = ptbl->score; //  - 1;  FIX by K. Sian, was ptbl->score - 1
  163.     }
  164.       return (true);
  165.     }
  166.   return (false);
  167. }
  168.  
  169.  
  170.  
  171. #ifndef V4PL66
  172. int
  173. PutInTTable (short side,
  174.              short score,
  175.              short depth,
  176.              short ply,
  177.              short alpha,
  178.              short beta,
  179.              unsigned short mv)
  180.  
  181. /*
  182.  * Store the current board position in the transposition table.
  183.  */
  184.  
  185. {
  186.   register struct hashentry *ptbl;
  187.   register /*unsigned*/ short i = 0;    /*to match new type of rehash --tpm*/
  188.  
  189.   ptbl = &ttable[side][hashkey % ttblsize];
  190.   while (true)
  191.     {
  192.       if (ptbl->depth == 0 || ptbl->hashbd == hashbd)
  193.         break;
  194.       if (++i > rehash)
  195.         {
  196. #ifndef BAREBONES
  197.           THashCol++;
  198. #endif
  199.           ptbl += recycle;
  200.           break;
  201.         }
  202.       ptbl++;
  203.     }
  204.  
  205.   TTadd++;
  206. #ifndef BAREBONES
  207.   HashAdd++;
  208. #endif
  209.   ptbl->hashbd = hashbd;
  210.   ptbl->depth = (unsigned char) depth;
  211.   ptbl->mv = mv;
  212. #ifdef DEBUG
  213.   if (debuglevel & 32)
  214.     {
  215.       algbr (mv >> 8, mv & 0xff, 0);
  216.       printf ("-add-> h=%lx d=%d s=%d p=%d a=%d b=%d %s\n", hashbd, depth,
  217.                  score , ply, alpha, beta, mvstr);
  218.     }
  219. #endif
  220.   if (score > beta)
  221.     {
  222.       ptbl->flags = lowerbound;
  223.       ptbl->score = beta + 1;
  224.     }
  225.   else
  226.     {
  227.       ptbl->flags = truescore;
  228.       /* adjust score so moves to mate is from this ply */
  229.       if (score > 9000)
  230.         score += ply;
  231.       else if (score < -9000)
  232.         score -= ply;
  233.       ptbl->score = score;
  234.     }
  235.  
  236. #ifdef HASHTEST
  237.   for (i = 0; i < 32; i++)
  238.     {
  239.       ptbl->bd[i] = CB (i);
  240.     }
  241. #endif /* HASHTEST */
  242.   return true;
  243. }
  244.  
  245. #else
  246. int
  247. PutInTTable (short int side,
  248.          short int score,
  249.          short int depth,
  250.          short int ply,
  251.          short int alpha,
  252.          short int beta,
  253.          short unsigned int mv)
  254.  
  255. /*
  256.  * Store the current board position in the transposition table.
  257.  */
  258.  
  259. {
  260.   register struct hashentry *ptbl;
  261.   register /*unsigned*/ short i= 0;  /*to match new type of rehash --tpm*/
  262.  
  263.   ptbl = &ttable[side][hashkey % (ttblsize)];
  264.  
  265.   while (true)
  266.     {
  267. //      if (ptbl->depth == 0 || ptbl->hashbd == hashbd)
  268. //    break;
  269.        if (ptbl->depth == 0) break;
  270.        else if(ptbl->hashbd == hashbd  && ptbl->depth > depth && abs(score) <9000) 
  271.          return false;
  272.        else if (ptbl->hashbd == hashbd) break;
  273.       if (++i > rehash)
  274.     {
  275. #ifndef BAREBONES
  276.       THashCol++;
  277. #endif
  278.       ptbl += recycle;
  279.       break;
  280.     }
  281.       ptbl++;
  282.     }
  283.  
  284. #ifndef BAREBONES
  285.   TTadd++;
  286.   HashAdd++;
  287. #endif
  288.   /* adjust score so moves to mate is from this ply */
  289.   if (score > 9000)
  290.     score += ply;
  291.   else if (score < -9000)
  292.     score -= ply;
  293.   ptbl->hashbd = hashbd;
  294.   ptbl->depth = (unsigned char) depth;
  295.   ptbl->score = score;
  296.   ptbl->mv = mv;
  297. #ifdef DEBUG4
  298.   if (debuglevel & 32)
  299.     {
  300.       algbr (mv >> 8, mv & 0xff, 0);
  301.       printf ("-add-> h=%lx d=%d s=%d p=%d a=%d b=%d %s\n", hashbd, depth, score, ply, alpha, beta, mvstr);
  302.     }
  303. #endif
  304.   if (score > beta)
  305.     {
  306.       ptbl->flags = lowerbound;
  307.       ptbl->score = beta + 1;
  308.     }
  309.   else
  310.     ptbl->flags = truescore;
  311.  
  312. #ifdef HASHTEST
  313.   for (i = 0; i < 32; i++)
  314.     {
  315.       ptbl->bd[i] = CB (i);
  316.     }
  317. #endif /* HASHTEST */
  318.   return true;
  319. }
  320. #endif // 4pl66
  321.  
  322.    
  323. // static struct hashentry *ttagew, *ttageb;
  324.  
  325. void
  326. ZeroTTable (void)
  327. {
  328. //   register struct hashentry *w, *b;
  329.  
  330. /* I am adding these 2 memsets! */
  331.  if (!TTadd)
  332.   return;
  333. #ifndef AMIGA
  334.   memset((char *)ttable[0],0,sizeof(struct hashentry)*(ttblsize+rehash));
  335.   memset((char *)ttable[1],0,sizeof(struct hashentry)*(ttblsize+rehash));
  336. #else
  337.   ClearMem(ttable[0],sizeof(struct hashentry)*(ttblsize+rehash));
  338.   ClearMem(ttable[1],sizeof(struct hashentry)*(ttblsize+rehash));
  339. #endif
  340. #ifdef ZEROCACHE
  341. #ifdef CACHE
  342. #ifndef AMIGA
  343.    memset ((char *) etab, 0, sizeof (etab));
  344. #else
  345.    ClearMem(etab,sizeof(etab));
  346. #endif // amiga
  347. #endif // cache
  348. #endif // zerocache
  349.     TTadd = 0; 
  350. }
  351.  
  352. #ifdef HASHFILE
  353. int Fbdcmp(char *a,char *b)
  354. {
  355.     register int i;
  356.     for(i = 0;i<32;i++)
  357.         if(a[i] != b[i])return false;
  358.     return true;
  359. }
  360. int
  361. ProbeFTable (short int side,
  362.          short int depth,
  363.          short int ply,
  364.          short int *alpha,
  365.          short int *beta,
  366.          short int *score)
  367.  
  368. /*
  369.  * Look for the current board position in the persistent transposition table.
  370.  */
  371.  
  372. {
  373.   register short int i;
  374.   register unsigned long hashix;
  375.   struct fileentry new, t;
  376.  
  377.   hashix = ((side == white) ? (hashkey & 0xFFFFFF