home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / winchess / search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  41.7 KB  |  1,664 lines

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-12-26
  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.  
  453.       /* This test has been modified in 3.1 from 1.55 code. I think the mods
  454.          have introduced an error in the search which causes the programme
  455.          to run away from checking moves - hence the failure to find mates
  456.          and to play through mate situations.
  457.          The fixes introduced solve the problem reported by:
  458.          cambell@rnd.GBA.NYU.EDU
  459.          in the mate example.
  460.                                  J.Birmingham.
  461.       */
  462.   if (depth > 0)
  463.     {
  464.       /* Allow opponent a chance to check again */
  465.       if (InChk)
  466.         depth = (depth < 2) ? 2 : depth;
  467.       else if (PawnThreat[ply - 1] || ReCapture)
  468.         ++depth;
  469.     }
  470.   else
  471.     {
  472.       if (score >= alpha &&
  473.           (InChk || PawnThreat[ply - 1] || Parry))
  474.         ++depth;           /* this was depth=1 in original ?bug? */
  475.       else if (score <= beta && MateThreat)
  476.         ++depth;           /* this was also set to depth=1  ?bug? */
  477.     }
  478.  
  479.    /* end of changed section      J.Birmingham.          */
  480.  
  481. #if ttblsz
  482.   if (depth > 0 && flag.hash && ply > 1)
  483.     {
  484.       if (ProbeTTable (side, depth, &alpha, &beta, &score) == false)
  485. #ifdef HASHFILE 
  486.         if (hashfile && (depth > 5) && (GameCnt < 12))
  487.           ProbeFTable (side, depth, &alpha, &beta, &score);
  488. #else
  489.       /* do nothing */;
  490. #endif /* HASHFILE */      
  491.       bstline[ply] = PV;
  492.       bstline[ply + 1] = 0;
  493.       if (beta == -20000)
  494.         return (score);
  495.       if (alpha > beta)
  496.         return (alpha);
  497.     }
  498. #endif /* ttblsz */
  499.   if (Sdepth == 1)
  500.     d = 7;
  501.   else
  502.     d = 11;
  503.   if (ply > Sdepth + d || (depth < 1 && score > beta))
  504.     /* score >= beta ?? */
  505.     return (score);
  506.  
  507.   if (ply > 1)
  508.     if (depth > 0)
  509.       MoveList (side, ply);
  510.     else
  511.       CaptureList (side, ply);
  512.  
  513.   if (TrPnt[ply] == TrPnt[ply + 1])
  514.     return (score);
  515.  
  516.   cf = (depth < 1 && ply > Sdepth + 1 && !ChkFlag[ply - 2] && !slk);
  517.  
  518.   if (depth > 0)
  519.     best = -12000;
  520.   else
  521.     best = score;
  522.   if (best > alpha)
  523.     alpha = best;
  524.  
  525.   for (pnt = pbst = TrPnt[ply];
  526.        pnt < TrPnt[ply + 1] && best <= beta;    /* best < beta ?? */
  527.        pnt++)
  528.     {
  529.  
  530.       {
  531.        MSG msg;
  532.        if ( !flag.timeout && PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)){
  533.              TranslateMessage(&msg);       
  534.              DispatchMessage(&msg);
  535.        }
  536.       }
  537.  
  538.       if (ply > 1)
  539.         pick (pnt, TrPnt[ply + 1] - 1);
  540.       node = &Tree[pnt];
  541.       mv = (node->f << 8) | node->t;
  542.       nxtline[ply + 1] = 0;
  543.  
  544.       if (prune)
  545.         break;
  546.       if (ply == 1)
  547.         UpdateSearchStatus;
  548.  
  549.       if (!(node->flags & exact))
  550.         {
  551.           MakeMove (side, node, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  552.           CptrFlag[ply] = (node->flags & capture);
  553.           PawnThreat[ply] = (node->flags & pwnthrt);
  554.           Tscore[ply] = node->score;
  555.           PV = node->reply;
  556.           node->score = -search (xside, ply + 1,
  557.                                  (depth > 0) ? depth - 1 : 0,
  558.                                  -beta, -alpha,
  559.                                  nxtline, &rcnt);
  560.           if (abs (node->score) > 9000)
  561.             node->flags |= exact;
  562.           else if (rcnt == 1)
  563.             node->score /= 2;
  564.           if (rcnt >= 2 || GameCnt - Game50 > 99 ||
  565.               (node->score == 9999 - ply && !ChkFlag[ply]))
  566.             {
  567.               node->flags |= draw;
  568.               node->flags |= exact;
  569.               if (side == computer)
  570.                 node->score = contempt;
  571.               else
  572.                 node->score = -contempt;
  573.             }
  574.           node->reply = nxtline[ply + 1];
  575.           UnmakeMove (side, node, &tempb, &tempc, &tempsf, &tempst);
  576.         }
  577.       if (node->score > best && !flag.timeout)
  578.         {
  579.           if (depth > 0)
  580.             if (node->score > alpha && !(node->flags & exact))
  581.               node->score += depth;
  582.           best = node->score;
  583.           pbst = pnt;
  584.           if (best > alpha)
  585.             alpha = best;
  586.           for (j = ply + 1; nxtline[j] > 0; j++)
  587.             bstline[j] = nxtline[j];
  588.           bstline[j] = 0;
  589.           bstline[ply] = mv;
  590.           if (ply == 1)
  591.             {
  592.               if (best > root->score)
  593.                 {
  594.                   tmp = Tree[pnt];
  595.                   for (j = pnt - 1; j >= 0; j--)
  596.                     Tree[j + 1] = Tree[j];
  597.                   Tree[0] = tmp;
  598.                   pbst = 0;
  599.                 }
  600.               if (Sdepth > 2)
  601.                 if (best > beta)
  602.                   ShowResults (best, bstline, '+');
  603.                 else if (best < alpha)
  604.                   ShowResults (best, bstline, '-');
  605.                 else
  606.                   ShowResults (best, bstline, '&');
  607.             }
  608.         }
  609.       if (NodeCnt > ETnodes)
  610.         ElapsedTime (0);
  611.       if (flag.timeout)
  612.         return (-Tscore[ply - 1]);
  613.     }
  614.  
  615.   node = &Tree[pbst];
  616.   mv = (node->f << 8) | node->t;
  617. #if ttblsz
  618.   if (flag.hash && ply <= Sdepth && *rpt == 0 && best == alpha)
  619.     {
  620.       PutInTTable (side, best, depth, alpha, beta, mv);
  621. #ifdef HASHFILE      
  622.       if (hashfile && (depth > 5) && (GameCnt < 12))
  623.         PutInFTable (side, best, depth, alpha, beta, node->f, node->t);
  624. #endif /* HASHFILE */      
  625.     }
  626. #endif /* ttblsz */
  627.   if (depth > 0)
  628.     {
  629.       j = (node->f << 6) | node->t;
  630.       if (side == black)
  631.         j |= 0x1000;
  632. /*      if (history[j] < 150)
  633.           history[j] += (unsigned char) 2 * depth; */
  634.       if (*(history+j) < 150)
  635.         *(history+j) += (unsigned char) 2 * depth;
  636.       if (node->t != (GameList[GameCnt].gmove & 0xFF))
  637.         if (best <= beta)
  638.           killr3[ply] = mv;
  639.         else if (mv != killr1[ply])
  640.           {
  641.             killr2[ply] = killr1[ply];
  642.             killr1[ply] = mv;
  643.           }
  644.       if (best > 9000)
  645.         killr0[ply] = mv;
  646.       else
  647.         killr0[ply] = 0;
  648.     }
  649.   return (best);
  650. }
  651.  
  652. #if ttblsz
  653. /*
  654.   hashbd contains a 32 bit "signature" of the board position. hashkey
  655.   contains a 16 bit code used to address the hash table. When a move is
  656.   made, XOR'ing the hashcode of moved piece on the from and to squares with
  657.   the hashbd and hashkey values keeps things current.
  658. */
  659. #define UpdateHashbd(side, piece, f, t) \
  660. {\
  661.   if ((f) >= 0)\
  662.     {\
  663.       hashbd ^= (hashcode+(side)*7*64+(piece)*64+f)->bd;\
  664.       hashkey ^= (hashcode+(side)*7*64+(piece)*64+f)->key;\
  665.     }\
  666.   if ((t) >= 0)\
  667.     {\
  668.       hashbd ^= (hashcode+(side)*7*64+(piece)*64+t)->bd;\
  669.       hashkey ^= (hashcode+(side)*7*64+(piece)*64+t)->key;\
  670.     }\
  671. }
  672.  
  673. #define CB(i) (unsigned char) ((color[2 * (i)] ? 0x80 : 0)\
  674.                | (board[2 * (i)] << 4)\
  675.                | (color[2 * (i) + 1] ? 0x8 : 0)\
  676.                | (board[2 * (i) + 1]))
  677.  
  678. int
  679. ProbeTTable (short int side,
  680.              short int depth,
  681.              short int *alpha,
  682.              short int *beta,
  683.              short int *score)
  684.  
  685. /*
  686.   Look for the current board position in the transposition table.
  687. */
  688.  
  689. {
  690.   struct hashentry far *ptbl;
  691.   register unsigned short i;
  692.  
  693. /*  ptbl = &ttable[side][hashkey & (ttblsz - 1)]; */
  694.    ptbl = ttable+side*2+(hashkey & (ttblsz - 1));
  695.  
  696.   /* rehash max rehash times */
  697.   for (i = 1; ptbl->hashbd != hashbd && i <= rehash; i++)
  698. /*    ptbl = &ttable[side][(hashkey + i) & (ttblsz - 1)]; */
  699.     ptbl = ttable+side*2+((hashkey + i) & (ttblsz - 1));
  700.   if ((short) ptbl->depth >= depth && ptbl->hashbd == hashbd)
  701.     {
  702.       HashCnt++;
  703. #ifdef HASHTEST
  704.       for (i = 0; i < 32; i++)
  705.         {
  706.           if (ptbl->bd[i] != CB(i))
  707.             {
  708.               HashCol++;
  709.               ShowMessage("ttable collision detected");
  710.               break;
  711.             }
  712.         }
  713. #endif /* HASHTEST */
  714.  
  715.       PV = ptbl->mv;
  716.       if (ptbl->flags & truescore)
  717.         {
  718.           *score = ptbl->score;
  719.           *beta = -20000;
  720.         }
  721. #if 0 /* commented out, why? */
  722.       else if (ptbl->flags & upperbound)
  723.         {
  724.           if (ptbl->score < *beta) *beta = ptbl->score+1;
  725.         }
  726. #endif
  727.       else if (ptbl->flags & lowerbound)
  728.         {
  729.           if (ptbl->score > *alpha)
  730.             *alpha = ptbl->score - 1;
  731.         }
  732.       return(true);
  733.     }
  734.   return(false);
  735. }
  736.  
  737. void
  738. PutInTTable (short int side,
  739.              short int score,
  740.              short int depth,
  741.              short int alpha,
  742.              short int beta,
  743.              short unsigned int mv)
  744.  
  745. /*
  746.   Store the current board position in the transposition table.
  747. */
  748.  
  749. {
  750.   struct hashentry far *ptbl;
  751.   register unsigned short i;
  752.  
  753. /*  ptbl = &ttable[side][hashkey & (ttblsz - 1)]; */
  754.   ptbl = ttable+side*2+(hashkey & (ttblsz - 1));
  755.  
  756.   /* rehash max rehash times */
  757.   for (i = 1; depth < ptbl->depth && ptbl->hashbd != hashbd && i <= rehash; i++)
  758. /*    ptbl = &ttable[side][(hashkey + i) & (ttblsz - 1)]; */
  759.     ptbl = ttable+side*2+((hashkey + i) & (ttblsz - 1));
  760.   if (depth >= ptbl->depth || ptbl->hashbd != hashbd)
  761.     {
  762.       ptbl->hashbd = hashbd;
  763.       ptbl->depth = (unsigned char) depth;
  764.       ptbl->score = score;
  765.       ptbl->mv = mv;
  766.       ptbl->flags = 0;
  767.       if (score < alpha)
  768.         ptbl->flags |= upperbound;
  769.       else if (score > beta)
  770.         ptbl->flags |= lowerbound;
  771.       else
  772.         ptbl->flags |= truescore;
  773. #ifdef HASHTEST
  774.       for (i = 0; i < 32; i++)
  775.         {
  776.           ptbl->bd[i] = CB(i);
  777.         }
  778. #endif /* HASHTEST */
  779.     }
  780. }
  781.  
  782. void
  783. ZeroTTable (void)
  784. {
  785.   register int side, i;
  786.  
  787.   if (flag.hash)
  788.     for (side = white; side <= black; side++)
  789.       for (i = 0; i < ttblsz; i++)
  790. /*        ttable[side][i].depth = 0; */
  791.         (ttable+side*2+i)->depth = 0;
  792. }
  793.  
  794. #ifdef HASHFILE
  795. int
  796. ProbeFTable(short int side,
  797.             short int depth,
  798.             short int *alpha,
  799.             short int *beta,
  800.             short int *score)
  801.  
  802. /*
  803.   Look for the current board position in the persistent transposition table.
  804. */
  805.  
  806. {
  807.   register unsigned short i, j;
  808.   unsigned long hashix;
  809.   short s;
  810.   struct fileentry new, t;
  811.  
  812.   if (side == white)
  813.     hashix = hashkey & 0xFFFFFFFE & (filesz - 1);
  814.   else
  815.     hashix = hashkey | 1 & (filesz - 1);
  816.  
  817.   for (i = 0; i < 32; i++)
  818.     new.bd[i] = CB(i);
  819.   new.flags = 0;
  820.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[qrook[side]] == 0))
  821.     new.flags |= queencastle;
  822.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[krook[side]] == 0))
  823.     new.flags |= kingcastle;
  824.  
  825.   for (i = 0; i < frehash; i++)
  826.     {
  827.       fseek(hashfile,
  828.             sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  829.             SEEK_SET);
  830.       fread(&t, sizeof(struct fileentry), 1, hashfile);
  831.       for (j = 0; j < 32; j++)
  832.         if (t.bd[j] != new.bd[j])
  833.           break;
  834.       if (((short) t.depth >= depth) && (j >= 32)
  835.           && (new.flags == (t.flags & (kingcastle | queencastle))))
  836.         {
  837.           FHashCnt++;
  838.           PV = (t.f << 8) | t.t;
  839.           s = (t.sh << 8) | t.sl;
  840.           if (t.flags & truescore)
  841.             {
  842.               *score = s;
  843.               *beta = -20000;
  844.             }
  845.           else if (t.flags & lowerbound)
  846.             {
  847.               if (s > *alpha)
  848.                 *alpha = s - 1;
  849.             }
  850.           return(true);
  851.         }
  852.     }
  853.   return(false);
  854. }
  855.  
  856. void
  857. PutInFTable (short int side,
  858.              short int score,
  859.              short int depth,
  860.              short int alpha,
  861.              short int beta,
  862.              short unsigned int f,
  863.              short unsigned int t)
  864.  
  865. /*
  866.   Store the current board position in the persistent transposition table.
  867. */
  868.  
  869. {
  870.   register unsigned short i;
  871.   unsigned long hashix;
  872.   struct fileentry new, tmp;
  873.  
  874.   if (side == white)
  875.     hashix = hashkey & 0xFFFFFFFE & (filesz - 1);
  876.   else
  877.     hashix = hashkey | 1 & (filesz - 1);
  878.  
  879.   for (i = 0; i < 32; i++)
  880.     new.bd[i] = CB(i);
  881.   new.f = (unsigned char) f;
  882.   new.t = (unsigned char) t;
  883.   new.flags = 0;
  884.   if (score < alpha)
  885.     new.flags |= upperbound;
  886.   else if (score > beta)
  887.     new.flags |= lowerbound;
  888.   else
  889.     new.flags |= truescore;
  890.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[qrook[side]] == 0))
  891.     new.flags |= queencastle;
  892.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[krook[side]] == 0))
  893.     new.flags |= kingcastle;
  894.   new.depth = (unsigned char) depth;
  895.   new.sh = (unsigned char) (score >> 8);
  896.   new.sl = (unsigned char) (score & 0xFF);
  897.  
  898.   for (i = 0; i < frehash; i++)
  899.     {
  900.       fseek(hashfile,
  901.             sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  902.             SEEK_SET);
  903.       fread(&tmp, sizeof(struct fileentry), 1, hashfile);
  904.       if ((short) tmp.depth <= depth)
  905.         {
  906.           fseek(hashfile,
  907.                 sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  908.                 SEEK_SET);
  909.           fwrite (&new, sizeof(struct fileentry), 1, hashfile);
  910.           break;
  911.         }
  912.     }
  913. }
  914. #endif /* HASHFILE */
  915. #endif /* ttblsz */
  916.  
  917. void
  918. ZeroRPT (void)
  919. {
  920.   register int side, i;
  921.  
  922.   for (side = white; side <= black; side++)
  923.     for (i = 0; i < 256; i++)
  924.       rpthash[side][i] = 0;
  925. }
  926.  
  927. #define Link(from,to,flag,s) \
  928. {\
  929.    node->f = from; node->t = to;\
  930.      node->reply = 0;\
  931.        node->flags = flag;\
  932.          node->score = s;\
  933.            ++node;\
  934.              ++TrPnt[ply+1];\
  935.              }
  936.  
  937. static inline void
  938. LinkMove (short int ply,
  939.           short int f,
  940.           short int t,
  941.           short int flag,
  942.           short int xside)
  943.  
  944. /*
  945.   Add a move to the tree.  Assign a bonus to order the moves
  946.   as follows:
  947.   1. Principle variation
  948.   2. Capture of last moved piece
  949.   3. Other captures (major pieces first)
  950.   4. Killer moves
  951.   5. "history" killers
  952. */
  953.  
  954. {
  955.   register short s, z;
  956.   unsigned short mv;
  957.   struct leaf far *node;
  958.  
  959.   node = &Tree[TrPnt[ply + 1]];
  960.   mv = (f << 8) | t;
  961.   s = 0;
  962.   if (mv == Swag0)
  963.     s = 2000;
  964.   else if (mv == Swag1)
  965.     s = 60;
  966.   else if (mv == Swag2)
  967.     s = 50;
  968.   else if (mv == Swag3)
  969.     s = 40;
  970.   else if (mv == Swag4)
  971.     s = 30;
  972.   z = (f << 6) | t;
  973.   if (xside == white)
  974.     z |= 0x1000;
  975. /*  s += history[z]; */
  976.   s += *(history+z);
  977.   if (color[t] != neutral)
  978.     {
  979.       if (t == TOsquare)
  980.         s += 500;
  981.       s += value[board[t]] - board[f];
  982.     }
  983.   if (board[f] == pawn)
  984.     if (row (t) == 0 || row (t) == 7)
  985.       {
  986.         flag |= promote;
  987.         s += 800;
  988.         Link (f, t, flag | queen, s - 20000);
  989.         s -= 200;
  990.         Link (f, t, flag | knight, s - 20000);
  991.         s -= 50;
  992.         Link (f, t, flag | rook, s - 20000);
  993.         flag |= bishop;
  994.         s -= 50;
  995.       }
  996.     else if (row (t) == 1 || row (t) == 6)
  997.       {
  998.         flag |= pwnthrt;
  999.         s += 600;
  1000.       }
  1001.   Link (f, t, flag, s - 20000);
  1002. }
  1003.  
  1004.  
  1005. static inline void
  1006. GenMoves (short int ply, short int sq, short int side, short int xside)
  1007.  
  1008. /*
  1009.   Generate moves for a piece. The moves are taken from the precalulated
  1010.   array nextpos/nextdir. If the board is free, next move is choosen from
  1011.   nextpos else from nextdir.
  1012. */
  1013.  
  1014. {
  1015.   register short u, piece;
  1016.   unsigned char far *ppos, far *pdir;
  1017.  
  1018.   piece = board[sq];
  1019.   ppos = nextpos+ptype[side][piece]*64*64+sq*64;
  1020.   pdir = nextdir+ptype[side][piece]*64*64+sq*64;
  1021.   if (piece == pawn)
  1022.     {
  1023.       u = ppos[sq];     /* follow no captures thread */
  1024.       if (color[u] == neutral)
  1025.         {
  1026.           LinkMove (ply, sq, u, 0, xside);
  1027.           u = ppos[u];
  1028.           if (color[u] == neutral)
  1029.             LinkMove (ply, sq, u, 0, xside);
  1030.         }
  1031.       u = pdir[sq];     /* follow captures thread */
  1032.       if (color[u] == xside)
  1033.         LinkMove (ply, sq, u, capture, xside);
  1034.       else
  1035.         if (u == epsquare)
  1036.           LinkMove (ply, sq, u, capture | epmask, xside);
  1037.       u = pdir[u];
  1038.       if (color[u] == xside)
  1039.         LinkMove (ply, sq, u, capture, xside);
  1040.       else
  1041.         if (u == epsquare)
  1042.           LinkMove (ply, sq, u, capture | epmask, xside);
  1043.     }
  1044.   else
  1045.     {
  1046.       u = ppos[sq];
  1047.       do
  1048.         {
  1049.           if (color[u] == neutral)
  1050.             {
  1051.               LinkMove (ply, sq, u, 0, xside);
  1052.               u = ppos[u];
  1053.             }
  1054.           else
  1055.             {
  1056.               if (color[u] == xside)
  1057.                 LinkMove (ply, sq, u, capture, xside);
  1058.               u = pdir[u];
  1059.             }
  1060.       } while (u != sq);
  1061.     }
  1062. }
  1063.  
  1064. void
  1065. MoveList (short int side, short int ply)
  1066.  
  1067. /*
  1068.   Fill the array Tree[] with all available moves for side to play. Array
  1069.   TrPnt[ply] contains the index into Tree[] of the first move at a ply.
  1070. */
  1071.  
  1072. {
  1073.   short i, xside, f;
  1074.  
  1075.   xside = otherside[side];
  1076.   TrPnt[ply + 1] = TrPnt[ply];
  1077.   if (PV == 0)
  1078.     Swag0 = killr0[ply];
  1079.   else
  1080.     Swag0 = PV;
  1081.   Swag1 = killr1[ply];
  1082.   Swag2 = killr2[ply];
  1083.   Swag3 = killr3[ply];
  1084.   if (ply > 2)
  1085.     Swag4 = killr1[ply - 2];
  1086.   else
  1087.     Swag4 = 0;
  1088.   for (i = PieceCnt[side]; i >= 0; i--)
  1089.     GenMoves (ply, PieceList[side][i], side, xside);
  1090.   if (!castld[side])
  1091.     {
  1092.       f = PieceList[side][0];
  1093.       if (castle (side, f, f + 2, 0))
  1094.         {
  1095.           LinkMove (ply, f, f + 2, cstlmask, xside);
  1096.         }
  1097.       if (castle (side, f, f - 2, 0))
  1098.         {
  1099.           LinkMove (ply, f, f - 2, cstlmask, xside);
  1100.         }
  1101.     }
  1102. }
  1103.  
  1104. void
  1105. CaptureList (short int side, short int ply)
  1106.  
  1107. /*
  1108.   Fill the array Tree[] with all available cature and promote moves for
  1109.   side to play. Array TrPnt[ply] contains the index into Tree[]
  1110.   of the first move at a ply.
  1111. */
  1112.  
  1113. {
  1114.   short u, sq, xside;
  1115.   struct leaf far *node;
  1116.   unsigned char far *ppos, far *pdir;
  1117.   short i, piece, *PL, r7;
  1118.  
  1119.   xside = otherside[side];
  1120.   TrPnt[ply + 1] = TrPnt[ply];
  1121.   node = &Tree[TrPnt[ply]];
  1122.   r7 = rank7[side];
  1123.   PL = PieceList[side];
  1124.   for (i = 0; i <= PieceCnt[side]; i++)
  1125.     {
  1126.       sq = PL[i];
  1127.       piece = board[sq];
  1128.       if (sweep[piece])
  1129.         {
  1130.           ppos = nextpos+piece*64*64+sq*64;
  1131.           pdir = nextdir+piece*64*64+sq*64;
  1132.           u = ppos[sq];
  1133.           do
  1134.             {
  1135.               if (color[u] == neutral)
  1136.                 u = ppos[u];
  1137.               else
  1138.                 {
  1139.                   if (color[u] == xside)
  1140.                     Link (sq, u, capture,
  1141.                           value[board[u]] + svalue[board[u]] - piece);
  1142.                   u = pdir[u];
  1143.                 }
  1144.           } while (u != sq);
  1145.         }
  1146.       else
  1147.         {
  1148.           pdir = nextdir+ptype[side][piece]*64*64+sq*64;
  1149.           if (piece == pawn && row (sq) == r7)
  1150.             {
  1151.               u = pdir[sq];
  1152.               if (color[u] == xside)
  1153.                 Link (sq, u, capture | promote | queen, valueQ);
  1154.               u = pdir[u];
  1155.               if (color[u] == xside)
  1156.                 Link (sq, u, capture | promote | queen, valueQ);
  1157.               ppos = nextpos+ptype[side][piece]*64*64+sq*64;
  1158.               u = ppos[sq]; /* also generate non capture promote */
  1159.               if (color[u] == neutral)
  1160.                 Link (sq, u, promote | queen, valueQ);
  1161.             }
  1162.           else
  1163.             {
  1164.               u = pdir[sq];
  1165.               do
  1166.                 {
  1167.                   if (color[u] == xside)
  1168.                     Link (sq, u, capture,
  1169.                           value[board[u]] + svalue[board[u]] - piece);
  1170.                   u = pdir[u];
  1171.               } while (u != sq);
  1172.             }
  1173.         }
  1174.     }
  1175. }
  1176.  
  1177.  
  1178. int
  1179. castle (short int side, short int kf, short int kt, short int iop)
  1180.  
  1181. /* Make or Unmake a castling move. */
  1182.  
  1183. {
  1184.   short rf, rt, t0, xside;
  1185.  
  1186.   xside = otherside[side];
  1187.   if (kt > kf)
  1188.     {
  1189.       rf = kf + 3;
  1190.       rt = kt - 1;
  1191.     }
  1192.   else
  1193.     {
  1194.       rf = kf - 4;
  1195.       rt = kt + 1;
  1196.     }
  1197.   if (iop == 0)
  1198.     {
  1199.       if (kf != kingP[side] ||
  1200.           board[kf] != king ||
  1201.           board[rf] != rook ||
  1202.           Mvboard[kf] != 0 ||
  1203.           Mvboard[rf] != 0 ||
  1204.           color[kt] != neutral ||
  1205.           color[rt] != neutral ||
  1206.           color[kt - 1] != neutral ||
  1207.           SqAtakd (kf, xside) ||
  1208.           SqAtakd (kt, xside) ||
  1209.           SqAtakd (rt, xside))
  1210.         return (false);
  1211.     }
  1212.   else
  1213.     {
  1214.       if (iop == 1)
  1215.         {
  1216.           castld[side] = true;
  1217.           Mvboard[kf]++;
  1218.           Mvboard[rf]++;
  1219.         }
  1220.       else
  1221.         {
  1222.           castld[side] = false;
  1223.           Mvboard[kf]--;
  1224.           Mvboard[rf]--;
  1225.           t0 = kt;
  1226.           kt = kf;
  1227.           kf = t0;
  1228.           t0 = rt;
  1229.           rt = rf;
  1230.           rf = t0;
  1231.         }
  1232.       board[kt] = king;
  1233.       color[kt] = side;
  1234.       Pindex[kt] = 0;
  1235.       board[kf] = no_piece;
  1236.       color[kf] = neutral;
  1237.       board[rt] = rook;
  1238.       color[rt] = side;
  1239.       Pindex[rt] = Pindex[rf];
  1240.       board[rf] = no_piece;
  1241.       color[rf] = neutral;
  1242.       PieceList[side][Pindex[kt]] = kt;
  1243.       PieceList[side][Pindex[rt]] = rt;
  1244. #if ttblsz
  1245.       UpdateHashbd (side, king, kf, kt);
  1246.       UpdateHashbd (side, rook, rf, rt);
  1247. #endif /* ttblsz */
  1248.     }
  1249.   return (true);
  1250. }
  1251.  
  1252.  
  1253. static inline void
  1254. EnPassant (short int xside, short int f, short int t, short int iop)
  1255.  
  1256. /*
  1257.   Make or unmake an en passant move.
  1258. */
  1259.  
  1260. {
  1261.   register short l;
  1262.  
  1263.   if (t > f)
  1264.     l = t - 8;
  1265.   else
  1266.     l = t + 8;
  1267.   if (iop == 1)
  1268.     {
  1269.       board[l] = no_piece;
  1270.       color[l] = neutral;
  1271.     }
  1272.   else
  1273.     {
  1274.       board[l] = pawn;
  1275.       color[l] = xside;
  1276.     }
  1277.   InitializeStats ();
  1278. }
  1279.  
  1280.  
  1281. static inline void
  1282. UpdatePieceList (short int side, short int sq, short int iop)
  1283.  
  1284. /*
  1285.   Update the PieceList and Pindex arrays when a piece is captured or when a
  1286.   capture is unmade.
  1287. */
  1288.  
  1289. {
  1290.   register short i;
  1291.   if (iop == 1)
  1292.     {
  1293.       PieceCnt[side]--;
  1294.       for (i = Pindex[sq]; i <= PieceCnt[side]; i++)
  1295.         {
  1296.           PieceList[side][i] = PieceList[side][i + 1];
  1297.           Pindex[PieceList[side][i]] = i;
  1298.         }
  1299.     }
  1300.   else
  1301.     {
  1302.       PieceCnt[side]++;
  1303.       PieceList[side][PieceCnt[side]] = sq;
  1304.       Pindex[sq] = PieceCnt[side];
  1305.     }
  1306. }
  1307.  
  1308. void
  1309. MakeMove (short int side,
  1310.           struct leaf far * node,
  1311.           short int *tempb,
  1312.           short int *tempc,
  1313.           short int *tempsf,
  1314.           short int *tempst,
  1315.           short int *INCscore)
  1316.  
  1317. /*
  1318.   Update Arrays board[], color[], and Pindex[] to reflect the new board
  1319.   position obtained after making the move pointed to by node. Also update
  1320.   miscellaneous stuff that changes when a move is made.
  1321. */
  1322.  
  1323. {
  1324.   short f, t, xside, ct, cf;
  1325.  
  1326.   xside = otherside[side];
  1327.   GameCnt++;
  1328.   f = node->f;
  1329.   t = node->t;
  1330.   epsquare = -1;
  1331.   FROMsquare = f;
  1332.   TOsquare = t;
  1333.   *INCscore = 0;
  1334.   GameList[GameCnt].gmove = (f << 8) | t;
  1335.   if (node->flags & cstlmask)
  1336.     {
  1337.       GameList[GameCnt].piece = no_piece;
  1338.       GameList[GameCnt].color = side;
  1339.       (void) castle (side, f, t, 1);
  1340.     }
  1341.   else
  1342.     {
  1343.       if (!(node->flags & capture) && (board[f] != pawn))
  1344.         rpthash[side][hashkey & 0xFF]++;
  1345.       *tempc = color[t];
  1346.       *tempb = board[t];
  1347.       *tempsf = svalue[f];
  1348.       *tempst = svalue[t];
  1349.       GameList[GameCnt].piece = *tempb;
  1350.       GameList[GameCnt].color = *tempc;
  1351.       if (*tempc != neutral)
  1352.         {
  1353.           UpdatePieceList (*tempc, t, 1);
  1354.           if (*tempb == pawn)
  1355.             --PawnCnt[*tempc][column (t)];
  1356.           if (board[f] == pawn)
  1357.             {
  1358.               --PawnCnt[side][column (f)];
  1359.               ++PawnCnt[side][column (t)];
  1360.               cf = column (f);
  1361.               ct = column (t);
  1362.               if (PawnCnt[side][ct] > 1 + PawnCnt[side][cf])
  1363.                 *INCscore -= 15;
  1364.               else if (PawnCnt[side][ct] < 1 + PawnCnt[side][cf])
  1365.                 *INCscore += 15;
  1366.               else if (ct == 0 || ct == 7 || PawnCnt[side][ct + ct - cf] == 0)
  1367.                 *INCscore -= 15;
  1368.             }
  1369.           mtl[xside] -= value[*tempb];
  1370.           if (*tempb == pawn)
  1371.             pmtl[xside] -= valueP;
  1372. #if ttblsz
  1373.           UpdateHashbd (xside, *tempb, -1, t);
  1374. #endif /* ttblsz */
  1375.           *INCscore += *tempst;
  1376.           Mvboard[t]++;
  1377.         }
  1378.       color[t] = color[f];
  1379.       board[t] = board[f];
  1380.       svalue[t] = svalue[f];
  1381.       Pindex[t] = Pindex[f];
  1382.       PieceList[side][Pindex[t]] = t;
  1383.       color[f] = neutral;
  1384.       board[f] = no_piece;
  1385.       if (board[t] == pawn)
  1386.         if (t - f == 16)
  1387.           epsquare = f + 8;
  1388.         else if (f - t == 16)
  1389.           epsquare = f - 8;
  1390.       if (node->flags & promote)
  1391.         {
  1392.           board[t] = node->flags & pmask;
  1393.           if (board[t] == queen)
  1394.             HasQueen[side]++;
  1395.           else if (board[t] == rook)
  1396.             HasRook[side]++;
  1397.           else if (board[t] == bishop)
  1398.             HasBishop[side]++;
  1399.           else if (board[t] == knight)
  1400.             HasKnight[side]++;
  1401.           --PawnCnt[side][column (t)];
  1402.           mtl[side] += value[board[t]] - valueP;
  1403.           pmtl[side] -= valueP;
  1404. #if ttblsz
  1405.           UpdateHashbd (side, pawn, f, -1);
  1406.           UpdateHashbd (side, board[t], f, -1);
  1407. #endif /* ttblsz */
  1408.           *INCscore -= *tempsf;
  1409.         }
  1410.       if (node->flags & epmask)
  1411.         EnPassant (xside, f, t, 1);
  1412.       else
  1413. #if ttblsz
  1414.         UpdateHashbd (side, board[t], f, t);
  1415. #else
  1416.         /* NOOP */;     
  1417. #endif /* ttblsz */
  1418.       Mvboard[f]++;
  1419.     }
  1420. }
  1421.  
  1422. void
  1423. UnmakeMove (short int side,
  1424.             struct leaf far * node,
  1425.             short int *tempb,
  1426.             short int *tempc,
  1427.             short int *tempsf,
  1428.             short int *tempst)
  1429.  
  1430. /*
  1431.   Take back a move.
  1432. */
  1433.  
  1434. {
  1435.   short f, t, xside;
  1436.  
  1437.   xside = otherside[side];
  1438.   f = node->f;
  1439.   t = node->t;
  1440.   epsquare = -1;
  1441.   GameCnt--;
  1442.   if (node->flags & cstlmask)
  1443.     (void) castle (side, f, t, 2);
  1444.   else
  1445.     {
  1446.       color[f] = color[t];
  1447.       board[f] = board[t];
  1448.       svalue[f] = *tempsf;
  1449.       Pindex[f] = Pindex[t];
  1450.       PieceList[side][Pindex[f]] = f;
  1451.       color[t] = *tempc;
  1452.       board[t] = *tempb;
  1453.       svalue[t] = *tempst;
  1454.       if (node->flags & promote)
  1455.         {
  1456.           board[f] = pawn;
  1457.           ++PawnCnt[side][column (t)];
  1458.           mtl[side] += valueP - value[node->flags & pmask];
  1459.           pmtl[side] += valueP;
  1460. #if ttblsz
  1461.           UpdateHashbd (side, (short) node->flags & pmask, -1, t);
  1462.           UpdateHashbd (side, pawn, -1, t);
  1463. #endif /* ttblsz */
  1464.         }
  1465.       if (*tempc != neutral)
  1466.         {
  1467.           UpdatePieceList (*tempc, t, 2);
  1468.           if (*tempb == pawn)
  1469.             ++PawnCnt[*tempc][column (t)];
  1470.           if (board[f] == pawn)
  1471.             {
  1472.               --PawnCnt[side][column (t)];
  1473.               ++PawnCnt[side][column (f)];
  1474.             }
  1475.           mtl[xside] += value[*tempb];
  1476.           if (*tempb == pawn)
  1477.             pmtl[xside] += valueP;
  1478. #if ttblsz
  1479.           UpdateHashbd (xside, *tempb, -1, t);
  1480. #endif /* ttblsz */
  1481.           Mvboard[t]--;
  1482.         }
  1483.       if (node->flags & epmask)
  1484.         EnPassant (xside, f, t, 2);
  1485.       else
  1486. #if ttblsz
  1487.         UpdateHashbd (side, board[f], f, t);
  1488. #else
  1489.       /* NOOP */;
  1490. #endif /* ttblsz */
  1491.       Mvboard[f]--;
  1492.       if (!(node->flags & capture) && (board[f] != pawn))
  1493.         rpthash[side][hashkey & 0xFF]--;
  1494.     }
  1495. }
  1496.  
  1497.  
  1498. void
  1499. InitializeStats (void)
  1500.  
  1501. /*
  1502.   Scan thru the board seeing what's on each square. If a piece is found,
  1503.   update the variables PieceCnt, PawnCnt, Pindex and PieceList. Also
  1504.   determine the material for each side and set the hashkey and hashbd
  1505.   variables to represent the current board position. Array
  1506.   PieceList[side][indx] contains the location of all the pieces of either
  1507.   side. Array Pindex[sq] contains the indx into PieceList for a given
  1508.   square.
  1509. */
  1510.  
  1511. {
  1512.   short i, sq;
  1513.   
  1514.   epsquare = -1;
  1515.   for (i = 0; i < 8; i++)
  1516.     PawnCnt[white][i] = PawnCnt[black][i] = 0;
  1517.   mtl[white] = mtl[black] = pmtl[white] = pmtl[black] = 0;
  1518.   PieceCnt[white] = PieceCnt[black] = 0;
  1519. #if ttblsz
  1520.   hashbd = hashkey = 0;
  1521. #endif /* ttblsz */
  1522.   for (sq = 0; sq < 64; sq++)
  1523.     if (color[sq] != neutral)
  1524.       {
  1525.         mtl[color[sq]] += value[board[sq]];
  1526.         if (board[sq] == pawn)
  1527.           {
  1528.             pmtl[color[sq]] += valueP;
  1529.             ++PawnCnt[color[sq]][column (sq)];
  1530.           }
  1531.         if (board[sq] == king)
  1532.           Pindex[sq] = 0;
  1533.         else
  1534.           Pindex[sq] = ++PieceCnt[color[sq]];
  1535.         PieceList[color[sq]][Pindex[sq]] = sq;
  1536. #if ttblsz
  1537.         hashbd ^= (hashcode+color[sq]*7*64+board[sq]*64+sq)->bd;
  1538.         hashkey ^= (hashcode+color[sq]*7*64+board[sq]*64+sq)->key;
  1539. #endif /* ttblsz */
  1540.       }
  1541. }
  1542.  
  1543.  
  1544. int
  1545. SqAtakd (short int sq, short int side)
  1546.  
  1547. /*
  1548.   See if any piece with color 'side' ataks sq.  First check pawns then Queen,
  1549.   Bishop, Rook and King and last Knight.
  1550. */
  1551.  
  1552. {
  1553.   register short u;
  1554.   unsigned char far *ppos, far *pdir;
  1555.   short xside;
  1556.  
  1557.   xside = otherside[side];
  1558.   pdir = nextdir+ptype[xside][pawn]*64*64+sq*64;
  1559.   u = pdir[sq];         /* follow captures thread */
  1560.   if (u != sq)
  1561.     {
  1562.       if (board[u] == pawn && color[u] == side)
  1563.         return (true);
  1564.       u = pdir[u];
  1565.       if (u != sq && board[u] == pawn && color[u] == side)
  1566.         return (true);
  1567.     }
  1568.   /* king capture */
  1569.   if (distance (sq, PieceList[side][0]) == 1)
  1570.     return (true);
  1571.   /* try a queen bishop capture */
  1572.   ppos = nextpos+bishop*64*64+sq*64;
  1573.   pdir = nextdir+bishop*64*64+sq*64;
  1574.   u = ppos[sq];
  1575.   do
  1576.     {
  1577.       if (color[u] == neutral)
  1578.         u = ppos[u];
  1579.       else
  1580.         {
  1581.           if (color[u] == side &&
  1582.               (board[u] == queen || board[u] == bishop))
  1583.             return (true);
  1584.           u = pdir[u];
  1585.         }
  1586.   } while (u != sq);
  1587.   /* try a queen rook capture */
  1588.   ppos = nextpos+rook*64*64+sq*64;
  1589.   pdir = nextdir+rook*64*64+sq*64;
  1590.   u = ppos[sq];
  1591.   do
  1592.     {
  1593.       if (color[u] == neutral)
  1594.         u = ppos[u];
  1595.       else
  1596.         {
  1597.           if (color[u] == side &&
  1598.               (board[u] == queen || board[u] == rook))
  1599.             return (true);
  1600.           u = pdir[u];
  1601.         }
  1602.   } while (u != sq);
  1603.   /* try a knight capture */
  1604.   pdir = nextdir+knight*64*64+sq*64;
  1605.   u = pdir[sq];
  1606.   do
  1607.     {
  1608.       if (color[u] == side && board[u] == knight)
  1609.         return (true);
  1610.       u = pdir[u];
  1611.   } while (u != sq);
  1612.   return (false);
  1613. }
  1614.  
  1615. void
  1616. ataks (short int side, short int *a)
  1617.  
  1618. /*
  1619.   Fill array atak[][] with info about ataks to a square.  Bits 8-15 are set
  1620.   if the piece (king..pawn) ataks the square.  Bits 0-7 contain a count of
  1621.   total ataks to the square.
  1622. */
  1623.  
  1624. {
  1625.   short u, c, sq;
  1626.   unsigned char far *ppos, far *pdir;
  1627.   short i, piece, *PL;
  1628.  
  1629. #ifdef NOMEMSET
  1630.   for (u = 64; u; a[--u] = 0) ;
  1631. #else
  1632.   memset ((char *) a, 0, 64 * sizeof (a[0]));
  1633. #endif /* NOMEMSET */
  1634.   PL = PieceList[side];
  1635.   for (i = PieceCnt[side]; i >= 0; i--)
  1636.     {
  1637.       sq = PL[i];
  1638.       piece = board[sq];
  1639.       c = control[piece];
  1640.       if (sweep[piece])
  1641.         {
  1642.           ppos = nextpos+piece*64*64+sq*64;
  1643.           pdir = nextdir+piece*64*64+sq*64;
  1644.           u = ppos[sq];
  1645.           do
  1646.             {
  1647.               a[u] = ++a[u] | c;
  1648.               u = (color[u] == neutral) ? ppos[u] : pdir[u];
  1649.           } while (u != sq);
  1650.         }
  1651.       else
  1652.         {
  1653.           pdir = nextdir+ptype[side][piece]*64*64+sq*64;
  1654.           u = pdir[sq]; /* follow captures thread for pawns */
  1655.           do
  1656.             {
  1657.               a[u] = ++a[u] | c;
  1658.               u = pdir[u];
  1659.           } while (u != sq);
  1660.         }
  1661.     }
  1662. }
  1663.  
  1664.