home *** CD-ROM | disk | FTP | other *** search
/ Dream 44 / Amiga_Dream_44.iso / RiscPc / jeux / ArcBoard004.arc / !GNUChessX / src / c / dspcom < prev    next >
Text File  |  1996-01-10  |  39KB  |  1,615 lines

  1. /*
  2.  * dspcom.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #ifdef WIN32
  25. #define SHORT WIN_SHORT
  26. #include <windows.h>
  27. #undef SHORT
  28. #endif
  29. #include "gnuchess.h"
  30. #include "ataks.h"
  31. #include "ttable.h" /* calls ZeroTTable(), probably belongs in main.c*/
  32. #ifdef HAVE_GETTIMEOFDAY
  33. #include <sys/time.h>
  34. #endif
  35. CHAR mvstr[5][7];
  36. #if defined(Think_C) && defined(Window_Events)
  37. #include "doevents.h"
  38. #endif
  39. extern SHORT Mwpawn[64], Mbpawn[64], Mknight[2][64], Mbishop[2][64];
  40. extern CHAR *version, *patchlevel;
  41. extern void LOpeningBook();
  42. #ifdef ECO
  43. extern void EOpeningBook();
  44. SHORT ecomove;
  45. #endif
  46. CHAR *InPtr;
  47. #ifndef __arm__
  48. #include <unistd.h>
  49. #endif
  50.  
  51. #if defined(sun) && defined(sparc) && defined(svr4)
  52. #include <sys/filio.h>
  53. #endif
  54. #include <ctype.h>
  55. #include <signal.h>
  56. #if defined(MSDOS)
  57. #include <dos.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #else
  61. #ifdef Think_C
  62. #include "doevents.h"
  63. #else
  64. #ifdef WIN32
  65. #include <sys/types.h>
  66. #else /* Other */
  67. #include <sys/param.h>
  68. #ifndef __arm__
  69. #include <sys/types.h>
  70. #endif
  71. #include <sys/file.h>
  72. #if !defined(AMIGADOS) && !defined(__arm__)
  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. #elif defined(__arm__)
  726.       sprintf(fname, "<gnuchessx$dir>.gnuchess.chesslst");
  727. #else
  728.       time (&when);
  729.       strncpy (dbuf, ctime (&when), 20);
  730.       dbuf[7] = '\0';
  731.       dbuf[10] = '\0';
  732.       dbuf[13] = '\0';
  733.       dbuf[16] = '\0';
  734.       dbuf[19] = '\0';
  735. /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
  736.    date is Jan 1
  737.    time is 02:03:04
  738.    program played black */
  739.       sprintf (fname, "CLp%s.%s%s-%s%s%s%c", patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14, dbuf + 17, ColorStr[computer][0]);
  740.       /* replace space padding with 0 */
  741.       for (i = 0; fname[i] != '\0'; i++)
  742.     if (fname[i] == ' ')
  743.       fname[i] = '0';
  744. #endif /* MSDOS */
  745.     }
  746.   fd = fopen (fname, "w");
  747.   if (!fd)
  748.     {
  749.       printf (CP[219], fname);
  750.       exit (1);
  751.     }
  752.   /*fprintf (fd, "gnuchess game %d\n", u);*/
  753.   fprintf (fd, CP[161], patchlevel);
  754.   fprintf (fd, CP[10]);
  755.   fprintf (fd, CP[11]);
  756.   for (i = 1; i <= GameCnt; i++)
  757.     {
  758.       f = GameList[i].gmove >> 8;
  759.       t = (GameList[i].gmove & 0xFF);
  760.       algbr (f, t, GameList[i].flags);
  761.       if(GameList[i].flags & book)
  762.           fprintf (fd, "%6s  %5d    Book%7ld %5ld", mvstr[0],
  763.            GameList[i].score, 
  764.            GameList[i].nodes, GameList[i].time);
  765.       else
  766.           fprintf (fd, "%6s  %5d     %2d %7ld %5ld", mvstr[0],
  767.            GameList[i].score, GameList[i].depth,
  768.            GameList[i].nodes, GameList[i].time);
  769.       if ((i % 2) == 0)
  770.     {
  771. #ifdef DEBUG40
  772.     if(computer == black){
  773.       int p;
  774.     for(p=0;GameList[i-1].d1[p] && p < 7;p++){
  775.         algbr((GameList[i-1].d1[p]>>8) & 0xff,GameList[i-1].d1[p] & 0xff,0);
  776.             fprintf(fd," %s",mvstr[0]);
  777.     }
  778.     fprintf(fd,"\n");
  779.     } else {
  780.     int p;
  781.     for(p=0;GameList[i].d1[p] && p < 7;p++){
  782.         algbr(GameList[i].d1[p]>>8 & 0xff,GameList[i].d1[p] & 0xff,0);
  783.             fprintf(fd," %s",mvstr[0]);
  784.     }
  785.     fprintf(fd,"\n");
  786.     }
  787. #else
  788.     fprintf(fd,"\n");
  789. #endif
  790.     }
  791.    }
  792.   fprintf (fd, "\n\n");
  793.   if (GameList[GameCnt].flags & draw)
  794.     {
  795.       fprintf (fd, CP[54], DRAW);
  796.     }
  797.   else if (GameList[GameCnt].score == -9999)
  798.     {
  799.       fprintf (fd, "%s\n", ColorStr[player ]);
  800.     }
  801.   else if (GameList[GameCnt].score == 9998)
  802.     {
  803.       fprintf (fd, "%s\n", ColorStr[player ^ 1]);
  804.     }
  805.   fclose (fd);
  806. }
  807.  
  808. void
  809. Undo (void)
  810.  
  811. /*
  812.  * Undo the most recent half-move.
  813.  */
  814.  
  815. {
  816.   SHORT f, t;
  817.   f = GameList[GameCnt].gmove >> 8;
  818.   t = GameList[GameCnt].gmove & 0xFF;
  819.   if (board[t] == king && distance (t, f) > 1)
  820.     (void) castle (GameList[GameCnt].color, f, t, 2);
  821.   else
  822.     {
  823.       /* Check for promotion: */
  824.       if (GameList[GameCnt].flags & promote)
  825.     {
  826.       board[t] = pawn;
  827.     }
  828.       board[f] = board[t];
  829.       color[f] = color[t];
  830.       board[t] = GameList[GameCnt].piece;
  831.       color[t] = GameList[GameCnt].color;
  832.       if (color[t] != neutral)
  833.     Mvboard[t]--;
  834.       Mvboard[f]--;
  835.     }
  836.   if (GameList[GameCnt].flags & epmask)
  837.     EnPassant (otherside[color[f]], f, t, 2);
  838.   else
  839.     InitializeStats ();
  840.   epsquare = GameList[GameCnt].epssq;
  841.   if (TCflag && (TCmoves>1))
  842.     ++TimeControl.moves[color[f]];
  843.   hashkey = GameList[GameCnt].hashkey;
  844.   hashbd = GameList[GameCnt].hashbd;
  845.   GameCnt--;
  846.   computer = computer ^ 1;
  847.   opponent = opponent ^ 1;
  848.   flag.mate = false;
  849.   Sdepth = 0;
  850.   player = player ^ 1;
  851.   ShowSidetoMove ();
  852.   UpdateDisplay (0, 0, 1, 0);
  853. /*
  854.   if (flag.regularstart)
  855. */
  856.     Book = BOOKFAIL;
  857. }
  858.  
  859. SHORT
  860.  TestSpeed (void (*f) (SHORT side, SHORT ply), unsigned j)
  861. {
  862. #ifdef test
  863.   unsigned jj;
  864. #endif
  865.   unsigned i;
  866.   long cnt, rate, t1, t2;
  867. #ifdef HAVE_GETTIMEOFDAY
  868. struct timeval tv;
  869. #endif
  870.  
  871. #ifdef HAVE_GETTIMEOFDAY
  872.   gettimeofday(&tv,NULL);
  873.   t1 = (tv.tv_sec*100+(tv.tv_usec/10000));
  874. #else
  875.   t1 = time (0);
  876. #endif
  877.   for (i = 0; i < j; i++)
  878.     {
  879.       f (opponent, 2);
  880. #ifdef test
  881.     for(jj=TrPnt[2];i<TrPnt[3];jj++)if(!pick(jj,TrPnt[3]-1))break;
  882. #endif
  883.     }
  884. #ifdef HAVE_GETTIMEOFDAY
  885.   gettimeofday(&tv,NULL);
  886.   t2 = (tv.tv_sec*100+(tv.tv_usec/10000));
  887. #else
  888.   t2 = time (0);
  889. #endif
  890.   cnt = j * (TrPnt[3] - TrPnt[2]);
  891.   if (t2 - t1)
  892.     et = (t2 - t1);
  893.   else
  894.     et = 1;
  895.   rate = (((et) ? ((cnt*100) / et) : 0));
  896.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  897. #ifdef NONDSP
  898.   printz (CP[91], cnt, rate);
  899. #ifdef DEBUG9
  900.   for (j = TrPnt[2]; j < TrPnt[3]; j++)
  901.     {
  902.       struct leaf *node = &Tree[j];
  903.       algbr (node->f, node->t, node->flags);
  904.       printf ("%s %s %s %s %d %x\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],node->score,node->flags);
  905.     }
  906. #endif
  907. #else
  908.   ShowNodeCnt (cnt);
  909. #endif
  910.     return(TrPnt[3] - TrPnt[2]);
  911. }
  912.  
  913. void
  914.  TestPSpeed (SHORT (*f) (SHORT side), unsigned j)
  915. {
  916.   SHORT i;
  917.   long cnt, rate, t1, t2;
  918. #ifdef HAVE_GETTIMEOFDAY
  919. struct timeval tv;
  920. #endif
  921.  
  922. #ifdef HAVE_GETTIMEOFDAY
  923.   gettimeofday(&tv,NULL);
  924.   t1 = (tv.tv_sec*100+(tv.tv_usec/10000));
  925. #else
  926.   t1 = time (0);
  927. #endif
  928.   for (i = 0; i < j; i++)
  929.     {
  930.       (void) f (opponent);
  931.     }
  932. #ifdef HAVE_GETTIMEOFDAY
  933.   gettimeofday(&tv,NULL);
  934.   t2 = (tv.tv_sec*100+(tv.tv_usec/10000));
  935. #else
  936.   t2 = time (0);
  937. #endif
  938.   cnt = j;
  939.   if (t2 - t1)
  940.     et = (t2 - t1);
  941.   else
  942.     et = 1;
  943.   rate = (et) ? ((cnt*100) / et) : 0;
  944.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  945. #ifdef NONDSP
  946.   printz (CP[91], cnt, rate);
  947. #else
  948.   ShowNodeCnt (cnt);
  949. #endif
  950. }
  951.  
  952.  
  953. void
  954. SetOppTime (CHAR *s)
  955. {
  956.   CHAR *time;
  957.   register int error, oldet;
  958.   int m, t, sec;
  959.   sec = 0;
  960.   time = &s[strlen (CP[197])];
  961.   t = (int)strtol (time, &time, 10);
  962.    if (*time == ':') {
  963.        /* If the time does not contain a ":", it is in hundredths of
  964.         a second; if it does, it is in minutes:seconds. */
  965.        time++;
  966.        sec = (int)strtol(time, &time, 10);
  967.        t = t*6000 + sec*100;
  968.    }
  969.    /* This is a little tricky because opponent's clock is running.
  970.       TimeControl.clock[opponent] records the time the clock showed
  971.       when we started it running, not the time it should show now.
  972.    */
  973.    ElapsedTime(0);
  974.    error = (t + et) - TimeControl.clock[opponent];
  975.  
  976.    /* Assume our error was in deciding when to start the opponent's
  977.       clock, not in the time it showed at the start.
  978.    */
  979.    time0 += error;
  980.    oldet = et;
  981.    et -= error;
  982.  
  983.    m = (int)strtol (time, &time, 10);
  984.    if (m) TimeControl.moves[opponent] = m;
  985.   }
  986.  
  987. void
  988. SetMachineTime (CHAR *s)
  989. {
  990.   CHAR *time;
  991.   long tmp = 0;
  992.   int m, t,sec;
  993.   sec = 0;
  994.   time = &s[strlen (CP[197])];
  995.   t = (int)strtol (time, &time, 10);
  996.    if (*time == ':') {
  997.        /* If the time does not contain a ":", it is in hundredths of
  998.         a second; if it does, it is in minutes:seconds. */
  999.        time++;
  1000.        sec = (int)strtol(time, &time, 10);
  1001.        t = t*6000 + sec*100;
  1002.    }
  1003.     m = (int)strtol (time, &time, 10);
  1004.    if (m) TimeControl.moves[computer] = m;
  1005.  
  1006.    tmp = TimeControl.clock[computer];
  1007.    TimeControl.clock[computer] = t;
  1008.    timecomp[compptr] += (tmp - TimeControl.clock[computer]);
  1009.   }
  1010.  
  1011.  
  1012. void
  1013. InputCommand (void)
  1014.  
  1015. /*
  1016.  * Process the users command. If easy mode is OFF (the computer is thinking
  1017.  * on opponents time) and the program is out of book, then make the 'hint'
  1018.  * move on the board and call SelectMove() to find a response. The user
  1019.  * terminates the search by entering ^C (quit siqnal) before entering a
  1020.  * command. If the opponent does not make the hint move, then set Sdepth to
  1021.  * zero.
  1022.  */
  1023.  
  1024. {
  1025.   int eof = 0;
  1026. #ifdef QUIETBACKGROUND
  1027.   SHORT have_shown_prompt = false;
  1028. #endif
  1029.   SHORT ok, tmp;
  1030.   UTSHORT mv;
  1031.   CHAR s[80], sx[80];
  1032.   CHAR *p, *q;
  1033.  
  1034. #if defined CHESSTOOL
  1035.   SHORT normal = false;
  1036. #endif
  1037.  
  1038.   ok = flag.quit = false;
  1039.   player = opponent;
  1040. /* if transposition table is filling start it over */
  1041.  ZeroTTable(1); /* clear ageing table entries */
  1042. /* if we have a move for our opponent, and we are thinking on his time, and not in force mode */
  1043.   if (hint > 0 && !flag.easy && !flag.force)
  1044. /* if the hint is a promotion don't do anything, we don't know what to promote to. */
  1045.     if ((board[hint >> 8] != pawn) || ((row (hint & 0x3f) != 0) && (row (hint & 0x3f) != 7)))
  1046.       {
  1047.     ft = time0;
  1048.     fflush (stdout);
  1049. /* create the hint move as string so we can do it */
  1050.     algbr ((SHORT) hint >> 8, (SHORT) hint & 0x3f, false);
  1051.     strcpy (s, mvstr[0]);
  1052.     tmp = epsquare;
  1053. #ifdef DEBUG12
  1054. #include "debug12.h"
  1055. #endif
  1056. #if !defined CHESSTOOL
  1057.     if (flag.post) GiveHint ();
  1058. #endif
  1059. /* do the hint move */
  1060.     if (VerifyMove (s, 1, &mv))
  1061.       {
  1062. #ifdef QUIETBACKGROUND
  1063. #ifdef NONDSP
  1064.         PromptForMove ();
  1065. #else
  1066.         ShowSidetoMove ();
  1067.         ShowPrompt ();
  1068. #endif
  1069.         have_shown_prompt = true;
  1070. #endif /* QUIETBACKGROUND */
  1071. /* would love to put null move in here */
  1072. /* after we make the hint move make a 2 ply search with both plys our moves */
  1073. /* think on opponents time */
  1074.         SelectMove (computer, 2);
  1075. /* undo the hint and carry on */
  1076.         VerifyMove (s, 2, &mv);
  1077.       }
  1078.     epsquare = tmp;
  1079.     time0 = ft;
  1080.       }
  1081. /*  printf("OK = %d Flag.quit = %d\n", ok, flag.quit);
  1082.     while (!(ok || flag.quit)) */
  1083.     while (!ok && !flag.quit)
  1084.     {
  1085. #if defined CHESSTOOL
  1086.       normal = false;
  1087. #endif
  1088.       player = opponent;
  1089. #ifdef QUIETBACKGROUND
  1090.       if (!have_shown_prompt)
  1091.     {
  1092. #endif /* QUIETBACKGROUND */
  1093. #ifdef NONDSP
  1094.       PromptForMove ();
  1095. #else
  1096.       ShowSidetoMove ();
  1097.       ShowPrompt ();
  1098. #endif
  1099. #ifdef QUIETBACKGROUND
  1100.     }
  1101.       have_shown_prompt = false;
  1102. #endif /* QUIETBACKGROUND */
  1103. #ifdef NONDSP
  1104.       s[0] = sx[0] = '\0';
  1105.       while (!sx[0])
  1106.     (void) gets (sx);
  1107. #else
  1108.       fflush (stdout);
  1109. #ifdef MSDOS
  1110.       s[0] = '\0';
  1111.       eof = ( gets (sx) == NULL );
  1112. #else
  1113.       eof = ( getstr (sx) == ERR );
  1114. #endif
  1115. #endif
  1116.       sscanf (sx, "%s", s);
  1117.       if (eof)
  1118.     ExitChess ();
  1119.       if (s[0] == '\0')
  1120.     continue;
  1121.       if(s[0] == '!') printf("%s\n",sx);
  1122.       else if (strcmp (s, CP[131]) == 0)    /*bd*/
  1123.     {
  1124. #if defined CHESSTOOL || defined XBOARD
  1125.       chesstool = 0;
  1126. #endif /* CHESSTOOL */
  1127.       ClrScreen ();
  1128.       UpdateDisplay (0, 0, 1, 0);
  1129. #if defined CHESSTOOL || defined XBOARD
  1130.       chesstool = 1;
  1131. #endif /* CHESSTOOL */
  1132.     }
  1133.       else if (strcmp (s, CP[129]) == 0) /* noop */ ;    /*alg*/
  1134.       else if ((strcmp (s, CP[180]) == 0) || (strcmp (s, CP[216]) == 0))    /* quit exit*/
  1135.     flag.quit = true;
  1136.       else if (strcmp (s, CP[178]) == 0)    /*post*/
  1137.     { flag.post = true; }
  1138.       else if (strcmp (s, CP[19]) == 0)    /*nopost*/
  1139.     { flag.post = false; }
  1140.       else if ((strcmp (s, CP[191]) == 0) || (strcmp (s, CP[154]) == 0))    /*set edit*/
  1141.     EditBoard ();
  1142. #ifdef NONDSP
  1143.       else if (strcmp (s, CP[190]) == 0)    /*setup*/
  1144.     SetupBoard ();
  1145. #endif
  1146.       else if (strcmp (s, CP[156]) == 0)    /*first*/
  1147.     {
  1148. #if defined CHESSTOOL
  1149.       computer = white;
  1150.       opponent = black;
  1151.       flag.force = false;
  1152.       Sdepth = 0;
  1153. #endif /* CHESSTOOL */
  1154.       ok = true;
  1155.     }
  1156.       else if (strcmp (s, CP[162]) == 0)    /*go*/
  1157.     {
  1158.       ok = true;
  1159.       flag.force = false;
  1160.       if (computer == white)
  1161.         {
  1162.           computer = black;
  1163.           opponent = white;
  1164.         }
  1165.       else
  1166.         {
  1167.           computer = white;
  1168.           opponent = black;
  1169.         }
  1170.     }
  1171.       else if (strcmp (s, CP[166]) == 0)    /*help*/
  1172.     help ();
  1173.       else if (strcmp (s, CP[221]) == 0)    /*material*/
  1174.     flag.material = !flag.material;
  1175.       else if (strcmp (s, CP[157]) == 0)    /*force*/
  1176.     {flag.force = !flag.force; flag.bothsides = false;}
  1177.       else if (strcmp (s, CP[134]) == 0)    /*book*/
  1178.     Book = Book ? 0 : BOOKFAIL;
  1179.       else if (strcmp (s, CP[172]) == 0)    /*new*/
  1180.     {
  1181.       NewGame ();
  1182. #ifdef XBOARD
  1183.       /* Make this work the old way for compatibility */
  1184.       flag.beep = true;
  1185.       dither = 0;
  1186.       flag.easy = true;
  1187. #endif XBOARD
  1188.       UpdateDisplay (0, 0, 1, 0);
  1189.     }
  1190.       else if (strcmp (s, CP[171]) == 0)    /*list*/
  1191.     ListGame ();
  1192.       else if (strcmp (s, CP[169]) == 0 || strcmp (s, CP[217]) == 0)    /*level clock*/
  1193.     SelectLevel (sx);
  1194.       else if (strcmp (s, CP[165]) == 0)    /*hash*/
  1195.     flag.hash = !flag.hash;
  1196. #ifdef DEBUG
  1197.       else if (strcmp (s, CP[18]) == 0)    /*cache*/
  1198.     flag.nocache = !flag.nocache;
  1199.       else if (strcmp (s, CP[20]) == 0)    /*tt*/
  1200.     flag.nott = !flag.nott;
  1201.       else if (strcmp (s, CP[21]) == 0)    /*ft*/
  1202.     flag.noft = !flag.noft;
  1203. #endif
  1204.       else if (strcmp (s, CP[227]) == 0)    /*gamein*/
  1205.     flag.gamein = !flag.gamein;
  1206.       else if (strcmp (s, CP[226]) == 0)    /*beep*/
  1207.     flag.beep = !flag.beep;
  1208.       else if (strcmp (s, CP[197]) == 0)    /*time*/
  1209.     { SetMachineTime (sx); }
  1210.       else if (strcmp (s, CP[228]) == 0)    /*time*/
  1211.     { SetOppTime (sx); }
  1212.       else if (strcmp (s, CP[33]) == 0)    /*Awindow*/
  1213.     ChangeAlphaWindow ();
  1214.       else if (strcmp (s, CP[39]) == 0)    /*Bwindow*/
  1215.     ChangeBetaWindow ();
  1216.       else if (strcmp (s, CP[183]) == 0)    /*rcptr*/
  1217.     flag.rcptr = !flag.rcptr;
  1218.       else if (strcmp (s, CP[168]) == 0)    /*hint*/
  1219.     GiveHint ();
  1220.       else if (strcmp (s, CP[22]) == 0)    /*book hint*/
  1221.         LOpeningBook(opponent);
  1222. #ifdef ECO
  1223.       else if (strcmp (s, "eco") == 0){    /*book hint*/
  1224.     ecomove = GameCnt+1;
  1225.     GameList[ecomove].hashbd = hashbd;
  1226.     GameList[ecomove].hashkey = hashkey;
  1227.         EOpeningBook(opponent);}
  1228. #endif
  1229.       else if (strcmp (s, CP[135]) == 0)    /*both*/
  1230.     {
  1231.       flag.bothsides = !flag.bothsides;
  1232.           flag.force = false;
  1233.       Sdepth = 0;
  1234.       ElapsedTime (1);
  1235.       SelectMove (opponent, 1);
  1236.       ok = true;
  1237.     }
  1238.       else if (strcmp (s, CP[185]) == 0)    /*reverse*/
  1239.     {
  1240.       flag.reverse = !flag.reverse;
  1241.       ClrScreen ();
  1242.       UpdateDisplay (0, 0, 1, 0);
  1243.     }
  1244.       else if (strcmp (s, CP[195]) == 0)    /*switch*/
  1245.     {
  1246.       computer = computer ^ 1;
  1247.       opponent = opponent ^ 1;
  1248.       xwndw = (computer == white) ? WXWNDW : BXWNDW;
  1249.       flag.force = false;
  1250.       Sdepth = 0;
  1251.       ok = true;
  1252.     }
  1253.       else if (strcmp (s, CP[203]) == 0)    /*white*/
  1254.     {
  1255.       computer = black;
  1256.       opponent = white;
  1257.       xwndw = WXWNDW;
  1258.       flag.force = false;
  1259.       Sdepth = 0;
  1260.  
  1261.       /*
  1262.            * ok = true; don't automatically start with white command
  1263.            */
  1264.     }
  1265.       else if (strcmp (s, CP[133]) == 0)    /*black*/
  1266.     {
  1267.       computer = white;
  1268.       opponent = black;
  1269.       xwndw = BXWNDW;
  1270.       flag.force = false;
  1271.       Sdepth = 0;
  1272.  
  1273.       /*
  1274.            * ok = true; don't automatically start with black command
  1275.            */
  1276.     }
  1277.       else if (strcmp (s, CP[201]) == 0 && GameCnt > 0)    /*undo*/
  1278.     {
  1279.       Undo ();
  1280.     }
  1281.       else if (strcmp (s, CP[184]) == 0 && GameCnt > 1)    /*remove*/
  1282.     {
  1283.       Undo ();
  1284.       Undo ();
  1285.     }
  1286.       else if (strcmp (s, CP[160]) == 0)    /*get*/
  1287.     GetGame ();
  1288.       else if (strcmp (s, CP[207]) == 0)    /*xget*/
  1289.     GetXGame ();
  1290.       else if (strcmp (s, CP[189]) == 0)    /*save*/
  1291.     SaveGame ();
  1292.       else if (strcmp (s, CP[151]) == 0)    /*depth*/
  1293.     ChangeSearchDepth ();
  1294. #ifdef DEBUG
  1295.       else if (strcmp (s, CP[147]) == 0)    /*debuglevel*/
  1296.     ChangeDbLev ();
  1297. #endif /* DEBUG */
  1298.       else if (strcmp (s, CP[164]) == 0)    /*hashdepth*/
  1299.     ChangeHashDepth ();
  1300.       else if (strcmp (s, CP[182]) == 0)    /*random*/
  1301.     dither = (dither)?0:DITHER;
  1302.       else if (strcmp (s, "threat") == 0)       /*threat*/
  1303.         flag.threat = !flag.threat; /* tom@izf.tno.nl */
  1304.       else if (strcmp (s, "null") == 0)         /*null*/
  1305.         flag.nonull = !flag.nonull; /* tom@izf.tno.nl */
  1306.       else if (strcmp (s, "pvs") == 0)     /*PVS*/
  1307.         flag.pvs = !flag.pvs; /* tom@izf.tno.nl */
  1308.       else if (strcmp (s, "noscore") == 0)     /*noscore*/
  1309.         flag.noscore = !flag.noscore; /* tom@izf.tno.nl */
  1310.       else if (strcmp (s, "eval") == 0)     /*new eval*/
  1311.         flag.neweval = !flag.neweval; /* tom@izf.tno.nl */
  1312.       else if (strcmp (s, "deepnull") == 0)     /*deepnull*/
  1313.         flag.deepnull = !flag.deepnull; /* tom@izf.tno.nl */
  1314.       else if (strcmp (s, "verydeep") == 0)     /*verydeep*/
  1315.         flag.verydeep = !flag.verydeep; /* tom@izf.tno.nl */
  1316.       else if (strcmp (s, CP[229]) == 0)    /*hard*/
  1317.     flag.easy = false;
  1318.       else if (strcmp (s, CP[152]) == 0)    /*easy*/
  1319.     flag.easy = !flag.easy;
  1320.       else if (strcmp (s, CP[143]) == 0)    /*contempt*/
  1321.     SetContempt ();
  1322.       else if (strcmp (s, CP[209]) == 0)    /*xwndw*/
  1323.     ChangeXwindow ();
  1324.       else if (strcmp (s, CP[186]) == 0)    /*rv*/
  1325.     {
  1326.       flag.rv = !flag.rv;
  1327.       UpdateDisplay (0, 0, 1, 0);
  1328.     }
  1329.       else if (strcmp (s, CP[145]) == 0)    /*coords*/
  1330.     {
  1331.       flag.coords = !flag.coords;
  1332.       UpdateDisplay (0, 0, 1, 0);
  1333.     }
  1334.       else if (strcmp (s, CP[193]) == 0)    /*stars*/
  1335.     {
  1336.       flag.stars = !flag.stars;
  1337.       UpdateDisplay (0, 0, 1, 0);
  1338.     }
  1339. #ifdef DEBUG
  1340.       else if (strcmp (s, CP[196]) == 0)    /*test*/
  1341.     {
  1342.       SHORT cnt;
  1343.       ShowMessage (CP[108]);/*test movelist*/
  1344.       cnt = TestSpeed (MoveList, 20000);
  1345. printf("moves = %d\n",cnt);
  1346.       ShowMessage (CP[107]);/*test capturelist*/
  1347.       cnt = TestSpeed (CaptureList, 30000);
  1348. printf("moves = %d\n",cnt);
  1349.       ShowMessage (CP[85]);/*test score position*/
  1350.       TestPSpeed (ScorePosition, 15000);
  1351.     }
  1352.       else
  1353.       if (strcmp (s, CP[179]) == 0)    /*p*/
  1354.     ShowPostnValues ();
  1355.       else if (strcmp (s, CP[148]) == 0)    /*debug*/
  1356.     DoDebug ();
  1357.     else if (strcmp (s, "Mwpawn") == 0)        /*debug*/
  1358.         DoTable (Mwpawn);
  1359.     else if (strcmp (s, "Mbpawn") == 0)        /*debug*/
  1360.         DoTable (Mbpawn);
  1361.     else if (strcmp (s, "Mwknight") == 0)        /*debug*/
  1362.         DoTable (Mknight[white]);
  1363.     else if (strcmp (s, "Mbknight") == 0)        /*debug*/
  1364.         DoTable (Mknight[black]);
  1365.     else if (strcmp (s, "Mwbishop") == 0)        /*debug*/
  1366.         DoTable (Mbishop[white]);
  1367.     else if (strcmp (s, "Mbbishop") == 0)        /*debug*/
  1368.         DoTable (Mbishop[black]);
  1369. #endif
  1370.       else
  1371.     {
  1372. /* It must be a move */
  1373. /* preprocess move;
  1374.     remove x or +
  1375.     change promotion from h1=Q to h1q
  1376. */
  1377.     p=s; q=sx;
  1378.     while (*p != '\0'){
  1379.     if(*p == 'x' || *p == '+') p++;
  1380.     else if(*p == '='){ p++; *q = tolower(*p); p++; q++;}
  1381.         else {*q++ = *p++; }
  1382.     }
  1383.     *q = '\0';
  1384.  
  1385. #if defined CHESSTOOL
  1386.       normal = (ok = VerifyMove (sx, 0, &mv));
  1387. #else
  1388.       ok = VerifyMove (sx, 0, &mv);
  1389. #endif
  1390.     }
  1391.     }
  1392.  
  1393.   /* guess is correct ?*/
  1394.   Sdepth = (hint == mv) ? Sdepth-1 : 0;
  1395.   if (Tree[0].score > 9000) Sdepth = 0;
  1396.  
  1397.   if (flag.force)
  1398.     {
  1399.       computer = opponent;
  1400.       opponent = computer ^ 1;
  1401.     }
  1402. #if defined CHESSTOOL || defined XBOARD
  1403. #if defined CHESSTOOL
  1404.   if (normal)
  1405.     if (computer == black)
  1406.       printz ("%d. %s\n", ++mycnt2, s);
  1407.     else
  1408.       printz ("%d. ... %s\n", ++mycnt2, s);
  1409. #else
  1410.   printz ("%d. %s\n", ++mycnt2, s);
  1411. #endif
  1412. #ifdef notdef /* optional pass best line to frontend with move */
  1413.   if (flag.post)
  1414.     {
  1415.       register int i;
  1416.  
  1417.       printz (" %6d ", MSCORE);
  1418.       for (i = 1; MV[i] > 0; i++)
  1419.     {
  1420.       algbr ((SHORT) (MV[i] >> 8), (SHORT) (MV[i] & 0xFF), false);
  1421.       printz ("%5s ", mvstr[0]);
  1422.     }
  1423.     }
  1424.   printz ("\n");
  1425. #endif
  1426. #endif /* CHESSTOOL */
  1427. #if !defined CHESSTOOL
  1428.   signal (SIGINT, TerminateSearch);
  1429. #endif
  1430. #if !defined(MSDOS) && !defined(WIN32)
  1431.   signal (SIGQUIT, TerminateSearch);
  1432. #endif /* MSDOS */
  1433. }
  1434.  
  1435. #ifdef HAVE_GETTIMEOFDAY
  1436. void
  1437. ElapsedTime (SHORT iop)
  1438.  
  1439.  
  1440. /*
  1441.  * Determine the time that has passed since the search was started. If the
  1442.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1443.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1444.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1445.  */
  1446.  
  1447. {
  1448.   struct timeval tv;
  1449. #ifndef MSDOS
  1450.   int nchar;
  1451.   extern int errno;
  1452.   int i;
  1453. #ifdef FIONREAD
  1454.   if (i = ioctl ((int) 0, FIONREAD, &nchar))
  1455.     {
  1456.       perror ("FIONREAD");
  1457.       fprintf (stderr,
  1458.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %lx\n",
  1459.     i, errno, FIONREAD);
  1460.       exit (1);
  1461.     }
  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.     {
  1537.       perror ("FIONREAD");
  1538.       fprintf (stderr,
  1539.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  1540.     i, errno, FIONREAD);
  1541.       exit (1);
  1542.     }
  1543.  
  1544.   if (nchar)
  1545.     {
  1546.       if (!flag.timeout)
  1547.     flag.back = true;
  1548.       flag.bothsides = false;
  1549.     }
  1550. #endif /*FIONREAD*/
  1551. #else /* MSDOS */
  1552.   if (kbhit ())
  1553.     {
  1554.       if (!flag.timeout)
  1555.     flag.back = true;
  1556.       flag.bothsides = false;
  1557.     }
  1558. #endif /* MSDOS */
  1559. #endif /* !WIN32 */
  1560.   et = (time ((time_t *) 0) - time0) * 100;
  1561.   ETnodes = NodeCnt + ZNODES;
  1562.   if (et < 0)
  1563.     et = 0;
  1564.   if (iop == 1)
  1565.     {
  1566.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1567.     flag.timeout = true;
  1568.       ETnodes = NodeCnt + ZNODES;
  1569.       time0 = time ((time_t *) 0);
  1570.     }
  1571. #if !defined NONDSP
  1572. #ifdef QUIETBACKGROUND
  1573.   if (!background)
  1574. #endif /* QUIETBACKGROUND */
  1575.     UpdateClocks ();
  1576. #endif
  1577. #if defined(Think_C) && defined(Window_Events)
  1578.         check_events();
  1579. #endif
  1580. }
  1581. #endif
  1582.  
  1583.  
  1584. static SHORT Nmoves[] = NMOVEINIT;
  1585. void
  1586. SetTimeControl (void)
  1587. {
  1588.      flag.gamein = TCflag = false;
  1589.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  1590. if (XC){
  1591.                   if (XCmore < XC)
  1592.                     {
  1593.                       TCmoves = XCmoves[XCmore];
  1594.                       TCminutes = XCminutes[XCmore];
  1595.                       TCseconds = XCseconds[XCmore];
  1596.               TCadd = XCadd[XCmore];
  1597.                       XCmore++;
  1598.                     }
  1599.       if((TCminutes+TCseconds) == 0){TCflag = false;} else {
  1600.       TCflag = true;
  1601.       if(TCmoves == 0)
  1602.     {int i; i = (TCminutes*60+TCseconds + 40 * (TCadd/100))/60; 
  1603.         if(i > NMOVELIMIT) i = NMOVELIMIT; 
  1604.         TCmoves = Nmoves[i]; flag.gamein = true;}
  1605.     else flag.gamein = false;
  1606.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  1607.       TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
  1608.       TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
  1609.     }
  1610.   }
  1611.   flag.onemove = (TCmoves == 1);
  1612.   et = 0;
  1613.   ElapsedTime (1);
  1614. }
  1615.