home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuch40.zip / gnuchess-4.0.pl79 / src / dspcom.c < prev    next >
C/C++ Source or Header  |  1999-01-16  |  41KB  |  1,619 lines

  1. /*
  2.  * dspcom.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1985-1996 Stuart Cracraft, John Stanback,
  5.  *                         Daryl Baker, Conor McCarthy,
  6.  *                         Mike McGann, Chua Kong Sian
  7.  * Copyright (c) 1985-1996 Free Software Foundation
  8.  *
  9.  * This file is part of GNU CHESS.
  10.  *
  11.  * GNU Chess is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2, or (at your option)
  14.  * any later version.
  15.  *
  16.  * GNU Chess is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with GNU Chess; see the file COPYING.  If not, write to
  23.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. #ifdef WIN32
  27. #define SHORT WIN_SHORT
  28. #include <windows.h>
  29. #undef SHORT
  30. #endif
  31. #include "gnuchess.h"
  32. #include "ataks.h"
  33. #include "ttable.h" /* calls ZeroTTable(), probably belongs in main.c*/
  34. #ifdef HAVE_GETTIMEOFDAY
  35. #include <sys/time.h>
  36. #endif
  37. CHAR mvstr[5][7];
  38. #if defined(Think_C) && defined(Window_Events)
  39. #include "doevents.h"
  40. #endif
  41. extern SHORT Mwpawn[64], Mbpawn[64], Mknight[2][64], Mbishop[2][64];
  42. extern CHAR *version, *patchlevel;
  43. extern void LOpeningBook();
  44. #ifdef ECO
  45. extern void EOpeningBook();
  46. SHORT ecomove;
  47. #endif
  48. CHAR *InPtr;
  49. #if !defined(AMIGADOS) && !defined(Think_C) && !defined(WIN32)
  50. #include <unistd.h>
  51. #endif
  52.  
  53. #if defined(sun) && defined(sparc) && defined(svr4)
  54. #include <sys/filio.h>
  55. #endif
  56. #include <ctype.h>
  57. #include <signal.h>
  58. #if defined(MSDOS)
  59. #include <dos.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #else
  63. #ifdef Think_C
  64. #include "doevents.h"
  65. #else
  66. #ifdef WIN32
  67. #include <sys/types.h>
  68. #else /* Other */
  69. #include <sys/param.h>
  70. #include <sys/types.h>
  71. #include <sys/file.h>
  72. #ifndef AMIGADOS
  73. #include <sys/ioctl.h>
  74. #endif
  75. #endif
  76. #endif /* Think_C */
  77. #endif /* MSDOS */
  78.  
  79. void
  80. algbr (SHORT f, SHORT t, SHORT flag)
  81.  
  82.  
  83. /*
  84.  * Generate move strings in different formats.
  85.  */
  86.  
  87. {
  88.   int m3p;
  89.  
  90.   if (f != t)
  91.     {
  92.       /* algebraic notation */
  93.       mvstr[0][0] = cxx[column (f)];
  94.       mvstr[0][1] = rxx[row (f)];
  95.       mvstr[0][2] = cxx[column (t)];
  96.       mvstr[0][3] = rxx[row (t)];
  97.       mvstr[4][0] = mvstr[0][4] = mvstr[3][0] = '\0';
  98.       if (((mvstr[1][0] = pxx[board[f]]) == CP[2][1]) || (flag & promote))
  99.     {
  100.       if (mvstr[0][0] == mvstr[0][2])    /* pawn did not eat */
  101.         {
  102.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  103.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  104.           m3p = 2;
  105.         }
  106.       else
  107.         /* pawn ate */
  108.         {
  109.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* column */
  110.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  111.           mvstr[2][2] = mvstr[0][3];
  112.           m3p = 3;        /* to row */
  113.         }
  114.       if (flag & promote)
  115.         {
  116.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = qxx[flag & pmask];
  117.           mvstr[0][5] = mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[3][0] = '\0';
  118. #ifdef CHESSTOOL 
  119.           mvstr[3][0] = mvstr[0][0];    /* Allow e7e8 for chesstool */
  120.           mvstr[3][1] = mvstr[0][1];
  121.           mvstr[3][2] = mvstr[0][2];
  122.           mvstr[3][3] = mvstr[0][3];
  123.           mvstr[3][4] = '\0';
  124. #endif
  125.         } else mvstr[2][m3p] = mvstr[1][2] = '\0';
  126.     }
  127.       else
  128.     /* not a pawn */
  129.     {
  130.       mvstr[2][0] = mvstr[1][0];
  131.       mvstr[2][1] = mvstr[0][1];
  132.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  133.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  134.       mvstr[2][4] = mvstr[1][3] = '\0';
  135.       strcpy (mvstr[3], mvstr[2]);
  136.           mvstr[3][1] = mvstr[0][0];
  137.       mvstr[4][0] = mvstr[1][0]; strcpy(&mvstr[4][1],mvstr[0]);
  138.       if (flag & cstlmask)
  139.         {
  140.           if (t > f)
  141.         {
  142.           strcpy (mvstr[1], mvstr[0]);
  143.           strcpy (mvstr[0], CP[5]);
  144.           strcpy (mvstr[2], CP[7]);
  145.         }
  146.           else
  147.         {
  148.           strcpy (mvstr[1], mvstr[0]);
  149.           strcpy (mvstr[0], CP[6]);
  150.           strcpy (mvstr[2], CP[8]);
  151.         }
  152.         }
  153.     }
  154.     }
  155.   else
  156.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = mvstr[4][0] = '\0';
  157. }
  158.  
  159.  
  160. int
  161. VerifyMove (CHAR *s, SHORT iop, UTSHORT *mv)
  162.  
  163. /*
  164.  * Compare the string 's' to the list of legal moves available for the
  165.  * opponent. If a match is found, make the move on the board.
  166.  */
  167.  
  168. {
  169.   static SHORT pnt, tempb, tempc, tempsf, tempst, cnt;
  170.   static struct leaf xnode;
  171.   struct leaf *node;
  172.   *mv = 0;
  173.  
  174.   if (iop == 2)
  175.     {
  176.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  177.       return (false);
  178.     }
  179.   cnt = 0;
  180.   VMoveList (opponent, 2);
  181.   pnt = TrPnt[2];
  182.   while (pnt < TrPnt[3])
  183.     {
  184.       node = &Tree[pnt++];
  185.       algbr (node->f, node->t, (SHORT) node->flags);
  186.       if (strcmp (s, mvstr[0]) == 0 || strcmp (s, mvstr[1]) == 0 ||
  187.       strcmp (s, mvstr[2]) == 0 || strcmp (s, mvstr[3]) == 0
  188.         || strcmp (s, mvstr[4]) == 0)
  189.     {
  190.       cnt++;
  191.       xnode = *node;
  192.     }
  193.     }
  194.   if (cnt == 1)
  195.     {
  196.       MakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  197.       if (SqAtakd (PieceList[opponent][0], computer))
  198.     {
  199.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  200. #if defined CHESSTOOL
  201.       printz (CP[78]);
  202. #else
  203. #ifdef NONDSP
  204. /* Illegal move in check */
  205.       printz (CP[77]);
  206.       printz ("\n");
  207. #ifdef DEBUG8
  208.   if (1)
  209.     {   
  210.       FILE *D;
  211.       int r, c, l;
  212.       extern UTSHORT PrVar[];
  213.       D = fopen ("DEBUG", "a+");
  214.       pnt = TrPnt[2];
  215.       fprintf (D, "resp = %ld\n", ResponseTime);
  216.       fprintf (D, "iop = %d\n", iop);
  217.       fprintf (D, "matches = %d\n", cnt);
  218.       algbr (hint >> 8, hint & 0xff, (SHORT) 0);
  219.       fprintf (D, "hint %s\n", mvstr[0]);
  220.       fprintf (D, "inout move is %s\n", s);
  221.       for (r = 1; PrVar[r]; r++)
  222.         {
  223.           algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (SHORT) 0);
  224.           fprintf (D, " %s", mvstr[0]);
  225.         }
  226.       fprintf (D, "\n");
  227.       fprintf (D, "legal move are \n");
  228.       while (pnt < TrPnt[3])
  229.         {
  230.           node = &Tree[pnt++];
  231.           algbr (node->f, node->t, (SHORT) node->flags);
  232.           fprintf (D, "%s %s %s %s %s\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],mvstr[4]);
  233.         } 
  234.       fprintf (D, "\n current board is\n");
  235.       for (r = 7; r >= 0; r--)
  236.         {
  237.           for (c = 0; c <= 7; c++)
  238.             {
  239.               l = locn (r, c);
  240.               if (color[l] == neutral)
  241.                 fprintf (D, " -");
  242.               else if (color[l] == white)
  243.                 fprintf (D, " %c", qxx[board[l]]);
  244.               else
  245.                 fprintf (D, " %c", pxx[board[l]]);
  246.             }
  247.           fprintf (D, "\n");
  248.         }
  249.       fprintf (D, "\n");
  250.       fclose (D);
  251.     }  
  252. #endif 
  253.  
  254. #else
  255. /* Illegal move in check */
  256.       ShowMessage (CP[77]);
  257. #endif
  258. #endif /* CHESSTOOL */
  259.       return (false);
  260.     }
  261.       else
  262.     {
  263.       if (iop == 1)
  264.         return (true);
  265.       UpdateDisplay (xnode.f, xnode.t, 0, (SHORT) xnode.flags);
  266.       if ((board[xnode.t] == pawn)
  267.           || (xnode.flags & capture)
  268.           || (xnode.flags & cstlmask))
  269.         {
  270.           Game50 = GameCnt;
  271.           ZeroRPT ();
  272.         }
  273.       GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  274.       GameList[GameCnt].nodes = 0;
  275.       ElapsedTime (1);
  276.       GameList[GameCnt].time = (SHORT) (et+50)/100;
  277.       if (TCflag)
  278.         {
  279.           TimeControl.clock[opponent] -= (et-TCadd);
  280.           timeopp[oppptr] = et;
  281.           --TimeControl.moves[opponent];
  282.         }
  283.       *mv = (xnode.f << 8) | xnode.t;
  284.       algbr (xnode.f, xnode.t, false);
  285.       return (true);
  286.     }
  287.     }
  288. #if defined CHESSTOOL
  289.   printz (CP[78]);
  290. #else
  291. #ifdef NONDSP
  292. /* Illegal move */
  293.   printz (CP[75], s);
  294. #ifdef DEBUG8
  295.   if (1)
  296.     {
  297.       FILE *D;
  298.       int r, c, l;
  299.       extern UTSHORT PrVar[];
  300.       D = fopen ("DEBUG", "a+");
  301.       pnt = TrPnt[2];
  302.       fprintf (D, "resp = %ld\n", ResponseTime);
  303.       fprintf (D, "iop = %d\n", iop);
  304.       fprintf (D, "matches = %d\n", cnt);
  305.       algbr (hint >> 8, hint & 0xff, (SHORT) 0);
  306.       fprintf (D, "hint %s\n", mvstr[0]);
  307.       fprintf (D, "inout move is %s\n", s);
  308.       for (r = 1; PrVar[r]; r++)
  309.     {
  310.       algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (SHORT) 0);
  311.       fprintf (D, " %s", mvstr[0]);
  312.     }
  313.       fprintf (D, "\n");
  314.       fprintf (D, "legal move are \n");
  315.       while (pnt < TrPnt[3])
  316.     {
  317.       node = &Tree[pnt++];
  318.       algbr (node->f, node->t, (SHORT) node->flags);
  319.       fprintf (D, "%s %s %s %s %s\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],mvstr[4]);
  320.     }
  321.       fprintf (D, "\n current board is\n");
  322.       for (r = 7; r >= 0; r--)
  323.     {
  324.       for (c = 0; c <= 7; c++)
  325.         {
  326.           l = locn (r, c);
  327.           if (color[l] == neutral)
  328.         fprintf (D, " -");
  329.           else if (color[l] == white)
  330.         fprintf (D, " %c", qxx[board[l]]);
  331.           else
  332.         fprintf (D, " %c", pxx[board[l]]);
  333.         }
  334.       fprintf (D, "\n");
  335.     }
  336.       fprintf (D, "\n");
  337.       fclose (D);
  338.     }
  339. #endif
  340. #else
  341. /* Illegal move */
  342.   ShowMessage (CP[76]);
  343. #endif
  344. #endif /* CHESSTOOL */
  345. #if !defined CHESSTOOL && !defined XBOARD
  346.   if (cnt > 1)
  347.     ShowMessage (CP[32]);
  348. #endif /* CHESSTOOL */
  349.   return (false);
  350. }
  351.  
  352. int
  353. parser (CHAR *f, int side)
  354. {
  355.   int c1, r1, c2, r2;
  356.  
  357.   if (f[4] == 'o')
  358.     if (side == black)
  359.       return 0x3C3A;
  360.     else
  361.       return 0x0402;
  362.   else if (f[0] == 'o')
  363.     if (side == black)
  364.       return 0x3C3E;
  365.     else
  366.       return 0x0406;
  367.   else
  368.     {
  369.       c1 = f[0] - 'a';
  370.       r1 = f[1] - '1';
  371.       c2 = f[2] - 'a';
  372.       r2 = f[3] - '1';
  373.       return (locn (r1, c1) << 8) | locn (r2, c2);
  374.     }
  375.   /*NOTREACHED*/
  376. }
  377.  
  378. void
  379. GetGame (void)
  380. {
  381.   FILE *fd;
  382.   CHAR fname[256], *p;
  383.   int c, i, j;
  384.   int eps = -1;
  385.   SHORT sq;
  386. /* enter file name */
  387.   ShowMessage (CP[63]);
  388.   scanz ("%s", fname);
  389. /* chess.000 */
  390.   if (fname[0] == '\0')
  391.     strcpy (fname, CP[137]);
  392.   if ((fd = fopen (fname, "r")) != NULL)
  393.     {
  394.       NewGame ();
  395.       fgets (fname, 256, fd);
  396.       computer = opponent = white;
  397.       InPtr = fname;
  398.       skip ();
  399.       if (*InPtr == 'c')
  400.     computer = black;
  401.       else
  402.     opponent = black;
  403.       skip ();
  404.       skip ();
  405.       skip ();
  406.       Game50 = atoi (InPtr);
  407.       skip();
  408.       skip();
  409.       eps = atoi(InPtr);
  410.       fgets (fname, 256, fd);
  411.       InPtr = &fname[14];
  412.       castld[white] = ((*InPtr == CP[214][0]) ? true : false);
  413.       skip ();
  414.       skip ();
  415.       castld[black] = ((*InPtr == CP[214][0]) ? true : false);
  416.       fgets (fname, 256, fd);
  417.       InPtr = &fname[11];
  418.       skipb ();
  419.       TCflag = atoi (InPtr);
  420.       skip ();
  421.       InPtr += 14;
  422.       skipb ();
  423.       OperatorTime = atoi (InPtr);
  424.       fgets (fname, 256, fd);
  425.       InPtr = &fname[11];
  426.       skipb ();
  427.       TimeControl.clock[white] = atol (InPtr);
  428.       skip ();
  429.       skip ();
  430.       TimeControl.moves[white] = atoi (InPtr);
  431.       fgets (fname, 256, fd);
  432.       InPtr = &fname[11];
  433.       skipb ();
  434.       TimeControl.clock[black] = atol (InPtr);
  435.       skip ();
  436.       skip ();
  437.       TimeControl.moves[black] = atoi (InPtr);
  438.       fgets (fname, 256, fd);
  439.       for (i = 7; i > -1; i--)
  440.     {
  441.       fgets (fname, 256, fd);
  442.       p = &fname[2];
  443.       InPtr = &fname[11];
  444.       skipb ();
  445.       for (j = 0; j < 8; j++)
  446.         {
  447.           sq = i * 8 + j;
  448.           if (*p == '.')
  449.         {
  450.           board[sq] = no_piece;
  451.           color[sq] = neutral;
  452.         }
  453.           else
  454.         {
  455.           for (c = 0; c < 8; c++)
  456.             {
  457.               if (*p == pxx[c])
  458.             {
  459.               board[sq] = c;
  460.               color[sq] = black;
  461.             }
  462.             }
  463.           for (c = 0; c < 8; c++)
  464.             {
  465.               if (*p == qxx[c])
  466.             {
  467.               board[sq] = c;
  468.               color[sq] = white;
  469.             }
  470.             }
  471.         }
  472.           p++;
  473.           Mvboard[sq] = atoi (InPtr);
  474.           skip ();
  475.         }
  476.     }
  477.       GameCnt = 0;
  478.       flag.regularstart = true;
  479.       Book = BOOKFAIL;
  480.       fgets (fname, 256, fd);
  481.       fgets (fname, 256, fd);
  482.       fgets (fname, 256, fd);
  483.       while (fgets (fname, 256, fd))
  484.     {
  485.       struct GameRec *g;
  486.       int side = black;
  487.  
  488.       ++GameCnt;
  489.       InPtr = fname;
  490.       skipb ();
  491.       g = &GameList[GameCnt];
  492.       side = side ^ 1;
  493.       g->gmove = parser (InPtr, side);
  494.       skip ();
  495.       g->score = atoi (InPtr);
  496.       skip ();
  497.       g->depth = atoi (InPtr);
  498.       skip ();
  499.       g->nodes = atol (InPtr);
  500.       skip ();
  501.       g->time = atol (InPtr);
  502.       skip ();
  503.       g->flags = c = atoi (InPtr);
  504.       skip ();
  505. #ifdef HAVE_STRTOUL
  506.       g->hashkey = strtoul (InPtr, (CHAR **) NULL, 16);
  507.       skip ();
  508.       g->hashbd = strtoul (InPtr, (CHAR **) NULL, 16);
  509. #else
  510.       g->hashkey = strtol (InPtr, (CHAR **) NULL, 16);
  511.       skip ();
  512.       g->hashbd = strtol (InPtr, (CHAR **) NULL, 16);
  513. #endif
  514.       skip ();
  515.       g->epssq = atoi(InPtr);
  516.       g->piece = no_piece;
  517.       g->color = neutral;
  518.       if (c & (capture | cstlmask))
  519.         {
  520.           if (c & capture)
  521.         {
  522.           skip ();
  523.           for (c = 0; c < 8; c++)
  524.             if (pxx[c] == *InPtr)
  525.               break;
  526.           g->piece = c;
  527.         }
  528.           skip ();
  529.           g->color = ((*InPtr == CP[119][0]) ? black : white);
  530.         }
  531.     }
  532. /*      if (TimeControl.clock[white] > 0)
  533.     TCflag = true; */
  534.       if (TCflag == false)
  535.         MaxResponseTime = TimeControl.clock[white];
  536.       fclose (fd);
  537.     }
  538.   ZeroRPT ();
  539.   InitializeStats ();
  540.   epsquare = eps;
  541.   UpdateDisplay (0, 0, 1, 0);
  542.   Sdepth = 0;
  543.   hint = 0;
  544. }
  545.  
  546. void
  547. GetXGame (void)
  548. {
  549.   FILE *fd;
  550.   CHAR fname[256], *p;
  551.   int  i, j;
  552.   SHORT emptycount;
  553. /* Enter file name */
  554.   ShowMessage (CP[63]);
  555.   scanz ("%s", fname);
  556.   if (fname[0] == '\0')
  557. /* xboard.position.read*/
  558.     strcpy (fname, CP[205]);
  559.   if ((fd = fopen (fname, "r")) != NULL)
  560.     {
  561.       NewGame ();
  562.       flag.regularstart = false;
  563.       Book = false;
  564.       fgets (fname, 256, fd);
  565.     p = fname;
  566.  
  567.     /* Piece placement data */
  568.     for (i = 8 - 1; i >= 0; i--) {
  569.         j = 0;
  570.         while (j < 8) {
  571.             if (isdigit(*p)) {
  572.                 emptycount = *p++ - '0';
  573.                 if (j + emptycount > 8) return ;
  574.                 while (emptycount--) {board[(i)*8 + j] = no_piece;color[(i)*8 + j++] = neutral;}
  575.             } else {
  576.         int c;
  577.         int sq = (i)*8 + j++;
  578.         for (c = 0; c < 8; c++)
  579.                     {
  580.                       if (*p == qxx[c])
  581.                         {
  582.                           board[sq] = c;
  583.                           color[sq] = black;
  584.                         }
  585.                     }    
  586.                   for (c = 0; c < 8; c++)
  587.                     {
  588.                       if (*p == pxx[c])
  589.                         {
  590.                           board[sq] = c;
  591.                           color[sq] = white;
  592.                         }
  593.                     }    
  594.             p++;
  595.             }   
  596.         }
  597.         if (*p != '/' && *p != ' ') return ;
  598.         p++;
  599.     }   
  600.  
  601.     /* Active color */
  602.     switch (*p) {
  603.       case 'w':
  604.         computer = white;
  605.         break;
  606.       case 'b':
  607.         computer = black;
  608.         break;
  609.       default:
  610.         return ;
  611.     }
  612.        
  613.     /* !!We ignore the rest of the FEN notation */
  614.  
  615.  
  616.           xwndw = BXWNDW;
  617.     }
  618.       fclose (fd);
  619.   ZeroRPT ();
  620.   InitializeStats ();
  621.   UpdateDisplay (0, 0, 1, 0);
  622.   Sdepth = 0;
  623.   hint = 0;
  624. }
  625.  
  626. void
  627. SaveGame (void)
  628. {
  629.   FILE *fd;
  630.   CHAR fname[256];
  631.   SHORT sq, i, c, f, t;
  632.   CHAR p;
  633.  
  634.   if (savefile[0])
  635.     strcpy (fname, savefile);
  636.   else
  637.     {
  638. /* Enter file name*/
  639.       ShowMessage (CP[63]);
  640.       scanz ("%s", fname);
  641.     }
  642.  
  643.   if (fname[0] == '\0')
  644. /* chess.000 */
  645.     strcpy (fname, CP[137]);
  646.   if ((fd = fopen (fname, "w")) != NULL)
  647.     {
  648.       CHAR *b, *w;
  649.  
  650.       b = w = CP[74];
  651.       if (computer == black)
  652.     b = CP[141];
  653.       if (computer == white)
  654.     w = CP[141];
  655.       fprintf (fd, CP[37], b, w, Game50,epsquare);
  656.       fprintf (fd, CP[42], castld[white] ? CP[214] : CP[215], castld[black] ? CP[214] : CP[215]);
  657.       fprintf (fd, CP[111], TCflag, OperatorTime);
  658.       fprintf (fd, CP[117],
  659.            TimeControl.clock[white], TimeControl.moves[white],
  660.            TimeControl.clock[black], TimeControl.moves[black]);
  661.       for (i = 7; i > -1; i--)
  662.     {
  663.       fprintf (fd, "%1d ", i + 1);
  664.       for (c = 0; c < 8; c++)
  665.         {
  666.           sq = i * 8 + c;
  667.           switch (color[sq])
  668.         {
  669.         case black:
  670.           p = pxx[board[sq]];
  671.           break;
  672.         case white:
  673.           p = qxx[board[sq]];
  674.           break;
  675.         default:
  676.           p = '.';
  677.         }
  678.           fprintf (fd, "%c", p);
  679.         }
  680.       for (f = i * 8; f < i * 8 + 8; f++)
  681.         fprintf (fd, " %d", Mvboard[f]);
  682.       fprintf (fd, "\n");
  683.     }
  684.       fprintf (fd, "  %s\n", cxx);
  685.       fprintf (fd, CP[126]);
  686.       for (i = 1; i <= GameCnt; i++)
  687.     {
  688.       struct GameRec *g = &GameList[i];
  689.  
  690.       f = g->gmove >> 8;
  691.       t = (g->gmove & 0xFF);
  692.       algbr (f, t, g->flags);
  693.       fprintf (fd, "%s %5d %5d %7ld %6ld %5d  %#08lx %#08lx %d %c   %s\n",
  694.            mvstr[0], g->score, g->depth,
  695.            g->nodes, g->time, g->flags, g->hashkey, g->hashbd,g->epssq,
  696.        pxx[g->piece], (((int)(g->color) == 2) ? (CHAR *)"     " : ColorStr[g->color]));
  697.     }
  698.       fclose (fd);
  699. /* Game saved */
  700.       ShowMessage (CP[70]);
  701.     }
  702.   else
  703.     /*ShowMessage ("Could not open file");*/
  704.     ShowMessage (CP[48]);
  705. }
  706.  
  707. void
  708. ListGame (void)
  709. {
  710.   FILE *fd;
  711.   SHORT i, f, t;
  712. #ifndef MSDOS
  713.   time_t when;
  714.   CHAR fname[256], dbuf[256];
  715. #else
  716.   CHAR fname[256];
  717. #endif
  718.  
  719.   if (listfile[0])
  720.     strcpy (fname, listfile);
  721.   else
  722.     {
  723. #ifdef MSDOS
  724.       sprintf (fname, "chess.lst");
  725. #else
  726.       time (&when);
  727.       strncpy (dbuf, ctime (&when), 20);
  728.       dbuf[7] = '\0';
  729.       dbuf[10] = '\0';
  730.       dbuf[13] = '\0';
  731.       dbuf[16] = '\0';
  732.       dbuf[19] = '\0';
  733. /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
  734.    date is Jan 1
  735.    time is 02:03:04
  736.    program played black */
  737.       sprintf (fname, "CLp%s.%s%s-%s%s%s%c", patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14, dbuf + 17, ColorStr[computer][0]);
  738.       /* replace space padding with 0 */
  739.       for (i = 0; fname[i] != '\0'; i++)
  740.     if (fname[i] == ' ')
  741.       fname[i] = '0';
  742. #endif /* MSDOS */
  743.     }
  744.   fd = fopen (fname, "w");
  745.   if (!fd)
  746.     {
  747.       printf (CP[219], fname);
  748.       exit (1);
  749.     }
  750.   /*fprintf (fd, "gnuchess game %d\n", u);*/
  751.   fprintf (fd, CP[161], patchlevel);
  752.   fprintf (fd, CP[10]);
  753.   fprintf (fd, CP[11]);
  754.   for (i = 1; i <= GameCnt; i++)
  755.     {
  756.       f = GameList[i].gmove >> 8;
  757.       t = (GameList[i].gmove & 0xFF);
  758.       algbr (f, t, GameList[i].flags);
  759.       if(GameList[i].flags & book)
  760.           fprintf (fd, "%6s  %5d    Book%7ld %5ld", mvstr[0],
  761.            GameList[i].score, 
  762.            GameList[i].nodes, GameList[i].time);
  763.       else
  764.           fprintf (fd, "%6s  %5d     %2d %7ld %5ld", mvstr[0],
  765.            GameList[i].score, GameList[i].depth,
  766.            GameList[i].nodes, GameList[i].time);
  767.       if ((i % 2) == 0)
  768.     {
  769. #ifdef DEBUG40
  770.     if(computer == black){
  771.       int p;
  772.     for(p=0;GameList[i-1].d1[p] && p < 7;p++){
  773.         algbr((GameList[i-1].d1[p]>>8) & 0xff,GameList[i-1].d1[p] & 0xff,0);
  774.             fprintf(fd," %s",mvstr[0]);
  775.     }
  776.     fprintf(fd,"\n");
  777.     } else {
  778.     int p;
  779.     for(p=0;GameList[i].d1[p] && p < 7;p++){
  780.         algbr(GameList[i].d1[p]>>8 & 0xff,GameList[i].d1[p] & 0xff,0);
  781.             fprintf(fd," %s",mvstr[0]);
  782.     }
  783.     fprintf(fd,"\n");
  784.     }
  785. #else
  786.     fprintf(fd,"\n");
  787. #endif
  788.     }
  789.    }
  790.   fprintf (fd, "\n\n");
  791.   if (GameList[GameCnt].flags & draw)
  792.     {
  793.       fprintf (fd, CP[54], DRAW);
  794.     }
  795.   else if (GameList[GameCnt].score == -9999)
  796.     {
  797.       fprintf (fd, "%s\n", ColorStr[player ]);
  798.     }
  799.   else if (GameList[GameCnt].score == 9998)
  800.     {
  801.       fprintf (fd, "%s\n", ColorStr[player ^ 1]);
  802.     }
  803.   fclose (fd);
  804. }
  805.  
  806. void
  807. Undo (void)
  808.  
  809. /*
  810.  * Undo the most recent half-move.
  811.  */
  812.  
  813. {
  814.   SHORT f, t;
  815.   f = GameList[GameCnt].gmove >> 8;
  816.   t = GameList[GameCnt].gmove & 0xFF;
  817.   if (board[t] == king && distance (t, f) > 1)
  818.     (void) castle (GameList[GameCnt].color, f, t, 2);
  819.   else
  820.     {
  821.       /* Check for promotion: */
  822.       if (GameList[GameCnt].flags & promote)
  823.     {
  824.       board[t] = pawn;
  825.     }
  826.       board[f] = board[t];
  827.       color[f] = color[t];
  828.       board[t] = GameList[GameCnt].piece;
  829.       color[t] = GameList[GameCnt].color;
  830.       if (color[t] != neutral)
  831.     Mvboard[t]--;
  832.       Mvboard[f]--;
  833.     }
  834.   if (GameList[GameCnt].flags & epmask)
  835.     EnPassant (otherside[color[f]], f, t, 2);
  836.   else
  837.     InitializeStats ();
  838.   epsquare = GameList[GameCnt].epssq;
  839.   if (TCflag && (TCmoves>1))
  840.     ++TimeControl.moves[color[f]];
  841.   hashkey = GameList[GameCnt].hashkey;
  842.   hashbd = GameList[GameCnt].hashbd;
  843.   GameCnt--;
  844.   computer = computer ^ 1;
  845.   opponent = opponent ^ 1;
  846.   flag.mate = false;
  847.   Sdepth = 0;
  848.   player = player ^ 1;
  849.   ShowSidetoMove ();
  850.   UpdateDisplay (0, 0, 1, 0);
  851. /*
  852.   if (flag.regularstart)
  853. */
  854.     Book = BOOKFAIL;
  855. }
  856.  
  857. SHORT
  858.  TestSpeed (void (*f) (SHORT side, SHORT ply), unsigned j)
  859. {
  860. #ifdef test
  861.   unsigned jj;
  862. #endif
  863.   unsigned i;
  864.   long cnt, rate, t1, t2;
  865. #ifdef HAVE_GETTIMEOFDAY
  866. struct timeval tv;
  867. #endif
  868.  
  869. #ifdef HAVE_GETTIMEOFDAY
  870.   gettimeofday(&tv,NULL);
  871.   t1 = (tv.tv_sec*100+(tv.tv_usec/10000));
  872. #else
  873.   t1 = time (0);
  874. #endif
  875.   for (i = 0; i < j; i++)
  876.     {
  877.       f (opponent, 2);
  878. #ifdef test
  879.     for(jj=TrPnt[2];i<TrPnt[3];jj++)if(!pick(jj,TrPnt[3]-1))break;
  880. #endif
  881.     }
  882. #ifdef HAVE_GETTIMEOFDAY
  883.   gettimeofday(&tv,NULL);
  884.   t2 = (tv.tv_sec*100+(tv.tv_usec/10000));
  885. #else
  886.   t2 = time (0);
  887. #endif
  888.   cnt = j * (TrPnt[3] - TrPnt[2]);
  889.   if (t2 - t1)
  890.     et = (t2 - t1);
  891.   else
  892.     et = 1;
  893.   rate = (((et) ? ((cnt*100) / et) : 0));
  894.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  895. #ifdef NONDSP
  896.   printz (CP[91], cnt, rate);
  897. #ifdef DEBUG9
  898.   for (j = TrPnt[2]; j < TrPnt[3]; j++)
  899.     {
  900.       struct leaf *node = &Tree[j];
  901.       algbr (node->f, node->t, node->flags);
  902.       printf ("%s %s %s %s %d %x\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],node->score,node->flags);
  903.     }
  904. #endif
  905. #else
  906.   ShowNodeCnt (cnt);
  907. #endif
  908.     return(TrPnt[3] - TrPnt[2]);
  909. }
  910.  
  911. void
  912.  TestPSpeed (SHORT (*f) (SHORT side), unsigned j)
  913. {
  914.   SHORT i;
  915.   long cnt, rate, t1, t2;
  916. #ifdef HAVE_GETTIMEOFDAY
  917. struct timeval tv;
  918. #endif
  919.  
  920. #ifdef HAVE_GETTIMEOFDAY
  921.   gettimeofday(&tv,NULL);
  922.   t1 = (tv.tv_sec*100+(tv.tv_usec/10000));
  923. #else
  924.   t1 = time (0);
  925. #endif
  926.   for (i = 0; i < j; i++)
  927.     {
  928.       (void) f (opponent);
  929.     }
  930. #ifdef HAVE_GETTIMEOFDAY
  931.   gettimeofday(&tv,NULL);
  932.   t2 = (tv.tv_sec*100+(tv.tv_usec/10000));
  933. #else
  934.   t2 = time (0);
  935. #endif
  936.   cnt = j;
  937.   if (t2 - t1)
  938.     et = (t2 - t1);
  939.   else
  940.     et = 1;
  941.   rate = (et) ? ((cnt*100) / et) : 0;
  942.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  943. #ifdef NONDSP
  944.   printz (CP[91], cnt, rate);
  945. #else
  946.   ShowNodeCnt (cnt);
  947. #endif
  948. }
  949.  
  950.  
  951. void
  952. SetOppTime (CHAR *s)
  953. {
  954.   CHAR *time;
  955.   register int error, oldet;
  956.   int m, t, sec;
  957.   sec = 0;
  958.   time = &s[strlen (CP[197])];
  959.   t = (int)strtol (time, &time, 10);
  960.    if (*time == ':') {
  961.        /* If the time does not contain a ":", it is in hundredths of
  962.         a second; if it does, it is in minutes:seconds. */
  963.        time++;
  964.        sec = (int)strtol(time, &time, 10);
  965.        t = t*6000 + sec*100;
  966.    }
  967.    /* This is a little tricky because opponent's clock is running.
  968.       TimeControl.clock[opponent] records the time the clock showed
  969.       when we started it running, not the time it should show now.
  970.    */
  971.    ElapsedTime(0);
  972.    error = (t + et) - TimeControl.clock[opponent];
  973.  
  974.    /* Assume our error was in deciding when to start the opponent's
  975.       clock, not in the time it showed at the start.
  976.    */
  977.    time0 += error;
  978.    oldet = et;
  979.    et -= error;
  980.  
  981.    m = (int)strtol (time, &time, 10);
  982.    if (m) TimeControl.moves[opponent] = m;
  983.   }
  984.  
  985. void
  986. SetMachineTime (CHAR *s)
  987. {
  988.   CHAR *time;
  989.   long tmp = 0;
  990.   int m, t,sec;
  991.   sec = 0;
  992.   time = &s[strlen (CP[197])];
  993.   t = (int)strtol (time, &time, 10);
  994.    if (*time == ':') {
  995.        /* If the time does not contain a ":", it is in hundredths of
  996.         a second; if it does, it is in minutes:seconds. */
  997.        time++;
  998.        sec = (int)strtol(time, &time, 10);
  999.        t = t*6000 + sec*100;
  1000.    }
  1001.     m = (int)strtol (time, &time, 10);
  1002.    if (m) TimeControl.moves[computer] = m;
  1003.  
  1004.    tmp = TimeControl.clock[computer];
  1005.    TimeControl.clock[computer] = t;
  1006.    timecomp[compptr] += (tmp - TimeControl.clock[computer]);
  1007.   }
  1008.  
  1009.  
  1010. void
  1011. InputCommand (void)
  1012.  
  1013. /*
  1014.  * Process the users command. If easy mode is OFF (the computer is thinking
  1015.  * on opponents time) and the program is out of book, then make the 'hint'
  1016.  * move on the board and call SelectMove() to find a response. The user
  1017.  * terminates the search by entering ^C (quit siqnal) before entering a
  1018.  * command. If the opponent does not make the hint move, then set Sdepth to
  1019.  * zero.
  1020.  */
  1021.  
  1022. {
  1023.   int eof = 0;
  1024. #ifdef QUIETBACKGROUND
  1025.   SHORT have_shown_prompt = false;
  1026. #endif
  1027.   SHORT ok, tmp;
  1028.   UTSHORT mv;
  1029.   CHAR s[80], sx[80];
  1030.   CHAR *p, *q;
  1031.  
  1032. #if defined CHESSTOOL
  1033.   SHORT normal = false;
  1034. #endif
  1035.  
  1036.   ok = flag.quit = false;
  1037.   player = opponent;
  1038. /* if transposition table is filling start it over */
  1039.  ZeroTTable(1); /* clear ageing table entries */
  1040. /* if we have a move for our opponent, and we are thinking on his time, and not in force mode */
  1041.   if (hint > 0 && !flag.easy && !flag.force)
  1042. /* if the hint is a promotion don't do anything, we don't know what to promote to. */
  1043.     if ((board[hint >> 8] != pawn) || ((row (hint & 0x3f) != 0) && (row (hint & 0x3f) != 7)))
  1044.       {
  1045.     ft = time0;
  1046.     fflush (stdout);
  1047. /* create the hint move as string so we can do it */
  1048.     algbr ((SHORT) hint >> 8, (SHORT) hint & 0x3f, false);
  1049.     strcpy (s, mvstr[0]);
  1050.     tmp = epsquare;
  1051. #ifdef DEBUG12
  1052. #include "debug12.h"
  1053. #endif
  1054. #if !defined CHESSTOOL
  1055.     if (flag.post) GiveHint ();
  1056. #endif
  1057. /* do the hint move */
  1058.     if (VerifyMove (s, 1, &mv))
  1059.       {
  1060. #ifdef QUIETBACKGROUND
  1061. #ifdef NONDSP
  1062.         PromptForMove ();
  1063. #else
  1064.         ShowSidetoMove ();
  1065.         ShowPrompt ();
  1066. #endif
  1067.         have_shown_prompt = true;
  1068. #endif /* QUIETBACKGROUND */
  1069. /* would love to put null move in here */
  1070. /* after we make the hint move make a 2 ply search with both plys our moves */
  1071. /* think on opponents time */
  1072.         SelectMove (computer, 2);
  1073. /* undo the hint and carry on */
  1074.         VerifyMove (s, 2, &mv);
  1075.       }
  1076.     epsquare = tmp;
  1077.     time0 = ft;
  1078.       }
  1079.   while (!(ok || flag.quit))
  1080.     {
  1081. #if defined CHESSTOOL
  1082.       normal = false;
  1083. #endif
  1084.       player = opponent;
  1085. #ifdef QUIETBACKGROUND
  1086.       if (!have_shown_prompt)
  1087.     {
  1088. #endif /* QUIETBACKGROUND */
  1089. #ifdef NONDSP
  1090.       PromptForMove ();
  1091. #else
  1092.       ShowSidetoMove ();
  1093.       ShowPrompt ();
  1094. #endif
  1095. #ifdef QUIETBACKGROUND
  1096.     }
  1097.       have_shown_prompt = false;
  1098. #endif /* QUIETBACKGROUND */
  1099. #ifdef NONDSP
  1100.       s[0] = sx[0] = '\0';
  1101.       while (!sx[0])
  1102.     (void) gets (sx);
  1103. #else
  1104.       fflush (stdout);
  1105. #if defined (MSDOS) || defined (DOSLIKE_ANSI_DISPLAY)
  1106.       s[0] = '\0';
  1107.       eof = ( gets (sx) == NULL );
  1108. #else
  1109.       eof = ( getstr (sx) == ERR );
  1110. #endif
  1111. #endif
  1112.       sscanf (sx, "%s", s);
  1113.       if (eof)
  1114.     ExitChess ();
  1115.       if (s[0] == '\0')
  1116.     continue;
  1117.       if(s[0] == '!') printf("%s\n",sx);
  1118.       else if (strcmp (s, CP[131]) == 0)    /*bd*/
  1119.     {
  1120. #if defined CHESSTOOL || defined XBOARD
  1121.       chesstool = 0;
  1122. #endif /* CHESSTOOL */
  1123.       ClrScreen ();
  1124.       UpdateDisplay (0, 0, 1, 0);
  1125. #if defined CHESSTOOL || defined XBOARD
  1126.       chesstool = 1;
  1127. #endif /* CHESSTOOL */
  1128.     }
  1129.       else if (strcmp (s, CP[129]) == 0) /* noop */ ;    /*alg*/
  1130.       else if ((strcmp (s, CP[180]) == 0) || (strcmp (s, CP[216]) == 0))    /* quit exit*/
  1131.     flag.quit = true;
  1132.       else if (strcmp (s, CP[178]) == 0)    /*post*/
  1133.     { flag.post = true; }
  1134.       else if (strcmp (s, CP[19]) == 0)    /*nopost*/
  1135.     { flag.post = false; }
  1136.       else if ((strcmp (s, CP[191]) == 0) || (strcmp (s, CP[154]) == 0))    /*set edit*/
  1137.     EditBoard ();
  1138. #ifdef NONDSP
  1139.       else if (strcmp (s, CP[190]) == 0)    /*setup*/
  1140.     SetupBoard ();
  1141. #endif
  1142.       else if (strcmp (s, CP[156]) == 0)    /*first*/
  1143.     {
  1144. #if defined CHESSTOOL
  1145.       computer = white;
  1146.       opponent = black;
  1147.       flag.force = false;
  1148.       Sdepth = 0;
  1149. #endif /* CHESSTOOL */
  1150.       ok = true;
  1151.     }
  1152.       else if (strcmp (s, CP[162]) == 0)    /*go*/
  1153.     {
  1154.       ok = true;
  1155.       flag.force = false;
  1156.       if (computer == white)
  1157.         {
  1158.           computer = black;
  1159.           opponent = white;
  1160.         }
  1161.       else
  1162.         {
  1163.           computer = white;
  1164.           opponent = black;
  1165.         }
  1166.     }
  1167.       else if (strcmp (s, CP[166]) == 0)    /*help*/
  1168.     help ();
  1169.       else if (strcmp (s, CP[221]) == 0)    /*material*/
  1170.     flag.material = !flag.material;
  1171.       else if (strcmp (s, CP[157]) == 0)    /*force*/
  1172.     {flag.force = !flag.force; flag.bothsides = false;}
  1173.       else if (strcmp (s, CP[134]) == 0)    /*book*/
  1174.     Book = Book ? 0 : BOOKFAIL;
  1175.       else if (strcmp (s, CP[172]) == 0)    /*new*/
  1176.     {
  1177.       NewGame ();
  1178. #ifdef XBOARD
  1179.       /* Make this work the old way for compatibility */
  1180.       flag.beep = true;
  1181.       dither = 0;
  1182.       flag.easy = true;
  1183. #endif XBOARD
  1184.       UpdateDisplay (0, 0, 1, 0);
  1185.     }
  1186.       else if (strcmp (s, CP[171]) == 0)    /*list*/
  1187.     ListGame ();
  1188.       else if (strcmp (s, CP[169]) == 0 || strcmp (s, CP[217]) == 0)    /*level clock*/
  1189.     SelectLevel (sx);
  1190.       else if (strcmp (s, CP[165]) == 0)    /*hash*/
  1191.     flag.hash = !flag.hash;
  1192. #ifdef DEBUG
  1193.       else if (strcmp (s, CP[18]) == 0)    /*cache*/
  1194.     flag.nocache = !flag.nocache;
  1195.       else if (strcmp (s, CP[20]) == 0)    /*tt*/
  1196.     flag.nott = !flag.nott;
  1197.       else if (strcmp (s, CP[21]) == 0)    /*ft*/
  1198.     flag.noft = !flag.noft;
  1199. #endif
  1200.       else if (strcmp (s, CP[227]) == 0)    /*gamein*/
  1201.     flag.gamein = !flag.gamein;
  1202.       else if (strcmp (s, CP[226]) == 0)    /*beep*/
  1203.     flag.beep = !flag.beep;
  1204.       else if (strcmp (s, CP[197]) == 0)    /*time*/
  1205.     { SetMachineTime (sx); }
  1206.       else if (strcmp (s, CP[228]) == 0)    /*time*/
  1207.     { SetOppTime (sx); }
  1208.       else if (strcmp (s, CP[33]) == 0)    /*Awindow*/
  1209.     ChangeAlphaWindow ();
  1210.       else if (strcmp (s, CP[39]) == 0)    /*Bwindow*/
  1211.     ChangeBetaWindow ();
  1212.       else if (strcmp (s, CP[183]) == 0)    /*rcptr*/
  1213.     flag.rcptr = !flag.rcptr;
  1214.       else if (strcmp (s, CP[168]) == 0)    /*hint*/
  1215.     GiveHint ();
  1216.       else if (strcmp (s, CP[22]) == 0)    /*book hint*/
  1217.         LOpeningBook(opponent);
  1218. #ifdef ECO
  1219.       else if (strcmp (s, "eco") == 0){    /*book hint*/
  1220.     ecomove = GameCnt+1;
  1221.     GameList[ecomove].hashbd = hashbd;
  1222.     GameList[ecomove].hashkey = hashkey;
  1223.         EOpeningBook(opponent);}
  1224. #endif
  1225.       else if (strcmp (s, CP[135]) == 0)    /*both*/
  1226.     {
  1227.       flag.bothsides = !flag.bothsides;
  1228.           flag.force = false;
  1229.       Sdepth = 0;
  1230.       ElapsedTime (1);
  1231.       SelectMove (opponent, 1);
  1232.       ok = true;
  1233.     }
  1234.       else if (strcmp (s, CP[185]) == 0)    /*reverse*/
  1235.     {
  1236.       flag.reverse = !flag.reverse;
  1237.       ClrScreen ();
  1238.       UpdateDisplay (0, 0, 1, 0);
  1239.     }
  1240.       else if (strcmp (s, CP[195]) == 0)    /*switch*/
  1241.     {
  1242.       computer = computer ^ 1;
  1243.       opponent = opponent ^ 1;
  1244.       xwndw = (computer == white) ? WXWNDW : BXWNDW;
  1245.       flag.force = false;
  1246.       Sdepth = 0;
  1247.       ok = true;
  1248.     }
  1249.       else if (strcmp (s, CP[203]) == 0)    /*white*/
  1250.     {
  1251.       computer = black;
  1252.       opponent = white;
  1253.       xwndw = WXWNDW;
  1254.       flag.force = false;
  1255.       Sdepth = 0;
  1256.  
  1257.       /*
  1258.            * ok = true; don't automatically start with white command
  1259.            */
  1260.     }
  1261.       else if (strcmp (s, CP[133]) == 0)    /*black*/
  1262.     {
  1263.       computer = white;
  1264.       opponent = black;
  1265.       xwndw = BXWNDW;
  1266.       flag.force = false;
  1267.       Sdepth = 0;
  1268.  
  1269.       /*
  1270.            * ok = true; don't automatically start with black command
  1271.            */
  1272.     }
  1273.       else if (strcmp (s, CP[201]) == 0 && GameCnt > 0)    /*undo*/
  1274.     {
  1275.       Undo ();
  1276.     }
  1277.       else if (strcmp (s, CP[184]) == 0 && GameCnt > 1)    /*remove*/
  1278.     {
  1279.       Undo ();
  1280.       Undo ();
  1281.     }
  1282.       else if (strcmp (s, CP[160]) == 0)    /*get*/
  1283.     GetGame ();
  1284.       else if (strcmp (s, CP[207]) == 0)    /*xget*/
  1285.     GetXGame ();
  1286.       else if (strcmp (s, CP[189]) == 0)    /*save*/
  1287.     SaveGame ();
  1288.       else if (strcmp (s, CP[151]) == 0)    /*depth*/
  1289.     ChangeSearchDepth ();
  1290. #ifdef DEBUG
  1291.       else if (strcmp (s, CP[147]) == 0)    /*debuglevel*/
  1292.     ChangeDbLev ();
  1293. #endif /* DEBUG */
  1294.       else if (strcmp (s, CP[164]) == 0)    /*hashdepth*/
  1295.     ChangeHashDepth ();
  1296.       else if (strcmp (s, CP[182]) == 0)    /*random*/
  1297.     dither = (dither)?0:DITHER;
  1298.       else if (strcmp (s, "threat") == 0)       /*threat*/
  1299.         flag.threat = !flag.threat; /* tom@izf.tno.nl */
  1300.       else if (strcmp (s, "null") == 0)         /*null*/
  1301.         flag.nonull = !flag.nonull; /* tom@izf.tno.nl */
  1302.       else if (strcmp (s, "pvs") == 0)     /*PVS*/
  1303.         flag.pvs = !flag.pvs; /* tom@izf.tno.nl */
  1304.       else if (strcmp (s, "noscore") == 0)     /*noscore*/
  1305.         flag.noscore = !flag.noscore; /* tom@izf.tno.nl */
  1306.       else if (strcmp (s, "eval") == 0)     /*new eval*/
  1307.         flag.neweval = !flag.neweval; /* tom@izf.tno.nl */
  1308.       else if (strcmp (s, "deepnull") == 0)     /*deepnull*/
  1309.         flag.deepnull = !flag.deepnull; /* tom@izf.tno.nl */
  1310.       else if (strcmp (s, "verydeep") == 0)     /*verydeep*/
  1311.         flag.verydeep = !flag.verydeep; /* tom@izf.tno.nl */
  1312.       else if (strcmp (s, CP[229]) == 0)    /*hard*/
  1313.     flag.easy = false;
  1314.       else if (strcmp (s, CP[152]) == 0)    /*easy*/
  1315.     flag.easy = !flag.easy;
  1316.       else if (strcmp (s, CP[143]) == 0)    /*contempt*/
  1317.     SetContempt ();
  1318.       else if (strcmp (s, CP[209]) == 0)    /*xwndw*/
  1319.     ChangeXwindow ();
  1320.       else if (strcmp (s, CP[186]) == 0)    /*rv*/
  1321.     {
  1322.       flag.rv = !flag.rv;
  1323.       UpdateDisplay (0, 0, 1, 0);
  1324.     }
  1325.       else if (strcmp (s, CP[145]) == 0)    /*coords*/
  1326.     {
  1327.       flag.coords = !flag.coords;
  1328.       UpdateDisplay (0, 0, 1, 0);
  1329.     }
  1330.       else if (strcmp (s, CP[193]) == 0)    /*stars*/
  1331.     {
  1332.       flag.stars = !flag.stars;
  1333.       UpdateDisplay (0, 0, 1, 0);
  1334.     }
  1335. #ifdef DEBUG
  1336.       else if (strcmp (s, CP[196]) == 0)    /*test*/
  1337.     {
  1338.       SHORT cnt;
  1339.       ShowMessage (CP[108]);/*test movelist*/
  1340.       cnt = TestSpeed (MoveList, 20000);
  1341. printf("moves = %d\n",cnt);
  1342.       ShowMessage (CP[107]);/*test capturelist*/
  1343.       cnt = TestSpeed (CaptureList, 30000);
  1344. printf("moves = %d\n",cnt);
  1345.       ShowMessage (CP[85]);/*test score position*/
  1346.       TestPSpeed (ScorePosition, 15000);
  1347.     }
  1348.       else
  1349.       if (strcmp (s, CP[179]) == 0)    /*p*/
  1350.     ShowPostnValues ();
  1351.       else if (strcmp (s, CP[148]) == 0)    /*debug*/
  1352.     DoDebug ();
  1353.     else if (strcmp (s, "Mwpawn") == 0)        /*debug*/
  1354.         DoTable (Mwpawn);
  1355.     else if (strcmp (s, "Mbpawn") == 0)        /*debug*/
  1356.         DoTable (Mbpawn);
  1357.     else if (strcmp (s, "Mwknight") == 0)        /*debug*/
  1358.         DoTable (Mknight[white]);
  1359.     else if (strcmp (s, "Mbknight") == 0)        /*debug*/
  1360.         DoTable (Mknight[black]);
  1361.     else if (strcmp (s, "Mwbishop") == 0)        /*debug*/
  1362.         DoTable (Mbishop[white]);
  1363.     else if (strcmp (s, "Mbbishop") == 0)        /*debug*/
  1364.         DoTable (Mbishop[black]);
  1365. #endif
  1366.       else
  1367.     {
  1368. /* It must be a move */
  1369. /* preprocess move;
  1370.     remove x or +
  1371.     change promotion from h1=Q to h1q
  1372. */
  1373.     p=s; q=sx;
  1374.     while (*p != '\0'){
  1375.     if(*p == 'x' || *p == '+') p++;
  1376.     else if(*p == '='){ p++; *q = tolower(*p); p++; q++;}
  1377.         else {*q++ = *p++; }
  1378.     }
  1379.     *q = '\0';
  1380.  
  1381. #if defined CHESSTOOL
  1382.       normal = (ok = VerifyMove (sx, 0, &mv));
  1383. #else
  1384.       ok = VerifyMove (sx, 0, &mv);
  1385. #endif
  1386.     }
  1387.     }
  1388.  
  1389.   /* guess is correct ?*/
  1390.   Sdepth = (hint == mv) ? Sdepth-1 : 0;
  1391.   if (Tree[0].score > 9000) Sdepth = 0;
  1392.  
  1393.   if (flag.force)
  1394.     {
  1395.       computer = opponent;
  1396.       opponent = computer ^ 1;
  1397.     }
  1398. #if defined CHESSTOOL || defined XBOARD
  1399. #if defined CHESSTOOL
  1400.   if (normal)
  1401.     if (computer == black)
  1402.       printz ("%d. %s\n", ++mycnt2, s);
  1403.     else
  1404.       printz ("%d. ... %s\n", ++mycnt2, s);
  1405. #else
  1406.   printz ("%d. %s\n", ++mycnt2, s);
  1407. #endif
  1408. #ifdef notdef /* optional pass best line to frontend with move */
  1409.   if (flag.post)
  1410.     {
  1411.       register int i;
  1412.  
  1413.       printz (" %6d ", MSCORE);
  1414.       for (i = 1; MV[i] > 0; i++)
  1415.     {
  1416.       algbr ((SHORT) (MV[i] >> 8), (SHORT) (MV[i] & 0xFF), false);
  1417.       printz ("%5s ", mvstr[0]);
  1418.     }
  1419.     }
  1420.   printz ("\n");
  1421. #endif
  1422. #endif /* CHESSTOOL */
  1423. #if !defined CHESSTOOL
  1424.   signal (SIGINT, TerminateSearch);
  1425. #endif
  1426. #if !defined(MSDOS) && !defined(WIN32)
  1427.   signal (SIGQUIT, TerminateSearch);
  1428. #endif /* MSDOS */
  1429. }
  1430.  
  1431. #ifdef HAVE_GETTIMEOFDAY
  1432. void
  1433. ElapsedTime (SHORT iop)
  1434.  
  1435.  
  1436. /*
  1437.  * Determine the time that has passed since the search was started. If the
  1438.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1439.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1440.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1441.  */
  1442.  
  1443. {
  1444.   struct timeval tv;
  1445. #ifndef MSDOS
  1446.   int nchar;
  1447.   extern int errno;
  1448.   int i;
  1449. #ifdef FIONREAD
  1450.   if (i = ioctl ((int) 0, FIONREAD, &nchar))
  1451. #ifdef __EMX__
  1452.     nchar = 2;
  1453. #else
  1454.     {
  1455.       perror ("FIONREAD");
  1456.       fprintf (stderr,
  1457.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %lx\n",
  1458.     i, errno, FIONREAD);
  1459.       exit (1);
  1460.     }
  1461. #endif
  1462.  
  1463.   if (nchar)
  1464.     {
  1465.       if (!flag.timeout)
  1466.     flag.back = true;
  1467.       flag.bothsides = false;
  1468.     }
  1469. #endif /*FIONREAD*/
  1470. #else /* MSDOS */
  1471.   if (kbhit ())
  1472.     {
  1473.       if (!flag.timeout)
  1474.     flag.back = true;
  1475.       flag.bothsides = false;
  1476.     }
  1477. #endif /* MSDOS */
  1478.   gettimeofday(&tv,NULL);
  1479.   et = (tv.tv_sec*100+(tv.tv_usec/10000)) - time0;
  1480.   ETnodes = NodeCnt + ZNODES;
  1481.   if (et < 0)
  1482.     et = 0;
  1483.   if (iop == 1)
  1484.     {
  1485.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1486.     flag.timeout = true;
  1487.       ETnodes = NodeCnt + ZNODES;
  1488.       gettimeofday(&tv,NULL);
  1489.       time0 = tv.tv_sec*100+tv.tv_usec/10000;
  1490.     }
  1491. #if !defined NONDSP
  1492. #ifdef QUIETBACKGROUND
  1493.   if (!background)
  1494. #endif /* QUIETBACKGROUND */
  1495.     UpdateClocks ();
  1496. #endif
  1497. #if defined(Think_C) && defined(Window_Events)
  1498.         check_events();
  1499. #endif
  1500.  
  1501. }
  1502. #else
  1503. void
  1504. ElapsedTime (SHORT iop)
  1505.  
  1506.  
  1507. /*
  1508.  * Determine the time that has passed since the search was started. If the
  1509.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1510.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1511.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1512.  */
  1513.  
  1514. {
  1515. #ifdef WIN32
  1516. #ifdef XBOARD
  1517.   /* This code works only with a pipe (it doesn't have to be named),
  1518.      so we use it only in gnuchessx. */
  1519.   DWORD nchar;
  1520.   if (PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE),
  1521.                   NULL, 0, NULL, &nchar, NULL)) {
  1522.     if (nchar) {
  1523.       if (!flag.timeout)
  1524.       flag.back = true;
  1525.       flag.bothsides = false;
  1526.     }
  1527.   }
  1528. #endif /* XBOARD */
  1529. #else /*!WIN32*/
  1530. #if !defined(MSDOS)
  1531.   int nchar;
  1532.   extern int errno;
  1533.   int i;
  1534. #ifdef FIONREAD
  1535.   if (i = ioctl ((int) 0, FIONREAD, &nchar))
  1536. #ifdef __EMX__
  1537.     nchar = 2;
  1538. #else
  1539.     {
  1540.       perror ("FIONREAD");
  1541.       fprintf (stderr,
  1542.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  1543.     i, errno, FIONREAD);
  1544.       exit (1);
  1545.     }
  1546. #endif
  1547.  
  1548.   if (nchar)
  1549.     {
  1550.       if (!flag.timeout)
  1551.     flag.back = true;
  1552.       flag.bothsides = false;
  1553.     }
  1554. #endif /*FIONREAD*/
  1555. #else /* MSDOS */
  1556.   if (kbhit ())
  1557.     {
  1558.       if (!flag.timeout)
  1559.     flag.back = true;
  1560.       flag.bothsides = false;
  1561.     }
  1562. #endif /* MSDOS */
  1563. #endif /* !WIN32 */
  1564.   et = (time ((time_t *) 0) * 100) - time0;
  1565.   ETnodes = NodeCnt + ZNODES;
  1566.   if (et < 0)
  1567.     et = 0;
  1568.   if (iop == 1)
  1569.     {
  1570.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1571.     flag.timeout = true;
  1572.       ETnodes = NodeCnt + ZNODES;
  1573.       time0 = time ((time_t *) 0) * 100;
  1574.     }
  1575. #if !defined NONDSP
  1576. #ifdef QUIETBACKGROUND
  1577.   if (!background)
  1578. #endif /* QUIETBACKGROUND */
  1579.     UpdateClocks ();
  1580. #endif
  1581. #if defined(Think_C) && defined(Window_Events)
  1582.         check_events();
  1583. #endif
  1584. }
  1585. #endif
  1586.  
  1587.  
  1588. static SHORT Nmoves[] = NMOVEINIT;
  1589. void
  1590. SetTimeControl (void)
  1591. {
  1592.      flag.gamein = TCflag = false;
  1593.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  1594. if (XC){
  1595.                   if (XCmore < XC)
  1596.                     {
  1597.                       TCmoves = XCmoves[XCmore];
  1598.                       TCminutes = XCminutes[XCmore];
  1599.                       TCseconds = XCseconds[XCmore];
  1600.               TCadd = XCadd[XCmore];
  1601.                       XCmore++;
  1602.                     }
  1603.       if((TCminutes+TCseconds) == 0){TCflag = false;} else {
  1604.       TCflag = true;
  1605.       if(TCmoves == 0)
  1606.     {int i; i = (TCminutes*60+TCseconds + 40 * (TCadd/100))/60; 
  1607.         if(i > NMOVELIMIT) i = NMOVELIMIT; 
  1608.         TCmoves = Nmoves[i]; flag.gamein = true;}
  1609.     else flag.gamein = false;
  1610.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  1611.       TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
  1612.       TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
  1613.     }
  1614.   }
  1615.   flag.onemove = (TCmoves == 1);
  1616.   et = 0;
  1617.   ElapsedTime (1);
  1618. }
  1619.