home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / useful / game / think / uchess.lha / UChess / src / util.c < prev    next >
C/C++ Source or Header  |  1994-02-18  |  12KB  |  523 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 __aligned TTadd = 1;
  25. short int __aligned recycle;
  26. short int __aligned ISZERO = 1;
  27. extern char mvstr[4][6];
  28. #ifdef CACHE
  29. extern struct etable __far __aligned etab[2][ETABLE];
  30. #endif
  31.  
  32. int
  33. parse (FILE * fd, short unsigned int *mv, short int side, char *opening)
  34. {
  35.   register int c, i, r1, r2, c1, c2;
  36.   char s[128];
  37.   char *p;
  38.  
  39.   while ((c = getc (fd)) == ' ' || c == '\n') ;
  40.   i = 0;
  41.   s[0] = (char) c;
  42.   if (c == '!')
  43.     {
  44.       p = opening;
  45.       do
  46.     {
  47.       *p++ = c;
  48.       c = getc (fd);
  49.       if (c == '\n' || c == EOF)
  50.         {
  51.           *p = '\0';
  52.           return 0;
  53.         }
  54.       } while (true);
  55.     }
  56.   while (c != '?' && c != ' ' && c != '\t' && c != '\n' && c != EOF)
  57.     s[++i] = (char) (c = getc (fd));
  58.   s[++i] = '\0';
  59.   if (c == EOF)
  60.     return (-1);
  61.   if (s[0] == '!' || s[0] == ';' || i < 3)
  62.     {
  63.       while (c != '\n' && c != EOF)
  64.     c = getc (fd);
  65.       return (0);
  66.     }
  67.   if (s[4] == 'o')
  68.     *mv = ((side == black) ? LONGBLACKCASTLE : LONGWHITECASTLE);
  69.   else if (s[0] == 'o')
  70.     *mv = ((side == black) ? BLACKCASTLE : WHITECASTLE);
  71.   else
  72.     {
  73.       c1 = s[0] - 'a';
  74.       r1 = s[1] - '1';
  75.       c2 = s[2] - 'a';
  76.       r2 = s[3] - '1';
  77.       *mv = (locn (r1, c1) << 8) | locn (r2, c2);
  78.     }
  79.   if (c == '?')
  80.     {                /* Bad move, not for the program to play */
  81.       *mv |= 0x8000;        /* Flag it ! */
  82.       c = getc (fd);
  83.     }
  84.   return (1);
  85. }
  86.  
  87.  
  88. #if ttblsz
  89.  
  90. #define CB(i) (unsigned char) ((color[2 * (i)] ? 0x80 : 0)\
  91.            | (board[2 * (i)] << 4)\
  92.            | (color[2 * (i) + 1] ? 0x8 : 0)\
  93.            | (board[2 * (i) + 1]))
  94.  
  95. int
  96. ProbeTTable (short int side,
  97.          short int depth,
  98.          short int ply,
  99.          short int *alpha,
  100.          short int *beta,
  101.          short int *score)
  102.  
  103. /*
  104.  * Look for the current board position in the transposition table.
  105.  */
  106.  
  107. {
  108.   register struct hashentry *ptbl;
  109.   register /*unsigned*/ short i= 0;  /*to match new type of rehash --tpm*/
  110.  
  111.   ptbl = &ttable[side][hashkey % (ttblsize)];
  112.  
  113.   while (true)
  114.     {
  115.       if (ptbl->depth == 0)
  116.     return false;
  117.       if (ptbl->hashbd == hashbd)
  118.     break;
  119.       if (++i > rehash)
  120.     return false;
  121.       ptbl++;
  122.     }
  123.  
  124.   /* rehash max rehash times */
  125.   PV = SwagHt = ptbl->mv; // this is out of loop, in loop was wierd
  126.   if ((ptbl->depth >= (short) depth))
  127.     {
  128. #ifdef HASHTEST
  129.       for (i = 0; i < 32; i++)
  130.     {
  131.       if (ptbl->bd[i] != CB (i))
  132.         {
  133. #ifndef BAREBONES
  134.           HashCol++;
  135.           ShowMessage (CP[199]);    /*ttable collision detected*/
  136. #endif
  137.           break;
  138.         }
  139.     }
  140. #endif /* HASHTEST */
  141.  
  142.  
  143. //      PV = SwagHt = ptbl->mv; // was in loop in 4PL64 moved out for better perf
  144. #ifndef BAREBONES
  145.       HashCnt++;
  146. #endif
  147.       if (ptbl->flags & truescore)
  148.     {
  149.       *score = ptbl->score;
  150.       /* adjust *score so moves to mate is from root */
  151.       if (*score > 9000)
  152.         *score -= ply;
  153.       else if (*score < -9000)
  154.         *score += ply;
  155.       *beta = -20000;
  156.     }
  157.       else if (ptbl->flags & lowerbound)
  158.     {
  159.       if (ptbl->score > *alpha)
  160.         *alpha = ptbl->score - 1;
  161.     }
  162.       return (true);
  163.     }
  164.   return (false);
  165. }
  166.  
  167.  
  168.  
  169. #ifndef V4PL66
  170. int
  171. PutInTTable (short side,
  172.              short score,
  173.              short depth,
  174.              short ply,
  175.              short alpha,
  176.              short beta,
  177.              unsigned short mv)
  178.  
  179. /*
  180.  * Store the current board position in the transposition table.
  181.  */
  182.  
  183. {
  184.   register struct hashentry *ptbl;
  185.   register /*unsigned*/ short i = 0;    /*to match new type of rehash --tpm*/
  186.  
  187.   ptbl = &ttable[side][hashkey % ttblsize];
  188.   while (true)
  189.     {
  190.       if (ptbl->depth == 0 || ptbl->hashbd == hashbd)
  191.         break;
  192.       if (++i > rehash)
  193.         {
  194. #ifndef BAREBONES
  195.           THashCol++;
  196. #endif
  197.           ptbl += recycle;
  198.           break;
  199.         }
  200.       ptbl++;
  201.     }
  202.  
  203.   TTadd++;
  204. #ifndef BAREBONES
  205.   HashAdd++;
  206. #endif
  207.   ptbl->hashbd = hashbd;
  208.   ptbl->depth = (unsigned char) depth;
  209.   ptbl->mv = mv;
  210. #ifdef DEBUG
  211.   if (debuglevel & 32)
  212.     {
  213.       algbr (mv >> 8, mv & 0xff, 0);
  214.       printf ("-add-> h=%lx d=%d s=%d p=%d a=%d b=%d %s\n", hashbd, depth,
  215.                  score , ply, alpha, beta, mvstr);
  216.     }
  217. #endif
  218.   if (score > beta)
  219.     {
  220.       ptbl->flags = lowerbound;
  221.       ptbl->score = beta + 1;
  222.     }
  223.   else
  224.     {
  225.       ptbl->flags = truescore;
  226.       /* adjust score so moves to mate is from this ply */
  227.       if (score > 9000)
  228.         score += ply;
  229.       else if (score < -9000)
  230.         score -= ply;
  231.       ptbl->score = score;
  232.     }
  233.  
  234. #ifdef HASHTEST
  235.   for (i = 0; i < 32; i++)
  236.     {
  237.       ptbl->bd[i] = CB (i);
  238.     }
  239. #endif /* HASHTEST */
  240.   return true;
  241. }
  242.  
  243. #else
  244. int
  245. PutInTTable (short int side,
  246.          short int score,
  247.          short int depth,
  248.          short int ply,
  249.          short int alpha,
  250.          short int beta,
  251.          short unsigned int mv)
  252.  
  253. /*
  254.  * Store the current board position in the transposition table.
  255.  */
  256.  
  257. {
  258.   register struct hashentry *ptbl;
  259.   register /*unsigned*/ short i= 0;  /*to match new type of rehash --tpm*/
  260.  
  261.   ptbl = &ttable[side][hashkey % (ttblsize)];
  262.  
  263.   while (true)
  264.     {
  265. //      if (ptbl->depth == 0 || ptbl->hashbd == hashbd)
  266. //    break;
  267.        if (ptbl->depth == 0) break;
  268.        else if(ptbl->hashbd == hashbd  && ptbl->depth > depth && abs(score) <9000) 
  269.          return false;
  270.        else if (ptbl->hashbd == hashbd) break;
  271.       if (++i > rehash)
  272.     {
  273. #ifndef BAREBONES
  274.       THashCol++;
  275. #endif
  276.       ptbl += recycle;
  277.       break;
  278.     }
  279.       ptbl++;
  280.     }
  281.  
  282. #ifndef BAREBONES
  283.   TTadd++;
  284.   HashAdd++;
  285. #endif
  286.   /* adjust score so moves to mate is from this ply */
  287.   if (score > 9000)
  288.     score += ply;
  289.   else if (score < -9000)
  290.     score -= ply;
  291.   ptbl->hashbd = hashbd;
  292.   ptbl->depth = (unsigned char) depth;
  293.   ptbl->score = score;
  294.   ptbl->mv = mv;
  295. #ifdef DEBUG4
  296.   if (debuglevel & 32)
  297.     {
  298.       algbr (mv >> 8, mv & 0xff, 0);
  299.       printf ("-add-> h=%lx d=%d s=%d p=%d a=%d b=%d %s\n", hashbd, depth, score, ply, alpha, beta, mvstr);
  300.     }
  301. #endif
  302.   if (score > beta)
  303.     {
  304.       ptbl->flags = lowerbound;
  305.       ptbl->score = beta + 1;
  306.     }
  307.   else
  308.     ptbl->flags = truescore;
  309.  
  310. #ifdef HASHTEST
  311.   for (i = 0; i < 32; i++)
  312.     {
  313.       ptbl->bd[i] = CB (i);
  314.     }
  315. #endif /* HASHTEST */
  316.   return true;
  317. }
  318. #endif // 4pl66
  319.  
  320.    
  321. // static struct hashentry *ttagew, *ttageb;
  322.  
  323. void
  324. ZeroTTable (void)
  325. {
  326. //   register struct hashentry *w, *b;
  327.  
  328. /* I am adding these 2 memsets! */
  329.  if (!TTadd)
  330.   return;
  331. #ifndef AMIGA
  332.   memset((char *)ttable[0],0,sizeof(struct hashentry)*(ttblsize+rehash));
  333.   memset((char *)ttable[1],0,sizeof(struct hashentry)*(ttblsize+rehash));
  334. #else
  335.   ClearMem(ttable[0],sizeof(struct hashentry)*(ttblsize+rehash));
  336.   ClearMem(ttable[1],sizeof(struct hashentry)*(ttblsize+rehash));
  337. #endif
  338. //  for(w=ttable[white],b=ttable[black];w<&ttable[white][ttblsize];w++,b++)
  339. //   { w->depth = 0; b->depth = 0;}
  340. //    ttagew = ttable[white]; ttageb = ttable[black];
  341. #ifdef CACHE
  342. #ifndef AMIGA
  343.    memset ((char *) etab, 0, sizeof (etab));
  344. #else
  345.    ClearMem(etab,sizeof(etab));
  346. #endif
  347. #endif
  348. #ifdef notdef
  349.   register unsigned int a;
  350.   for (a = 0; a < ttblsize + (unsigned int)rehash; a++)
  351.     {
  352.       ttable[white][a].depth = 0;
  353.       ttable[black][a].depth = 0;
  354.     }
  355. #endif
  356.     TTadd = 0; 
  357. }
  358.  
  359. #ifdef HASHFILE
  360. int Fbdcmp(char *a,char *b)
  361. {
  362.     register int i;
  363.     for(i = 0;i<32;i++)
  364.         if(a[i] != b[i])return false;
  365.     return true;
  366. }
  367. int
  368. ProbeFTable (short int side,
  369.          short int depth,
  370.          short int ply,
  371.          short int *alpha,
  372.          short int *beta,
  373.          short int *score)
  374.  
  375. /*
  376.  * Look for the current board position in the persistent transposition table.
  377.  */
  378.  
  379. {
  380.   register short int i;
  381.   register unsigned long hashix;
  382.   struct fileentry new, t;
  383.  
  384.   hashix = ((side == white) ? (hashkey & 0xFFFFFFFE) : (hashkey | 1)) & filesz;
  385.  
  386.   for (i = 0; i < 32; i++)
  387.     new.bd[i] = CB (i);
  388.   new.flags = 0;
  389.   if (Mvboard[kingP[side]] == 0)
  390.     {
  391.       if (Mvboard[qrook[side]] == 0)
  392.     new.flags |= queencastle;
  393.       if (Mvboard[krook[side]] == 0)
  394.     new.flags |= kingcastle;
  395.     }
  396.   for (i = 0; i < frehash; i++)
  397.     {
  398.       fseek (hashfile,
  399.          sizeof (struct fileentry) * ((hashix + 2 * i) & (filesz)),
  400.          SEEK_SET);
  401.       fread (&t, sizeof (struct fileentry), 1, hashfile);
  402.       if (!t.depth) break;
  403.        if(!Fbdcmp(t.bd, new.bd)) continue;
  404.       if (((short int) t.depth >= depth) 
  405.       && (new.flags == (unsigned short)(t.flags & (kingcastle | queencastle))))
  406.     {
  407. #ifndef BAREBONES
  408.       FHashCnt++;
  409. #endif
  410.       PV = (t.f << 8) | t.t;
  411.       *score = (t.sh << 8) | t.sl;
  412.       /* adjust *score so moves to mate is from root */
  413.       if (*score > 9000)
  414.         *score -= ply;
  415.       else if (*score < -9000)
  416.         *score += ply;
  417.       if (t.flags & truescore)
  418.         {
  419.           *beta = -20000;
  420.         }
  421.       else if (t.flags & lowerbound)
  422.         {
  423.           if (*score > *alpha)
  424.         *alpha = *score - 1;
  425.         }
  426.       else if (t.flags & upperbound)
  427.         {
  428.           if (*score < *beta)
  429.         *beta = *score + 1;
  430.         }
  431.       return (true);
  432.     }
  433.     }
  434.   return (false);
  435. }
  436.  
  437. void
  438. PutInFTable (short int side,
  439.          short int score,
  440.          short int depth,
  441.          short int ply,
  442.          short int alpha,
  443.          short int beta,
  444.          short unsigned int f,
  445.          short unsigned int t)
  446.  
  447. /*
  448.  * Store the current board position in the persistent transposition table.
  449.  */
  450.  
  451. {
  452.   register unsigned short i;
  453.   register unsigned long hashix;
  454.   struct fileentry new, tmp;
  455.  
  456.   hashix = ((side == white) ? (hashkey & 0xFFFFFFFE) : (hashkey | 1)) & filesz;
  457.   for (i = 0; i < 32; i++) new.bd[i] = CB (i);
  458.   new.f = (unsigned char) f;
  459.   new.t = (unsigned char) t;
  460.   if (score < alpha)
  461.     new.flags = upperbound;
  462.   else
  463.     new.flags = ((score > beta) ? lowerbound : truescore);
  464.   if (Mvboard[kingP[side]] == 0)
  465.     {
  466.       if (Mvboard[qrook[side]] == 0)
  467.     new.flags |= queencastle;
  468.       if (Mvboard[krook[side]] == 0)
  469.     new.flags |= kingcastle;
  470.     }
  471.   new.depth = (unsigned char) depth;
  472.   /* adjust *score so moves to mate is from root */
  473.   if (score > 9000)
  474.     score += ply;
  475.   else if (score < -9000)
  476.     score -= ply;
  477.  
  478.  
  479.   new.sh = (unsigned char) (score >> 8);
  480.   new.sl = (unsigned char) (score & 0xFF);
  481.  
  482.   for (i = 0; i < frehash; i++)
  483.     {
  484.       fseek (hashfile,
  485.          sizeof (struct fileentry) * ((hashix + 2 * i) & (filesz)),
  486.          SEEK_SET);
  487.       if(fread (&tmp, sizeof (struct fileentry), 1, hashfile) == NULL){ShowMessage("hashfile");exit(1);}
  488.       if (tmp.depth && !Fbdcmp(tmp.bd,new.bd))continue;
  489.       if(tmp.depth == depth)break;
  490.       if (!tmp.depth || (short) tmp.depth < depth)
  491.     {
  492.       fseek (hashfile,
  493.          sizeof (struct fileentry) * ((hashix + 2 * i) & (filesz)),
  494.          SEEK_SET);
  495.       fwrite (&new, sizeof (struct fileentry), 1, hashfile);
  496. #ifndef BAREBONES
  497.           FHashAdd++;
  498. #endif
  499.       break;
  500.     }
  501.     }
  502. }
  503.  
  504. #endif /* HASHFILE */
  505. #endif /* ttblsz */
  506.  
  507. void
  508. ZeroRPT (void)
  509. {
  510. #ifdef NOMEMSET
  511.   register int side, i;
  512.   for (side = white; side <= black; side++)
  513.     for (i = 0; i < 256;)
  514.       rpthash[side][i++] = 0;
  515. #else
  516. #ifndef AMIGA
  517.    if(ISZERO){memset ((char *) rpthash, 0, sizeof (rpthash));ISZERO=0;}
  518. #else
  519.    if(ISZERO){ClearMem(rpthash, sizeof (rpthash));ISZERO=0;}
  520. #endif
  521. #endif
  522. }
  523.