home *** CD-ROM | disk | FTP | other *** search
/ Más de 2,500 Juegos / CD1.iso / ZIPDAT / 0153 / 0153.ZIP / SRC / DSPCOM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-05  |  30.1 KB  |  1,187 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.   ofn.lpstrDefExt="chs";
  554.   if (!GetSaveFileName(&ofn))return;
  555.   if (fname[0] == '\0')
  556. /* chess.000 */
  557.     strcpy (fname, CP[44]);
  558.   if ((fd = fopen (fname, "w")) != NULL)
  559.     {
  560.       CHAR *b, *w;
  561.  
  562.       b = w = CP[24];
  563.       if (computer == black)
  564.    b = CP[45];
  565.       if (computer == white)
  566.    w = CP[45];
  567.       fprintf (fd, CP[13], b, w, Game50,epsquare);
  568.       fprintf (fd, CP[14], castld[white] ? CP[53] : CP[54], castld[black] ? CP[53] : CP[54]);
  569.       fprintf (fd, CP[37], TCflag, OperatorTime);
  570.       fprintf (fd, CP[39],
  571.           TimeControl.clock[white], TimeControl.moves[white],
  572.           TimeControl.clock[black], TimeControl.moves[black]);
  573.       for (i = 7; i > -1; i--)
  574.    {
  575.      fprintf (fd, "%1d ", i + 1);
  576.      for (c = 0; c < 8; c++)
  577.        {
  578.          sq = i * 8 + c;
  579.          switch (color[sq])
  580.       {
  581.       case black:
  582.         p = pxx[board[sq]];
  583.         break;
  584.       case white:
  585.         p = qxx[board[sq]];
  586.         break;
  587.       default:
  588.         p = '.';
  589.       }
  590.          fprintf (fd, "%c", p);
  591.        }
  592.      for (f = i * 8; f < i * 8 + 8; f++)
  593.        fprintf (fd, " %d", Mvboard[f]);
  594.      fprintf (fd, "\n");
  595.    }
  596.       fprintf (fd, "  %s\n", cxx);
  597.       fprintf (fd, CP[43]);
  598.       for (i = 1; i <= GameCnt; i++)
  599.    {
  600.      struct GameRec *g = &GameList[i];
  601.  
  602.      f = g->gmove >> 8;
  603.      t = (g->gmove & 0xFF);
  604.      algbr (f, t, g->flags);
  605.      fprintf (fd, "%s %5d %5d %7ld %6ld %5d  %#08lx %#08lx %d %c   %s\n",
  606.          mvstr[0], g->score, g->depth,
  607.          g->nodes, g->time, g->flags, g->hashkey, g->hashbd,g->epssq,
  608.       pxx[g->piece], (((int)(g->color) == 2) ? (CHAR *)"     " : ColorStr[g->color]));
  609.    }
  610.       fclose (fd);
  611. /* Game saved */
  612.       ShowMessage (CP[23]);
  613.     }
  614.   else
  615.     /*ShowMessage ("Could not open file");*/
  616.     ShowMessage (CP[17]);
  617. }
  618.  
  619. void
  620. ListGame (void)
  621. {
  622.   FILE *fd;
  623.   SHORT i, f, t;
  624.   CHAR fname[256];
  625.   OPENFILENAME ofn;
  626.  
  627.   fname[0]='\0';
  628.   memset(&ofn,0,sizeof(ofn));
  629.   ofn.hwndOwner=hWnd;
  630.   ofn.lpstrFilter="List files (*.lst)\0*.lst\0";
  631.   ofn.nFilterIndex=1;
  632.   ofn.lpstrFile=fname;
  633.   ofn.nMaxFile=256;
  634.   ofn.lpstrTitle="List Game In...";
  635. #ifdef WIN32
  636.   ofn.Flags=OFN_HIDEREADONLY|OFN_EXPLORER;
  637. #else
  638.   ofn.Flags=OFN_HIDEREADONLY;
  639. #endif
  640.   ofn.lStructSize=sizeof(ofn);
  641.   if (!GetSaveFileName(&ofn))return;
  642.   fd = fopen (fname, "w");
  643.   if (!fd)
  644.     {
  645.       sprintf (msg,CP[55], fname);
  646.       ShowMessage(msg);
  647.       return;
  648.     }
  649.   /*fprintf (fd, "gnuchess game %d\n", u);*/
  650.   fprintf (fd, CP[48], patchlevel);
  651.   fprintf (fd, CP[8]);
  652.   fprintf (fd, CP[9]);
  653.   for (i = 1; i <= GameCnt; i++)
  654.     {
  655.       f = GameList[i].gmove >> 8;
  656.       t = (GameList[i].gmove & 0xFF);
  657.       algbr (f, t, GameList[i].flags);
  658.       if(GameList[i].flags & book)
  659.           fprintf (fd, "%6s  %5d    Book%7ld %5ld", mvstr[0],
  660.           GameList[i].score, 
  661.           GameList[i].nodes, GameList[i].time);
  662.       else
  663.           fprintf (fd, "%6s  %5d     %2d %7ld %5ld", mvstr[0],
  664.           GameList[i].score, GameList[i].depth,
  665.           GameList[i].nodes, GameList[i].time);
  666.       if ((i % 2) == 0)
  667.    {
  668. #ifdef DEBUG40
  669.    if(computer == black){
  670.      int p;
  671.    for(p=0;GameList[i-1].d1[p] && p < 7;p++){
  672.       algbr((GameList[i-1].d1[p]>>8) & 0xff,GameList[i-1].d1[p] & 0xff,0);
  673.            fprintf(fd," %s",mvstr[0]);
  674.    }
  675.    fprintf(fd,"\n");
  676.    } else {
  677.    int p;
  678.    for(p=0;GameList[i].d1[p] && p < 7;p++){
  679.       algbr(GameList[i].d1[p]>>8 & 0xff,GameList[i].d1[p] & 0xff,0);
  680.            fprintf(fd," %s",mvstr[0]);
  681.    }
  682.    fprintf(fd,"\n");
  683.    }
  684. #else
  685.    fprintf(fd,"\n");
  686. #endif
  687.     }
  688.    }
  689.   fprintf (fd, "\n\n");
  690.   if (GameList[GameCnt].flags & draw)
  691.     {
  692.       fprintf (fd, CP[20], DRAW);
  693.     }
  694.   else if (GameList[GameCnt].score == -9999)
  695.     {
  696.       fprintf (fd, "%s\n", ColorStr[player ]);
  697.     }
  698.   else if (GameList[GameCnt].score == 9998)
  699.     {
  700.       fprintf (fd, "%s\n", ColorStr[player ^ 1]);
  701.     }
  702.   fclose (fd);
  703. }
  704.  
  705. void
  706. Undo (void)
  707.  
  708. /*
  709.  * Undo the most recent half-move.
  710.  */
  711.  
  712. {
  713.   SHORT f, t;
  714.   f = GameList[GameCnt].gmove >> 8;
  715.   t = GameList[GameCnt].gmove & 0xFF;
  716.   if (board[t] == king && distance (t, f) > 1)
  717.     (void) castle (GameList[GameCnt].color, f, t, 2);
  718.   else
  719.     {
  720.       /* Check for promotion: */
  721.       if (GameList[GameCnt].flags & promote)
  722.    {
  723.      board[t] = pawn;
  724.    }
  725.       board[f] = board[t];
  726.       color[f] = color[t];
  727.       board[t] = GameList[GameCnt].piece;
  728.       color[t] = GameList[GameCnt].color;
  729.       if (color[t] != neutral)
  730.    Mvboard[t]--;
  731.       Mvboard[f]--;
  732.     }
  733.   if (GameList[GameCnt].flags & epmask)
  734.     EnPassant (otherside[color[f]], f, t, 2);
  735.   else
  736.     InitializeStats ();
  737.   epsquare = GameList[GameCnt].epssq;
  738.   if (TCflag && (TCmoves>1))
  739.     ++TimeControl.moves[color[f]];
  740.   hashkey = GameList[GameCnt].hashkey;
  741.   hashbd = GameList[GameCnt].hashbd;
  742.   GameCnt--;
  743.   flag.mate = false;
  744.   hint = 0;
  745.   Sdepth = 0;
  746.   origplayer ^= 1;
  747.   ShowSidetoMove ();
  748. /*
  749.   if (flag.regularstart)
  750. */
  751.   if (flag.usebook) Book = BOOKFAIL;
  752. }
  753.  
  754. SHORT
  755.  TestSpeed (void (*f) (SHORT side, SHORT ply))
  756. {
  757.   unsigned i;
  758.   long cnt, rate=0, t1, t2;
  759.  
  760.   t1 = GetTickCount();
  761.   for (i = 0; i < 5000; i++)
  762.     {
  763.       f (opponent, 2);
  764.     }
  765.   t2 = GetTickCount();
  766.   cnt = 5000L * (TrPnt[3] - TrPnt[2]);
  767.   if (t2!=t1)rate = cnt*1000/(t2-t1);
  768.   sprintf(msg,"Nodes= %u, Nodes/Sec= %u", cnt, rate);
  769.   MessageBox(0,msg,"",0);
  770.   return(TrPnt[3] - TrPnt[2]);
  771. }
  772.  
  773. void
  774.  TestPSpeed (SHORT (*f) (SHORT side), unsigned j)
  775. {
  776.   SHORT i;
  777.   long cnt, rate, t1, t2;
  778. #ifdef HAVE_GETTIMEOFDAY
  779. struct timeval tv;
  780. #endif
  781.  
  782. #ifdef HAVE_GETTIMEOFDAY
  783.   gettimeofday(&tv,NULL);
  784.   t1 = (tv.tv_sec*100+(tv.tv_usec/10000));
  785. #else
  786.   t1 = time (0);
  787. #endif
  788.   for (i = 0; i < j; i++)
  789.     {
  790.       (void) f (opponent);
  791.     }
  792. #ifdef HAVE_GETTIMEOFDAY
  793.   gettimeofday(&tv,NULL);
  794.   t2 = (tv.tv_sec*100+(tv.tv_usec/10000));
  795. #else
  796.   t2 = time (0);
  797. #endif
  798.   cnt = j;
  799.   if (t2 - t1)
  800.     et = (t2 - t1);
  801.   else
  802.     et = 1;
  803.   rate = (et) ? ((cnt*100) / et) : 0;
  804.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  805.   ShowNodeCnt (cnt);
  806. }
  807.  
  808. void
  809. InputCommand (void)
  810.  
  811. /*
  812.  * Process the users command. If easy mode is OFF (the computer is thinking
  813.  * on opponents time) and the program is out of book, then make the 'hint'
  814.  * move on the board and call SelectMove() to find a response. The user
  815.  * terminates the search by entering ^C (quit siqnal) before entering a
  816.  * command. If the opponent does not make the hint move, then set Sdepth to
  817.  * zero.
  818.  */
  819.  
  820. {
  821. #ifdef QUIETBACKGROUND
  822.   SHORT have_shown_prompt = false;
  823. #endif
  824.   SHORT tmp;
  825.   UTSHORT mv;
  826.   CHAR s[80], sx[80];
  827.   CHAR *p, *q;
  828.  
  829. #if defined CHESSTOOL
  830.   SHORT normal = false;
  831. #endif
  832.  
  833.   ok = flag.quit = flag.bothsides = false;
  834.   if (command>0 && !(command & 0x8000))origplayer=computer;
  835.   else origplayer=opponent;
  836.   command&=0x7FFF;
  837.   ElapsedTime(1);
  838.   player = opponent;
  839. /* if transposition table is filling start it over */
  840.  ZeroTTable(1); /* clear ageing table entries */
  841. /* if we have a move for our opponent, and we are thinking on his time, and not in force mode */
  842.   if (hint > 0 && !flag.easy && !flag.force && !command)
  843. /* if the hint is a promotion don't do anything, we don't know what to promote to. */
  844.     if ((board[hint >> 8] != pawn) || ((row (hint & 0x3f) != 0) && (row (hint & 0x3f) != 7)))
  845.       {
  846.    ft = time0;
  847. /* create the hint move as string so we can do it */
  848.    algbr ((SHORT) hint >> 8, (SHORT) hint & 0x3f, false);
  849.    strcpy (s, mvstr[0]);
  850.    tmp = epsquare;
  851. #ifdef DEBUG12
  852. #include "debug12.h"
  853. #endif
  854.    SaveBoard();
  855. /* do the hint move */
  856.    if (VerifyMove (s, 1, &mv))
  857.      {
  858. #ifdef QUIETBACKGROUND
  859.        ShowSidetoMove ();
  860.        have_shown_prompt = true;
  861. #endif /* QUIETBACKGROUND */
  862. /* would love to put null move in here */
  863. /* after we make the hint move make a 2 ply search with both plys our moves */
  864. /* think on opponents time */
  865.        SelectMove (computer, 2);
  866. /* undo the hint and carry on */
  867.        VerifyMove (s, 2, &mv);
  868.      }
  869.    epsquare = tmp;
  870.    time0 = ft;
  871.       }
  872.   while (!(ok || flag.quit))
  873.     {
  874. #if defined CHESSTOOL
  875.       normal = false;
  876. #endif
  877.       player = opponent;
  878. #ifdef QUIETBACKGROUND
  879.       if (!have_shown_prompt)
  880.    {
  881. #endif /* QUIETBACKGROUND */
  882.      ShowSidetoMove ();
  883. #ifdef QUIETBACKGROUND
  884.    }
  885.       have_shown_prompt = false;
  886. #endif /* QUIETBACKGROUND */
  887.       while (!command)
  888.        {
  889.          CheckMessage();
  890.          ElapsedTime(0);
  891.        }
  892.       if (flag.quit)Exit(0);
  893.       strcpy(sx,move);
  894.       sscanf (sx, "%s", s);
  895.       if (command>100)
  896.       {
  897.        short cmnd;
  898.        cmnd=command;
  899.        command=0;
  900.        switch (cmnd)
  901.        {
  902.         case IDM_NEW:
  903.           origplayer=white;
  904.           NewGame();
  905.           UpdateDisplay(0,0,1,0,0);
  906.           break;
  907.         case IDM_GET:
  908.           if (!GetGame())
  909.            {
  910.             player=origplayer;
  911.             if (player==computer)ok=true;
  912.            }
  913.           else origplayer=opponent;
  914.           break;
  915.         case IDM_SAVE:
  916.           SaveGame();
  917.           break;
  918.         case IDM_LIST:
  919.           ListGame();
  920.           player=origplayer;
  921.           if (player==computer)ok=true;
  922.           break;
  923.         case IDM_QUIT:
  924.           flag.quit=true;
  925.           break;
  926.         case IDM_EDIT:
  927.           EditBoard();
  928.           origplayer=opponent;
  929.           hint=0;
  930.           break;
  931.         case IDM_REVIEW:
  932.           Replay();
  933.           player=origplayer;
  934.           if (player==computer)ok=true;
  935.           break;
  936.         case IDM_REMOVE:
  937.           Undo();
  938.         case IDM_UNDO:
  939.           Undo();
  940.           player=origplayer;
  941.           if (player==computer) ok = true;
  942.           UpdateDisplay (0, 0, 1, 0, 0);
  943.           break;
  944.         case IDM_FORCE:
  945.           flag.force = !flag.force;
  946.           flag.bothsides = false;
  947.           if (!flag.force && forcing)
  948.            {
  949.             Sdepth=0;
  950.             ok=true;
  951.             computer^=1;
  952.             opponent^=1;
  953.            }
  954.           else
  955.            {
  956.             if (origplayer==opponent) forcing=0;
  957.             else
  958.              {
  959.               forcing=1;
  960.               computer^=1;
  961.               opponent^=1;
  962.              }
  963.            }
  964.           break;
  965.         case IDM_COMPILE:
  966.           lpfnDlgProc = MakeProcInstance ( CompileBookDlgProc, hInst);
  967.           DialogBox ( hInst, MAKEINTRESOURCE(COMPILEBOOK), hWnd,
  968.                 lpfnDlgProc);
  969.           FreeProcInstance ( lpfnDlgProc);
  970.           break;
  971.         case IDM_BOTH:
  972.          flag.bothsides = true;
  973.          if (flag.force && forcing)
  974.           {
  975.            computer^=1;
  976.            opponent^=1;
  977.           }
  978.          flag.force=false;
  979.          Sdepth = 0;
  980.          ElapsedTime (1);
  981.          SaveBoard();
  982.          SelectMove (opponent, 1);
  983.          ok = true;
  984.          break;
  985.  
  986.         case IDM_SWITCHDM:
  987.           player=origplayer=computer;
  988.         case IDM_SWITCH:
  989.           computer = computer ^ 1;
  990.           opponent = opponent ^ 1;
  991.           xwndw = (computer == white) ? WXwindow : BXwindow;
  992.           goto changeside;
  993.         case IDM_WHITE:
  994.         case IDM_BLACK:
  995.          {
  996.           SHORT color=(cmnd-IDM_BLACK)^1;
  997.           if (computer==color^(flag.force && forcing))continue;
  998.           if (flag.force && forcing)
  999.            {
  1000.             computer = color^1;
  1001.             opponent = color;
  1002.            }
  1003.           else
  1004.            {
  1005.             computer = color;
  1006.             opponent = color^1;
  1007.            }
  1008.           xwndw = computer==white ? WXwindow : BXwindow;
  1009.          }
  1010. changeside:
  1011.           if (!flag.force)
  1012.            {
  1013.             Sdepth = 0;
  1014.             player=origplayer;
  1015.             if (player==computer) ok=true;
  1016.             ShowPlayers ();
  1017.            }
  1018.           else
  1019.            {
  1020.             computer^=forcing;
  1021.             ShowPlayers();
  1022.             computer^=forcing;
  1023.             computer^=1;
  1024.             opponent^=1;
  1025.             forcing^=1;
  1026.            }
  1027.           if (computer^(flag.force & forcing)==flag.reverse)
  1028.            {
  1029.             flag.reverse=!(computer^(flag.force & forcing));
  1030.             InvalidateRect(hWnd,0,true);
  1031.             UpdateWindow(hWnd);
  1032.            }
  1033.           hint=0;
  1034.           break;
  1035.         case IDM_WTM:
  1036.         case IDM_BTM:
  1037.           player = origplayer = (cmnd==IDM_WTM)? white : black;
  1038.           if (player==computer)ok = true;
  1039.           if (flag.force)
  1040.            {
  1041.             computer = opponent;
  1042.             opponent = computer ^ 1;
  1043.             forcing ^= 1;
  1044.            }
  1045.           InitializeStats(); /*May crash or make illegal move without this.*/
  1046.        }
  1047.       }
  1048.    else
  1049.    {
  1050. /* It must be a move */
  1051. /* preprocess move;
  1052.    remove x or +
  1053.    change promotion from h1=Q to h1q
  1054. */
  1055.    p=s; q=sx;
  1056.    while (*p != '\0'){
  1057.    if(*p == 'x' || *p == '+') p++;
  1058.    else if(*p == '='){ p++; *q = tolower(*p); p++; q++;}
  1059.       else {*q++ = *p++; }
  1060.    }
  1061.    *q = '\0';
  1062.  
  1063.    ok = VerifyMove (sx, 0, &mv);
  1064.    command=0;
  1065.    if (flag.force && ok)
  1066.     {
  1067.       hint=0;
  1068.       computer = opponent;
  1069.       opponent = computer ^ 1;
  1070.       forcing ^= 1;
  1071.     }
  1072.    }
  1073.     }
  1074.  
  1075.   /* guess is correct ?*/
  1076.   Sdepth = (hint == mv) ? Sdepth-1 : 0;
  1077.   if (Tree[0].score > 9000) Sdepth = 0;
  1078.  
  1079. }
  1080.  
  1081. #ifdef HAVE_GETTIMEOFDAY
  1082. void
  1083. ElapsedTime (SHORT iop)
  1084.  
  1085.  
  1086. /*
  1087.  * Determine the time that has passed since the search was started. If the
  1088.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1089.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1090.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1091.  */
  1092.  
  1093. { long old_et;
  1094.   struct timeval tv;
  1095.  
  1096.   old_et=et;
  1097.   gettimeofday(&tv,NULL);
  1098.   et = (tv.tv_sec*100+(tv.tv_usec/10000)) - time0;
  1099.   ETnodes = NodeCnt + Znodes;
  1100.   if (et < 0)
  1101.     et = 0;
  1102.   if (iop == 1)
  1103.     {
  1104.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1105.    flag.timeout = true;
  1106.       ETnodes = NodeCnt + Znodes;
  1107.       gettimeofday(&tv,NULL);
  1108.       time0 = tv.tv_sec*100+tv.tv_usec/10000;
  1109.     }
  1110. #if !defined NONDSP
  1111. #ifdef QUIETBACKGROUND
  1112.   if (!background)
  1113. #endif /* QUIETBACKGROUND */
  1114.     if(et/100>old_et/100 && !flag.mate)UpdateClocks ();
  1115. #endif
  1116. #if defined(Think_C) && defined(Window_Events)
  1117.         check_events();
  1118. #endif
  1119.  
  1120. }
  1121. #else
  1122. void
  1123. ElapsedTime (SHORT iop)
  1124.  
  1125. /*
  1126.  * Determine the time that has passed since the search was started. If the
  1127.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1128.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1129.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1130.  */
  1131. { long old_et;
  1132.  
  1133.   old_et=et;
  1134.   et = (GetTickCount() - time0)/10;
  1135.   ETnodes = NodeCnt + Znodes;
  1136.   if (et < 0)
  1137.     et = -et;
  1138.   if (iop == 1)
  1139.     {
  1140.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1141.    flag.timeout = true;
  1142.       ETnodes = NodeCnt + Znodes;
  1143.       time0 = GetTickCount();
  1144.     }
  1145. #ifdef QUIETBACKGROUND
  1146.   if (!background)
  1147. #endif /* QUIETBACKGROUND */
  1148.     if (et/100>old_et/100 && !flag.mate)UpdateClocks ();
  1149. #if defined(Think_C) && defined(Window_Events)
  1150.         check_events();
  1151. #endif
  1152. }
  1153. #endif
  1154.  
  1155.  
  1156. static SHORT Nmoves[] = NMOVEINIT;
  1157. void
  1158. SetTimeControl (void)
  1159. {
  1160.      flag.gamein = TCflag = false;
  1161.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  1162. if (XC){
  1163.                   if (XCmore < XC)
  1164.                     {
  1165.                       TCmoves = XCmoves[XCmore];
  1166.                       TCminutes = XCminutes[XCmore];
  1167.                       TCseconds = XCseconds[XCmore];
  1168.             TCadd = XCadd[XCmore];
  1169.                       XCmore++;
  1170.                     }
  1171.       if((TCminutes+TCseconds) == 0){TCflag = false;} else {
  1172.       TCflag = true;
  1173.       if(TCmoves == 0)
  1174.    {int i; i = (TCminutes*60+TCseconds + 40 * (TCadd/100))/60; 
  1175.       if(i > NMOVELIMIT) i = NMOVELIMIT; 
  1176.       TCmoves = Nmoves[i]; flag.gamein = true;}
  1177.    else flag.gamein = false;
  1178.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  1179.       TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
  1180.       TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
  1181.     }
  1182.   }
  1183.   flag.onemove = (TCmoves == 1);
  1184.   et = 0;
  1185.   ElapsedTime (1);
  1186. }
  1187.