home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 5 / cd.iso / wingames / chess / search.c < prev    next >
Text File  |  1990-09-02  |  44KB  |  1,651 lines

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-09-30
  5.  
  6.   Modified by Daryl Baker for use in MS WINDOWS environment
  7.  
  8.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  9.   Copyright (c) 1988, 1989, 1990  John Stanback
  10.  
  11.   This file is part of CHESS.
  12.  
  13.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  14.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  15.   the consequences of using it or for whether it serves any particular
  16.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  17.   General Public License for full details.
  18.  
  19.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  20.   only under the conditions described in the CHESS General Public License.
  21.   A copy of this license is supposed to have been given to you along with
  22.   CHESS so you can know your rights and responsibilities.  It should be in a
  23.   file named COPYING.  Among other things, the copyright notice and this
  24.   notice must be preserved on all copies.
  25. */
  26.  
  27. #define NOATOM 
  28. #define NOCLIPBOARD
  29. #define NOCREATESTRUCT
  30. #define NOFONT
  31. #define NOREGION
  32. #define NOSOUND
  33. #define NOWH
  34. #define NOWINOFFSETS
  35. #define NOCOMM
  36. #define NOKANJI
  37.  
  38. #include <windows.h>
  39. #include <string.h>
  40. #include <time.h>
  41. #include <stdlib.h>
  42. #include <malloc.h>
  43. #include <stdio.h>
  44.  
  45. #include "gnuchess.h"
  46. #include "defs.h"
  47.  
  48. #if ttblsz
  49. extern unsigned long hashkey, hashbd;
  50. /*extern struct hashval hashcode[2][7][64];*/
  51. /*extern struct hashentry huge ttable[2][ttblsz];*/
  52. extern struct hashval far *hashcode;
  53. extern struct hashentry far *ttable;
  54. #endif /* ttblsz */
  55.  
  56. /*extern unsigned char history[8192];*/
  57. extern unsigned char far * history;
  58.  
  59. extern short rpthash[2][256];
  60. /*extern unsigned char nextpos[8][64][64];*/
  61. /*extern unsigned char nextdir[8][64][64];*/
  62. extern unsigned char far *nextpos;
  63. extern unsigned char far *nextdir;
  64.  
  65. extern short FROMsquare, TOsquare, Zscore, zwndw;
  66. extern unsigned short PV, Swag0, Swag1, Swag2, Swag3, Swag4;
  67. extern unsigned short killr0[maxdepth], killr1[maxdepth];
  68. extern unsigned short killr2[maxdepth], killr3[maxdepth];
  69. extern short Pscore[maxdepth], Tscore[maxdepth];
  70. extern unsigned long hashkey, hashbd;
  71. extern short ChkFlag[maxdepth], CptrFlag[maxdepth], PawnThreat[maxdepth];
  72. extern short mtl[2], pmtl[2], emtl[2], hung[2];
  73. extern short player, xwndw, rehash;
  74. extern short PieceCnt[2];
  75. extern long NodeCnt, ETnodes, EvalNodes, HashCnt, FHashCnt, HashCol;
  76. extern short HasKnight[2], HasBishop[2], HasRook[2], HasQueen[2];
  77. extern short Pindex[64];
  78.  
  79. static short _based(_segname("_CODE")) rank7[3] = {6, 1, 0};
  80. static short _based(_segname("_CODE")) kingP[3] = {4, 60, 0};
  81. static short _based(_segname("_CODE")) value[7] =
  82. {0, valueP, valueN, valueB, valueR, valueQ, valueK};
  83.  
  84. static short _based(_segname("_CODE")) sweep[8] =
  85. {false, false, false, true, true, true, false, false};
  86.  
  87. static short _based(_segname("_CODE")) ptype[2][8] = {
  88.   no_piece, pawn, knight, bishop, rook, queen, king, no_piece,
  89.   no_piece, bpawn, knight, bishop, rook, queen, king, no_piece};
  90.  
  91. static short _based(_segname("_CODE")) control[7] =
  92. {0, ctlP, ctlN, ctlB, ctlR, ctlQ, ctlK};
  93.  
  94. /* ............    MOVE GENERATION & SEARCH ROUTINES    .............. */
  95.  
  96. void
  97. pick (short int p1, short int p2)
  98.  
  99. /*
  100.   Find the best move in the tree between indexes p1 and p2. Swap the best
  101.   move into the p1 element.
  102. */
  103.  
  104. {
  105.   register short p, s;
  106.   short p0, s0;
  107.   struct leaf temp;
  108.  
  109.   s0 = Tree[p1].score;
  110.   p0 = p1;
  111.   for (p = p1 + 1; p <= p2; p++)
  112.     if ((s = Tree[p].score) > s0)
  113.       {
  114.         s0 = s;
  115.         p0 = p;
  116.       }
  117.   if (p0 != p1)
  118.     {
  119.       temp = Tree[p1];
  120.       Tree[p1] = Tree[p0];
  121.       Tree[p0] = temp;
  122.     }
  123. }
  124.  
  125. void
  126. SelectMove (HWND hWnd, short int side, short int iop)
  127.  
  128. /*
  129.   Select a move by calling function search() at progressively deeper ply
  130.   until time is up or a mate or draw is reached. An alpha-beta window of -90
  131.   to +90 points is set around the score returned from the previous
  132.   iteration. If Sdepth != 0 then the program has correctly predicted the
  133.   opponents move and the search will start at a depth of Sdepth+1 rather
  134.   than a depth of 1.
  135. */
  136.  
  137. {
  138.   static short i, tempb, tempc, tempsf, tempst, xside, rpt;
  139.   static short alpha, beta, score;
  140.  
  141.   flag.timeout = false;
  142.   xside = otherside[side];
  143.   if (iop != 2)
  144.     player = side;
  145.   if (TCflag)
  146.     {
  147.       if ((TimeControl.moves[side] + 3) != 0)
  148.         ResponseTime = (TimeControl.clock[side]) /
  149.           (TimeControl.moves[side] + 3) -
  150.           OperatorTime;
  151.       else
  152.         ResponseTime = 0;
  153.       ResponseTime += (ResponseTime * TimeControl.moves[side]) / (2 * TCmoves + 1);
  154.     }
  155.   else
  156.     ResponseTime = Level;
  157.   if (iop == 2)
  158.     ResponseTime = 99999;
  159.   if (Sdepth > 0 && root->score > Zscore - zwndw)
  160.     ResponseTime -= ft;
  161.   else if (ResponseTime < 1)
  162.     ResponseTime = 1;
  163.   ExtraTime = 0;
  164.   ExaminePosition ();
  165.   ScorePosition (side, &score);
  166.   /* Pscore[0] = -score; */
  167.   ShowSidetoMove ();
  168.  
  169.   if (Sdepth == 0)
  170.     {
  171. #if ttblsz
  172.       /* ZeroTTable (); */
  173. #endif /* ttblsz */
  174.       SearchStartStuff (side);
  175. #ifdef NOMEMSET
  176.       for (i = 0; i < 8192; i++)
  177. /*        history[i] = 0; */
  178.           *(history+i) = 0;
  179.  
  180. #else
  181.       _fmemset ( history, 0, 8192 * sizeof (char));
  182. #endif /* NOMEMSET */
  183.       FROMsquare = TOsquare = -1;
  184.       PV = 0;
  185.       if (iop != 2)
  186.         hint = 0;
  187.       for (i = 0; i < maxdepth; i++)
  188.         PrVar[i] = killr0[i] = killr1[i] = killr2[i] = killr3[i] = 0;
  189.       alpha = score - 90;
  190.       beta = score + 90;
  191.       rpt = 0;
  192.       TrPnt[1] = 0;
  193.       root = &Tree[0];
  194.       MoveList (side, 1);
  195.       for (i = TrPnt[1]; i < TrPnt[2]; i++)
  196.         pick (i, TrPnt[2] - 1);
  197.       if (Book != NULL)
  198.         OpeningBook (&hint);
  199.       if (Book != NULL)
  200.         flag.timeout = true;
  201.       NodeCnt = ETnodes = EvalNodes = HashCnt = FHashCnt = HashCol = 0;
  202.       Zscore = 0;
  203.       zwndw = 20;
  204.     }
  205.   while (!flag.timeout && Sdepth < MaxSearchDepth)
  206.     {
  207.       Sdepth++;
  208.       ShowDepth (' ');
  209.       score = search (side, 1, Sdepth, alpha, beta, PrVar, &rpt);
  210.       for (i = 1; i <= Sdepth; i++)
  211.         killr0[i] = PrVar[i];
  212.       if (score < alpha)
  213.         {
  214.           ShowDepth ('-');
  215.           ExtraTime = 10 * ResponseTime;
  216.           /* ZeroTTable (); */
  217.           score = search (side, 1, Sdepth, -9000, score, PrVar, &rpt);
  218.         }
  219.       if (score > beta && !(root->flags & exact))
  220.         {
  221.           ShowDepth ('+');
  222.           ExtraTime = 0;
  223.           /* ZeroTTable (); */
  224.           score = search (side, 1, Sdepth, score, 9000, PrVar, &rpt);
  225.         }
  226.       score = root->score;
  227.       if (!flag.timeout)
  228.         for (i = TrPnt[1] + 1; i < TrPnt[2]; i++)
  229.           pick (i, TrPnt[2] - 1);
  230.       ShowResults (score, PrVar, '.');
  231.       for (i = 1; i <= Sdepth; i++)
  232.         killr0[i] = PrVar[i];
  233.       if (score > Zscore - zwndw && score > Tree[1].score + 250)
  234.         ExtraTime = 0;
  235.       else if (score > Zscore - 3 * zwndw)
  236.         ExtraTime = ResponseTime;
  237.       else
  238.         ExtraTime = 3 * ResponseTime;
  239.       if (root->flags & exact)
  240.         flag.timeout = true;
  241.       if (Tree[1].score < -9000)
  242.         flag.timeout = true;
  243.       if (4 * et > 2 * ResponseTime + ExtraTime)
  244.         flag.timeout = true;
  245.       if (!flag.timeout)
  246.         {
  247.           Tscore[0] = score;
  248.           if (Zscore == 0)
  249.             Zscore = score;
  250.           else
  251.             Zscore = (Zscore + score) / 2;
  252.         }
  253.       zwndw = 20 + abs (Zscore / 12);
  254.       beta = score + Bwindow;
  255.       if (Zscore < score)
  256.         alpha = Zscore - Awindow - zwndw;
  257.       else
  258.         alpha = score - Awindow - zwndw;
  259.     }
  260.  
  261.   score = root->score;
  262.   if (rpt >= 2 || score < -12000)
  263.     root->flags |= draw;
  264.   if (iop == 2)
  265.     return;
  266.   if (Book == NULL)
  267.     hint = PrVar[2];
  268.   ElapsedTime (1);
  269.  
  270.   if (score > -9999 && rpt <= 2)
  271.     {
  272.       MakeMove (side, root, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  273.       algbr (root->f, root->t, (short) root->flags);
  274.     }
  275.   else
  276.     algbr (0, 0, 0);
  277.   OutputMove (hWnd);
  278.   if (score == -9999 || score == 9998)
  279.     flag.mate = true;
  280.   if (flag.mate)
  281.     hint = 0;
  282.   if ((board[root->t] == pawn)
  283.       || (root->flags & capture)
  284.       || (root->flags & cstlmask))
  285.     {
  286.       Game50 = GameCnt;
  287.       ZeroRPT ();
  288.     }
  289.   GameList[GameCnt].score = score;
  290.   GameList[GameCnt].nodes = NodeCnt;
  291.   GameList[GameCnt].time = (short) et;
  292.   GameList[GameCnt].depth = Sdepth;
  293.   if (TCflag)
  294.     {
  295.       TimeControl.clock[side] -= (et + OperatorTime);
  296.       if (--TimeControl.moves[side] == 0)
  297.         SetTimeControl ();
  298.     }
  299.   if ((root->flags & draw) && flag.bothsides)
  300.     flag.mate = true;
  301.   if (GameCnt > 470)
  302.     flag.mate = true; /* out of move store, you loose */
  303.   player = xside;
  304.   Sdepth = 0;
  305. }
  306.  
  307. int
  308. parse (FILE *fd, short unsigned int *mv, short int side)
  309. {
  310.   int c, i, r1, r2, c1, c2;
  311.   char s[100];
  312.   while ((c = getc (fd)) == ' ') ;
  313.   i = 0;
  314.   s[0] = (char) c;
  315.   while (c != ' ' && c != '\n' && c != EOF)
  316.     s[++i] = (char) (c = getc (fd));
  317.   s[++i] = '\0';
  318.   if (c == EOF)
  319.     return (-1);
  320.   if (s[0] == '!' || s[0] == ';' || i < 3)
  321.     {
  322.       while (c != '\n' && c != EOF)
  323.         c = getc (fd);
  324.       return (0);
  325.     }
  326.   if (s[4] == 'o')
  327.     if (side == black)
  328.       *mv = 0x3C3A;
  329.     else
  330.       *mv = 0x0402;
  331.   else if (s[0] == 'o')
  332.     if (side == black)
  333.       *mv = 0x3C3E;
  334.     else
  335.       *mv = 0x0406;
  336.   else
  337.     {
  338.       c1 = s[0] - 'a';
  339.       r1 = s[1] - '1';
  340.       c2 = s[2] - 'a';
  341.       r2 = s[3] - '1';
  342.       *mv = (locn (r1, c1) << 8) | locn (r2, c2);
  343.     }
  344.   return (1);
  345. }
  346.  
  347. inline void
  348. repetition (short int *cnt)
  349.  
  350. /*
  351.   Check for draw by threefold repetition.
  352. */
  353.  
  354. {
  355.   short i, c, f, t;
  356.   short b[64];
  357.   unsigned short m;
  358.  
  359.   *cnt = c = 0;
  360.   if (GameCnt > Game50 + 3)
  361.     {
  362. #ifdef NOMEMSET
  363.       for (i = 0; i < 64; b[i++] = 0) ;
  364. #else
  365.       memset ((char *) b, 0, sizeof (b));
  366. #endif /* NOMEMSET */
  367.       for (i = GameCnt; i > Game50; i--)
  368.         {
  369.           m = GameList[i].gmove;
  370.           f = m >> 8;
  371.           t = m & 0xFF;
  372.           if (++b[f] == 0)
  373.             c--;
  374.           else
  375.             c++;
  376.           if (--b[t] == 0)
  377.             c--;
  378.           else
  379.             c++;
  380.           if (c == 0)
  381.             (*cnt)++;
  382.         }
  383.     }
  384. }
  385.  
  386. int
  387. search (short int side,
  388.         short int ply,
  389.         short int depth,
  390.         short int alpha,
  391.         short int beta,
  392.         short unsigned int *bstline,
  393.         short int *rpt)
  394.  
  395. /*
  396.   Perform an alpha-beta search to determine the score for the current board
  397.   position. If depth <= 0 only capturing moves, pawn promotions and
  398.   responses to check are generated and searched, otherwise all moves are
  399.   processed. The search depth is modified for check evasions, certain
  400.   re-captures and threats. Extensions may continue for up to 11 ply beyond
  401.   the nominal search depth.
  402. */
  403.  
  404. #define UpdateSearchStatus \
  405. {\
  406.    if (flag.post) ShowCurrentMove(pnt,node->f,node->t);\
  407.      if (pnt > TrPnt[1])\
  408.        {\
  409.           d = best-Zscore; e = best-node->score;\
  410.             if (best < alpha) ExtraTime = 10*ResponseTime;\
  411.             else if (d > -zwndw && e > 4*zwndw) ExtraTime = -ResponseTime/3;\
  412.             else if (d > -zwndw) ExtraTime = 0;\
  413.             else if (d > -3*zwndw) ExtraTime = ResponseTime;\
  414.             else if (d > -9*zwndw) ExtraTime = 3*ResponseTime;\
  415.             else ExtraTime = 5*ResponseTime;\
  416.             }\
  417.             }
  418. #define prune (cf && score+node->score < alpha)
  419. #define ReCapture (flag.rcptr && score > alpha && score < beta &&\
  420.                    ply > 2 && CptrFlag[ply-1] && CptrFlag[ply-2])
  421. /* && depth == Sdepth-ply+1 */
  422. #define Parry (hung[side] > 1 && ply == Sdepth+1)
  423. #define MateThreat (ply < Sdepth+4 && ply > 4 &&\
  424.                     ChkFlag[ply-2] && ChkFlag[ply-4] &&\
  425.                     ChkFlag[ply-2] != ChkFlag[ply-4])
  426.  
  427. {
  428.   register short j, pnt;
  429.   short best, tempb, tempc, tempsf, tempst;
  430.   short xside, pbst, d, e, cf, score, rcnt, slk, InChk;
  431.   unsigned short mv, nxtline[maxdepth];
  432.   struct leaf far *node, tmp;
  433.  
  434.   NodeCnt++;
  435.   xside = otherside[side];
  436.  
  437.   if ((ply <= Sdepth + 3) && rpthash[side][hashkey & 0xFF] > 0)
  438.     repetition (rpt);
  439.   else
  440.     *rpt = 0;
  441.   /* Detect repetitions a bit earlier. SMC. 12/89 */
  442.   if (*rpt == 1 && ply > 1)
  443.     return (0);
  444.   /* if (*rpt >= 2) return(0); */
  445.  
  446.   score = evaluate (side, ply, alpha, beta, INCscore, &slk, &InChk);
  447.   if (score > 9000)
  448.     {
  449.       bstline[ply] = 0;
  450.       return (score);
  451.     }
  452.   if (depth > 0)
  453.     {
  454.       /* Allow opponent a chance to check again */
  455.       if (InChk)
  456.         depth = (depth < 2) ? 2 : depth;
  457.       else if (PawnThreat[ply - 1] || ReCapture)
  458.         ++depth;
  459.     }
  460.   else
  461.     {
  462.       if (score >= alpha &&
  463.           (InChk || PawnThreat[ply - 1] || Parry))
  464.         depth = 1;
  465.       else if (score <= beta && MateThreat)
  466.         depth = 1;
  467.     }
  468.  
  469. #if ttblsz
  470.   if (depth > 0 && flag.hash && ply > 1)
  471.     {
  472.       if (ProbeTTable (side, depth, &alpha, &beta, &score) == false)
  473. #ifdef HASHFILE 
  474.         if (hashfile && (depth > 5) && (GameCnt < 12))
  475.           ProbeFTable (side, depth, &alpha, &beta, &score);
  476. #else
  477.       /* do nothing */;
  478. #endif /* HASHFILE */      
  479.       bstline[ply] = PV;
  480.       bstline[ply + 1] = 0;
  481.       if (beta == -20000)
  482.         return (score);
  483.       if (alpha > beta)
  484.         return (alpha);
  485.     }
  486. #endif /* ttblsz */
  487.   if (Sdepth == 1)
  488.     d = 7;
  489.   else
  490.     d = 11;
  491.   if (ply > Sdepth + d || (depth < 1 && score > beta))
  492.     /* score >= beta ?? */
  493.     return (score);
  494.  
  495.   if (ply > 1)
  496.     if (depth > 0)
  497.       MoveList (side, ply);
  498.     else
  499.       CaptureList (side, ply);
  500.  
  501.   if (TrPnt[ply] == TrPnt[ply + 1])
  502.     return (score);
  503.  
  504.   cf = (depth < 1 && ply > Sdepth + 1 && !ChkFlag[ply - 2] && !slk);
  505.  
  506.   if (depth > 0)
  507.     best = -12000;
  508.   else
  509.     best = score;
  510.   if (best > alpha)
  511.     alpha = best;
  512.  
  513.   for (pnt = pbst = TrPnt[ply];
  514.        pnt < TrPnt[ply + 1] && best <= beta;    /* best < beta ?? */
  515.        pnt++)
  516.     {
  517.  
  518.       {
  519.        MSG msg;
  520.        if ( !flag.timeout && PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)){
  521.              TranslateMessage(&msg);       
  522.              DispatchMessage(&msg);
  523.        }
  524.       }
  525.  
  526.       if (ply > 1)
  527.         pick (pnt, TrPnt[ply + 1] - 1);
  528.       node = &Tree[pnt];
  529.       mv = (node->f << 8) | node->t;
  530.       nxtline[ply + 1] = 0;
  531.  
  532.       if (prune)
  533.         break;
  534.       if (ply == 1)
  535.         UpdateSearchStatus;
  536.  
  537.       if (!(node->flags & exact))
  538.         {
  539.           MakeMove (side, node, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  540.           CptrFlag[ply] = (node->flags & capture);
  541.           PawnThreat[ply] = (node->flags & pwnthrt);
  542.           Tscore[ply] = node->score;
  543.           PV = node->reply;
  544.           node->score = -search (xside, ply + 1,
  545.                                  (depth > 0) ? depth - 1 : 0,
  546.                                  -beta, -alpha,
  547.                                  nxtline, &rcnt);
  548.           if (abs (node->score) > 9000)
  549.             node->flags |= exact;
  550.           else if (rcnt == 1)
  551.             node->score /= 2;
  552.           if (rcnt >= 2 || GameCnt - Game50 > 99 ||
  553.               (node->score == 9999 - ply && !ChkFlag[ply]))
  554.             {
  555.               node->flags |= draw;
  556.               node->flags |= exact;
  557.               if (side == computer)
  558.                 node->score = contempt;
  559.               else
  560.                 node->score = -contempt;
  561.             }
  562.           node->reply = nxtline[ply + 1];
  563.           UnmakeMove (side, node, &tempb, &tempc, &tempsf, &tempst);
  564.         }
  565.       if (node->score > best && !flag.timeout)
  566.         {
  567.           if (depth > 0)
  568.             if (node->score > alpha && !(node->flags & exact))
  569.               node->score += depth;
  570.           best = node->score;
  571.           pbst = pnt;
  572.           if (best > alpha)
  573.             alpha = best;
  574.           for (j = ply + 1; nxtline[j] > 0; j++)
  575.             bstline[j] = nxtline[j];
  576.           bstline[j] = 0;
  577.           bstline[ply] = mv;
  578.           if (ply == 1)
  579.             {
  580.               if (best > root->score)
  581.                 {
  582.                   tmp = Tree[pnt];
  583.                   for (j = pnt - 1; j >= 0; j--)
  584.                     Tree[j + 1] = Tree[j];
  585.                   Tree[0] = tmp;
  586.                   pbst = 0;
  587.                 }
  588.               if (Sdepth > 2)
  589.                 if (best > beta)
  590.                   ShowResults (best, bstline, '+');
  591.                 else if (best < alpha)
  592.                   ShowResults (best, bstline, '-');
  593.                 else
  594.                   ShowResults (best, bstline, '&');
  595.             }
  596.         }
  597.       if (NodeCnt > ETnodes)
  598.         ElapsedTime (0);
  599.       if (flag.timeout)
  600.         return (-Tscore[ply - 1]);
  601.     }
  602.  
  603.   node = &Tree[pbst];
  604.   mv = (node->f << 8) | node->t;
  605. #if ttblsz
  606.   if (flag.hash && ply <= Sdepth && *rpt == 0 && best == alpha)
  607.     {
  608.       PutInTTable (side, best, depth, alpha, beta, mv);
  609. #ifdef HASHFILE      
  610.       if (hashfile && (depth > 5) && (GameCnt < 12))
  611.         PutInFTable (side, best, depth, alpha, beta, node->f, node->t);
  612. #endif /* HASHFILE */      
  613.     }
  614. #endif /* ttblsz */
  615.   if (depth > 0)
  616.     {
  617.       j = (node->f << 6) | node->t;
  618.       if (side == black)
  619.         j |= 0x1000;
  620. /*      if (history[j] < 150)
  621.           history[j] += (unsigned char) 2 * depth; */
  622.       if (*(history+j) < 150)
  623.         *(history+j) += (unsigned char) 2 * depth;
  624.       if (node->t != (GameList[GameCnt].gmove & 0xFF))
  625.         if (best <= beta)
  626.           killr3[ply] = mv;
  627.         else if (mv != killr1[ply])
  628.           {
  629.             killr2[ply] = killr1[ply];
  630.             killr1[ply] = mv;
  631.           }
  632.       if (best > 9000)
  633.         killr0[ply] = mv;
  634.       else
  635.         killr0[ply] = 0;
  636.     }
  637.   return (best);
  638. }
  639.  
  640. #if ttblsz
  641. /*
  642.   hashbd contains a 32 bit "signature" of the board position. hashkey
  643.   contains a 16 bit code used to address the hash table. When a move is
  644.   made, XOR'ing the hashcode of moved piece on the from and to squares with
  645.   the hashbd and hashkey values keeps things current.
  646. */
  647. #define UpdateHashbd(side, piece, f, t) \
  648. {\
  649.   if ((f) >= 0)\
  650.     {\
  651.       hashbd ^= (hashcode+(side)*7*64+(piece)*64+f)->bd;\
  652.       hashkey ^= (hashcode+(side)*7*64+(piece)*64+f)->key;\
  653.     }\
  654.   if ((t) >= 0)\
  655.     {\
  656.       hashbd ^= (hashcode+(side)*7*64+(piece)*64+t)->bd;\
  657.       hashkey ^= (hashcode+(side)*7*64+(piece)*64+t)->key;\
  658.     }\
  659. }
  660.  
  661. #define CB(i) (unsigned char) ((color[2 * (i)] ? 0x80 : 0)\
  662.                | (board[2 * (i)] << 4)\
  663.                | (color[2 * (i) + 1] ? 0x8 : 0)\
  664.                | (board[2 * (i) + 1]))
  665.  
  666. int
  667. ProbeTTable (short int side,
  668.              short int depth,
  669.              short int *alpha,
  670.              short int *beta,
  671.              short int *score)
  672.  
  673. /*
  674.   Look for the current board position in the transposition table.
  675. */
  676.  
  677. {
  678.   struct hashentry far *ptbl;
  679.   register unsigned short i;
  680.  
  681. /*  ptbl = &ttable[side][hashkey & (ttblsz - 1)]; */
  682.    ptbl = ttable+side*2+(hashkey & (ttblsz - 1));
  683.  
  684.   /* rehash max rehash times */
  685.   for (i = 1; ptbl->hashbd != hashbd && i <= rehash; i++)
  686. /*    ptbl = &ttable[side][(hashkey + i) & (ttblsz - 1)]; */
  687.     ptbl = ttable+side*2+((hashkey + i) & (ttblsz - 1));
  688.   if ((short) ptbl->depth >= depth && ptbl->hashbd == hashbd)
  689.     {
  690.       HashCnt++;
  691. #ifdef HASHTEST
  692.       for (i = 0; i < 32; i++)
  693.         {
  694.           if (ptbl->bd[i] != CB(i))
  695.             {
  696.               HashCol++;
  697.               ShowMessage("ttable collision detected");
  698.               break;
  699.             }
  700.         }
  701. #endif /* HASHTEST */
  702.  
  703.       PV = ptbl->mv;
  704.       if (ptbl->flags & truescore)
  705.         {
  706.           *score = ptbl->score;
  707.           *beta = -20000;
  708.         }
  709. #if 0 /* commented out, why? */
  710.       else if (ptbl->flags & upperbound)
  711.         {
  712.           if (ptbl->score < *beta) *beta = ptbl->score+1;
  713.         }
  714. #endif
  715.       else if (ptbl->flags & lowerbound)
  716.         {
  717.           if (ptbl->score > *alpha)
  718.             *alpha = ptbl->score - 1;
  719.         }
  720.       return(true);
  721.     }
  722.   return(false);
  723. }
  724.  
  725. void
  726. PutInTTable (short int side,
  727.              short int score,
  728.              short int depth,
  729.              short int alpha,
  730.              short int beta,
  731.              short unsigned int mv)
  732.  
  733. /*
  734.   Store the current board position in the transposition table.
  735. */
  736.  
  737. {
  738.   struct hashentry far *ptbl;
  739.   register unsigned short i;
  740.  
  741. /*  ptbl = &ttable[side][hashkey & (ttblsz - 1)]; */
  742.   ptbl = ttable+side*2+(hashkey & (ttblsz - 1));
  743.  
  744.   /* rehash max rehash times */
  745.   for (i = 1; depth < ptbl->depth && ptbl->hashbd != hashbd && i <= rehash; i++)
  746. /*    ptbl = &ttable[side][(hashkey + i) & (ttblsz - 1)]; */
  747.     ptbl = ttable+side*2+((hashkey + i) & (ttblsz - 1));
  748.   if (depth >= ptbl->depth || ptbl->hashbd != hashbd)
  749.     {
  750.       ptbl->hashbd = hashbd;
  751.       ptbl->depth = (unsigned char) depth;
  752.       ptbl->score = score;
  753.       ptbl->mv = mv;
  754.       ptbl->flags = 0;
  755.       if (score < alpha)
  756.         ptbl->flags |= upperbound;
  757.       else if (score > beta)
  758.         ptbl->flags |= lowerbound;
  759.       else
  760.         ptbl->flags |= truescore;
  761. #ifdef HASHTEST
  762.       for (i = 0; i < 32; i++)
  763.         {
  764.           ptbl->bd[i] = CB(i);
  765.         }
  766. #endif /* HASHTEST */
  767.     }
  768. }
  769.  
  770. void
  771. ZeroTTable (void)
  772. {
  773.   register int side, i;
  774.  
  775.   if (flag.hash)
  776.     for (side = white; side <= black; side++)
  777.       for (i = 0; i < ttblsz; i++)
  778. /*        ttable[side][i].depth = 0; */
  779.         (ttable+side*2+i)->depth = 0;
  780. }
  781.  
  782. #ifdef HASHFILE
  783. int
  784. ProbeFTable(short int side,
  785.             short int depth,
  786.             short int *alpha,
  787.             short int *beta,
  788.             short int *score)
  789.  
  790. /*
  791.   Look for the current board position in the persistent transposition table.
  792. */
  793.  
  794. {
  795.   register unsigned short i, j;
  796.   unsigned long hashix;
  797.   short s;
  798.   struct fileentry new, t;
  799.  
  800.   if (side == white)
  801.     hashix = hashkey & 0xFFFFFFFE & (filesz - 1);
  802.   else
  803.     hashix = hashkey | 1 & (filesz - 1);
  804.  
  805.   for (i = 0; i < 32; i++)
  806.     new.bd[i] = CB(i);
  807.   new.flags = 0;
  808.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[qrook[side]] == 0))
  809.     new.flags |= queencastle;
  810.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[krook[side]] == 0))
  811.     new.flags |= kingcastle;
  812.  
  813.   for (i = 0; i < frehash; i++)
  814.     {
  815.       fseek(hashfile,
  816.             sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  817.             SEEK_SET);
  818.       fread(&t, sizeof(struct fileentry), 1, hashfile);
  819.       for (j = 0; j < 32; j++)
  820.         if (t.bd[j] != new.bd[j])
  821.           break;
  822.       if (((short) t.depth >= depth) && (j >= 32)
  823.           && (new.flags == (t.flags & (kingcastle | queencastle))))
  824.         {
  825.           FHashCnt++;
  826.           PV = (t.f << 8) | t.t;
  827.           s = (t.sh << 8) | t.sl;
  828.           if (t.flags & truescore)
  829.             {
  830.               *score = s;
  831.               *beta = -20000;
  832.             }
  833.           else if (t.flags & lowerbound)
  834.             {
  835.               if (s > *alpha)
  836.                 *alpha = s - 1;
  837.             }
  838.           return(true);
  839.         }
  840.     }
  841.   return(false);
  842. }
  843.  
  844. void
  845. PutInFTable (short int side,
  846.              short int score,
  847.              short int depth,
  848.              short int alpha,
  849.              short int beta,
  850.              short unsigned int f,
  851.              short unsigned int t)
  852.  
  853. /*
  854.   Store the current board position in the persistent transposition table.
  855. */
  856.  
  857. {
  858.   register unsigned short i;
  859.   unsigned long hashix;
  860.   struct fileentry new, tmp;
  861.  
  862.   if (side == white)
  863.     hashix = hashkey & 0xFFFFFFFE & (filesz - 1);
  864.   else
  865.     hashix = hashkey | 1 & (filesz - 1);
  866.  
  867.   for (i = 0; i < 32; i++)
  868.     new.bd[i] = CB(i);
  869.   new.f = (unsigned char) f;
  870.   new.t = (unsigned char) t;
  871.   new.flags = 0;
  872.   if (score < alpha)
  873.     new.flags |= upperbound;
  874.   else if (score > beta)
  875.     new.flags |= lowerbound;
  876.   else
  877.     new.flags |= truescore;
  878.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[qrook[side]] == 0))
  879.     new.flags |= queencastle;
  880.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[krook[side]] == 0))
  881.     new.flags |= kingcastle;
  882.   new.depth = (unsigned char) depth;
  883.   new.sh = (unsigned char) (score >> 8);
  884.   new.sl = (unsigned char) (score & 0xFF);
  885.  
  886.   for (i = 0; i < frehash; i++)
  887.     {
  888.       fseek(hashfile,
  889.             sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  890.             SEEK_SET);
  891.       fread(&tmp, sizeof(struct fileentry), 1, hashfile);
  892.       if ((short) tmp.depth <= depth)
  893.         {
  894.           fseek(hashfile,
  895.                 sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  896.                 SEEK_SET);
  897.           fwrite (&new, sizeof(struct fileentry), 1, hashfile);
  898.           break;
  899.         }
  900.     }
  901. }
  902. #endif /* HASHFILE */
  903. #endif /* ttblsz */
  904.  
  905. void
  906. ZeroRPT (void)
  907. {
  908.   register int side, i;
  909.  
  910.   for (side = white; side <= black; side++)
  911.     for (i = 0; i < 256; i++)
  912.       rpthash[side][i] = 0;
  913. }
  914.  
  915. #define Link(from,to,flag,s) \
  916. {\
  917.    node->f = from; node->t = to;\
  918.      node->reply = 0;\
  919.        node->flags = flag;\
  920.          node->score = s;\
  921.            ++node;\
  922.              ++TrPnt[ply+1];\
  923.              }
  924.  
  925. static inline void
  926. LinkMove (short int ply,
  927.           short int f,
  928.           short int t,
  929.           short int flag,
  930.           short int xside)
  931.  
  932. /*
  933.   Add a move to the tree.  Assign a bonus to order the moves
  934.   as follows:
  935.   1. Principle variation
  936.   2. Capture of last moved piece
  937.   3. Other captures (major pieces first)
  938.   4. Killer moves
  939.   5. "history" killers
  940. */
  941.  
  942. {
  943.   register short s, z;
  944.   unsigned short mv;
  945.   struct leaf far *node;
  946.  
  947.   node = &Tree[TrPnt[ply + 1]];
  948.   mv = (f << 8) | t;
  949.   s = 0;
  950.   if (mv == Swag0)
  951.     s = 2000;
  952.   else if (mv == Swag1)
  953.     s = 60;
  954.   else if (mv == Swag2)
  955.     s = 50;
  956.   else if (mv == Swag3)
  957.     s = 40;
  958.   else if (mv == Swag4)
  959.     s = 30;
  960.   z = (f << 6) | t;
  961.   if (xside == white)
  962.     z |= 0x1000;
  963. /*  s += history[z]; */
  964.   s += *(history+z);
  965.   if (color[t] != neutral)
  966.     {
  967.       if (t == TOsquare)
  968.         s += 500;
  969.       s += value[board[t]] - board[f];
  970.     }
  971.   if (board[f] == pawn)
  972.     if (row (t) == 0 || row (t) == 7)
  973.       {
  974.         flag |= promote;
  975.         s += 800;
  976.         Link (f, t, flag | queen, s - 20000);
  977.         s -= 200;
  978.         Link (f, t, flag | knight, s - 20000);
  979.         s -= 50;
  980.         Link (f, t, flag | rook, s - 20000);
  981.         flag |= bishop;
  982.         s -= 50;
  983.       }
  984.     else if (row (t) == 1 || row (t) == 6)
  985.       {
  986.         flag |= pwnthrt;
  987.         s += 600;
  988.       }
  989.   Link (f, t, flag, s - 20000);
  990. }
  991.  
  992.  
  993. static inline void
  994. GenMoves (short int ply, short int sq, short int side, short int xside)
  995.  
  996. /*
  997.   Generate moves for a piece. The moves are taken from the precalulated
  998.   array nextpos/nextdir. If the board is free, next move is choosen from
  999.   nextpos else from nextdir.
  1000. */
  1001.  
  1002. {
  1003.   register short u, piece;
  1004.   unsigned char far *ppos, far *pdir;
  1005.  
  1006.   piece = board[sq];
  1007.   ppos = nextpos+ptype[side][piece]*64*64+sq*64;
  1008.   pdir = nextdir+ptype[side][piece]*64*64+sq*64;
  1009.   if (piece == pawn)
  1010.     {
  1011.       u = ppos[sq];     /* follow no captures thread */
  1012.       if (color[u] == neutral)
  1013.         {
  1014.           LinkMove (ply, sq, u, 0, xside);
  1015.           u = ppos[u];
  1016.           if (color[u] == neutral)
  1017.             LinkMove (ply, sq, u, 0, xside);
  1018.         }
  1019.       u = pdir[sq];     /* follow captures thread */
  1020.       if (color[u] == xside)
  1021.         LinkMove (ply, sq, u, capture, xside);
  1022.       else
  1023.         if (u == epsquare)
  1024.           LinkMove (ply, sq, u, capture | epmask, xside);
  1025.       u = pdir[u];
  1026.       if (color[u] == xside)
  1027.         LinkMove (ply, sq, u, capture, xside);
  1028.       else
  1029.         if (u == epsquare)
  1030.           LinkMove (ply, sq, u, capture | epmask, xside);
  1031.     }
  1032.   else
  1033.     {
  1034.       u = ppos[sq];
  1035.       do
  1036.         {
  1037.           if (color[u] == neutral)
  1038.             {
  1039.               LinkMove (ply, sq, u, 0, xside);
  1040.               u = ppos[u];
  1041.             }
  1042.           else
  1043.             {
  1044.               if (color[u] == xside)
  1045.                 LinkMove (ply, sq, u, capture, xside);
  1046.               u = pdir[u];
  1047.             }
  1048.       } while (u != sq);
  1049.     }
  1050. }
  1051.  
  1052. void
  1053. MoveList (short int side, short int ply)
  1054.  
  1055. /*
  1056.   Fill the array Tree[] with all available moves for side to play. Array
  1057.   TrPnt[ply] contains the index into Tree[] of the first move at a ply.
  1058. */
  1059.  
  1060. {
  1061.   short i, xside, f;
  1062.  
  1063.   xside = otherside[side];
  1064.   TrPnt[ply + 1] = TrPnt[ply];
  1065.   if (PV == 0)
  1066.     Swag0 = killr0[ply];
  1067.   else
  1068.     Swag0 = PV;
  1069.   Swag1 = killr1[ply];
  1070.   Swag2 = killr2[ply];
  1071.   Swag3 = killr3[ply];
  1072.   if (ply > 2)
  1073.     Swag4 = killr1[ply - 2];
  1074.   else
  1075.     Swag4 = 0;
  1076.   for (i = PieceCnt[side]; i >= 0; i--)
  1077.     GenMoves (ply, PieceList[side][i], side, xside);
  1078.   if (!castld[side])
  1079.     {
  1080.       f = PieceList[side][0];
  1081.       if (castle (side, f, f + 2, 0))
  1082.         {
  1083.           LinkMove (ply, f, f + 2, cstlmask, xside);
  1084.         }
  1085.       if (castle (side, f, f - 2, 0))
  1086.         {
  1087.           LinkMove (ply, f, f - 2, cstlmask, xside);
  1088.         }
  1089.     }
  1090. }
  1091.  
  1092. void
  1093. CaptureList (short int side, short int ply)
  1094.  
  1095. /*
  1096.   Fill the array Tree[] with all available cature and promote moves for
  1097.   side to play. Array TrPnt[ply] contains the index into Tree[]
  1098.   of the first move at a ply.
  1099. */
  1100.  
  1101. {
  1102.   short u, sq, xside;
  1103.   struct leaf far *node;
  1104.   unsigned char far *ppos, far *pdir;
  1105.   short i, piece, *PL, r7;
  1106.  
  1107.   xside = otherside[side];
  1108.   TrPnt[ply + 1] = TrPnt[ply];
  1109.   node = &Tree[TrPnt[ply]];
  1110.   r7 = rank7[side];
  1111.   PL = PieceList[side];
  1112.   for (i = 0; i <= PieceCnt[side]; i++)
  1113.     {
  1114.       sq = PL[i];
  1115.       piece = board[sq];
  1116.       if (sweep[piece])
  1117.         {
  1118.           ppos = nextpos+piece*64*64+sq*64;
  1119.           pdir = nextdir+piece*64*64+sq*64;
  1120.           u = ppos[sq];
  1121.           do
  1122.             {
  1123.               if (color[u] == neutral)
  1124.                 u = ppos[u];
  1125.               else
  1126.                 {
  1127.                   if (color[u] == xside)
  1128.                     Link (sq, u, capture,
  1129.                           value[board[u]] + svalue[board[u]] - piece);
  1130.                   u = pdir[u];
  1131.                 }
  1132.           } while (u != sq);
  1133.         }
  1134.       else
  1135.         {
  1136.           pdir = nextdir+ptype[side][piece]*64*64+sq*64;
  1137.           if (piece == pawn && row (sq) == r7)
  1138.             {
  1139.               u = pdir[sq];
  1140.               if (color[u] == xside)
  1141.                 Link (sq, u, capture | promote | queen, valueQ);
  1142.               u = pdir[u];
  1143.               if (color[u] == xside)
  1144.                 Link (sq, u, capture | promote | queen, valueQ);
  1145.               ppos = nextpos+ptype[side][piece]*64*64+sq*64;
  1146.               u = ppos[sq]; /* also generate non capture promote */
  1147.               if (color[u] == neutral)
  1148.                 Link (sq, u, promote | queen, valueQ);
  1149.             }
  1150.           else
  1151.             {
  1152.               u = pdir[sq];
  1153.               do
  1154.                 {
  1155.                   if (color[u] == xside)
  1156.                     Link (sq, u, capture,
  1157.                           value[board[u]] + svalue[board[u]] - piece);
  1158.                   u = pdir[u];
  1159.               } while (u != sq);
  1160.             }
  1161.         }
  1162.     }
  1163. }
  1164.  
  1165.  
  1166. int
  1167. castle (short int side, short int kf, short int kt, short int iop)
  1168.  
  1169. /* Make or Unmake a castling move. */
  1170.  
  1171. {
  1172.   short rf, rt, t0, xside;
  1173.  
  1174.   xside = otherside[side];
  1175.   if (kt > kf)
  1176.     {
  1177.       rf = kf + 3;
  1178.       rt = kt - 1;
  1179.     }
  1180.   else
  1181.     {
  1182.       rf = kf - 4;
  1183.       rt = kt + 1;
  1184.     }
  1185.   if (iop == 0)
  1186.     {
  1187.       if (kf != kingP[side] ||
  1188.           board[kf] != king ||
  1189.           board[rf] != rook ||
  1190.           Mvboard[kf] != 0 ||
  1191.           Mvboard[rf] != 0 ||
  1192.           color[kt] != neutral ||
  1193.           color[rt] != neutral ||
  1194.           color[kt - 1] != neutral ||
  1195.           SqAtakd (kf, xside) ||
  1196.           SqAtakd (kt, xside) ||
  1197.           SqAtakd (rt, xside))
  1198.         return (false);
  1199.     }
  1200.   else
  1201.     {
  1202.       if (iop == 1)
  1203.         {
  1204.           castld[side] = true;
  1205.           Mvboard[kf]++;
  1206.           Mvboard[rf]++;
  1207.         }
  1208.       else
  1209.         {
  1210.           castld[side] = false;
  1211.           Mvboard[kf]--;
  1212.           Mvboard[rf]--;
  1213.           t0 = kt;
  1214.           kt = kf;
  1215.           kf = t0;
  1216.           t0 = rt;
  1217.           rt = rf;
  1218.           rf = t0;
  1219.         }
  1220.       board[kt] = king;
  1221.       color[kt] = side;
  1222.       Pindex[kt] = 0;
  1223.       board[kf] = no_piece;
  1224.       color[kf] = neutral;
  1225.       board[rt] = rook;
  1226.       color[rt] = side;
  1227.       Pindex[rt] = Pindex[rf];
  1228.       board[rf] = no_piece;
  1229.       color[rf] = neutral;
  1230.       PieceList[side][Pindex[kt]] = kt;
  1231.       PieceList[side][Pindex[rt]] = rt;
  1232. #if ttblsz
  1233.       UpdateHashbd (side, king, kf, kt);
  1234.       UpdateHashbd (side, rook, rf, rt);
  1235. #endif /* ttblsz */
  1236.     }
  1237.   return (true);
  1238. }
  1239.  
  1240.  
  1241. static inline void
  1242. EnPassant (short int xside, short int f, short int t, short int iop)
  1243.  
  1244. /*
  1245.   Make or unmake an en passant move.
  1246. */
  1247.  
  1248. {
  1249.   register short l;
  1250.  
  1251.   if (t > f)
  1252.     l = t - 8;
  1253.   else
  1254.     l = t + 8;
  1255.   if (iop == 1)
  1256.     {
  1257.       board[l] = no_piece;
  1258.       color[l] = neutral;
  1259.     }
  1260.   else
  1261.     {
  1262.       board[l] = pawn;
  1263.       color[l] = xside;
  1264.     }
  1265.   InitializeStats ();
  1266. }
  1267.  
  1268.  
  1269. static inline void
  1270. UpdatePieceList (short int side, short int sq, short int iop)
  1271.  
  1272. /*
  1273.   Update the PieceList and Pindex arrays when a piece is captured or when a
  1274.   capture is unmade.
  1275. */
  1276.  
  1277. {
  1278.   register short i;
  1279.   if (iop == 1)
  1280.     {
  1281.       PieceCnt[side]--;
  1282.       for (i = Pindex[sq]; i <= PieceCnt[side]; i++)
  1283.         {
  1284.           PieceList[side][i] = PieceList[side][i + 1];
  1285.           Pindex[PieceList[side][i]] = i;
  1286.         }
  1287.     }
  1288.   else
  1289.     {
  1290.       PieceCnt[side]++;
  1291.       PieceList[side][PieceCnt[side]] = sq;
  1292.       Pindex[sq] = PieceCnt[side];
  1293.     }
  1294. }
  1295.  
  1296. void
  1297. MakeMove (short int side,
  1298.           struct leaf far * node,
  1299.           short int *tempb,
  1300.           short int *tempc,
  1301.           short int *tempsf,
  1302.           short int *tempst,
  1303.           short int *INCscore)
  1304.  
  1305. /*
  1306.   Update Arrays board[], color[], and Pindex[] to reflect the new board
  1307.   position obtained after making the move pointed to by node. Also update
  1308.   miscellaneous stuff that changes when a move is made.
  1309. */
  1310.  
  1311. {
  1312.   short f, t, xside, ct, cf;
  1313.  
  1314.   xside = otherside[side];
  1315.   GameCnt++;
  1316.   f = node->f;
  1317.   t = node->t;
  1318.   epsquare = -1;
  1319.   FROMsquare = f;
  1320.   TOsquare = t;
  1321.   *INCscore = 0;
  1322.   GameList[GameCnt].gmove = (f << 8) | t;
  1323.   if (node->flags & cstlmask)
  1324.     {
  1325.       GameList[GameCnt].piece = no_piece;
  1326.       GameList[GameCnt].color = side;
  1327.       (void) castle (side, f, t, 1);
  1328.     }
  1329.   else
  1330.     {
  1331.       if (!(node->flags & capture) && (board[f] != pawn))
  1332.         rpthash[side][hashkey & 0xFF]++;
  1333.       *tempc = color[t];
  1334.       *tempb = board[t];
  1335.       *tempsf = svalue[f];
  1336.       *tempst = svalue[t];
  1337.       GameList[GameCnt].piece = *tempb;
  1338.       GameList[GameCnt].color = *tempc;
  1339.       if (*tempc != neutral)
  1340.         {
  1341.           UpdatePieceList (*tempc, t, 1);
  1342.           if (*tempb == pawn)
  1343.             --PawnCnt[*tempc][column (t)];
  1344.           if (board[f] == pawn)
  1345.             {
  1346.               --PawnCnt[side][column (f)];
  1347.               ++PawnCnt[side][column (t)];
  1348.               cf = column (f);
  1349.               ct = column (t);
  1350.               if (PawnCnt[side][ct] > 1 + PawnCnt[side][cf])
  1351.                 *INCscore -= 15;
  1352.               else if (PawnCnt[side][ct] < 1 + PawnCnt[side][cf])
  1353.                 *INCscore += 15;
  1354.               else if (ct == 0 || ct == 7 || PawnCnt[side][ct + ct - cf] == 0)
  1355.                 *INCscore -= 15;
  1356.             }
  1357.           mtl[xside] -= value[*tempb];
  1358.           if (*tempb == pawn)
  1359.             pmtl[xside] -= valueP;
  1360. #if ttblsz
  1361.           UpdateHashbd (xside, *tempb, -1, t);
  1362. #endif /* ttblsz */
  1363.           *INCscore += *tempst;
  1364.           Mvboard[t]++;
  1365.         }
  1366.       color[t] = color[f];
  1367.       board[t] = board[f];
  1368.       svalue[t] = svalue[f];
  1369.       Pindex[t] = Pindex[f];
  1370.       PieceList[side][Pindex[t]] = t;
  1371.       color[f] = neutral;
  1372.       board[f] = no_piece;
  1373.       if (board[t] == pawn)
  1374.         if (t - f == 16)
  1375.           epsquare = f + 8;
  1376.         else if (f - t == 16)
  1377.           epsquare = f - 8;
  1378.       if (node->flags & promote)
  1379.         {
  1380.           board[t] = node->flags & pmask;
  1381.           if (board[t] == queen)
  1382.             HasQueen[side]++;
  1383.           else if (board[t] == rook)
  1384.             HasRook[side]++;
  1385.           else if (board[t] == bishop)
  1386.             HasBishop[side]++;
  1387.           else if (board[t] == knight)
  1388.             HasKnight[side]++;
  1389.           --PawnCnt[side][column (t)];
  1390.           mtl[side] += value[board[t]] - valueP;
  1391.           pmtl[side] -= valueP;
  1392. #if ttblsz
  1393.           UpdateHashbd (side, pawn, f, -1);
  1394.           UpdateHashbd (side, board[t], f, -1);
  1395. #endif /* ttblsz */
  1396.           *INCscore -= *tempsf;
  1397.         }
  1398.       if (node->flags & epmask)
  1399.         EnPassant (xside, f, t, 1);
  1400.       else
  1401. #if ttblsz
  1402.         UpdateHashbd (side, board[t], f, t);
  1403. #else
  1404.         /* NOOP */;     
  1405. #endif /* ttblsz */
  1406.       Mvboard[f]++;
  1407.     }
  1408. }
  1409.  
  1410. void
  1411. UnmakeMove (short int side,
  1412.             struct leaf far * node,
  1413.             short int *tempb,
  1414.             short int *tempc,
  1415.             short int *tempsf,
  1416.             short int *tempst)
  1417.  
  1418. /*
  1419.   Take back a move.
  1420. */
  1421.  
  1422. {
  1423.   short f, t, xside;
  1424.  
  1425.   xside = otherside[side];
  1426.   f = node->f;
  1427.   t = node->t;
  1428.   epsquare = -1;
  1429.   GameCnt--;
  1430.   if (node->flags & cstlmask)
  1431.     (void) castle (side, f, t, 2);
  1432.   else
  1433.     {
  1434.       color[f] = color[t];
  1435.       board[f] = board[t];
  1436.       svalue[f] = *tempsf;
  1437.       Pindex[f] = Pindex[t];
  1438.       PieceList[side][Pindex[f]] = f;
  1439.       color[t] = *tempc;
  1440.       board[t] = *tempb;
  1441.       svalue[t] = *tempst;
  1442.       if (node->flags & promote)
  1443.         {
  1444.           board[f] = pawn;
  1445.           ++PawnCnt[side][column (t)];
  1446.           mtl[side] += valueP - value[node->flags & pmask];
  1447.           pmtl[side] += valueP;
  1448. #if ttblsz
  1449.           UpdateHashbd (side, (short) node->flags & pmask, -1, t);
  1450.           UpdateHashbd (side, pawn, -1, t);
  1451. #endif /* ttblsz */
  1452.         }
  1453.       if (*tempc != neutral)
  1454.         {
  1455.           UpdatePieceList (*tempc, t, 2);
  1456.           if (*tempb == pawn)
  1457.             ++PawnCnt[*tempc][column (t)];
  1458.           if (board[f] == pawn)
  1459.             {
  1460.               --PawnCnt[side][column (t)];
  1461.               ++PawnCnt[side][column (f)];
  1462.             }
  1463.           mtl[xside] += value[*tempb];
  1464.           if (*tempb == pawn)
  1465.             pmtl[xside] += valueP;
  1466. #if ttblsz
  1467.           UpdateHashbd (xside, *tempb, -1, t);
  1468. #endif /* ttblsz */
  1469.           Mvboard[t]--;
  1470.         }
  1471.       if (node->flags & epmask)
  1472.         EnPassant (xside, f, t, 2);
  1473.       else
  1474. #if ttblsz
  1475.         UpdateHashbd (side, board[f], f, t);
  1476. #else
  1477.       /* NOOP */;
  1478. #endif /* ttblsz */
  1479.       Mvboard[f]--;
  1480.       if (!(node->flags & capture) && (board[f] != pawn))
  1481.         rpthash[side][hashkey & 0xFF]--;
  1482.     }
  1483. }
  1484.  
  1485.  
  1486. void
  1487. InitializeStats (void)
  1488.  
  1489. /*
  1490.   Scan thru the board seeing what's on each square. If a piece is found,
  1491.   update the variables PieceCnt, PawnCnt, Pindex and PieceList. Also
  1492.   determine the material for each side and set the hashkey and hashbd
  1493.   variables to represent the current board position. Array
  1494.   PieceList[side][indx] contains the location of all the pieces of either
  1495.   side. Array Pindex[sq] contains the indx into PieceList for a given
  1496.   square.
  1497. */
  1498.  
  1499. {
  1500.   short i, sq;
  1501.   
  1502.   epsquare = -1;
  1503.   for (i = 0; i < 8; i++)
  1504.     PawnCnt[white][i] = PawnCnt[black][i] = 0;
  1505.   mtl[white] = mtl[black] = pmtl[white] = pmtl[black] = 0;
  1506.   PieceCnt[white] = PieceCnt[black] = 0;
  1507. #if ttblsz
  1508.   hashbd = hashkey = 0;
  1509. #endif /* ttblsz */
  1510.   for (sq = 0; sq < 64; sq++)
  1511.     if (color[sq] != neutral)
  1512.       {
  1513.         mtl[color[sq]] += value[board[sq]];
  1514.         if (board[sq] == pawn)
  1515.           {
  1516.             pmtl[color[sq]] += valueP;
  1517.             ++PawnCnt[color[sq]][column (sq)];
  1518.           }
  1519.         if (board[sq] == king)
  1520.           Pindex[sq] = 0;
  1521.         else
  1522.           Pindex[sq] = ++PieceCnt[color[sq]];
  1523.         PieceList[color[sq]][Pindex[sq]] = sq;
  1524. #if ttblsz
  1525.         hashbd ^= (hashcode+color[sq]*7*64+board[sq]*64+sq)->bd;
  1526.         hashkey ^= (hashcode+color[sq]*7*64+board[sq]*64+sq)->key;
  1527. #endif /* ttblsz */
  1528.       }
  1529. }
  1530.  
  1531.  
  1532. int
  1533. SqAtakd (short int sq, short int side)
  1534.  
  1535. /*
  1536.   See if any piece with color 'side' ataks sq.  First check pawns then Queen,
  1537.   Bishop, Rook and King and last Knight.
  1538. */
  1539.  
  1540. {
  1541.   register short u;
  1542.   unsigned char far *ppos, far *pdir;
  1543.   short xside;
  1544.  
  1545.   xside = otherside[side];
  1546.   pdir = nextdir+ptype[xside][pawn]*64*64+sq*64;
  1547.   u = pdir[sq];         /* follow captures thread */
  1548.   if (u != sq)
  1549.     {
  1550.       if (board[u] == pawn && color[u] == side)
  1551.         return (true);
  1552.       u = pdir[u];
  1553.       if (u != sq && board[u] == pawn && color[u] == side)
  1554.         return (true);
  1555.     }
  1556.   /* king capture */
  1557.   if (distance (sq, PieceList[side][0]) == 1)
  1558.     return (true);
  1559.   /* try a queen bishop capture */
  1560.   ppos = nextpos+bishop*64*64+sq*64;
  1561.   pdir = nextdir+bishop*64*64+sq*64;
  1562.   u = ppos[sq];
  1563.   do
  1564.     {
  1565.       if (color[u] == neutral)
  1566.         u = ppos[u];
  1567.       else
  1568.         {
  1569.           if (color[u] == side &&
  1570.               (board[u] == queen || board[u] == bishop))
  1571.             return (true);
  1572.           u = pdir[u];
  1573.         }
  1574.   } while (u != sq);
  1575.   /* try a queen rook capture */
  1576.   ppos = nextpos+rook*64*64+sq*64;
  1577.   pdir = nextdir+rook*64*64+sq*64;
  1578.   u = ppos[sq];
  1579.   do
  1580.     {
  1581.       if (color[u] == neutral)
  1582.         u = ppos[u];
  1583.       else
  1584.         {
  1585.           if (color[u] == side &&
  1586.               (board[u] == queen || board[u] == rook))
  1587.             return (true);
  1588.           u = pdir[u];
  1589.         }
  1590.   } while (u != sq);
  1591.   /* try a knight capture */
  1592.   pdir = nextdir+knight*64*64+sq*64;
  1593.   u = pdir[sq];
  1594.   do
  1595.     {
  1596.       if (color[u] == side && board[u] == knight)
  1597.         return (true);
  1598.       u = pdir[u];
  1599.   } while (u != sq);
  1600.   return (false);
  1601. }
  1602.  
  1603. void
  1604. ataks (short int side, short int *a)
  1605.  
  1606. /*
  1607.   Fill array atak[][] with info about ataks to a square.  Bits 8-15 are set
  1608.   if the piece (king..pawn) ataks the square.  Bits 0-7 contain a count of
  1609.   total ataks to the square.
  1610. */
  1611.  
  1612. {
  1613.   short u, c, sq;
  1614.   unsigned char far *ppos, far *pdir;
  1615.   short i, piece, *PL;
  1616.  
  1617. #ifdef NOMEMSET
  1618.   for (u = 64; u; a[--u] = 0) ;
  1619. #else
  1620.   memset ((char *) a, 0, 64 * sizeof (a[0]));
  1621. #endif /* NOMEMSET */
  1622.   PL = PieceList[side];
  1623.   for (i = PieceCnt[side]; i >= 0; i--)
  1624.     {
  1625.       sq = PL[i];
  1626.       piece = board[sq];
  1627.       c = control[piece];
  1628.       if (sweep[piece])
  1629.         {
  1630.           ppos = nextpos+piece*64*64+sq*64;
  1631.           pdir = nextdir+piece*64*64+sq*64;
  1632.           u = ppos[sq];
  1633.           do
  1634.             {
  1635.               a[u] = ++a[u] | c;
  1636.               u = (color[u] == neutral) ? ppos[u] : pdir[u];
  1637.           } while (u != sq);
  1638.         }
  1639.       else
  1640.         {
  1641.           pdir = nextdir+ptype[side][piece]*64*64+sq*64;
  1642.           u = pdir[sq]; /* follow captures thread for pawns */
  1643.           do
  1644.             {
  1645.               a[u] = ++a[u] | c;
  1646.               u = pdir[u];
  1647.           } while (u != sq);
  1648.         }
  1649.     }
  1650. }
  1651.