home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 December / CHIP_CD_1997_12_PL.iso / software / szachy / gnuchess / src / dspcom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-07  |  30.1 KB  |  1,186 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.  * Modified by Conor McCarthy for the Windows environment
  7.  *
  8.  * This file is part of GNU CHESS.
  9.  *
  10.  * GNU Chess is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2, or (at your option)
  13.  * any later version.
  14.  *
  15.  * GNU Chess is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with GNU Chess; see the file COPYING.  If not, write to
  22.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. #ifdef WIN386
  26.  #define INCLUDE_COMMDLG_H
  27. #endif
  28. #include "gnuchess.h"
  29. #ifndef WIN386
  30.  #include <commdlg.h>
  31. #endif
  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. extern SHORT ok;
  50. char filter[]="Chess files (*.chs)\0*.chs\0All files (*.*)\0*.*\0";
  51. int Znodes;
  52. SHORT origplayer,forcing;
  53. void SaveSettings(void);
  54. extern BOOL CALLBACK (*lpfnDlgProc)(HWND, UINT, WPARAM, LPARAM);
  55. BOOL CALLBACK CompileBookDlgProc (HWND, UINT, WPARAM, LPARAM);
  56.  
  57. #include <ctype.h>
  58. #include <dos.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61.  
  62. void Replay(void);
  63.  
  64. void
  65. algbr (SHORT f, SHORT t, SHORT flag)
  66.  
  67. /*
  68.  * Generate move strings in different formats.
  69.  */
  70.  
  71. {
  72.   int m3p;
  73.  
  74.   if (f != t)
  75.     {
  76.       /* algebraic notation */
  77.       mvstr[0][0] = cxx[column (f)];
  78.       mvstr[0][1] = rxx[row (f)];
  79.       mvstr[0][2] = cxx[column (t)];
  80.       mvstr[0][3] = rxx[row (t)];
  81.       mvstr[4][0] = mvstr[0][4] = mvstr[3][0] = '\0';
  82.       if (((mvstr[1][0] = pxx[board[f]]) == CP[2][1]) || (flag & promote))
  83.    {
  84.      if (mvstr[0][0] == mvstr[0][2])   /* pawn did not eat */
  85.        {
  86.          mvstr[2][0] = mvstr[1][0] = mvstr[0][2];  /* to column */
  87.          mvstr[2][1] = mvstr[1][1] = mvstr[0][3];  /* to row */
  88.          m3p = 2;
  89.        }
  90.      else
  91.        /* pawn ate */
  92.        {
  93.          mvstr[2][0] = mvstr[1][0] = mvstr[0][0];  /* column */
  94.          mvstr[2][1] = mvstr[1][1] = mvstr[0][2];  /* to column */
  95.          mvstr[2][2] = mvstr[0][3];
  96.          m3p = 3;    /* to row */
  97.        }
  98.      if (flag & promote)
  99.        {
  100.          mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = qxx[flag & pmask];
  101.          mvstr[0][5] = mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[3][0] = '\0';
  102. #ifdef CHESSTOOL 
  103.          mvstr[3][0] = mvstr[0][0]; /* Allow e7e8 for chesstool */
  104.          mvstr[3][1] = mvstr[0][1];
  105.          mvstr[3][2] = mvstr[0][2];
  106.          mvstr[3][3] = mvstr[0][3];
  107.          mvstr[3][4] = '\0';
  108. #endif
  109.        } else mvstr[2][m3p] = mvstr[1][2] = '\0';
  110.    }
  111.       else
  112.    /* not a pawn */
  113.    {
  114.      mvstr[2][0] = mvstr[1][0];
  115.      mvstr[2][1] = mvstr[0][1];
  116.      mvstr[2][2] = mvstr[1][1] = mvstr[0][2];   /* to column */
  117.      mvstr[2][3] = mvstr[1][2] = mvstr[0][3];   /* to row */
  118.      mvstr[2][4] = mvstr[1][3] = '\0';
  119.      strcpy (mvstr[3], mvstr[2]);
  120.           mvstr[3][1] = mvstr[0][0];
  121.      mvstr[4][0] = mvstr[1][0]; strcpy(&mvstr[4][1],mvstr[0]);
  122.      if (flag & cstlmask)
  123.        {
  124.          if (t > f)
  125.       {
  126.         strcpy (mvstr[1], mvstr[0]);
  127.         strcpy (mvstr[0], CP[3]);
  128.         strcpy (mvstr[2], CP[5]);
  129.       }
  130.          else
  131.       {
  132.         strcpy (mvstr[1], mvstr[0]);
  133.         strcpy (mvstr[0], CP[4]);
  134.         strcpy (mvstr[2], CP[6]);
  135.       }
  136.        }
  137.    }
  138.     }
  139.   else
  140.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = mvstr[4][0] = '\0';
  141. }
  142.  
  143.  
  144. int
  145. VerifyMove (CHAR *s, SHORT iop, UTSHORT *mv)
  146.  
  147. /*
  148.  * Compare the string 's' to the list of legal moves available for the
  149.  * opponent. If a match is found, make the move on the board.
  150.  */
  151.  
  152. {
  153.   static SHORT pnt, tempb, tempc, tempsf, tempst, cnt;
  154.   static struct leaf xnode;
  155.   struct leaf *node;
  156.   *mv = 0;
  157.  
  158.   if (iop == 2)
  159.     {
  160.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  161.       return (false);
  162.     }
  163.   cnt = 0;
  164.   VMoveList (opponent, 2);
  165.   pnt = TrPnt[2];
  166.   while (pnt < TrPnt[3])
  167.     {
  168.       node = &Tree[pnt++];
  169.       algbr (node->f, node->t, (SHORT) node->flags);
  170.       if (strcmp (s, mvstr[0]) == 0 || strcmp (s, mvstr[1]) == 0 ||
  171.      strcmp (s, mvstr[2]) == 0 || strcmp (s, mvstr[3]) == 0
  172.       || strcmp (s, mvstr[4]) == 0)
  173.    {
  174.      cnt++;
  175.      xnode = *node;
  176.    }
  177.     }
  178.   if (cnt == 1)
  179.     {
  180.       MakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  181.       if (SqAtakd (PieceList[opponent][0], computer))
  182.    {
  183.      UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  184. #ifdef DEBUG8
  185.   if (1)
  186.     {   
  187.       FILE *D;
  188.       int r, c, l;
  189.       extern UTSHORT PrVar[];
  190.       D = fopen ("DEBUG", "a+");
  191.       pnt = TrPnt[2];
  192.       fprintf (D, "resp = %ld\n", ResponseTime);
  193.       fprintf (D, "iop = %d\n", iop);
  194.       fprintf (D, "matches = %d\n", cnt);
  195.       algbr (hint >> 8, hint & 0xff, (SHORT) 0);
  196.       fprintf (D, "hint %s\n", mvstr[0]);
  197.       fprintf (D, "inout move is %s\n", s);
  198.       for (r = 1; PrVar[r]; r++)
  199.         {
  200.           algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (SHORT) 0);
  201.           fprintf (D, " %s", mvstr[0]);
  202.         }
  203.       fprintf (D, "\n");
  204.       fprintf (D, "legal move are \n");
  205.       while (pnt < TrPnt[3])
  206.         {
  207.           node = &Tree[pnt++];
  208.           algbr (node->f, node->t, (SHORT) node->flags);
  209.           fprintf (D, "%s %s %s %s %s\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],mvstr[4]);
  210.         } 
  211.       fprintf (D, "\n current board is\n");
  212.       for (r = 7; r >= 0; r--)
  213.         {
  214.           for (c = 0; c <= 7; c++)
  215.             {
  216.               l = locn (r, c);
  217.               if (color[l] == neutral)
  218.                 fprintf (D, " -");
  219.               else if (color[l] == white)
  220.                 fprintf (D, " %c", qxx[board[l]]);
  221.               else
  222.                 fprintf (D, " %c", pxx[board[l]]);
  223.             }
  224.           fprintf (D, "\n");
  225.         }
  226.       fprintf (D, "\n");
  227.       fclose (D);
  228.     }  
  229. #endif 
  230.  
  231. /* Illegal move in check */
  232.      ShowMessage (CP[27]);
  233.  
  234.      return (false);
  235.    }
  236.       else
  237.    {
  238.      if (iop == 1)
  239.        return (true);
  240.      UpdateDisplay (xnode.f, xnode.t, 0, (SHORT) xnode.flags, 0);
  241.      if ((board[xnode.t] == pawn)
  242.          || (xnode.flags & capture)
  243.          || (xnode.flags & cstlmask))
  244.        {
  245.          Game50 = GameCnt;
  246.          ZeroRPT ();
  247.        }
  248.      GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  249.      GameList[GameCnt].nodes = 0;
  250.      ElapsedTime (1);
  251.      GameList[GameCnt].time = (SHORT) (et+50)/100;
  252.      if (TCflag)
  253.        {
  254.          TimeControl.clock[opponent] -= (et-TCadd);
  255.          timeopp[oppptr] = et;
  256.          --TimeControl.moves[opponent];
  257.        }
  258.      *mv = (xnode.f << 8) | xnode.t;
  259.      algbr (xnode.f, xnode.t, false);
  260.      return (true);
  261.    }
  262.     }
  263. #ifdef DEBUG8
  264.   if (1)
  265.     {
  266.       FILE *D;
  267.       int r, c, l;
  268.       extern UTSHORT PrVar[];
  269.       D = fopen ("DEBUG", "a+");
  270.       pnt = TrPnt[2];
  271.       fprintf (D, "resp = %ld\n", ResponseTime);
  272.       fprintf (D, "iop = %d\n", iop);
  273.       fprintf (D, "matches = %d\n", cnt);
  274.       algbr (hint >> 8, hint & 0xff, (SHORT) 0);
  275.       fprintf (D, "hint %s\n", mvstr[0]);
  276.       fprintf (D, "inout move is %s\n", s);
  277.       for (r = 1; PrVar[r]; r++)
  278.    {
  279.      algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (SHORT) 0);
  280.      fprintf (D, " %s", mvstr[0]);
  281.    }
  282.       fprintf (D, "\n");
  283.       fprintf (D, "legal move are \n");
  284.       while (pnt < TrPnt[3])
  285.    {
  286.      node = &Tree[pnt++];
  287.      algbr (node->f, node->t, (SHORT) node->flags);
  288.      fprintf (D, "%s %s %s %s %s\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],mvstr[4]);
  289.    }
  290.       fprintf (D, "\n current board is\n");
  291.       for (r = 7; r >= 0; r--)
  292.    {
  293.      for (c = 0; c <= 7; c++)
  294.        {
  295.          l = locn (r, c);
  296.          if (color[l] == neutral)
  297.       fprintf (D, " -");
  298.          else if (color[l] == white)
  299.       fprintf (D, " %c", qxx[board[l]]);
  300.          else
  301.       fprintf (D, " %c", pxx[board[l]]);
  302.        }
  303.      fprintf (D, "\n");
  304.    }
  305.       fprintf (D, "\n");
  306.       fclose (D);
  307.     }
  308. #endif
  309. /* Illegal move */
  310.   ShowMessage (CP[26]);
  311.   if (cnt > 1)
  312.     ShowMessage (CP[10]);
  313.   return (false);
  314. }
  315.  
  316. int
  317. parser (CHAR *f, int side)
  318. {
  319.   int c1, r1, c2, r2;
  320.  
  321.   if (f[4] == 'o')
  322.     if (side == black)
  323.       return 0x3C3A;
  324.     else
  325.       return 0x0402;
  326.   else if (f[0] == 'o')
  327.     if (side == black)
  328.       return 0x3C3E;
  329.     else
  330.       return 0x0406;
  331.   else
  332.     {
  333.       c1 = f[0] - 'a';
  334.       r1 = f[1] - '1';
  335.       c2 = f[2] - 'a';
  336.       r2 = f[3] - '1';
  337.       return (locn (r1, c1) << 8) | locn (r2, c2);
  338.     }
  339.   /*NOTREACHED*/
  340. }
  341.  
  342. SHORT
  343. GetGame (void)
  344. {
  345.   FILE *fd;
  346.   CHAR fname[256], *p;
  347.   int c, i, j;
  348.   int eps = -1;
  349.   int side;
  350.   SHORT sq;
  351.   OPENFILENAME ofn;
  352.   fname[0]='\0';
  353.   memset(&ofn,0,sizeof(ofn));
  354.   ofn.hwndOwner=hWnd;
  355.   ofn.lpstrFilter=filter;
  356.   ofn.nFilterIndex=1;
  357.   ofn.lpstrFile=fname;
  358.   ofn.nMaxFile=256;
  359.   ofn.lpstrTitle="Open Game";
  360. #ifdef WIN32
  361.   ofn.Flags=OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_EXPLORER;
  362. #else
  363.   ofn.Flags=OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;
  364. #endif
  365.   ofn.lStructSize=sizeof(ofn);
  366.   if (!GetOpenFileName(&ofn))return false;
  367.   if (fname[0] == '\0')
  368.     strcpy (fname, CP[44]);   /* chess.000 */
  369.   if ((fd = fopen (fname, "r")) != NULL)
  370.     {
  371.       NewGame ();
  372.       fgets (fname, 256, fd);
  373.       computer = opponent = white;
  374.       InPtr = fname;
  375.       skip ();
  376.       if (*InPtr == 'c')
  377.    computer = black;
  378.       else
  379.    opponent = black;
  380.       skip ();
  381.       skip ();
  382.       skip ();
  383.       Game50 = atoi (InPtr);
  384.       skip();
  385.       skip();
  386.       eps = atoi(InPtr);
  387.       fgets (fname, 256, fd);
  388.       InPtr = &fname[14];
  389.       castld[white] = ((*InPtr == CP[53][0]) ? true : false);
  390.       skip ();
  391.       skip ();
  392.       castld[black] = ((*InPtr == CP[53][0]) ? true : false);
  393.       fgets (fname, 256, fd);
  394.       InPtr = &fname[11];
  395.       skipb ();
  396.       TCflag = atoi (InPtr);
  397.       skip ();
  398.       InPtr += 14;
  399.       skipb ();
  400.       OperatorTime = atoi (InPtr);
  401.       fgets (fname, 256, fd);
  402.       InPtr = &fname[11];
  403.       skipb ();
  404.       TimeControl.clock[white] = atol (InPtr);
  405.       skip ();
  406.       skip ();
  407.       TimeControl.moves[white] = atoi (InPtr);
  408.       fgets (fname, 256, fd);
  409.       InPtr = &fname[11];
  410.       skipb ();
  411.       TimeControl.clock[black] = atol (InPtr);
  412.       skip ();
  413.       skip ();
  414.       TimeControl.moves[black] = atoi (InPtr);
  415.       fgets (fname, 256, fd);
  416.       for (i = 7; i > -1; i--)
  417.    {
  418.      fgets (fname, 256, fd);
  419.      p = &fname[2];
  420.      InPtr = &fname[11];
  421.      skipb ();
  422.      for (j = 0; j < 8; j++)
  423.        {
  424.          sq = i * 8 + j;
  425.          if (*p == '.')
  426.       {
  427.         board[sq] = no_piece;
  428.         color[sq] = neutral;
  429.       }
  430.          else
  431.       {
  432.         for (c = 0; c < 8; c++)
  433.           {
  434.             if (*p == pxx[c])
  435.          {
  436.            board[sq] = c;
  437.            color[sq] = black;
  438.          }
  439.           }
  440.         for (c = 0; c < 8; c++)
  441.           {
  442.             if (*p == qxx[c])
  443.          {
  444.            board[sq] = c;
  445.            color[sq] = white;
  446.          }
  447.           }
  448.       }
  449.          p++;
  450.          Mvboard[sq] = atoi (InPtr);
  451.          skip ();
  452.        }
  453.    }
  454.       GameCnt = 0;
  455.       flag.regularstart = true;
  456.       Book = BOOKFAIL;
  457.       fgets (fname, 256, fd);
  458.       fgets (fname, 256, fd);
  459.       fgets (fname, 256, fd);
  460.       side = black;
  461.       while (fgets (fname, 256, fd))
  462.    {
  463.      struct GameRec *g;
  464.  
  465.      ++GameCnt;
  466.      InPtr = fname;
  467.      skipb ();
  468.      g = &GameList[GameCnt];
  469.      side = side ^ 1;
  470.      g->gmove = parser (InPtr, side);
  471.      skip ();
  472.      g->score = atoi (InPtr);
  473.      skip ();
  474.      g->depth = atoi (InPtr);
  475.      skip ();
  476.      g->nodes = atol (InPtr);
  477.      skip ();
  478.      g->time = atol (InPtr);
  479.      skip ();
  480.      g->flags = c = atoi (InPtr);
  481.      skip ();
  482. #ifdef HAVE_STRTOUL
  483.      g->hashkey = strtoul (InPtr, (CHAR **) NULL, 16);
  484.      skip ();
  485.      g->hashbd = strtoul (InPtr, (CHAR **) NULL, 16);
  486. #else
  487.      g->hashkey = strtol (InPtr, (CHAR **) NULL, 16);
  488.      skip ();
  489.      g->hashbd = strtol (InPtr, (CHAR **) NULL, 16);
  490. #endif
  491.      skip ();
  492.      g->epssq = atoi(InPtr);
  493.      g->piece = no_piece;
  494.      g->color = neutral;
  495.      if (c & (capture | cstlmask))
  496.        {
  497.          if (c & capture)
  498.       {
  499.         skip ();
  500.         for (c = 0; c < 8; c++)
  501.           if (pxx[c] == *InPtr)
  502.             break;
  503.         g->piece = c;
  504.       }
  505.          skip ();
  506.          g->color = ((*InPtr == CP[41][0]) ? black : white);
  507.        }
  508.    }
  509. /*      if (TimeControl.clock[white] > 0)
  510.    TCflag = true; */
  511.       if (TCflag == false && !MaxResponseTime)
  512.          MaxResponseTime = 3000;
  513.       fclose (fd);
  514.       ZeroRPT ();
  515.       InitializeStats ();
  516.       epsquare = eps;
  517.       UpdateDisplay (0, 0, 1, 0, 0);
  518.       ShowPlayers();
  519.       Sdepth = 0;
  520.       hint = 0;
  521.       return true;
  522.     }
  523.    else
  524.     {
  525.      ShowMessage("Could not open file");
  526.      return false;
  527.     }
  528. }
  529.  
  530. void
  531. SaveGame (void)
  532. {
  533.   FILE *fd;
  534.   CHAR fname[256];
  535.   SHORT sq, i, c, f, t;
  536.   CHAR p;
  537.   OPENFILENAME ofn;
  538.  
  539.   fname[0]='\0';
  540.   memset(&ofn,0,sizeof(ofn));
  541.   ofn.hwndOwner=hWnd;
  542.   ofn.lpstrFilter=filter;
  543.   ofn.nFilterIndex=1;
  544.   ofn.lpstrFile=fname;
  545.   ofn.nMaxFile=256;
  546.   ofn.lpstrTitle="Save Game";
  547. #ifdef WIN32
  548.   ofn.Flags=OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_EXPLORER;
  549. #else
  550.   ofn.Flags=OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;
  551. #endif
  552.   ofn.lStructSize=sizeof(ofn);
  553.   if (!GetSaveFileName(&ofn))return;
  554.   if (fname[0] == '\0')
  555. /* chess.000 */
  556.     strcpy (fname, CP[44]);
  557.   if ((fd = fopen (fname, "w")) != NULL)
  558.     {
  559.       CHAR *b, *w;
  560.  
  561.       b = w = CP[24];
  562.       if (computer == black)
  563.    b = CP[45];
  564.       if (computer == white)
  565.    w = CP[45];
  566.       fprintf (fd, CP[13], b, w, Game50,epsquare);
  567.       fprintf (fd, CP[14], castld[white] ? CP[53] : CP[54], castld[black] ? CP[53] : CP[54]);
  568.       fprintf (fd, CP[37], TCflag, OperatorTime);
  569.       fprintf (fd, CP[39],
  570.           TimeControl.clock[white], TimeControl.moves[white],
  571.           TimeControl.clock[black], TimeControl.moves[black]);
  572.       for (i = 7; i > -1; i--)
  573.    {
  574.      fprintf (fd, "%1d ", i + 1);
  575.      for (c = 0; c < 8; c++)
  576.        {
  577.          sq = i * 8 + c;
  578.          switch (color[sq])
  579.       {
  580.       case black:
  581.         p = pxx[board[sq]];
  582.         break;
  583.       case white:
  584.         p = qxx[board[sq]];
  585.         break;
  586.       default:
  587.         p = '.';
  588.       }
  589.          fprintf (fd, "%c", p);
  590.        }
  591.      for (f = i * 8; f < i * 8 + 8; f++)
  592.        fprintf (fd, " %d", Mvboard[f]);
  593.      fprintf (fd, "\n");
  594.    }
  595.       fprintf (fd, "  %s\n", cxx);
  596.       fprintf (fd, CP[43]);
  597.       for (i = 1; i <= GameCnt; i++)
  598.    {
  599.      struct GameRec *g = &GameList[i];
  600.  
  601.      f = g->gmove >> 8;
  602.      t = (g->gmove & 0xFF);
  603.      algbr (f, t, g->flags);
  604.      fprintf (fd, "%s %5d %5d %7ld %6ld %5d  %#08lx %#08lx %d %c   %s\n",
  605.          mvstr[0], g->score, g->depth,
  606.          g->nodes, g->time, g->flags, g->hashkey, g->hashbd,g->epssq,
  607.       pxx[g->piece], (((int)(g->color) == 2) ? (CHAR *)"     " : ColorStr[g->color]));
  608.    }
  609.       fclose (fd);
  610. /* Game saved */
  611.       ShowMessage (CP[23]);
  612.     }
  613.   else
  614.     /*ShowMessage ("Could not open file");*/
  615.     ShowMessage (CP[17]);
  616. }
  617.  
  618. void
  619. ListGame (void)
  620. {
  621.   FILE *fd;
  622.   SHORT i, f, t;
  623.   CHAR fname[256];
  624.   OPENFILENAME ofn;
  625.  
  626.   fname[0]='\0';
  627.   memset(&ofn,0,sizeof(ofn));
  628.   ofn.hwndOwner=hWnd;
  629.   ofn.lpstrFilter="List files (*.lst)\0*.lst\0";
  630.   ofn.nFilterIndex=1;
  631.   ofn.lpstrFile=fname;
  632.   ofn.nMaxFile=256;
  633.   ofn.lpstrTitle="List Game In...";
  634. #ifdef WIN32
  635.   ofn.Flags=OFN_HIDEREADONLY|OFN_EXPLORER;
  636. #else
  637.   ofn.Flags=OFN_HIDEREADONLY;
  638. #endif
  639.   ofn.lStructSize=sizeof(ofn);
  640.   if (!GetSaveFileName(&ofn))return;
  641.   fd = fopen (fname, "w");
  642.   if (!fd)
  643.     {
  644.       sprintf (msg,CP[55], fname);
  645.       ShowMessage(msg);
  646.       return;
  647.     }
  648.   /*fprintf (fd, "gnuchess game %d\n", u);*/
  649.   fprintf (fd, CP[48], patchlevel);
  650.   fprintf (fd, CP[8]);
  651.   fprintf (fd, CP[9]);
  652.   for (i = 1; i <= GameCnt; i++)
  653.     {
  654.       f = GameList[i].gmove >> 8;
  655.       t = (GameList[i].gmove & 0xFF);
  656.       algbr (f, t, GameList[i].flags);
  657.       if(GameList[i].flags & book)
  658.           fprintf (fd, "%6s  %5d    Book%7ld %5ld", mvstr[0],
  659.           GameList[i].score, 
  660.           GameList[i].nodes, GameList[i].time);
  661.       else
  662.           fprintf (fd, "%6s  %5d     %2d %7ld %5ld", mvstr[0],
  663.           GameList[i].score, GameList[i].depth,
  664.           GameList[i].nodes, GameList[i].time);
  665.       if ((i % 2) == 0)
  666.    {
  667. #ifdef DEBUG40
  668.    if(computer == black){
  669.      int p;
  670.    for(p=0;GameList[i-1].d1[p] && p < 7;p++){
  671.       algbr((GameList[i-1].d1[p]>>8) & 0xff,GameList[i-1].d1[p] & 0xff,0);
  672.            fprintf(fd," %s",mvstr[0]);
  673.    }
  674.    fprintf(fd,"\n");
  675.    } else {
  676.    int p;
  677.    for(p=0;GameList[i].d1[p] && p < 7;p++){
  678.       algbr(GameList[i].d1[p]>>8 & 0xff,GameList[i].d1[p] & 0xff,0);
  679.            fprintf(fd," %s",mvstr[0]);
  680.    }
  681.    fprintf(fd,"\n");
  682.    }
  683. #else
  684.    fprintf(fd,"\n");
  685. #endif
  686.     }
  687.    }
  688.   fprintf (fd, "\n\n");
  689.   if (GameList[GameCnt].flags & draw)
  690.     {
  691.       fprintf (fd, CP[20], DRAW);
  692.     }
  693.   else if (GameList[GameCnt].score == -9999)
  694.     {
  695.       fprintf (fd, "%s\n", ColorStr[player ]);
  696.     }
  697.   else if (GameList[GameCnt].score == 9998)
  698.     {
  699.       fprintf (fd, "%s\n", ColorStr[player ^ 1]);
  700.     }
  701.   fclose (fd);
  702. }
  703.  
  704. void
  705. Undo (void)
  706.  
  707. /*
  708.  * Undo the most recent half-move.
  709.  */
  710.  
  711. {
  712.   SHORT f, t;
  713.   f = GameList[GameCnt].gmove >> 8;
  714.   t = GameList[GameCnt].gmove & 0xFF;
  715.   if (board[t] == king && distance (t, f) > 1)
  716.     (void) castle (GameList[GameCnt].color, f, t, 2);
  717.   else
  718.     {
  719.       /* Check for promotion: */
  720.       if (GameList[GameCnt].flags & promote)
  721.    {
  722.      board[t] = pawn;
  723.    }
  724.       board[f] = board[t];
  725.       color[f] = color[t];
  726.       board[t] = GameList[GameCnt].piece;
  727.       color[t] = GameList[GameCnt].color;
  728.       if (color[t] != neutral)
  729.    Mvboard[t]--;
  730.       Mvboard[f]--;
  731.     }
  732.   if (GameList[GameCnt].flags & epmask)
  733.     EnPassant (otherside[color[f]], f, t, 2);
  734.   else
  735.     InitializeStats ();
  736.   epsquare = GameList[GameCnt].epssq;
  737.   if (TCflag && (TCmoves>1))
  738.     ++TimeControl.moves[color[f]];
  739.   hashkey = GameList[GameCnt].hashkey;
  740.   hashbd = GameList[GameCnt].hashbd;
  741.   GameCnt--;
  742.   flag.mate = false;
  743.   hint = 0;
  744.   Sdepth = 0;
  745.   origplayer ^= 1;
  746.   ShowSidetoMove ();
  747. /*
  748.   if (flag.regularstart)
  749. */
  750.   if (flag.usebook) Book = BOOKFAIL;
  751. }
  752.  
  753. SHORT
  754.  TestSpeed (void (*f) (SHORT side, SHORT ply))
  755. {
  756.   unsigned i;
  757.   long cnt, rate=0, t1, t2;
  758.  
  759.   t1 = GetTickCount();
  760.   for (i = 0; i < 5000; i++)
  761.     {
  762.       f (opponent, 2);
  763.     }
  764.   t2 = GetTickCount();
  765.   cnt = 5000L * (TrPnt[3] - TrPnt[2]);
  766.   if (t2!=t1)rate = cnt*1000/(t2-t1);
  767.   sprintf(msg,"Nodes= %u, Nodes/Sec= %u", cnt, rate);
  768.   MessageBox(0,msg,"",0);
  769.   return(TrPnt[3] - TrPnt[2]);
  770. }
  771.  
  772. void
  773.  TestPSpeed (SHORT (*f) (SHORT side), unsigned j)
  774. {
  775.   SHORT i;
  776.   long cnt, rate, t1, t2;
  777. #ifdef HAVE_GETTIMEOFDAY
  778. struct timeval tv;
  779. #endif
  780.  
  781. #ifdef HAVE_GETTIMEOFDAY
  782.   gettimeofday(&tv,NULL);
  783.   t1 = (tv.tv_sec*100+(tv.tv_usec/10000));
  784. #else
  785.   t1 = time (0);
  786. #endif
  787.   for (i = 0; i < j; i++)
  788.     {
  789.       (void) f (opponent);
  790.     }
  791. #ifdef HAVE_GETTIMEOFDAY
  792.   gettimeofday(&tv,NULL);
  793.   t2 = (tv.tv_sec*100+(tv.tv_usec/10000));
  794. #else
  795.   t2 = time (0);
  796. #endif
  797.   cnt = j;
  798.   if (t2 - t1)
  799.     et = (t2 - t1);
  800.   else
  801.     et = 1;
  802.   rate = (et) ? ((cnt*100) / et) : 0;
  803.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  804.   ShowNodeCnt (cnt);
  805. }
  806.  
  807. void
  808. InputCommand (void)
  809.  
  810. /*
  811.  * Process the users command. If easy mode is OFF (the computer is thinking
  812.  * on opponents time) and the program is out of book, then make the 'hint'
  813.  * move on the board and call SelectMove() to find a response. The user
  814.  * terminates the search by entering ^C (quit siqnal) before entering a
  815.  * command. If the opponent does not make the hint move, then set Sdepth to
  816.  * zero.
  817.  */
  818.  
  819. {
  820. #ifdef QUIETBACKGROUND
  821.   SHORT have_shown_prompt = false;
  822. #endif
  823.   SHORT tmp;
  824.   UTSHORT mv;
  825.   CHAR s[80], sx[80];
  826.   CHAR *p, *q;
  827.  
  828. #if defined CHESSTOOL
  829.   SHORT normal = false;
  830. #endif
  831.  
  832.   ok = flag.quit = flag.bothsides = false;
  833.   if (command>0 && !(command & 0x8000))origplayer=computer;
  834.   else origplayer=opponent;
  835.   command&=0x7FFF;
  836.   ElapsedTime(1);
  837.   player = opponent;
  838. /* if transposition table is filling start it over */
  839.  ZeroTTable(1); /* clear ageing table entries */
  840. /* if we have a move for our opponent, and we are thinking on his time, and not in force mode */
  841.   if (hint > 0 && !flag.easy && !flag.force && !command)
  842. /* if the hint is a promotion don't do anything, we don't know what to promote to. */
  843.     if ((board[hint >> 8] != pawn) || ((row (hint & 0x3f) != 0) && (row (hint & 0x3f) != 7)))
  844.       {
  845.    ft = time0;
  846. /* create the hint move as string so we can do it */
  847.    algbr ((SHORT) hint >> 8, (SHORT) hint & 0x3f, false);
  848.    strcpy (s, mvstr[0]);
  849.    tmp = epsquare;
  850. #ifdef DEBUG12
  851. #include "debug12.h"
  852. #endif
  853.    SaveBoard();
  854. /* do the hint move */
  855.    if (VerifyMove (s, 1, &mv))
  856.      {
  857. #ifdef QUIETBACKGROUND
  858.        ShowSidetoMove ();
  859.        have_shown_prompt = true;
  860. #endif /* QUIETBACKGROUND */
  861. /* would love to put null move in here */
  862. /* after we make the hint move make a 2 ply search with both plys our moves */
  863. /* think on opponents time */
  864.        SelectMove (computer, 2);
  865. /* undo the hint and carry on */
  866.        VerifyMove (s, 2, &mv);
  867.      }
  868.    epsquare = tmp;
  869.    time0 = ft;
  870.       }
  871.   while (!(ok || flag.quit))
  872.     {
  873. #if defined CHESSTOOL
  874.       normal = false;
  875. #endif
  876.       player = opponent;
  877. #ifdef QUIETBACKGROUND
  878.       if (!have_shown_prompt)
  879.    {
  880. #endif /* QUIETBACKGROUND */
  881.      ShowSidetoMove ();
  882. #ifdef QUIETBACKGROUND
  883.    }
  884.       have_shown_prompt = false;
  885. #endif /* QUIETBACKGROUND */
  886.       while (!command)
  887.        {
  888.          CheckMessage();
  889.          ElapsedTime(0);
  890.        }
  891.       if (flag.quit)Exit(0);
  892.       strcpy(sx,move);
  893.       sscanf (sx, "%s", s);
  894.       if (command>100)
  895.       {
  896.        short cmnd;
  897.        cmnd=command;
  898.        command=0;
  899.        switch (cmnd)
  900.        {
  901.         case IDM_NEW:
  902.           origplayer=white;
  903.           NewGame();
  904.           UpdateDisplay(0,0,1,0,0);
  905.           break;
  906.         case IDM_GET:
  907.           if (!GetGame())
  908.            {
  909.             player=origplayer;
  910.             if (player==computer)ok=true;
  911.            }
  912.           else origplayer=opponent;
  913.           break;
  914.         case IDM_SAVE:
  915.           SaveGame();
  916.           break;
  917.         case IDM_LIST:
  918.           ListGame();
  919.           player=origplayer;
  920.           if (player==computer)ok=true;
  921.           break;
  922.         case IDM_QUIT:
  923.           flag.quit=true;
  924.           break;
  925.         case IDM_EDIT:
  926.           EditBoard();
  927.           origplayer=opponent;
  928.           hint=0;
  929.           break;
  930.         case IDM_REVIEW:
  931.           Replay();
  932.           player=origplayer;
  933.           if (player==computer)ok=true;
  934.           break;
  935.         case IDM_REMOVE:
  936.           Undo();
  937.         case IDM_UNDO:
  938.           Undo();
  939.           player=origplayer;
  940.           if (player==computer) ok = true;
  941.           UpdateDisplay (0, 0, 1, 0, 0);
  942.           break;
  943.         case IDM_FORCE:
  944.           flag.force = !flag.force;
  945.           flag.bothsides = false;
  946.           if (!flag.force && forcing)
  947.            {
  948.             Sdepth=0;
  949.             ok=true;
  950.             computer^=1;
  951.             opponent^=1;
  952.            }
  953.           else
  954.            {
  955.             if (origplayer==opponent) forcing=0;
  956.             else
  957.              {
  958.               forcing=1;
  959.               computer^=1;
  960.               opponent^=1;
  961.              }
  962.            }
  963.           break;
  964.         case IDM_COMPILE:
  965.           lpfnDlgProc = MakeProcInstance ( CompileBookDlgProc, hInst);
  966.           DialogBox ( hInst, MAKEINTRESOURCE(COMPILEBOOK), hWnd,
  967.                 lpfnDlgProc);
  968.           FreeProcInstance ( lpfnDlgProc);
  969.           break;
  970.         case IDM_BOTH:
  971.          flag.bothsides = true;
  972.          if (flag.force && forcing)
  973.           {
  974.            computer^=1;
  975.            opponent^=1;
  976.           }
  977.          flag.force=false;
  978.          Sdepth = 0;
  979.          ElapsedTime (1);
  980.          SaveBoard();
  981.          SelectMove (opponent, 1);
  982.          ok = true;
  983.          break;
  984.  
  985.         case IDM_SWITCHDM:
  986.           player=origplayer=computer;
  987.         case IDM_SWITCH:
  988.           computer = computer ^ 1;
  989.           opponent = opponent ^ 1;
  990.           xwndw = (computer == white) ? WXwindow : BXwindow;
  991.           goto changeside;
  992.         case IDM_WHITE:
  993.         case IDM_BLACK:
  994.          {
  995.           SHORT color=(cmnd-IDM_BLACK)^1;
  996.           if (computer==color^(flag.force && forcing))continue;
  997.           if (flag.force && forcing)
  998.            {
  999.             computer = color^1;
  1000.             opponent = color;
  1001.            }
  1002.           else
  1003.            {
  1004.             computer = color;
  1005.             opponent = color^1;
  1006.            }
  1007.           xwndw = computer==white ? WXwindow : BXwindow;
  1008.          }
  1009. changeside:
  1010.           if (!flag.force)
  1011.            {
  1012.             Sdepth = 0;
  1013.             player=origplayer;
  1014.             if (player==computer) ok=true;
  1015.             ShowPlayers ();
  1016.            }
  1017.           else
  1018.            {
  1019.             computer^=forcing;
  1020.             ShowPlayers();
  1021.             computer^=forcing;
  1022.             computer^=1;
  1023.             opponent^=1;
  1024.             forcing^=1;
  1025.            }
  1026.           if (computer^(flag.force & forcing)==flag.reverse)
  1027.            {
  1028.             flag.reverse=!(computer^(flag.force & forcing));
  1029.             InvalidateRect(hWnd,0,true);
  1030.             UpdateWindow(hWnd);
  1031.            }
  1032.           hint=0;
  1033.           break;
  1034.         case IDM_WTM:
  1035.         case IDM_BTM:
  1036.           player = origplayer = (cmnd==IDM_WTM)? white : black;
  1037.           if (player==computer)ok = true;
  1038.           if (flag.force)
  1039.            {
  1040.             computer = opponent;
  1041.             opponent = computer ^ 1;
  1042.             forcing ^= 1;
  1043.            }
  1044.           InitializeStats(); /*May crash or make illegal move without this.*/
  1045.        }
  1046.       }
  1047.    else
  1048.    {
  1049. /* It must be a move */
  1050. /* preprocess move;
  1051.    remove x or +
  1052.    change promotion from h1=Q to h1q
  1053. */
  1054.    p=s; q=sx;
  1055.    while (*p != '\0'){
  1056.    if(*p == 'x' || *p == '+') p++;
  1057.    else if(*p == '='){ p++; *q = tolower(*p); p++; q++;}
  1058.       else {*q++ = *p++; }
  1059.    }
  1060.    *q = '\0';
  1061.  
  1062.    ok = VerifyMove (sx, 0, &mv);
  1063.    command=0;
  1064.    if (flag.force && ok)
  1065.     {
  1066.       hint=0;
  1067.       computer = opponent;
  1068.       opponent = computer ^ 1;
  1069.       forcing ^= 1;
  1070.     }
  1071.    }
  1072.     }
  1073.  
  1074.   /* guess is correct ?*/
  1075.   Sdepth = (hint == mv) ? Sdepth-1 : 0;
  1076.   if (Tree[0].score > 9000) Sdepth = 0;
  1077.  
  1078. }
  1079.  
  1080. #ifdef HAVE_GETTIMEOFDAY
  1081. void
  1082. ElapsedTime (SHORT iop)
  1083.  
  1084.  
  1085. /*
  1086.  * Determine the time that has passed since the search was started. If the
  1087.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1088.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1089.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1090.  */
  1091.  
  1092. { long old_et;
  1093.   struct timeval tv;
  1094.  
  1095.   old_et=et;
  1096.   gettimeofday(&tv,NULL);
  1097.   et = (tv.tv_sec*100+(tv.tv_usec/10000)) - time0;
  1098.   ETnodes = NodeCnt + Znodes;
  1099.   if (et < 0)
  1100.     et = 0;
  1101.   if (iop == 1)
  1102.     {
  1103.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1104.    flag.timeout = true;
  1105.       ETnodes = NodeCnt + Znodes;
  1106.       gettimeofday(&tv,NULL);
  1107.       time0 = tv.tv_sec*100+tv.tv_usec/10000;
  1108.     }
  1109. #if !defined NONDSP
  1110. #ifdef QUIETBACKGROUND
  1111.   if (!background)
  1112. #endif /* QUIETBACKGROUND */
  1113.     if(et/100>old_et/100 && !flag.mate)UpdateClocks ();
  1114. #endif
  1115. #if defined(Think_C) && defined(Window_Events)
  1116.         check_events();
  1117. #endif
  1118.  
  1119. }
  1120. #else
  1121. void
  1122. ElapsedTime (SHORT iop)
  1123.  
  1124. /*
  1125.  * Determine the time that has passed since the search was started. If the
  1126.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1127.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1128.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1129.  */
  1130. { long old_et;
  1131.  
  1132.   old_et=et;
  1133.   et = (GetTickCount() - time0)/10;
  1134.   ETnodes = NodeCnt + Znodes;
  1135.   if (et < 0)
  1136.     et = -et;
  1137.   if (iop == 1)
  1138.     {
  1139.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1140.    flag.timeout = true;
  1141.       ETnodes = NodeCnt + Znodes;
  1142.       time0 = GetTickCount();
  1143.     }
  1144. #ifdef QUIETBACKGROUND
  1145.   if (!background)
  1146. #endif /* QUIETBACKGROUND */
  1147.     if (et/100>old_et/100 && !flag.mate)UpdateClocks ();
  1148. #if defined(Think_C) && defined(Window_Events)
  1149.         check_events();
  1150. #endif
  1151. }
  1152. #endif
  1153.  
  1154.  
  1155. static SHORT Nmoves[] = NMOVEINIT;
  1156. void
  1157. SetTimeControl (void)
  1158. {
  1159.      flag.gamein = TCflag = false;
  1160.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  1161. if (XC){
  1162.                   if (XCmore < XC)
  1163.                     {
  1164.                       TCmoves = XCmoves[XCmore];
  1165.                       TCminutes = XCminutes[XCmore];
  1166.                       TCseconds = XCseconds[XCmore];
  1167.             TCadd = XCadd[XCmore];
  1168.                       XCmore++;
  1169.                     }
  1170.       if((TCminutes+TCseconds) == 0){TCflag = false;} else {
  1171.       TCflag = true;
  1172.       if(TCmoves == 0)
  1173.    {int i; i = (TCminutes*60+TCseconds + 40 * (TCadd/100))/60; 
  1174.       if(i > NMOVELIMIT) i = NMOVELIMIT; 
  1175.       TCmoves = Nmoves[i]; flag.gamein = true;}
  1176.    else flag.gamein = false;
  1177.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  1178.       TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
  1179.       TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
  1180.     }
  1181.   }
  1182.   flag.onemove = (TCmoves == 1);
  1183.   et = 0;
  1184.   ElapsedTime (1);
  1185. }
  1186.