home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 7 Games / 07-Games.zip / PMCHESSR.ZIP / DSP.C < prev    next >
C/C++ Source or Header  |  1990-11-29  |  13KB  |  469 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 3.1 (PmChess)
  6. //
  7. //  Version:    1990-11-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. //   System:    OS2 1.2 using Microsoft C 6.0
  16. //
  17. //  Remarks:    This code converted to OS/2 almost as is.  Mostly minor changes
  18. //              to convert the small amount of Windows code to OS/2 PM.
  19. //
  20. //  License:
  21. //
  22. //    CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  23. //    WARRANTY.  No author or distributor accepts responsibility to anyone for
  24. //    the consequences of using it or for whether it serves any particular
  25. //    purpose or works at all, unless he says so in writing.  Refer to the
  26. //    CHESS General Public License for full details.
  27. //
  28. //    Everyone is granted permission to copy, modify and redistribute CHESS,
  29. //    but only under the conditions described in the CHESS General Public
  30. //    License.  A copy of this license is supposed to have been given to you
  31. //    along with CHESS so you can know your rights and responsibilities.  It
  32. //    should be in a file named COPYING.  Among other things, the copyright
  33. //    notice and this notice must be preserved on all copies.
  34. //
  35.  
  36. #define INCL_DOS
  37. #define INCL_PM
  38. #include <os2.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <time.h>
  42. #include "PmChess.h"
  43. #include "GnuChess.h"
  44. #include "Defs.h"
  45. #include "Resource.h"
  46.  
  47.  
  48. short Stboard[64] =
  49. {rook, knight, bishop, queen, king, bishop, knight, rook,
  50.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  51.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  54.  rook, knight, bishop, queen, king, bishop, knight, rook};
  55.  
  56. short Stcolor[64] =
  57. {white, white, white, white, white, white, white, white,
  58.  white, white, white, white, white, white, white, white,
  59.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  60.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  61.  black, black, black, black, black, black, black, black,
  62.  black, black, black, black, black, black, black, black};
  63.  
  64. int mycntl1, mycntl2;
  65.  
  66. char mvstr[4][6];
  67. long evrate;
  68. int PositionFlag = 0;
  69.  
  70. #define pxx " PNBRQK"
  71. #define qxx " pnbrqk"
  72.  
  73. void TerminateSearch (int), Die (int);
  74.  
  75. void
  76. Initialize (void)
  77. {
  78.   mycntl1 = mycntl2 = 0;
  79. }
  80.  
  81. void
  82. ExitChess (void)
  83. {
  84.  
  85. }
  86.  
  87. void
  88. Die (int Sig)
  89. {
  90. }
  91.  
  92. void
  93. TerminateSearch (int Sig)
  94. {
  95.   flag.timeout = true;
  96.   flag.bothsides = false;
  97. }
  98.  
  99. void
  100. algbr (short int f, short int t, short int flag)
  101.  
  102. /*
  103.    Generate move strings in different formats.
  104. */
  105.  
  106. {
  107.   int m3p;
  108.  
  109.   if (f != t)
  110.     {
  111.       /* algebraic notation */
  112.       mvstr[0][0] = (char) ('a'+column (f));
  113.       mvstr[0][1] = (char) ('1'+row (f));
  114.       mvstr[0][2] = (char) ('a'+column (t));
  115.       mvstr[0][3] = (char) ('1'+row (t));
  116.       mvstr[0][4] = mvstr[3][0] = '\0';
  117.       if ((mvstr[1][0] = pxx[board[f]]) == 'P')
  118.         {
  119.           if (mvstr[0][0] == mvstr[0][2])       /* pawn did not eat */
  120.             {
  121.               mvstr[2][0] = mvstr[1][0] = mvstr[0][2];  /* to column */
  122.               mvstr[2][1] = mvstr[1][1] = mvstr[0][3];  /* to row */
  123.               m3p = 2;
  124.             }
  125.           else
  126.             /* pawn ate */
  127.             {
  128.               mvstr[2][0] = mvstr[1][0] = mvstr[0][0];  /* from column */
  129.               mvstr[2][1] = mvstr[1][1] = mvstr[0][2];  /* to column */
  130.               mvstr[2][2] = mvstr[0][3];
  131.               m3p = 3;          /* to row */
  132.             }
  133.           mvstr[2][m3p] = mvstr[1][2] = '\0';
  134.           if (flag & promote)
  135.             {
  136.               mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = qxx[flag & pmask];
  137.               mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[0][5] = '\0';
  138.             }
  139.         }
  140.       else
  141.         /* not a pawn */
  142.         {
  143.           mvstr[2][0] = mvstr[1][0];
  144.           mvstr[2][1] = mvstr[0][1];
  145.           mvstr[2][2] = mvstr[1][1] = mvstr[0][2];      /* to column */
  146.           mvstr[2][3] = mvstr[1][2] = mvstr[0][3];      /* to row */
  147.           mvstr[2][4] = mvstr[1][3] = '\0';
  148.           strcpy (mvstr[3], mvstr[2]);
  149.           mvstr[3][1] = mvstr[0][0];
  150.           if (flag & cstlmask)
  151.             {
  152.               if (t > f)
  153.                 {
  154.                   strcpy (mvstr[1], "o-o");
  155.                   strcpy (mvstr[2], "O-O");
  156.                 }
  157.               else
  158.                 {
  159.                   strcpy (mvstr[1], "o-o-o");
  160.                   strcpy (mvstr[2], "O-O-O");
  161.                 }
  162.             }
  163.         }
  164.     }
  165.   else
  166.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  167. }
  168.  
  169. int
  170. VerifyMove (HWND hWnd, char *s, short int iop, short unsigned int *mv)
  171.  
  172. /*
  173.    Compare the string 's' to the list of legal moves available for the
  174.    opponent. If a match is found, make the move on the board.
  175. */
  176.  
  177. {
  178.   static short pnt, tempb, tempc, tempsf, tempst, cnt;
  179.   static struct leaf xnode;
  180.   struct leaf far *node;
  181.  
  182.   *mv = 0;
  183.   if (iop == 2)
  184.     {
  185.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  186.       return (false);
  187.     }
  188.   cnt = 0;
  189.   MoveList (opponent, 2);
  190.   pnt = TrPnt[2];
  191.   while (pnt < TrPnt[3])
  192.     {
  193.       node = &Tree[pnt++];
  194.       algbr (node->f, node->t, (short) node->flags);
  195.       if (strcmp (s, mvstr[0]) == 0 || strcmp (s, mvstr[1]) == 0 ||
  196.           strcmp (s, mvstr[2]) == 0 || strcmp (s, mvstr[3]) == 0)
  197.         {
  198.           cnt++;
  199.           xnode = *node;
  200.         }
  201.     }
  202.   if (cnt == 1)
  203.     {
  204.       MakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  205.       if (SqAtakd (PieceList[opponent][0], computer))
  206.         {
  207.           UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  208.           SMessageBox (hWnd, IDS_ILLEGALMOVE, IDS_CHESS);
  209.           return (false);
  210.         }
  211.       else
  212.         {
  213.           if (iop == 1)
  214.             return (true);
  215.           UpdateDisplay (hWnd, xnode.f, xnode.t, 0, (short) xnode.flags);
  216.           if ((board[xnode.t] == pawn)
  217.               || (xnode.flags & capture)
  218.               || (xnode.flags & cstlmask))
  219.             {
  220.               Game50 = GameCnt;
  221.               ZeroRPT ();
  222.             }
  223.           GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  224.           GameList[GameCnt].nodes = 0;
  225.           ElapsedTime (1);
  226.           GameList[GameCnt].time = (short) et;
  227.           TimeControl.clock[opponent] -= et;
  228.           --TimeControl.moves[opponent];
  229.           *mv = (xnode.f << 8) | xnode.t;
  230.           algbr (xnode.f, xnode.t, false);
  231.           return (true);
  232.         }
  233.     }
  234.   if (cnt > 1) SMessageBox (hWnd, IDS_AMBIGUOUSMOVE, IDS_CHESS);
  235.   return (false);
  236. }
  237.  
  238. void
  239. ElapsedTime (short int iop)
  240.  
  241. /*
  242.   Determine the time that has passed since the search was started. If
  243.   the elapsed time exceeds the target (ResponseTime+ExtraTime) then set
  244.   timeout to true which will terminate the search.
  245. */
  246.  
  247. {
  248.   et = time ((long *) 0) - time0;
  249.   if (et < 0)
  250.     et = 0;
  251.   ETnodes += 50;
  252.   if (et > et0 || iop == 1)
  253.     {
  254.       if (et > ResponseTime + ExtraTime && Sdepth > 1)
  255.         flag.timeout = true;
  256.       et0 = et;
  257.       if (iop == 1)
  258.         {
  259.           time0 = time ((long *) 0);
  260.           et0 = 0;
  261.         }
  262.       if (et > 0)
  263.         /* evrate used to be Nodes / cputime I dont` know why */
  264.         evrate = NodeCnt / (et + ft);
  265.       else
  266.         evrate = 0;
  267.       ETnodes = NodeCnt + 50;
  268.       UpdateClocks ();
  269.     }
  270. }
  271.  
  272. void
  273. SetTimeControl (void)
  274. {
  275.   if (TCflag)
  276.     {
  277.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  278.       TimeControl.clock[white] = TimeControl.clock[black] = 60 * (long) TCminutes;
  279.     }
  280.   else
  281.     {
  282.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  283.       TimeControl.clock[white] = TimeControl.clock[black] = 0;
  284.       Level = 60 * (long) TCminutes;
  285.     }
  286.   et = 0;
  287.   ElapsedTime (1);
  288. }
  289.  
  290. void
  291. GetGame (HWND hWnd, char *fname)
  292. {
  293.   FILE *fd;
  294.   int c;
  295.   short sq;
  296.   unsigned short m;
  297.  
  298.   struct GameRec tmp_rec;
  299.  
  300.   if ((fd = fopen (fname, "r")) == NULL)
  301.     {
  302.       SMessageBox (hWnd, IDS_LOADFAILED, IDS_CHESS);
  303.       return;
  304.     }
  305.  
  306.   fscanf (fd, "%hd%hd%hd", &computer, &opponent, &Game50);
  307.   fscanf (fd, "%hd%hd", &castld[white], &castld[black]);
  308.   fscanf (fd, "%hd%hd", &TCflag, &OperatorTime);
  309.   fscanf (fd, "%ld%ld%hd%hd",
  310.           &TimeControl.clock[white], &TimeControl.clock[black],
  311.           &TimeControl.moves[white], &TimeControl.moves[black]);
  312.   for (sq = 0; sq < 64; sq++)
  313.     {
  314.       fscanf (fd, "%hd%hd", &m, &Mvboard[sq]);
  315.       board[sq] = (m >> 8);
  316.       color[sq] = (m & 0xFF);
  317.       if (color[sq] == 0)
  318.         color[sq] = neutral;
  319.       else
  320.         --color[sq];
  321.     }
  322.   GameCnt = 0;
  323.   c = '?';
  324.   while (c != EOF)
  325.     {
  326.       ++GameCnt;
  327.       c = fscanf (fd, "%hd%hd%hd%ld%hd%hd%hd", &tmp_rec.gmove,
  328.                   &tmp_rec.score, &tmp_rec.depth,
  329.                   &tmp_rec.nodes, &tmp_rec.time,
  330.                   &tmp_rec.piece, &tmp_rec.color);
  331.       GameList[GameCnt] = tmp_rec;
  332.       if (GameList[GameCnt].color == 0)
  333.         GameList[GameCnt].color = neutral;
  334.       else
  335.         --GameList[GameCnt].color;
  336.     }
  337.  
  338.   GameCnt--;
  339.   if (TimeControl.clock[white] > 0)
  340.     TCflag = true;
  341.   computer--;
  342.   opponent--;
  343.  
  344.   fclose (fd);
  345.  
  346.   InitializeStats ();
  347.   Sdepth = 0;
  348.   UpdateDisplay (hWnd, 0, 0, 1, 0);
  349. }
  350.  
  351. void
  352. SaveGame (HWND hWnd, char *fname)
  353. {
  354.   FILE *fd;
  355.   short sq, i, c;
  356.  
  357.   if (NULL == (fd = fopen (fname, "w")))
  358.     {
  359.       ShowMessage (hWnd, "Not saved");
  360.       return;
  361.     }
  362.  
  363.   fprintf (fd, "%d %d %d\n", computer + 1, opponent + 1, Game50);
  364.   fprintf (fd, "%d %d\n", castld[white], castld[black]);
  365.   fprintf (fd, "%d %d\n", TCflag, OperatorTime);
  366.   fprintf (fd, "%ld %ld %d %d\n",
  367.            TimeControl.clock[white], TimeControl.clock[black],
  368.            TimeControl.moves[white], TimeControl.moves[black]);
  369.   for (sq = 0; sq < 64; sq++)
  370.     {
  371.       if (color[sq] == neutral)
  372.         c = 0;
  373.       else
  374.         c = color[sq] + 1;
  375.       fprintf (fd, "%d %d\n", 256 * board[sq] + c, Mvboard[sq]);
  376.     }
  377.   for (i = 1; i <= GameCnt; i++)
  378.     {
  379.       if (GameList[i].color == neutral)
  380.         c = 0;
  381.       else
  382.         c = GameList[i].color + 1;
  383.       fprintf (fd, "%d %d %d %ld %d %d %d\n",
  384.                GameList[i].gmove, GameList[i].score, GameList[i].depth,
  385.                GameList[i].nodes, GameList[i].time,
  386.                GameList[i].piece, c);
  387.     }
  388.   fclose (fd);
  389. }
  390.  
  391. void
  392. ListGame (HWND hWnd, char *fname)
  393. {
  394.   FILE *fd;
  395.   short i, f, t;
  396.  
  397.   if ( (fd = fopen (fname, "w")) == NULL) {
  398.       ShowMessage (hWnd, "Cannot write chess.lst");
  399.       return;
  400.   }
  401.  
  402.   fprintf (fd, "\n");
  403.   fprintf (fd, "       score  depth   nodes  time         ");
  404.   fprintf (fd, "       score  depth   nodes  time\n");
  405.   for (i = 1; i <= GameCnt; i++)
  406.     {
  407.       f = GameList[i].gmove >> 8;
  408.       t = (GameList[i].gmove & 0xFF);
  409.       algbr (f, t, false);
  410.       fprintf (fd, "%5s  %5d     %2d %7ld %5d", mvstr[0],
  411.                GameList[i].score, GameList[i].depth,
  412.                GameList[i].nodes, GameList[i].time);
  413.  
  414.       if ((i % 2) == 0)
  415.         fprintf (fd, "\n");
  416.       else
  417.         fprintf (fd, "         ");
  418.  
  419.     }
  420.   fprintf (fd, "\n\n");
  421.   fclose (fd);
  422. }
  423.  
  424. void
  425. Undo (HWND hWnd)
  426.  
  427. /*
  428.   Undo the most recent half-move.
  429. */
  430.  
  431. {
  432.   short f, t;
  433.   f = GameList[GameCnt].gmove >> 8;
  434.   t = GameList[GameCnt].gmove & 0xFF;
  435.   if (board[t] == king && distance (t, f) > 1)
  436.     (void) castle (GameList[GameCnt].color, f, t, 2);
  437.   else
  438.     {
  439.       /* Check for promotion: */
  440.       if ((color[t] == white && row (f) == 6 && row (t) == 7)
  441.           || (color[t] == black && row (f) == 1 && row (t) == 0))
  442.         {
  443.           int g, from = f;
  444.           for (g = GameCnt - 1; g > 0; g--)
  445.             if (GameList[g].gmove & 0xFF == from)
  446.               from = GameList[g].gmove >> 8;
  447.           if ((color[t] == white && row (from) == 1)
  448.               || (color[t] == black && row (from) == 6))
  449.             board[t] = pawn;
  450.         }
  451.       board[f] = board[t];
  452.       color[f] = color[t];
  453.       board[t] = GameList[GameCnt].piece;
  454.       color[t] = GameList[GameCnt].color;
  455.       if (color[t] != neutral)
  456.         Mvboard[t]--;
  457.       Mvboard[f]--;
  458.     }
  459.   if (TCflag)
  460.     ++TimeControl.moves[color[f]];
  461.   GameCnt--;
  462.   computer = otherside[computer];
  463.   opponent = otherside[opponent];
  464.   flag.mate = false;
  465.   Sdepth = 0;
  466.   UpdateDisplay (hWnd, 0, 0, 1, 0);
  467.   InitializeStats ();
  468. }
  469.