home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / useful / game / think / uchess.lha / UChess / src / dspcom.c < prev    next >
C/C++ Source or Header  |  1994-02-22  |  41KB  |  1,790 lines

  1. /*
  2.  * dspcom.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include "gnuchess.h"
  24. extern INTSIZE Mwpawn[64], Mbpawn[64], Mknight[2][64], Mbishop[2][64];
  25. extern char *version, *patchlevel;
  26. char __aligned mvstr[4][6];
  27. char __aligned *InPtr;
  28. #define BOOKMENUNUM 0xc2
  29.  
  30. extern unsigned int TTadd;
  31.  
  32. extern int IllegalMove;
  33. int __aligned func_num=0;
  34. int __aligned thinkahead=0;
  35. int __aligned ThinkInARow=0;
  36. int __aligned ThinkAheadWorked=0;
  37. int __aligned ThinkAheadDepth=0;
  38.  
  39. extern int backsrchaborted;
  40. extern short int ISZERO;
  41. extern short __aligned background;
  42. int __aligned verifyquiet=0;
  43. int __aligned MouseDropped=0;
  44.  
  45. #include <ctype.h>
  46. #include <signal.h>
  47.  
  48. #ifdef AMIGA
  49. #define __USE_SYSBASE
  50. #include <exec/types.h>
  51. #include <exec/exec.h>
  52. #include <proto/exec.h>
  53. #include <proto/dos.h>
  54. #include <proto/graphics.h>
  55. #include <proto/intuition.h>
  56. struct IntuiMessage __aligned globalmessage;
  57. int __aligned globalmessage_valid=0;
  58. extern struct Window __aligned *wG;
  59. extern int procpri;
  60. extern struct Process *myproc;
  61. extern struct MenuItem MenuItem6;
  62. extern struct Menu Menu1;
  63. extern unsigned char __far cookedchar[128];
  64. extern struct Menu __aligned Menu1;
  65. #define MenuList1 Menu1
  66. extern struct MenuItem __aligned MenuItem8ab;
  67. extern int __aligned MenuStripSet;
  68. #endif
  69.  
  70. #define SIGQUIT SIGINT
  71.  
  72. #ifdef MSDOS
  73. #include <dos.h>
  74. #include <conio.h>
  75. #include <stdlib.h>
  76. #include <string.h>
  77. #include <time.h>
  78. #else
  79. #include <dos.h>
  80. #include <stdlib.h>
  81. #include <string.h>
  82. #include <time.h>
  83. /*
  84. #include <sys/param.h>
  85. #include <sys/types.h>
  86. #include <sys/file.h>
  87. #include <sys/ioctl.h>
  88. */
  89. #endif
  90.  
  91.  
  92. /*
  93.  * ataks.h - Header source for GNU CHESS
  94.  *
  95.  * Copyright (c) 1988,1989,1990 John Stanback
  96.  * Copyright (c) 1992 Free Software Foundation
  97.  *
  98.  * This file is part of GNU CHESS.
  99.  *
  100.  * GNU Chess is free software; you can redistribute it and/or modify
  101.  * it under the terms of the GNU General Public License as published by
  102.  * the Free Software Foundation; either version 2, or (at your option)
  103.  * any later version.
  104.  *
  105.  * GNU Chess is distributed in the hope that it will be useful,
  106.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  107.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  108.  * GNU General Public License for more details.
  109.  *
  110.  * You should have received a copy of the GNU General Public License
  111.  * along with GNU Chess; see the file COPYING.  If not, write to
  112.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  113.  */
  114. inline int
  115. SqAtakd2 (ARGSZ int sq, ARGSZ int side)
  116.  
  117. /*
  118.  * See if any piece with color 'side' ataks sq.  First check pawns then
  119.  * Queen, Bishop, Rook and King and last Knight.
  120.  */
  121.  
  122. {
  123.   register INTSIZE u;
  124.   register unsigned char *ppos, *pdir;
  125.   INTSIZE xside;
  126.  
  127.   xside = side ^ 1;
  128.   pdir = nextdir[ptype[xside][pawn]][sq];
  129.   u = pdir[sq];            /* follow captures thread */
  130.   if (u != sq)
  131.     {
  132.       if (board[u] == pawn && color[u] == side)
  133.     return (true);
  134.       u = pdir[u];
  135.       if (u != sq && board[u] == pawn && color[u] == side)
  136.     return (true);
  137.     }
  138.   /* king capture */
  139.   if (distance (sq, PieceList[side][0]) == 1)
  140.     return (true);
  141.   /* try a queen bishop capture */
  142.   ppos = nextpos[bishop][sq];
  143.   pdir = nextdir[bishop][sq];
  144.   u = ppos[sq];
  145.   do
  146.     {
  147.       if (color[u] == neutral)
  148.     u = ppos[u];
  149.       else
  150.     {
  151.       if (color[u] == side && (board[u] == queen || board[u] == bishop))
  152.         return (true);
  153.       u = pdir[u];
  154.     }
  155.   } while (u != sq);
  156.   /* try a queen rook capture */
  157.   ppos = nextpos[rook][sq];
  158.   pdir = nextdir[rook][sq];
  159.   u = ppos[sq];
  160.   do
  161.     {
  162.       if (color[u] == neutral)
  163.     u = ppos[u];
  164.       else
  165.     {
  166.       if (color[u] == side && (board[u] == queen || board[u] == rook))
  167.         return (true);
  168.       u = pdir[u];
  169.     }
  170.   } while (u != sq);
  171.   /* try a knight capture */
  172.   pdir = nextdir[knight][sq];
  173.   u = pdir[sq];
  174.   do
  175.     {
  176.       if (color[u] == side && board[u] == knight)
  177.     return (true);
  178.       u = pdir[u];
  179.   } while (u != sq);
  180.   return (false);
  181. }
  182.  
  183.  
  184. void
  185. algbr (INTSIZE int f, INTSIZE int t, INTSIZE int flag)
  186.  
  187.  
  188. /*
  189.  * Generate move strings in different formats.
  190.  */
  191.  
  192. {
  193.   int m3p;
  194.  
  195.   if (f != t)
  196.     {
  197.       /* algebraic notation */
  198.       mvstr[0][0] = cxx[column (f)];
  199.       mvstr[0][1] = rxx[row (f)];
  200.       mvstr[0][2] = cxx[column (t)];
  201.       mvstr[0][3] = rxx[row (t)];
  202.       mvstr[0][4] = mvstr[3][0] = '\0';
  203.       if (((mvstr[1][0] = pxx[board[f]]) == 'P') || (flag & promote))
  204.     {
  205.       if (mvstr[0][0] == mvstr[0][2])    /* pawn did not eat */
  206.         {
  207.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  208.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  209.           m3p = 2;
  210.         }
  211.       else
  212.         /* pawn ate */
  213.         {
  214.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* column */
  215.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  216.           mvstr[2][2] = mvstr[0][3];
  217.           m3p = 3;        /* to row */
  218.         }
  219.       if (flag & promote)
  220.         {
  221.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = qxx[flag & pmask];
  222.           mvstr[0][5] = mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[3][0] = '\0';
  223. #ifdef CHESSTOOL
  224.           mvstr[3][0] = mvstr[0][0];    /* Allow e7e8 for chesstool */
  225.           mvstr[3][1] = mvstr[0][1];
  226.           mvstr[3][2] = mvstr[0][2];
  227.           mvstr[3][3] = mvstr[0][3];
  228.           mvstr[3][4] = '\0';
  229. #endif
  230.         }
  231.       mvstr[2][m3p] = mvstr[1][2] = '\0';
  232.     }
  233.       else
  234.     /* not a pawn */
  235.     {
  236.       mvstr[2][0] = mvstr[1][0];
  237.       mvstr[2][1] = mvstr[0][1];
  238.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  239.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  240.       mvstr[2][4] = mvstr[1][3] = '\0';
  241.       strcpy (mvstr[3], mvstr[2]);
  242.       mvstr[3][1] = mvstr[0][0];
  243.       if (flag & cstlmask)
  244.         {
  245.           if (t > f)
  246.         {
  247.           strcpy (mvstr[1], CP[5]);
  248.           strcpy (mvstr[2], CP[7]);
  249.         }
  250.           else
  251.         {
  252.           strcpy (mvstr[1], CP[6]);
  253.           strcpy (mvstr[2], CP[8]);
  254.         }
  255.         }
  256.     }
  257.     }
  258.   else
  259.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  260. }
  261.  
  262.  
  263. int
  264. VerifyMove (char *s, INTSIZE int iop, INTSIZE unsigned int *mv)
  265.  
  266. /*
  267.  * Compare the string 's' to the list of legal moves available for the
  268.  * opponent. If a match is found, make the move on the board.
  269.  */
  270.  
  271. {
  272.   static INTSIZE pnt, tempb, tempc, tempsf, tempst, cnt;
  273.   int r,c,l;
  274.   char mystr[80];
  275.   char piece;
  276.   static struct leaf xnode;
  277.   struct leaf *node;
  278.  
  279.   if ((s[1] >= 'a') && (s[1] <= 'h'))
  280.    {
  281.     if ((s[0] == 'n') || (s[0] == 'p') || (s[0] == 'b') ||
  282.         (s[0] == 'k') || (s[0] == 'r') || (s[0] == 'q'))
  283.      {
  284.       s[0] = toupper(s[0]);
  285.      }
  286.    }
  287.   *mv = 0;
  288.   if (iop == 2)
  289.     {
  290.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  291.       return (false);
  292.     }
  293.   cnt = 0;
  294.   MoveList (opponent, 2);
  295.   pnt = TrPnt[2];
  296.   while (pnt < TrPnt[3])
  297.     {
  298.       node = &Tree[pnt++];
  299.       algbr (node->f, node->t, (INTSIZE) node->flags);
  300.       if (strcmp (s, mvstr[0]) == 0 || strcmp (s, mvstr[1]) == 0 ||
  301.       strcmp (s, mvstr[2]) == 0 || strcmp (s, mvstr[3]) == 0)
  302.     {
  303.       cnt++;
  304.       xnode = *node;
  305.     }
  306.     }
  307.   if (cnt == 1)
  308.     {
  309.       MakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  310.       if (SqAtakd2 (PieceList[opponent][0], computer))
  311.     {
  312.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  313. #if defined CHESSTOOL
  314.       printz (CP[15]);
  315. #else
  316. #ifdef NONDSP
  317. /* Illegal move in check */
  318. #ifndef AMIGA
  319.       printz (CP[77]);
  320.       printz ("\n");
  321. #else
  322.           DisplayComputerMove(CP[77]);
  323. #endif
  324. #else
  325. /* Illegal move in check */
  326.           if (!verifyquiet)
  327.         ShowMessage (CP[77]);
  328. #endif
  329. #endif /* CHESSTOOL */
  330.       return (false);
  331.     }
  332.       else
  333.     {
  334.       if (iop == 1)
  335.         return (true);
  336.       UpdateDisplay (xnode.f, xnode.t, 0, (INTSIZE) xnode.flags);
  337.       if ((board[xnode.t] == pawn)
  338.           || (xnode.flags & capture)
  339.           || (xnode.flags & cstlmask))
  340.         {
  341.           Game50 = GameCnt;
  342.           ZeroRPT ();
  343.         }
  344.       GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  345.       GameList[GameCnt].nodes = 0;
  346.       ElapsedTime (1);
  347.       GameList[GameCnt].time = (INTSIZE) et;
  348.       if (TCflag)
  349.         {
  350.           TimeControl.clock[opponent] -= et;
  351.           timeopp[oppptr] = et;
  352.           --TimeControl.moves[opponent];
  353.         }
  354.       *mv = (xnode.f << 8) | xnode.t;
  355.       algbr (xnode.f, xnode.t, false);
  356. #ifdef AMIGA
  357.       strcpy(mystr,mvstr[0]);
  358.       r = mystr[3] - '1';
  359.       c = mystr[2] - 'a';
  360.       l = ((flag.reverse) ? locn (7 - r, 7 - c) : locn (r, c));
  361.       if (color[l] == neutral)
  362.        {
  363.     DisplayBeep(0L);
  364.         Delay(25L);
  365.     DisplayBeep(0L);
  366.         Delay(25L);
  367.     DisplayBeep(0L);
  368.         Delay(25L);
  369.         piece = ' ';
  370.        }
  371.       else if (color[l] == white)
  372.         piece = qxx[board[l]]; /* white are lower case pieces */
  373.       else
  374.         piece = pxx[board[l]]; /* black are upper case pieces */
  375.       AnimateAmigaMove(mystr,piece);
  376. #endif
  377.       return (true);
  378.     }
  379.     }
  380. #if defined CHESSTOOL
  381.   printz (CP[78]);
  382. #else
  383. #ifdef NONDSP
  384. /* Illegal move */
  385. #ifdef AMIGA
  386.   sprintf(mystr,CP[75],s);
  387.   if (!verifyquiet)
  388.    DisplayComputerMove(mystr);
  389. #else
  390.   printz (CP[75], s);
  391. #endif
  392. #ifdef DEBUG8
  393.   if (1)
  394.     {
  395.       FILE *D;
  396.       int r, c, l;
  397.       extern unsigned INTSIZE int PrVar[];
  398.       D = fopen ("/tmp/DEBUG", "a+");
  399.       pnt = TrPnt[2];
  400.       fprintf (D, "resp = %d\n", ResponseTime);
  401.       fprintf (D, "iop = %d\n", iop);
  402.       fprintf (D, "matches = %d\n", cnt);
  403.       algbr (hint >> 8, hint & 0xff, (INTSIZE) 0);
  404.       fprintf (D, "hint %s\n", mvstr[0]);
  405.       fprintf (D, "inout move is %s\n", s);
  406.       for (r = 1; PrVar[r]; r++)
  407.     {
  408.       algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (INTSIZE) 0);
  409.       fprintf (D, " %s", mvstr[0]);
  410.     }
  411.       fprintf (D, "\n");
  412.       fprintf (D, "legal move are \n");
  413.       while (pnt < TrPnt[3])
  414.     {
  415.       node = &Tree[pnt++];
  416.       algbr (node->f, node->t, (INTSIZE) node->flags);
  417.       fprintf (D, "%s %s %s %s\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3]);
  418.     }
  419.       fprintf (D, "\n current board is\n");
  420.       for (r = 7; r >= 0; r--)
  421.     {
  422.       for (c = 0; c <= 7; c++)
  423.         {
  424.           l = locn (r, c);
  425.           if (color[l] == neutral)
  426.         fprintf (D, " -");
  427.           else if (color[l] == white)
  428.         fprintf (D, " %c", qxx[board[l]]);
  429.           else
  430.         fprintf (D, " %c", pxx[board[l]]);
  431.         }
  432.       fprintf (D, "\n");
  433.     }
  434.       fprintf (D, "\n");
  435.       fclose (D);
  436.       abort ();
  437.     }
  438. #endif
  439. #else
  440. /* Illegal move */
  441.  if (!verifyquiet)
  442.   ShowMessage (CP[76]);
  443. #endif
  444. #endif /* CHESSTOOL */
  445. #if !defined CHESSTOOL && !defined XBOARD
  446.   if (cnt > 1)
  447.     ShowMessage (CP[32]);
  448. #endif /* CHESSTOOL */
  449.   return (false);
  450. }
  451.  
  452. int
  453. parser (char *f, int side)
  454. {
  455.   int c1, r1, c2, r2;
  456.  
  457.   if (f[4] == 'o')
  458.     if (side == black)
  459.       return 0x3C3A;
  460.     else
  461.       return 0x0402;
  462.   else if (f[0] == 'o')
  463.     if (side == black)
  464.       return 0x3C3E;
  465.     else
  466.       return 0x0406;
  467.   else
  468.     {
  469.       c1 = f[0] - 'a';
  470.       r1 = f[1] - '1';
  471.       c2 = f[2] - 'a';
  472.       r2 = f[3] - '1';
  473.       return (locn (r1, c1) << 8) | locn (r2, c2);
  474.     }
  475.   /*NOTREACHED*/
  476. }
  477.  
  478. int myfgets(char *buff,int len,BPTR fd)
  479. {
  480.  char tmpch;
  481.  int done=0;
  482.  int numchars=0;
  483.  int retval;
  484.  
  485.  retval = 1;
  486.  do{
  487.     if (Read(fd,&tmpch,1L) != 1L)
  488.      {
  489.       done = 1;
  490.       retval = 0;
  491.      }
  492.     else
  493.      {
  494.       buff[numchars++] = tmpch;
  495.       if (tmpch == '\n')
  496.        {
  497.         done = 1;
  498.        }
  499.       if (numchars >= (len-1))
  500.        {
  501.         done = 1;
  502.         retval = 0;
  503.        }
  504.      }
  505.   } while (!done);
  506.  buff[numchars] = '\0';
  507.  return(retval);
  508. }
  509.  
  510. void
  511. GetGame (void)
  512. {
  513.   char checkstr[8];
  514.   BPTR fd;
  515.   char fname[256], *p;
  516.   int c, i, j;
  517.   INTSIZE sq;
  518.  
  519. /* enter file name */
  520.  if (!GetFileName(fname))
  521.   {
  522.    return;
  523.   }
  524.  (void)SetTaskPri((struct Task *)myproc,0);
  525.  if (fname[0] == 0)
  526.   ShowMessage (CP[63]);
  527. #ifndef AMIGA
  528.   scanz ("%s", fname);
  529. #endif
  530. /* chess.000 */
  531.   if (fname[0] == '\0')
  532.     strcpy (fname, CP[137]);
  533.   ShowMessage("Loading Game..");
  534.   Delay(20L);
  535.   if ((fd = Open (fname, MODE_OLDFILE)) != NULL)
  536.     {
  537.       Delay(20L);
  538.       NewGame ();
  539.       ShowMessage("Loading Game..");
  540.       Delay(10L);
  541.       myfgets (fname, 256, fd);
  542.       for(i=0;i<5;i++)
  543.        checkstr[i] = fname[i];
  544.       checkstr[5] = 0;
  545.       if (stricmp(checkstr,"Black"))
  546.        {
  547.         DisplayBeep(0L);
  548.         Delay(25L);
  549.         DisplayBeep(0L);
  550.         Close(fd);
  551.         return;
  552.        }
  553.       computer = opponent = white;
  554.       InPtr = fname;
  555.       skip ();
  556.       if (*InPtr == 'c')
  557.     computer = black;
  558.       else
  559.     opponent = black;
  560.       skip ();
  561.       skip ();
  562.       skip ();
  563.       Game50 = atoi (InPtr);
  564.       myfgets (fname, 256, fd);
  565.       InPtr = &fname[14];
  566.       castld[white] = ((*InPtr == CP[214][0]) ? true : false);
  567.       skip ();
  568.       skip ();
  569.       castld[black] = ((*InPtr == CP[214][0]) ? true : false);
  570.       myfgets (fname, 256, fd);
  571.       InPtr = &fname[11];
  572.       skipb ();
  573.       TCflag = atoi (InPtr);
  574.       skip ();
  575.       InPtr += 14;
  576.       skipb ();
  577.       OperatorTime = atoi (InPtr);
  578.       myfgets (fname, 256, fd);
  579.       InPtr = &fname[11];
  580.       skipb ();
  581.       TimeControl.clock[white] = atoi (InPtr);
  582.       skip ();
  583.       skip ();
  584.       TimeControl.moves[white] = atoi (InPtr);
  585.       myfgets (fname, 256, fd);
  586.       InPtr = &fname[11];
  587.       skipb ();
  588.       TimeControl.clock[black] = atoi (InPtr);
  589.       skip ();
  590.       skip ();
  591.       TimeControl.moves[black] = atoi (InPtr);
  592.       myfgets (fname, 256, fd);
  593.       for (i = 7; i > -1; i--)
  594.     {
  595.       myfgets (fname, 256, fd);
  596.       p = &fname[2];
  597.       InPtr = &fname[11];
  598.       skipb ();
  599.       for (j = 0; j < 8; j++)
  600.         {
  601.           sq = i * 8 + j;
  602.           if (*p == '.')
  603.         {
  604.           board[sq] = no_piece;
  605.           color[sq] = neutral;
  606.         }
  607.           else
  608.         {
  609.           for (c = 0; c < 8; c++)
  610.             {
  611.               if (*p == pxx[c])
  612.             {
  613.               board[sq] = c;
  614.               color[sq] = black;
  615.             }
  616.             }
  617.           for (c = 0; c < 8; c++)
  618.             {
  619.               if (*p == qxx[c])
  620.             {
  621.               board[sq] = c;
  622.               color[sq] = white;
  623.             }
  624.             }
  625.         }
  626.           p++;
  627.           Mvboard[sq] = atoi (InPtr);
  628.           skip ();
  629.         }
  630.     }
  631.       GameCnt = 0;
  632.       flag.regularstart = false/*true*/;
  633.       myfgets (fname, 256, fd);
  634.       myfgets (fname, 256, fd);
  635.       myfgets (fname, 256, fd);
  636.       while (myfgets (fname, 256, fd))
  637.     {
  638.       struct GameRec *g;
  639.       int side = black; // was side = computer in 2.51
  640.  
  641. /*printf("in while loop\n");*/
  642.       side = side ^ 1;
  643.       ++GameCnt;
  644.       InPtr = fname;
  645.       skipb ();
  646.       g = &GameList[GameCnt];
  647.       g->gmove = parser (InPtr, side);
  648.       skip ();
  649.       g->score = atoi (InPtr);
  650.       skip ();
  651.       g->depth = atoi (InPtr);
  652.       skip ();
  653.       g->nodes = atoi (InPtr);
  654.       skip ();
  655.       g->time = atoi (InPtr);
  656.       skip ();
  657.       g->flags = c = atoi (InPtr);
  658.       skip ();
  659.       g->hashkey = strtol (InPtr, (char **) NULL, 16);
  660.       skip ();
  661.       g->hashbd = strtol (InPtr, (char **) NULL, 16);
  662.       g->piece = no_piece;
  663.       g->color = neutral;
  664.       if (c & (capture | cstlmask))
  665.         {
  666.           if (c & capture)
  667.         {
  668.           skip ();
  669.           for (c = 0; c < 8; c++)
  670.             if (pxx[c] == *InPtr)
  671.               break;
  672.           g->piece = c;
  673.         }
  674.           skip ();
  675.           g->color = ((*InPtr == CP[119][0]) ? black : white);
  676.         }
  677.     }
  678.       /* GameCnt--; */
  679.       if (TimeControl.clock[white] > 0)
  680.     TCflag = true;
  681.       Close (fd);
  682.     }
  683.   ISZERO = 1;
  684.   ZeroRPT ();
  685.   InitializeStats ();
  686.   UpdateDisplay (0, 0, 1, 0);
  687.   Sdepth = 0;
  688.   hint = 0;
  689.   DisableMoveNow();
  690.   if (!TCflag)
  691.    {
  692.     EnableMoveNow();
  693.    }
  694.   else
  695.    {
  696.     i = player;
  697.     player = black;
  698.     UpdateClocks();
  699.     player = white;
  700.     UpdateClocks();
  701.     player = i;
  702.     if ((((TimeControl.clock[black])/TimeControl.moves[black]) > 5900) ||
  703.         (((TimeControl.clock[white])/TimeControl.moves[white]) > 5900))
  704.      {
  705.       EnableMoveNow();
  706.      }
  707.    }
  708.   flag.regularstart = false;
  709.   Book = 0;
  710.   if (MenuStripSet)
  711.    {
  712.     
  713.     MenuItem8ab.Flags &= (0xffff ^ CHECKED);
  714.     SetMenuStrip(wG,&MenuList1);    /* attach any Menu */
  715.    }
  716.   ZeroTTable();
  717.   ShowMessage("Game Loaded");
  718. #ifdef AMIGA
  719.   DrawAmigaBoard();
  720. #endif
  721.   (void)SetTaskPri((struct Task *)myproc,procpri);
  722. }
  723.  
  724. void
  725. GetXGame (void)
  726. {
  727.   BPTR fd;
  728.   char fname[256], *p;
  729.   int c, i, j;
  730.   INTSIZE sq;
  731. /* Enter file name */
  732.   ShowMessage (CP[63]);
  733. #ifndef AMIGA
  734.   scanz ("%s", fname);
  735. #endif
  736.   if (fname[0] == '\0')
  737. /* xboard.position.read*/
  738.     strcpy (fname, CP[205]);
  739.   if ((fd = Open (fname, MODE_OLDFILE)) != NULL)
  740.     {
  741.       NewGame ();
  742.       flag.regularstart = false;
  743.       Book = false;
  744.       myfgets (fname, 256, fd);
  745.       fname[6] = '\0';
  746.       if (strcmp (fname, CP[206]))
  747.     return;
  748.       myfgets (fname, 256, fd);
  749.       myfgets (fname, 256, fd);
  750.       for (i = 7; i > -1; i--)
  751.     {
  752.       myfgets (fname, 256, fd);
  753.       p = fname;
  754.       for (j = 0; j < 8; j++)
  755.         {
  756.           sq = i * 8 + j;
  757.           if (*p == '.')
  758.         {
  759.           board[sq] = no_piece;
  760.           color[sq] = neutral;
  761.         }
  762.           else
  763.         {
  764.           for (c = 0; c < 8; c++)
  765.             {
  766.               if (*p == qxx[c])
  767.             {
  768.               board[sq] = c;
  769.               color[sq] = black;
  770.             }
  771.             }
  772.           for (c = 0; c < 8; c++)
  773.             {
  774.               if (*p == pxx[c])
  775.             {
  776.               board[sq] = c;
  777.               color[sq] = white;
  778.             }
  779.             }
  780.         }
  781.           p += 2;
  782.         }
  783.     }
  784.       Close (fd);
  785.     }
  786.   ISZERO = 1;
  787.   ZeroRPT ();
  788.   InitializeStats ();
  789.   UpdateDisplay (0, 0, 1, 0);
  790.   Sdepth = 0;
  791.   hint = 0;
  792. }
  793.  
  794. void
  795. SaveGame (void)
  796. {
  797.   FILE *fd;
  798.   char fname[256];
  799.   INTSIZE sq, i, c, f, t;
  800.   char p;
  801.  
  802.   if (!(GetFileName(savefile)))   
  803.    { 
  804.     return;
  805.    }
  806.   (void)SetTaskPri((struct Task *)myproc,0);
  807.   if (savefile[0])
  808.     strcpy (fname, savefile);
  809.   else
  810.     {
  811. /* Enter file name*/
  812.       ShowMessage (CP[63]);
  813. #ifndef AMIGA
  814.       scanz ("%s", fname);
  815. #endif
  816.     }
  817.  
  818.   if (fname[0] == '\0')
  819. /* chess.000 */
  820.     strcpy (fname, CP[137]);
  821.   Delay(25L);
  822.   if ((fd = fopen (fname, "w")) != NULL)
  823.     {
  824.       char *b, *w;
  825.  
  826.       Delay(25L);
  827.       b = w = CP[74];
  828.       if (computer == black)
  829.     b = CP[141];
  830.       if (computer == white)
  831.     w = CP[141];
  832.       fprintf (fd, CP[37], b, w, Game50);
  833.       fprintf (fd, CP[42], castld[white] ? CP[214] : CP[215], castld[black] ? CP[214] : CP[215]);
  834.       fprintf (fd, CP[111], TCflag, OperatorTime);
  835.       fprintf (fd, CP[117],
  836.            TimeControl.clock[white], TimeControl.moves[white],
  837.            TimeControl.clock[black], TimeControl.moves[black]);
  838.       for (i = 7; i > -1; i--)
  839.     {
  840.       fprintf (fd, "%1d ", i + 1);
  841.       for (c = 0; c < 8; c++)
  842.         {
  843.           sq = i * 8 + c;
  844.           switch (color[sq])
  845.         {
  846.         case black:
  847.           p = pxx[board[sq]];
  848.           break;
  849.         case white:
  850.           p = qxx[board[sq]];
  851.           break;
  852.         default:
  853.           p = '.';
  854.         }
  855.           fprintf (fd, "%c", p);
  856.         }
  857.       for (f = i * 8; f < i * 8 + 8; f++)
  858.         fprintf (fd, " %d", Mvboard[f]);
  859.       fprintf (fd, "\n");
  860.     }
  861.       fprintf (fd, "  %s\n", cxx);
  862.       fprintf (fd, CP[126]);
  863.       for (i = 1; i <= GameCnt; i++)
  864.     {
  865.       struct GameRec *g = &GameList[i];
  866.  
  867.       f = g->gmove >> 8;
  868.       t = (g->gmove & 0xFF);
  869.       algbr (f, t, g->flags);
  870.       fprintf (fd, "%s %5d %5d %7ld %5d %5d  %#08lx %#08lx  %c   %s\n",
  871.            mvstr[0], g->score, g->depth,
  872.            g->nodes, g->time, g->flags, g->hashkey, g->hashbd,
  873.        pxx[g->piece], ((g->color == 2) ? "     " : ColorStr[g->color]));
  874.     }
  875.       fclose (fd);
  876. /* Game saved */
  877.       ShowMessage (CP[70]);
  878.     }
  879.   else
  880.     /*ShowMessage ("Could not open file");*/
  881.     ShowMessage (CP[48]);
  882.   (void)SetTaskPri((struct Task *)myproc,procpri);
  883. }
  884.  
  885. void
  886. ListGame (getstr)
  887. int getstr;
  888. {
  889.   FILE *fd;
  890.   INTSIZE i, f, t;
  891.   long when;
  892.   char fname[256], dbuf[256];
  893.  
  894.   if (listfile[0])
  895.     strcpy (fname, listfile);
  896.   else
  897.     {
  898. #ifdef MSDOS
  899.       sprintf (fname, "chess.lst");
  900. #else
  901.       if (!getstr)
  902.        {
  903.         time (&when);
  904.         strncpy (dbuf, ctime (&when), 20);
  905.         dbuf[7] = '\0';
  906.         dbuf[10] = '\0';
  907.         dbuf[13] = '\0';
  908.         dbuf[16] = '\0';
  909.         dbuf[19] = '\0';
  910. /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
  911.    date is Jan 1
  912.    time is 02:03:04
  913.    program played black */
  914.         sprintf (fname, "t:UC%s.%s%s-%s%s%s%c", patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14, dbuf + 17, ColorStr[computer][0]);
  915.         /* replace space padding with 0 */
  916.         for (i = 0; fname[i] != '\0'; i++)
  917.         if (fname[i] == ' ')
  918.         fname[i] = '0';
  919.        }
  920.       else
  921.        {
  922.         if (!GetFileName(fname))
  923.          {
  924.           return;
  925.          }
  926.        }
  927. #endif /* MSDOS */
  928.     }
  929.   Delay(5L);
  930.   (void)SetTaskPri((struct Task *)myproc,0);
  931.   fd = fopen (fname, "w");
  932.   Delay(5L);
  933.   if (!fd)
  934.     {
  935.      (void)SetTaskPri((struct Task *)myproc,procpri);
  936.      return;
  937.     }
  938.   /*fprintf (fd, "gnuchess game %d\n", u);*/
  939.   fprintf (fd, "%s\n", VERSTRING);
  940.   fprintf (fd, CP[10]);
  941.   fprintf (fd, CP[11]);
  942.   for (i = 1; i <= GameCnt; i++)
  943.     {
  944.       f = GameList[i].gmove >> 8;
  945.       t = (GameList[i].gmove & 0xFF);
  946.       algbr (f, t, GameList[i].flags);
  947.       if(GameList[i].flags & book)
  948.           fprintf (fd, "%5s  %5d    Book%7ld %5d", mvstr[0],
  949.            GameList[i].score, 
  950.            GameList[i].nodes, GameList[i].time);
  951.       else
  952.           fprintf (fd, "%5s  %5d     %2d %7ld %5d", mvstr[0],
  953.            GameList[i].score, GameList[i].depth,
  954.            GameList[i].nodes, GameList[i].time);
  955.       if ((i % 2) == 0)
  956.     {
  957.       fprintf (fd, "\n");
  958. #ifdef DEBUG40
  959.       if (computer == black)
  960.         fprintf (fd, " %d %d %d %d %d %d %d\n",
  961.              GameList[i].d1,
  962.              GameList[i].d2,
  963.              GameList[i].d3,
  964.              GameList[i].d4,
  965.              GameList[i].d5,
  966.              GameList[i].d6,
  967.              GameList[i].d7);
  968.       else
  969.         fprintf (fd, " %d %d %d %d %d %d %d\n",
  970.              GameList[i - 1].d1,
  971.              GameList[i - 1].d2,
  972.              GameList[i - 1].d3,
  973.              GameList[i - 1].d4,
  974.              GameList[i - 1].d5,
  975.              GameList[i - 1].d6,
  976.              GameList[i - 1].d7);
  977. #endif
  978.     }
  979.       else
  980.     fprintf (fd, "         ");
  981.     }
  982.   fprintf (fd, "\n\n");
  983.   if (GameList[GameCnt].flags & draw)
  984.     {
  985.       fprintf (fd, CP[54], DRAW);
  986.     }
  987.   else if (GameList[GameCnt].score == -9999)
  988.     {
  989.       fprintf (fd, "%s\n", ColorStr[player ]);
  990.     }
  991.   else if (GameList[GameCnt].score == 9998)
  992.     {
  993.       fprintf (fd, "%s\n", ColorStr[player ^ 1]);
  994.     }
  995.   fclose (fd);
  996.   (void)SetTaskPri((struct Task *)myproc,procpri);
  997. }
  998.  
  999. void
  1000. Undo (void)
  1001.  
  1002. /*
  1003.  * Undo the most recent half-move.
  1004.  */
  1005.  
  1006. {
  1007.   INTSIZE f, t;
  1008.   f = GameList[GameCnt].gmove >> 8;
  1009.   t = GameList[GameCnt].gmove & 0xFF;
  1010.   if (board[t] == king && distance (t, f) > 1)
  1011.     (void) castle (GameList[GameCnt].color, f, t, 2);
  1012.   else
  1013.     {
  1014.       /* Check for promotion: */
  1015.       if (GameList[GameCnt].flags & promote)
  1016.     {
  1017.       board[t] = pawn;
  1018.     }
  1019.       board[f] = board[t];
  1020.       color[f] = color[t];
  1021.       board[t] = GameList[GameCnt].piece;
  1022.       color[t] = GameList[GameCnt].color;
  1023.       if (color[t] != neutral)
  1024.     Mvboard[t]--;
  1025.       Mvboard[f]--;
  1026.     }
  1027.   if (GameList[GameCnt].flags & epmask)
  1028.     EnPassant (otherside[color[f]], f, t, 2);
  1029.   else
  1030.     InitializeStats ();
  1031.   epsquare = GameList[GameCnt].epssq;
  1032.   if (TCflag && (TCmoves>1))
  1033.     ++TimeControl.moves[color[f]];
  1034.   hashkey = GameList[GameCnt].hashkey;
  1035.   hashbd = GameList[GameCnt].hashbd;
  1036.   GameCnt--;
  1037.   computer = computer ^ 1;
  1038.   opponent = opponent ^ 1;
  1039.   Mate = flag.mate = false;
  1040.   Sdepth = 0;
  1041.   player = player ^ 1;
  1042.   ShowSidetoMove ();
  1043.   UpdateDisplay (0, 0, 1, 0);
  1044.  
  1045.   InitializeStats();
  1046.   ZeroRPT();
  1047.   ZeroTTable();
  1048. #ifdef HISTORY
  1049. #ifdef AMIGA
  1050.  ClearMem(history,sizeof(history));
  1051. #else
  1052.  memset(history,0,sizeof(history));
  1053. #endif
  1054. #endif
  1055.  
  1056. /*  if (flag.regularstart)
  1057.     Book = BOOKFAIL;*/
  1058. }
  1059.  
  1060. void
  1061.  TestSpeed (void (*f) ( INTSIZE int side, INTSIZE int ply), unsigned j)
  1062. {
  1063.   char astr[256];
  1064.   INTSIZE i;
  1065.   long cnt, rate, t1, t2;
  1066.  
  1067.   t1 = time (0);
  1068.   Forbid();
  1069.   for (i = 0; i < j; i++)
  1070.     {
  1071.       f (opponent, 2);
  1072.     }
  1073.   Permit();
  1074.   t2 = time (0);
  1075.   cnt = j * (TrPnt[3] - TrPnt[2]);
  1076.   if (t2 - t1)
  1077.     et = (t2 - t1) * 100;
  1078.   else
  1079.     et = 1;
  1080.   rate = (et) ? (cnt / et) : 0;
  1081.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  1082. #ifdef NONDSP
  1083. #ifdef AMIGA
  1084. if (!func_num)
  1085.  sprintf(astr,"Mlst=%dN/s",rate*100);
  1086. else if (func_num == 1)
  1087.  sprintf(astr,"Clst=%dN/s",rate*100);
  1088. ShowMessage(astr);
  1089. #else
  1090.   printz (CP[91], cnt, rate*100);
  1091. #endif
  1092. #ifdef DEBUG9
  1093.   for (j = TrPnt[2]; j < TrPnt[3]; j++)
  1094.     {
  1095.       struct leaf *node = &Tree[j];
  1096.       algbr (node->f, node->t, node->flags);
  1097.       printf ("%s %s %s %s %d %x\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],node->score,node->flags);
  1098.     }
  1099. #endif
  1100. #else
  1101.   ShowNodeCnt (cnt);
  1102. #endif
  1103. }
  1104.  
  1105. void
  1106.  TestPSpeed (INTSIZE int (*f) (INTSIZE int side), unsigned j)
  1107. {
  1108.   char astr[256];
  1109.   INTSIZE i;
  1110.   long cnt, rate, t1, t2;
  1111.  
  1112.   t1 = time (0);
  1113.   Forbid();
  1114.   for (i = 0; i < j; i++)
  1115.     {
  1116.       (void) f (opponent);
  1117.     }
  1118.   Permit();
  1119.   t2 = time (0);
  1120.   cnt = j;
  1121.   if (t2 - t1)
  1122.     et = (t2 - t1) * 100;
  1123.   else
  1124.     et = 1;
  1125.   rate = (et) ? (cnt / et) : 0;
  1126.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  1127. #ifdef NONDSP
  1128. #ifdef AMIGA
  1129. sprintf(astr,"Eval=%ldN/s",rate*100);
  1130. ShowMessage(astr);
  1131. #else
  1132.   printz (CP[91], cnt, rate*100);
  1133. #endif
  1134. #else
  1135.   ShowNodeCnt (cnt);
  1136. #endif
  1137. }
  1138.  
  1139.  
  1140. void
  1141. SetMachineTime (char *s)
  1142. {
  1143.   char *time;
  1144.   int m, t;
  1145.   time = &s[strlen (CP[197])];
  1146.   m = strtol (time, &time, 10);
  1147.   t = strtol (time, &time, 10);
  1148.   if (t)
  1149.     TimeControl.clock[computer] = t;
  1150.   if (m)
  1151.     TimeControl.moves[computer] = m;
  1152. #ifdef XBOARD
  1153.   printz (CP[222], m, t);
  1154. #endif
  1155. }
  1156.  
  1157.  
  1158. void
  1159. InputCommand (cstring)
  1160.  
  1161. char *cstring;
  1162.  
  1163. /*
  1164.  * Process the users command. If easy mode is OFF (the computer is thinking
  1165.  * on opponents time) and the program is out of book, then make the 'hint'
  1166.  * move on the board and call SelectMove() to find a response. The user
  1167.  * terminates the search by entering ^C (quit siqnal) before entering a
  1168.  * command. If the opponent does not make the hint move, then set Sdepth to
  1169.  * zero.
  1170.  */
  1171.  
  1172. {
  1173.   int need_to_zero;
  1174.   char tstr[40];
  1175.   int i = 0;
  1176.   INTSIZE have_shown_prompt = false;
  1177.   INTSIZE ok, tmp;
  1178.   unsigned INTSIZE mv;
  1179.   char s[80], sx[80];
  1180.  
  1181. #if defined CHESSTOOL
  1182.   INTSIZE normal = false;
  1183.  
  1184. #endif
  1185.  
  1186.   ok = flag.quit = false;
  1187.   player = opponent;
  1188.   ft = 0;
  1189. #ifdef CACHE
  1190.   if(TTadd > ttblsize) // want to zero out ttable each time
  1191.    ZeroTTable();
  1192. #endif
  1193.   if (hint > 0 && !flag.easy && !flag.force)
  1194.     if ((board[hint >> 8] != pawn) || ((row (hint & 0x3f) != 0) && (row (hint & 0x3f) != 7)))
  1195.       {
  1196.         thinkahead = 1;
  1197. /*    fflush (stdout);*/
  1198.         ft = time0;
  1199.     /*time0 = time ((long *) 0);*/
  1200.     algbr ((INTSIZE) hint >> 8, (INTSIZE) hint & 0x3F, false);
  1201.     strcpy (s, mvstr[0]);
  1202.     tmp = epsquare;
  1203. #ifdef DEBUG12
  1204.     if (1)
  1205.       {
  1206.         FILE *D;
  1207.         int r, c, l;
  1208.         extern unsigned INTSIZE int PrVar[];
  1209.         D = fopen ("/tmp/DEBUGA", "a+");
  1210.         fprintf (D, "score = %d\n", root->score);
  1211.         fprintf (D, "inout move is %s\n", s);
  1212.         for (r = 1; PrVar[r]; r++)
  1213.           {
  1214.         algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (INTSIZE) 0);
  1215.         fprintf (D, " %s", mvstr[0]);
  1216.           }
  1217.         fprintf (D, "\n current board is\n");
  1218.         for (r = 7; r >= 0; r--)
  1219.           {
  1220.         for (c = 0; c <= 7; c++)
  1221.           {
  1222.             l = locn (r, c);
  1223.             if (color[l] == neutral)
  1224.               fprintf (D, " -");
  1225.             else if (color[l] == white)
  1226.               fprintf (D, " %c", qxx[board[l]]);
  1227.             else
  1228.               fprintf (D, " %c", pxx[board[l]]);
  1229.           }
  1230.         fprintf (D, "\n");
  1231.           }
  1232.         fprintf (D, "\n");
  1233.         fclose (D);
  1234.       }
  1235. #endif
  1236. #if !defined CHESSTOOL
  1237. #ifndef AMIGA
  1238.     if (flag.post)
  1239.       GiveHint ();
  1240. #endif
  1241. #endif
  1242.         verifyquiet = 1;
  1243.     if (VerifyMove (s, 1, &mv))
  1244.       {
  1245.         Sdepth = 0;
  1246. #ifdef QUIETBACKGROUND
  1247. #ifdef NONDSP
  1248.         PromptForMove ();
  1249. #else
  1250.         ShowSidetoMove ();
  1251.         ShowPrompt ();
  1252. #endif
  1253.         have_shown_prompt = true;
  1254. #endif /* QUIETBACKGROUND */
  1255.             backsrchaborted = 0;
  1256.         SelectMove (computer, 2);
  1257. #ifdef OLD_LOOKAHEAD
  1258.         VerifyMove (s, 2, &mv);
  1259.         Sdepth = 0;
  1260. #else // faster lookahead on predict
  1261.         VerifyMove (s, 2, &mv);
  1262. //sprintf(tstr,"sd = %d",Sdepth);
  1263. //ShowMessage(tstr);
  1264.         if ((Sdepth > 0)&&(backsrchaborted))
  1265.               Sdepth--;
  1266. #endif
  1267.       }
  1268.          verifyquiet = 0;
  1269.     /*ft = (time ((long *) 0) - time0) * 100;*/
  1270.     epsquare = tmp;
  1271.     time0 = ft;
  1272.       }
  1273.   while (!(ok || flag.quit))
  1274.     {
  1275.       need_to_zero = 0;
  1276. #if defined CHESSTOOL
  1277.       normal = false;
  1278. #endif
  1279.       player = opponent;
  1280. #ifdef QUIETBACKGROUND
  1281.       if (!have_shown_prompt)
  1282.     {
  1283. #endif /* QUIETBACKGROUND */
  1284. #ifdef NONDSP
  1285.       PromptForMove ();
  1286. #else
  1287.       ShowSidetoMove ();
  1288.       ShowPrompt ();
  1289. #endif
  1290. #ifdef QUIETBACKGROUND
  1291.     }
  1292.       have_shown_prompt = false;
  1293. #endif /* QUIETBACKGROUND */
  1294. #ifdef NONDSP
  1295.       s[0] = sx[0] = '\0';
  1296. #ifndef AMIGA
  1297.       while (!sx[0])
  1298.     i = (int) gets (sx);
  1299. #else
  1300.        thinking2 = 0;
  1301.        i = 1;
  1302.        thinkahead = 0;
  1303.        GetOperatorEntry(sx);
  1304. #endif
  1305. #else
  1306.      /* fflush (stdout);
  1307.       i = (int) getstr (sx);*/
  1308. #endif
  1309.       sscanf (sx, "%s", s);
  1310.       if (i == EOF)
  1311.     ExitChess ();
  1312.       if (s[0] == '\0')
  1313.     continue;
  1314.       if (strcmp (s, CP[131]) == 0)    /*bd*/
  1315.     {
  1316. #if defined CHESSTOOL || defined XBOARD
  1317.       chesstool = 0;
  1318. #endif /* CHESSTOOL */
  1319.       ClrScreen ();
  1320.       UpdateDisplay (0, 0, 1, 0);
  1321. #if defined CHESSTOOL || defined XBOARD
  1322.       chesstool = 1;
  1323. #endif /* CHESSTOOL */
  1324.     }
  1325.       else if (strcmp (s, CP[129]) == 0) /* noop */ ;    /*alg*/
  1326.       else if ((strcmp (s, CP[180]) == 0) || (strcmp (s, CP[216]) == 0))    /* quit exit*/
  1327.     flag.quit = true;
  1328.       else if (strcmp (s, CP[178]) == 0)    /*post*/
  1329.     {
  1330.      /* flag.post = !flag.post;*/
  1331.     }
  1332.       else if ((strcmp (s, CP[191]) == 0) || (strcmp (s, CP[154]) == 0))    /*set edit*/
  1333.     EditBoard ();
  1334. #ifdef NONDSP
  1335.       else if (strcmp (s, CP[190]) == 0)    /*setup*/
  1336.     SetupBoard ();
  1337. #endif
  1338.       else if (strcmp (s, CP[156]) == 0)    /*first*/
  1339.     {
  1340. #if defined CHESSTOOL
  1341.       computer = white;
  1342.       opponent = black;
  1343.       flag.force = false;
  1344.       Sdepth = 0;
  1345. #endif /* CHESSTOOL */
  1346.       ok = true;
  1347.     }
  1348.       else if (strcmp (s, CP[162]) == 0)    /*go*/
  1349.     {
  1350.       ok = true;
  1351.       flag.force = false;
  1352.       if (computer == white)
  1353.         {
  1354.           computer = black;
  1355.           opponent = white;
  1356.         }
  1357.       else
  1358.         {
  1359.           computer = white;
  1360.           opponent = black;
  1361.         }
  1362.     }
  1363.       else if (strcmp (s, CP[166]) == 0)    /*help*/
  1364.     help ();
  1365.       else if (strcmp (s, CP[221]) == 0)    /*material*/
  1366.     flag.material = !flag.material;
  1367.       else if (strcmp (s, CP[157]) == 0)    /*force*/
  1368.     {flag.force = !flag.force; flag.bothsides = false;}
  1369.       else if (strcmp (s, CP[134]) == 0)    /*book*/
  1370.     Book = Book ? 0 : BOOKFAIL;
  1371.       else if (strcmp (s, CP[172]) == 0)    /*new*/
  1372.     {
  1373.       NewGame ();
  1374.       UpdateDisplay (0, 0, 1, 0);
  1375.     }
  1376.       else if (strcmp (s, CP[171]) == 0)    /*list*/
  1377.     ListGame (0xff);
  1378.       else if (strcmp (s, CP[169]) == 0 || strcmp (s, CP[217]) == 0)    /*level clock*/
  1379.        {
  1380.         GetTimeString(tstr);
  1381.     SelectLevel (tstr);
  1382.        }
  1383.       else if (strcmp (s, CP[165]) == 0)    /*hash*/
  1384.     flag.hash = !flag.hash;
  1385.       else if (strcmp (s, CP[132]) == 0)    /*beep*/
  1386.     flag.beep = !flag.beep;
  1387.       else if (strcmp (s, CP[197]) == 0)    /*time*/
  1388.     {
  1389.       SetMachineTime (sx);
  1390.     }
  1391.       else if (strcmp (s, CP[33]) == 0)    /*Awindow*/
  1392.     ChangeAlphaWindow ();
  1393.       else if (strcmp (s, CP[39]) == 0)    /*Bwindow*/
  1394.     ChangeBetaWindow ();
  1395.       else if (strcmp (s, CP[183]) == 0)    /*rcptr*/
  1396.     flag.rcptr = !flag.rcptr;
  1397.       else if (strcmp (s, CP[168]) == 0)    /*hint*/
  1398.        {
  1399.     GiveHint ();
  1400.        }
  1401.       else if (strcmp (s, CP[135]) == 0)    /*both*/
  1402.     {
  1403.       flag.bothsides = !flag.bothsides;
  1404.           if (flag.bothsides)
  1405.            {
  1406.             (void)SetTaskPri((struct Task *)myproc,0);
  1407.             thinkahead = 1;
  1408.            }
  1409.           flag.force = false;
  1410.       Sdepth = 0;
  1411.       ElapsedTime (1);
  1412.       SelectMove (opponent, 1);
  1413.       ok = true;
  1414.     }
  1415.       else if (strcmp (s, CP[185]) == 0)    /*reverse*/
  1416.     {
  1417. #ifndef AMIGA
  1418.       flag.reverse = !flag.reverse;
  1419.       ClrScreen ();
  1420.       UpdateDisplay (0, 0, 1, 0);
  1421. #endif
  1422.     }
  1423.       else if (strcmp (s, CP[195]) == 0)    /*switch*/
  1424.     {
  1425.       computer = computer ^ 1;
  1426.       opponent = opponent ^ 1;
  1427.       xwndw = (computer == white) ? WXWNDW : BXWNDW;
  1428.       flag.force = false;
  1429.       Sdepth = 0;
  1430.       if ((!GameCnt) && (Book) && (flag.regularstart))
  1431.        GetOpenings(computer);
  1432.           else
  1433.            Book = 0;
  1434.       ok = true;
  1435.     }
  1436.       else if (strcmp (s, CP[203]) == 0)    /*white*/
  1437.     {
  1438.       computer = black;
  1439.       opponent = white;
  1440.       xwndw = WXWNDW;
  1441.       flag.force = false;
  1442.       Sdepth = 0;
  1443.  
  1444.       /*
  1445.            * ok = true; don't automatically start with white command
  1446.            */
  1447.     }
  1448.       else if (strcmp (s, CP[133]) == 0)    /*black*/
  1449.     {
  1450.       computer = white;
  1451.       opponent = black;
  1452.       xwndw = BXWNDW;
  1453.       flag.force = false;
  1454.       Sdepth = 0;
  1455.  
  1456.       /*
  1457.            * ok = true; don't automatically start with black command
  1458.            */
  1459.     }
  1460.       else if (strcmp (s, CP[201]) == 0 && GameCnt > 0)    /*undo*/
  1461.     {
  1462. #ifndef AMIGA
  1463.       Undo ();
  1464. #endif
  1465.     }
  1466.       else if (strcmp (s, CP[184]) == 0 && GameCnt > 1)    /*remove*/
  1467.     {
  1468. #ifndef AMIGA
  1469.       Undo ();
  1470.       Undo ();
  1471. #endif
  1472.     }
  1473.       else if (strcmp (s, CP[160]) == 0)    /*get*/
  1474.     GetGame ();
  1475.       else if (strcmp (s, CP[207]) == 0)    /*xget*/
  1476.     GetXGame ();
  1477.       else if (strcmp (s, CP[189]) == 0)    /*save*/
  1478.     SaveGame ();
  1479.       else if (strcmp (s, CP[151]) == 0)    /*depth*/
  1480.     ChangeSearchDepth ();
  1481. #ifdef DEBUG
  1482.       else if (strcmp (s, CP[147]) == 0)    /*debuglevel*/
  1483.     ChangeDbLev ();
  1484. #endif /* DEBUG */
  1485.       else if (strcmp (s, CP[164]) == 0)    /*hashdepth*/
  1486.     ChangeHashDepth ();
  1487.       else if (strcmp (s, CP[182]) == 0)    /*random*/
  1488.     dither = DITHER;
  1489.       else if (strcmp (s, CP[152]) == 0)    /*easy*/
  1490.        {
  1491.     /*flag.easy = !flag.easy;*/
  1492.     flag.easy = flag.easy; /* mod this for menu toggle on amiga */
  1493.        }
  1494.       else if (strcmp (s, CP[143]) == 0)    /*contempt*/
  1495.     SetContempt ();
  1496.       else if (strcmp (s, CP[209]) == 0)    /*xwndw*/
  1497.     ChangeXwindow ();
  1498.       else if (strcmp (s, CP[186]) == 0)    /*rv*/
  1499.     {
  1500.       flag.rv = !flag.rv;
  1501.       UpdateDisplay (0, 0, 1, 0);
  1502.     }
  1503.       else if (strcmp (s, CP[145]) == 0)    /*coords*/
  1504.     {
  1505.       flag.coords = !flag.coords;
  1506.       UpdateDisplay (0, 0, 1, 0);
  1507.     }
  1508.       else if (strcmp (s, CP[193]) == 0)    /*stras*/
  1509.     {
  1510.       flag.stars = !flag.stars;
  1511.       UpdateDisplay (0, 0, 1, 0);
  1512.     }
  1513.       else if (strcmp (s, CP[196]) == 0)    /*test*/
  1514.     {
  1515. /*      ShowMessage (CP[108]);/*test movelist*/
  1516.           ShowMessage ("Testing..");
  1517.           func_num = 0;
  1518.       TestSpeed (MoveList, 20000);
  1519. /*      ShowMessage (CP[107]);/*test capturelist*/
  1520.           func_num++;
  1521.       TestSpeed (CaptureList, 30000);
  1522. /*      ShowMessage (CP[85]);/*test score position*/
  1523.           func_num++;
  1524.       TestPSpeed (ScorePosition, 15000);
  1525.     }
  1526.       else
  1527.       if (strcmp (s, CP[179]) == 0)    /*p*/
  1528.     ShowPostnValues ();
  1529.       else if (strcmp (s, CP[148]) == 0)    /*debug*/
  1530.     DoDebug ();
  1531.     else if (strcmp (s, "Mwpawn") == 0)        /*debug*/
  1532.         DoTable (Mwpawn);
  1533.     else if (strcmp (s, "Mbpawn") == 0)        /*debug*/
  1534.         DoTable (Mbpawn);
  1535.     else if (strcmp (s, "Mwknight") == 0)        /*debug*/
  1536.         DoTable (Mknight[white]);
  1537.     else if (strcmp (s, "Mbknight") == 0)        /*debug*/
  1538.         DoTable (Mknight[black]);
  1539.     else if (strcmp (s, "Mwbishop") == 0)        /*debug*/
  1540.         DoTable (Mbishop[white]);
  1541.     else if (strcmp (s, "Mbbishop") == 0)        /*debug*/
  1542.         DoTable (Mbishop[black]);
  1543.       else
  1544.     { /* this is where we move the humans pieces */
  1545. #if defined CHESSTOOL
  1546.       normal = (ok = VerifyMove (s, 0, &mv));
  1547. #else
  1548.       ok = VerifyMove (s, 0, &mv);
  1549. #endif
  1550. #ifdef OLDVERSION1_01
  1551.       if ((ok && mv != hint))
  1552.         {
  1553.           Sdepth = 0;
  1554.           ft = 0;
  1555.         }
  1556.       else
  1557.         Sdepth = 0;
  1558. #else
  1559. #ifndef OLD_LOOKAHEAD
  1560.           if ((ok)&&((mv != hint)||(flag.easy)||(Sdepth2<3)))
  1561.            {
  1562.             Sdepth = 0;
  1563.             need_to_zero = 1;
  1564.            }
  1565. #endif
  1566.           if (ok)
  1567.            {
  1568.         if ((!flag.easy)&&(mv == hint)&&(Sdepth2>2))
  1569.              {
  1570.               ThinkAheadDepth = Sdepth2;
  1571.               if (ThinkInARow > 4) /* if it got to 4, we would have skipped one */
  1572.                ThinkInARow = 0;
  1573.               ThinkAheadWorked = 1;
  1574.              }
  1575.             else
  1576.              { 
  1577.              ThinkInARow = 0;
  1578.               ThinkAheadDepth = 0;
  1579.               ThinkAheadWorked = 0;
  1580.              }
  1581.            } // ok
  1582. #endif
  1583.           if ((!ok))
  1584.            {
  1585.             IllegalMove = 1;
  1586.            }
  1587.           if ((ok)&&(MouseDropped))
  1588.            {
  1589.                 DoLegalMove(s);
  1590.                 MouseDropped = 0;
  1591.            }
  1592.           if (need_to_zero)
  1593.            {
  1594.             need_to_zero = 0;
  1595.             ZeroTTable();
  1596.            }
  1597.           if (!ok)
  1598.            MouseDropped = 0;
  1599.     }
  1600.     }
  1601.  
  1602.   ElapsedTime (1);
  1603.   if (flag.force)
  1604.     {
  1605.       computer = opponent;
  1606.       opponent = computer ^ 1;
  1607.     }
  1608. #if defined CHESSTOOL || defined XBOARD
  1609. #if defined CHESSTOOL
  1610.   if (normal)
  1611.     if (computer == white)
  1612.       printz ("%d. %s", ++mycnt2, s);
  1613.     else
  1614.       printz ("%d. ... %s", ++mycnt2, s);
  1615. #else
  1616.   printz ("%d. %s\n", ++mycnt2, s);
  1617. #endif
  1618. #ifdef notdef
  1619.   if (flag.post)
  1620.     {
  1621.       REG int i;
  1622.  
  1623.       printz (" %6d ", MSCORE);
  1624.       for (i = 1; MV[i] > 0; i++)
  1625.     {
  1626.       algbr ((INTSIZE) (MV[i] >> 8), (INTSIZE) (MV[i] & 0xFF), false);
  1627.       printz ("%5s ", mvstr[0]);
  1628.     }
  1629.     }
  1630.   printz ("\n");
  1631. #endif
  1632. #endif /* CHESSTOOL */
  1633.   signal (SIGINT, TerminateSearch);
  1634. #ifndef MSDOS
  1635.   signal (SIGQUIT, TerminateSearch);
  1636. #endif /* MSDOS */
  1637. }
  1638.  
  1639.  
  1640. void
  1641. ElapsedTime (INTSIZE int iop)
  1642.  
  1643.  
  1644. /*
  1645.  * Determine the time that has passed since the search was started. If the
  1646.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1647.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1648.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1649.  */
  1650.  
  1651. {
  1652. #ifndef MSDOS
  1653.   extern int errno;
  1654. #ifdef AMIGA
  1655.    struct IntuiMessage *localmessage;
  1656.    long __aligned class,code;
  1657. #endif
  1658. #ifndef AMIGA
  1659. #ifdef FIONREAD
  1660.   if (i = ioctl ((int) 0, FIONREAD, &nchar))
  1661.     {
  1662.       perror ("FIONREAD");
  1663.       fprintf (stderr,
  1664.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  1665.     i, errno, FIONREAD);
  1666.       exit (1);
  1667.     }
  1668.  
  1669.   if (nchar)
  1670.     {
  1671.       if (!flag.timeout)
  1672.     flag.back = true;
  1673.       flag.bothsides = false;
  1674.     }
  1675. #endif /*FIONREAD*/
  1676. #endif
  1677. #ifdef AMIGA /* check if I need to interrupt thinking */
  1678.   if (thinkahead)
  1679.   {
  1680.   while ( (localmessage = (struct IntuiMessage *)
  1681.     GetMsg(wG->UserPort) )) /* got a message at window port */
  1682.    {
  1683.         if (localmessage->Class == RAWKEY)
  1684.          {
  1685.           if (localmessage->Code < 56)
  1686.            code = cookedchar[localmessage->Code];
  1687.           else
  1688.            code = 0;
  1689.          }
  1690.         else
  1691.          {
  1692.           code = 'A';
  1693.          }
  1694.         if (isalpha(code))
  1695.          {
  1696.           (void)SetTaskPri((struct Task *)myproc,procpri);
  1697.           if (!TCflag)
  1698.            backsrchaborted = 1;
  1699.           if (!flag.timeout)
  1700.            {
  1701.             flag.back = true;
  1702.            }
  1703.           flag.bothsides = false;
  1704.       globalmessage_valid = 0xffff;
  1705.           globalmessage = *localmessage;
  1706.          }
  1707.     ReplyMsg((struct Message *)localmessage);
  1708.    }
  1709.   }
  1710.   else if (thinking2) /* check for move now menu item */
  1711.   {
  1712.   while ( (localmessage = (struct IntuiMessage *)
  1713.     GetMsg(wG->UserPort) )) /* got a message at window port */
  1714.    {
  1715.     class = localmessage->Class;
  1716.         code = localmessage->Code;
  1717.     ReplyMsg((struct Message *)localmessage);
  1718.         if ((class == MENUPICK))
  1719.           {
  1720.         if (ItemAddress(&Menu1,code) == &MenuItem6)
  1721.             {
  1722.              if (!flag.timeout)
  1723.               {
  1724.                flag.back = true;
  1725.               flag.musttimeout = true;
  1726.               }
  1727.              flag.bothsides = false;
  1728.             }
  1729.           }
  1730.    }
  1731.   }
  1732. #endif
  1733. #else
  1734.   if (kbhit ())
  1735.     {
  1736.       if (!flag.timeout)
  1737.     flag.back = true;
  1738.       flag.bothsides = false;
  1739.     }
  1740. #endif /* MSDOS */
  1741.   et = (time ((long *) 0) - time0) * 100;
  1742.   ETnodes = NodeCnt + ZNODES;
  1743.   if (et < 0)
  1744.     et = 0;
  1745.   if (iop == 1)
  1746.     {
  1747.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1748.     flag.timeout = true;
  1749.       ETnodes = NodeCnt + ZNODES;
  1750.       time0 = time ((long *) 0);
  1751.     }
  1752. #ifdef AMIGA
  1753.     UpdateClocks ();
  1754. //sprintf(tstr,"%d",et);
  1755. //ShowMessage(tstr);
  1756. #endif
  1757. }
  1758.  
  1759.  
  1760. void
  1761. SetTimeControl (void)
  1762. {
  1763.  int tmp;
  1764.   if (TCflag)
  1765.     {
  1766.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  1767.       TimeControl.clock[white] = 6000L * TCminutes + TCseconds * 100;
  1768.       TimeControl.clock[black] = 6000L * TCminutes + TCseconds * 100;
  1769.       tmp = (TCminutes*60+TCseconds)/TCmoves;
  1770.       if (tmp < 10)
  1771.        {
  1772.         GlobalTgtDepth = 2;
  1773.        }
  1774.       else if (tmp < 180)
  1775.        {
  1776.         GlobalTgtDepth = 3;
  1777.        }
  1778.       else
  1779.        GlobalTgtDepth = 4;
  1780.     }
  1781.   else
  1782.     {
  1783.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  1784.       TimeControl.clock[white] = TimeControl.clock[black] = 0;
  1785.     }
  1786.   flag.onemove = (TCmoves == 1);
  1787.   et = 0;
  1788.   ElapsedTime (1);
  1789. }
  1790.