home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 3 / cd.iso / os2 / pmgnuchs / dsp.c < prev    next >
Text File  |  1994-04-19  |  16KB  |  647 lines

  1. //
  2. //  Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  3. //  Copyright (c) 1988, 1989, 1990  John Stanback
  4. //
  5. //  Project:    OS/2 PM Port of GNU CHESS 4.0 pl65 (PmGnuChs)
  6. //
  7. //  Version:    1994-4-17
  8. //
  9. //   Module:    Misc GNU Logic (Dsp.c)
  10. //
  11. //   Porter:    Ported to Windows 3.0 by Darly Baker
  12. //
  13. //   Porter:    Ported to OS/2 1.2+ by Kent Cedola
  14. //
  15. //   Porter:    Ported to OS/2 2.1+ by Yibing Fan
  16. //
  17. //   System:    OS/2 2.1 using emx 0.8h
  18. //
  19. //  Remarks:    This code is combination of Kent's OS/2 1.2 porting and
  20. //              original code from GNU CHESS 4.0 pl65.
  21. //
  22. //  License:
  23. //
  24. //    CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  25. //    WARRANTY.  No author or distributor accepts responsibility to anyone for
  26. //    the consequences of using it or for whether it serves any particular
  27. //    purpose or works at all, unless he says so in writing.  Refer to the
  28. //    CHESS General Public License for full details.
  29. //
  30. //    Everyone is granted permission to copy, modify and redistribute CHESS,
  31. //    but only under the conditions described in the CHESS General Public
  32. //    License.  A copy of this license is supposed to have been given to you
  33. //    along with CHESS so you can know your rights and responsibilities.  It
  34. //    should be in a file named COPYING.  Among other things, the copyright
  35. //    notice and this notice must be preserved on all copies.
  36. //
  37.  
  38. #define INCL_DOS
  39. #define INCL_PM
  40. #include <os2.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <time.h>
  44. #include "PmChess.h"
  45. #include "GnuChess.h"
  46. #include "Defs.h"
  47. #include "Resource.h"
  48. #include "ataks.h"
  49.  
  50.  
  51. int mycntl1, mycntl2;
  52.  
  53. char mvstr[4][6];
  54. char *InPtr;
  55. long evrate, Level;
  56. int PositionFlag = 0;
  57.  
  58. #define pxx " PNBRQK"
  59. #define qxx " pnbrqk"
  60.  
  61. void TerminateSearch (int), Die (int);
  62.  
  63. void
  64. Initialize (void)
  65. {
  66.   mycntl1 = mycntl2 = 0;
  67. }
  68.  
  69. void
  70. ExitChess (void)
  71. {
  72.  
  73. }
  74.  
  75. void
  76. Die (int Sig)
  77. {
  78. }
  79.  
  80. void
  81. TerminateSearch (int Sig)
  82. {
  83.   flag.timeout = true;
  84.   flag.bothsides = false;
  85. }
  86.  
  87. void
  88. algbr (short int f, short int t, short int flag)
  89.  
  90. /*
  91.    Generate move strings in different formats.
  92. */
  93.  
  94. {
  95.   int m3p;
  96.  
  97.   if (f != t)
  98.     {
  99.       /* algebraic notation */
  100.       mvstr[0][0] = (char) ('a'+column (f));
  101.       mvstr[0][1] = (char) ('1'+row (f));
  102.       mvstr[0][2] = (char) ('a'+column (t));
  103.       mvstr[0][3] = (char) ('1'+row (t));
  104.       mvstr[0][4] = mvstr[3][0] = '\0';
  105.       if ((mvstr[1][0] = pxx[board[f]]) == 'P')
  106.         {
  107.           if (mvstr[0][0] == mvstr[0][2])       /* pawn did not eat */
  108.             {
  109.               mvstr[2][0] = mvstr[1][0] = mvstr[0][2];  /* to column */
  110.               mvstr[2][1] = mvstr[1][1] = mvstr[0][3];  /* to row */
  111.               m3p = 2;
  112.             }
  113.           else
  114.             /* pawn ate */
  115.             {
  116.               mvstr[2][0] = mvstr[1][0] = mvstr[0][0];  /* from column */
  117.               mvstr[2][1] = mvstr[1][1] = mvstr[0][2];  /* to column */
  118.               mvstr[2][2] = mvstr[0][3];
  119.               m3p = 3;          /* to row */
  120.             }
  121.           mvstr[2][m3p] = mvstr[1][2] = '\0';
  122.           if (flag & promote)
  123.             {
  124.               mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = qxx[flag & pmask];
  125.               mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[0][5] = '\0';
  126.             }
  127.         }
  128.       else
  129.         /* not a pawn */
  130.         {
  131.           mvstr[2][0] = mvstr[1][0];
  132.           mvstr[2][1] = mvstr[0][1];
  133.           mvstr[2][2] = mvstr[1][1] = mvstr[0][2];      /* to column */
  134.           mvstr[2][3] = mvstr[1][2] = mvstr[0][3];      /* to row */
  135.           mvstr[2][4] = mvstr[1][3] = '\0';
  136.           strcpy (mvstr[3], mvstr[2]);
  137.           mvstr[3][1] = mvstr[0][0];
  138.           if (flag & cstlmask)
  139.             {
  140.               if (t > f)
  141.                 {
  142.                   strcpy (mvstr[1], "o-o");
  143.                   strcpy (mvstr[2], "O-O");
  144.                 }
  145.               else
  146.                 {
  147.                   strcpy (mvstr[1], "o-o-o");
  148.                   strcpy (mvstr[2], "O-O-O");
  149.                 }
  150.             }
  151.         }
  152.     }
  153.   else
  154.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  155. }
  156.  
  157. int
  158. VerifyMove (HWND hWnd, char *s, short int iop, short unsigned int *mv)
  159.  
  160. /*
  161.    Compare the string 's' to the list of legal moves available for the
  162.    opponent. If a match is found, make the move on the board.
  163. */
  164.  
  165. {
  166.   static short pnt, tempb, tempc, tempsf, tempst, cnt;
  167.   static struct leaf xnode;
  168.   struct leaf *node;
  169.  
  170.   *mv = 0;
  171.   if (iop == 2)
  172.     {
  173.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  174.       return (false);
  175.     }
  176.   cnt = 0;
  177.   MoveList (opponent, 2);
  178.   pnt = TrPnt[2];
  179.   while (pnt < TrPnt[3])
  180.     {
  181.       node = &Tree[pnt++];
  182.       algbr (node->f, node->t, (short) node->flags);
  183.       if (strcmp (s, mvstr[0]) == 0 || strcmp (s, mvstr[1]) == 0 ||
  184.           strcmp (s, mvstr[2]) == 0 || strcmp (s, mvstr[3]) == 0)
  185.         {
  186.           cnt++;
  187.           xnode = *node;
  188.         }
  189.     }
  190.   if (cnt == 1)
  191.     {
  192.       MakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  193.       if (SqAtakd (PieceList[opponent][0], computer))
  194.         {
  195.           UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  196.           SMessageBox (hWnd, IDS_ILLEGALMOVE, IDS_CHESS);
  197.           return (false);
  198.         }
  199.       else
  200.         {
  201.           if (iop == 1)
  202.             return (true);
  203.           UpdateDisplay (hWnd, xnode.f, xnode.t, 0, (short) xnode.flags);
  204.           if ((board[xnode.t] == pawn)
  205.               || (xnode.flags & capture)
  206.               || (xnode.flags & cstlmask))
  207.             {
  208.               Game50 = GameCnt;
  209.               ZeroRPT ();
  210.             }
  211.           GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  212.           GameList[GameCnt].nodes = 0;
  213.           ElapsedTime (1);
  214.           GameList[GameCnt].time = (short) et;
  215.           TimeControl.clock[opponent] -= et;
  216.           --TimeControl.moves[opponent];
  217.           *mv = (xnode.f << 8) | xnode.t;
  218.           algbr (xnode.f, xnode.t, false);
  219.           return (true);
  220.         }
  221.     }
  222.   if (cnt > 1) SMessageBox (hWnd, IDS_AMBIGUOUSMOVE, IDS_CHESS);
  223.   return (false);
  224. }
  225.  
  226.  
  227. void ElapsedTime (short int iop)
  228.  
  229. /*
  230.  * Determine the time that has passed since the search was started. If the
  231.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  232.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  233.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  234.  */
  235.  
  236. {
  237. //  if ( _read_kbd (0,0,0) != -1)
  238. //    {
  239. //      if (!flag.timeout)
  240. //    flag.back = true;
  241. //      flag.bothsides = false;
  242. //    }
  243.   et = (time ((long *) 0) - time0) * 100;
  244.   ETnodes = NodeCnt + ZNODES;
  245.   if (et < 0)
  246.     et = 0;
  247.   if (iop == 1)
  248.     {
  249.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  250.     flag.timeout = true;
  251.       ETnodes = NodeCnt + ZNODES;
  252.       time0 = time ((long *) 0);
  253.     }
  254.        if (et > 0)
  255.         evrate = NodeCnt * 100 / (et + ft);
  256.       else
  257.         evrate = 0;
  258. //    UpdateClocks ();
  259. }
  260.  
  261.  
  262. void
  263. SetTimeControl (void)
  264. {
  265.   if (TCflag)
  266.     {
  267.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  268.       TimeControl.clock[white] = TimeControl.clock[black] = 6000 * (long) TCminutes;
  269.     }
  270.   else
  271.     {
  272.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  273.       TimeControl.clock[white] = TimeControl.clock[black] = 0;
  274.       Level = 60 * (long) TCminutes;
  275.     }
  276.   et = 0;
  277.   ElapsedTime (1);
  278. }
  279.  
  280. void
  281. GetGame (HWND hWnd, char *fname)
  282. {
  283.   FILE *fd;
  284.   char *p;
  285.   int c, i, j;
  286.   int eps = -1;
  287.   short sq;
  288.   if ((fd = fopen (fname, "r")) != NULL)
  289.     {
  290.       NewGame (hWnd);
  291.       fgets (fname, 256, fd);
  292.       computer = opponent = white;
  293.       InPtr = fname;
  294.       skip ();
  295.       if (*InPtr == 'c')
  296.     computer = black;
  297.       else
  298.     opponent = black;
  299.       skip ();
  300.       skip ();
  301.       skip ();
  302.       Game50 = atoi (InPtr);
  303.       skip();
  304.       skip();
  305.       eps = atoi(InPtr);
  306.       fgets (fname, 256, fd);
  307.       InPtr = &fname[14];
  308.       castld[white] = ((*InPtr == CP[214][0]) ? true : false);
  309.       skip ();
  310.       skip ();
  311.       castld[black] = ((*InPtr == CP[214][0]) ? true : false);
  312.       fgets (fname, 256, fd);
  313.       InPtr = &fname[11];
  314.       skipb ();
  315.       TCflag = atoi (InPtr);
  316.       skip ();
  317.       InPtr += 14;
  318.       skipb ();
  319.       OperatorTime = atoi (InPtr);
  320.       fgets (fname, 256, fd);
  321.       InPtr = &fname[11];
  322.       skipb ();
  323.       TimeControl.clock[white] = atol (InPtr);
  324.       skip ();
  325.       skip ();
  326.       TimeControl.moves[white] = atoi (InPtr);
  327.       fgets (fname, 256, fd);
  328.       InPtr = &fname[11];
  329.       skipb ();
  330.       TimeControl.clock[black] = atol (InPtr);
  331.       skip ();
  332.       skip ();
  333.       TimeControl.moves[black] = atoi (InPtr);
  334.       fgets (fname, 256, fd);
  335.       for (i = 7; i > -1; i--)
  336.     {
  337.       fgets (fname, 256, fd);
  338.       p = &fname[2];
  339.       InPtr = &fname[11];
  340.       skipb ();
  341.       for (j = 0; j < 8; j++)
  342.         {
  343.           sq = i * 8 + j;
  344.           if (*p == '.')
  345.         {
  346.           board[sq] = no_piece;
  347.           color[sq] = neutral;
  348.         }
  349.           else
  350.         {
  351.           for (c = 0; c < 8; c++)
  352.             {
  353.               if (*p == pxx[c])
  354.             {
  355.               board[sq] = c;
  356.               color[sq] = black;
  357.             }
  358.             }
  359.           for (c = 0; c < 8; c++)
  360.             {
  361.               if (*p == qxx[c])
  362.             {
  363.               board[sq] = c;
  364.               color[sq] = white;
  365.             }
  366.             }
  367.         }
  368.           p++;
  369.           Mvboard[sq] = atoi (InPtr);
  370.           skip ();
  371.         }
  372.     }
  373.       GameCnt = 0;
  374.       flag.regularstart = true;
  375.       Book = BOOKFAIL;
  376.       fgets (fname, 256, fd);
  377.       fgets (fname, 256, fd);
  378.       fgets (fname, 256, fd);
  379.       while (fgets (fname, 256, fd))
  380.     {
  381.       struct GameRec *g;
  382.       int side = computer;
  383.  
  384.       side = side ^ 1;
  385.       ++GameCnt;
  386.       InPtr = fname;
  387.       skipb ();
  388.       g = &GameList[GameCnt];
  389.       g->gmove = parser (InPtr, side);
  390.       skip ();
  391.       g->score = atoi (InPtr);
  392.       skip ();
  393.       g->depth = atoi (InPtr);
  394.       skip ();
  395.       g->nodes = atol (InPtr);
  396.       skip ();
  397.       g->time = atol (InPtr);
  398.       skip ();
  399.       g->flags = c = atoi (InPtr);
  400.       skip ();
  401.       g->hashkey = strtol (InPtr, (char **) NULL, 16);
  402.       skip ();
  403.       g->hashbd = strtol (InPtr, (char **) NULL, 16);
  404.       skip ();
  405.       g->epssq = atoi(InPtr);
  406.       g->piece = no_piece;
  407.       g->color = neutral;
  408.       if (c & (capture | cstlmask))
  409.         {
  410.           if (c & capture)
  411.         {
  412.           skip ();
  413.           for (c = 0; c < 8; c++)
  414.             if (pxx[c] == *InPtr)
  415.               break;
  416.           g->piece = c;
  417.         }
  418.           skip ();
  419.           g->color = ((*InPtr == CP[119][0]) ? black : white);
  420.         }
  421.     }
  422.       if (TimeControl.clock[white] > 0)
  423.     TCflag = true;
  424.       fclose (fd);
  425.     }
  426.   ZeroRPT ();
  427.   InitializeStats ();
  428.   epsquare = eps;
  429.   UpdateDisplay (hWnd, 0, 0, 1, 0);
  430.   Sdepth = 0;
  431.   hint = 0;
  432. }
  433.  
  434.  
  435. void
  436. SaveGame (HWND hWnd, char *fname)
  437. {
  438.   FILE *fd;
  439.   short sq, i, c, f, t;
  440.   char p;
  441.  
  442.   if ((fd = fopen (fname, "w")) != NULL)
  443.     {
  444.       char *b, *w;
  445.  
  446.       b = w = CP[74];
  447.       if (computer == black)
  448.     b = CP[141];
  449.       if (computer == white)
  450.     w = CP[141];
  451.       fprintf (fd, CP[37], b, w, Game50,epsquare);
  452.       fprintf (fd, CP[42], castld[white] ? CP[214] : CP[215], castld[black] ? CP[214] : CP[215]);
  453.       fprintf (fd, CP[111], TCflag, OperatorTime);
  454.       fprintf (fd, CP[117],
  455.            TimeControl.clock[white], TimeControl.moves[white],
  456.            TimeControl.clock[black], TimeControl.moves[black]);
  457.       for (i = 7; i > -1; i--)
  458.     {
  459.       fprintf (fd, "%1d ", i + 1);
  460.       for (c = 0; c < 8; c++)
  461.         {
  462.           sq = i * 8 + c;
  463.           switch (color[sq])
  464.         {
  465.         case black:
  466.           p = pxx[board[sq]];
  467.           break;
  468.         case white:
  469.           p = qxx[board[sq]];
  470.           break;
  471.         default:
  472.           p = '.';
  473.         }
  474.           fprintf (fd, "%c", p);
  475.         }
  476.       for (f = i * 8; f < i * 8 + 8; f++)
  477.         fprintf (fd, " %d", Mvboard[f]);
  478.       fprintf (fd, "\n");
  479.     }
  480.       fprintf (fd, "  %s\n", cxx);
  481.       fprintf (fd, CP[126]);
  482.       for (i = 1; i <= GameCnt; i++)
  483.     {
  484.       struct GameRec *g = &GameList[i];
  485.  
  486.       f = g->gmove >> 8;
  487.       t = (g->gmove & 0xFF);
  488.       algbr (f, t, g->flags);
  489.       fprintf (fd, "%s %5d %5d %7ld %6ld %5d  %#08lx %#08lx %d %c   %s\n",
  490.            mvstr[0], g->score, g->depth,
  491.            g->nodes, g->time, g->flags, g->hashkey, g->hashbd,g->epssq,
  492.        pxx[g->piece], ((g->color == 2) ? "     " : ColorStr[g->color]));
  493.     }
  494.       fclose (fd);
  495. /* Game saved */
  496.       ShowMessage (hWnd,CP[70]);
  497.     }
  498.   else
  499.     /*ShowMessage ("Could not open file");*/
  500.     ShowMessage (hWnd, CP[48]);
  501. }
  502.  
  503.  
  504. void
  505. ListGame (void)
  506. {
  507.   FILE *fd;
  508.   CHAR listfile[128]= "";
  509.   short i, f, t;
  510.   char fname[256];
  511.  
  512.   if (listfile[0])
  513.     strcpy (fname, listfile);
  514.   else
  515.     {
  516.       sprintf (fname, "chess.lst");
  517.     }
  518.   fd = fopen (fname, "w");
  519.   if (!fd)
  520.     {
  521.       printf ( CP[219], fname);
  522.       exit (1);
  523.     }
  524.   fprintf (fd, CP[161], "65");
  525.   fprintf (fd, CP[10]);
  526.   fprintf (fd, CP[11]);
  527.   for (i = 1; i <= GameCnt; i++)
  528.     {
  529.       f = GameList[i].gmove >> 8;
  530.       t = (GameList[i].gmove & 0xFF);
  531.       algbr (f, t, GameList[i].flags);
  532.       if(GameList[i].flags & book)
  533.           fprintf (fd, "%5s  %5d    Book%7ld %5d", mvstr[0],
  534.            GameList[i].score,
  535.            GameList[i].nodes, GameList[i].time);
  536.       else
  537.           fprintf (fd, "%5s  %5d     %2d %7ld %5d", mvstr[0],
  538.            GameList[i].score, GameList[i].depth,
  539.            GameList[i].nodes, GameList[i].time);
  540.       if ((i % 2) == 0)
  541.     {
  542.     fprintf(fd,"\n");
  543.     }
  544.    }
  545.   fprintf (fd, "\n\n");
  546.   if (GameList[GameCnt].flags & draw)
  547.     {
  548.       fprintf (fd, CP[54], DRAW);
  549.     }
  550.   else if (GameList[GameCnt].score == -9999)
  551.     {
  552.       fprintf (fd, "%s\n", ColorStr[player ]);
  553.     }
  554.   else if (GameList[GameCnt].score == 9998)
  555.     {
  556.       fprintf (fd, "%s\n", ColorStr[player ^ 1]);
  557.     }
  558.   fclose (fd);
  559. }
  560.  
  561.  
  562. void
  563. Undo (HWND hWnd)
  564.  
  565. /*
  566.   Undo the most recent half-move.
  567. */
  568.  
  569. {
  570.   short f, t;
  571.   f = GameList[GameCnt].gmove >> 8;
  572.   t = GameList[GameCnt].gmove & 0xFF;
  573.   if (board[t] == king && distance (t, f) > 1)
  574.     (void) castle (GameList[GameCnt].color, f, t, 2);
  575.   else
  576.     {
  577.       /* Check for promotion: */
  578.       if ((color[t] == white && row (f) == 6 && row (t) == 7)
  579.           || (color[t] == black && row (f) == 1 && row (t) == 0))
  580.         {
  581.           int g, from = f;
  582.           for (g = GameCnt - 1; g > 0; g--)
  583.             if (GameList[g].gmove & 0xFF == from)
  584.               from = GameList[g].gmove >> 8;
  585.           if ((color[t] == white && row (from) == 1)
  586.               || (color[t] == black && row (from) == 6))
  587.             board[t] = pawn;
  588.         }
  589.       board[f] = board[t];
  590.       color[f] = color[t];
  591.       board[t] = GameList[GameCnt].piece;
  592.       color[t] = GameList[GameCnt].color;
  593.       if (color[t] != neutral)
  594.         Mvboard[t]--;
  595.       Mvboard[f]--;
  596.     }
  597.   if (TCflag)
  598.     ++TimeControl.moves[color[f]];
  599.   GameCnt--;
  600.   computer = otherside[computer];
  601.   opponent = otherside[opponent];
  602.   flag.mate = false;
  603.   Sdepth = 0;
  604.   UpdateDisplay (hWnd, 0, 0, 1, 0);
  605.   InitializeStats ();
  606. }
  607.  
  608. void skip ()
  609. {
  610.   while (*InPtr != ' ')
  611.     InPtr++;
  612.   while (*InPtr == ' ')
  613.     InPtr++;
  614. }
  615.  
  616. void skipb ()
  617. {
  618.   while (*InPtr == ' ')
  619.     InPtr++;
  620. }
  621.  
  622. int
  623. parser (char *f, int side)
  624. {
  625.   int c1, r1, c2, r2;
  626.  
  627.   if (f[4] == 'o')
  628.     if (side == black)
  629.       return 0x3C3A;
  630.     else
  631.       return 0x0402;
  632.   else if (f[0] == 'o')
  633.     if (side == black)
  634.       return 0x3C3E;
  635.     else
  636.       return 0x0406;
  637.   else
  638.     {
  639.       c1 = f[0] - 'a';
  640.       r1 = f[1] - '1';
  641.       c2 = f[2] - 'a';
  642.       r2 = f[3] - '1';
  643.       return (locn (r1, c1) << 8) | locn (r2, c2);
  644.     }
  645.   /*NOTREACHED*/
  646. }
  647.