home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuch40.zip / gnuchess-4.0.pl79 / src / main.c < prev    next >
C/C++ Source or Header  |  1998-09-28  |  11KB  |  446 lines

  1. /*
  2.  * main.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1985-1996 Stuart Cracraft, John Stanback,
  5.  *                         Daryl Baker, Conor McCarthy,
  6.  *                         Mike McGann, Chua Kong Sian
  7.  * Copyright (c) 1985-1996 Free Software Foundation
  8.  *
  9.  * This file is part of GNU CHESS.
  10.  *
  11.  * GNU Chess is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2, or (at your option)
  14.  * any later version.
  15.  *
  16.  * GNU Chess is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with GNU Chess; see the file COPYING.  If not, write to
  23.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. #include "gnuchess.h"
  27. #include "version.h"
  28. #include "ttable.h"
  29. #include <signal.h>
  30. #if defined Think_C && defined Window_Events
  31. #include <console.h>
  32. #endif
  33. CHAR *ColorStr[2];
  34. CHAR *CP[CPSIZE];
  35.  
  36. #ifdef BINBOOK
  37. extern CHAR *binbookfile;
  38. #endif
  39. extern CHAR *bookfile;
  40. UTSHORT ETABLE = DEFETABLE;
  41. CHAR savefile[128] = "";
  42. CHAR listfile[128] = "";
  43. #ifdef HISTORY
  44. UTSHORT history[32768];
  45. #endif
  46. tshort svalue[64];
  47. struct leaf Tree[TREE], *root;
  48. SHORT TrPnt[MAXDEPTH+1];
  49. SHORT PieceList[2][64], PawnCnt[2][8];
  50. SHORT castld[2], Mvboard[64];
  51. struct flags flag;
  52. SHORT opponent, computer, dither;
  53. tshort  WAwindow, WBwindow, BAwindow, BBwindow;
  54. SHORT  INCscore;
  55. long ResponseTime, ExtraTime, MaxResponseTime, et, et0, time0, ft;
  56. unsigned long GenCnt, NodeCnt, ETnodes, EvalNodes;
  57. long replus, reminus;
  58. tshort HashDepth = HASHDEPTH, HashMoveLimit = HASHMOVELIMIT;
  59. SHORT player;
  60. struct GameRec GameList[MAXMOVES + MAXDEPTH];
  61. SHORT Sdepth, Game50, MaxSearchDepth;
  62. SHORT GameCnt = 0;
  63. SHORT epsquare;
  64. tshort  contempt, xwndw;
  65. int Book;
  66. struct TimeControlRec TimeControl;
  67. int TCadd = 0;
  68. SHORT TCflag, TCmoves, TCminutes, TCseconds, OperatorTime;
  69. SHORT XCmoves[3], XCminutes[3], XCseconds[3],XCadd[3], XC, XCmore;
  70. const SHORT otherside[3] =
  71. {black, white, neutral};
  72. SHORT hint;
  73. SHORT TOflag;        /* force search re-init if we backup search */
  74.  
  75. SHORT mtl[2], pmtl[2], hung[2];
  76. SHORT Pindex[64];
  77. SHORT PieceCnt[2];
  78. SHORT FROMsquare, TOsquare;
  79. SHORT HasKnight[2], HasBishop[2], HasRook[2], HasQueen[2];
  80. SHORT ChkFlag[MAXDEPTH], CptrFlag[MAXDEPTH], PawnThreat[MAXDEPTH];
  81. SHORT QueenCheck[MAXDEPTH]; /* tom@izf.tno.nl */
  82. SHORT NMoves[MAXDEPTH]; /* tom@izf.tno.nl */
  83. SHORT Threat[MAXDEPTH]; /* tom@izf.tno.nl */
  84. SHORT ThreatSave[MAXDEPTH]; /* tom@izf.tno.nl */
  85. SHORT Pscore[MAXDEPTH], Tscore[MAXDEPTH];
  86. const SHORT qrook[3] =
  87. {0, 56, 0};
  88. const SHORT krook[3] =
  89. {7, 63, 0};
  90. const SHORT kingP[3] =
  91. {4, 60, 0};
  92. const SHORT rank7[3] =
  93. {6, 1, 0};
  94. const SHORT sweep[8] =
  95. {false, false, false, true, true, true, false, false};
  96. UTSHORT killr0[MAXDEPTH+1], killr1[MAXDEPTH+1], killr2[MAXDEPTH+1];
  97. UTSHORT PV, SwagHt, Swag0, Swag1, Swag2, Swag4, sidebit;
  98. #ifdef KILLT
  99. tshort killt[0x4000];
  100. #endif
  101. const SHORT value[7] =
  102. {0, valueP, valueN, valueB, valueR, valueQ, valueK};
  103. const SHORT control[7] =
  104. {0, ctlP, ctlN, ctlB, ctlR, ctlQ, ctlK};
  105. SHORT stage, stage2, Developed[2];
  106. unsigned int starttime;
  107. SHORT ahead = true, hash = true;
  108. SHORT PCRASH, PCENTER;
  109.  
  110. #if defined CHESSTOOL || defined XBOARD
  111. void
  112. TerminateChess (int sig)
  113. {
  114.   ExitChess ();
  115. }
  116.  
  117. #endif
  118. int timeopp[MINGAMEIN], timecomp[MINGAMEIN];
  119. int compptr, oppptr;
  120. inline void
  121. TimeCalc ()
  122. {
  123. /* adjust number of moves remaining in gamein games */
  124.   int increment = 0;
  125.   int topsum = 0;
  126.   int tcompsum = 0;
  127.   int me,him;
  128.   int i;
  129. /* dont do anything til you have enough numbers */
  130.   if (GameCnt < (MINGAMEIN * 2)) return;
  131. /* calculate average time in sec for last MINGAMEIN moves */
  132.   for (i = 0; i < MINGAMEIN; i++)
  133.     {
  134.       tcompsum += timecomp[i];
  135.       topsum += timeopp[i];
  136.     }
  137.   topsum /= (100 * MINGAMEIN);
  138.   tcompsum /= (100 * MINGAMEIN);
  139. /* if I have less time than opponent add another move */
  140.     me = TimeControl.clock[computer]/100; 
  141.     him = TimeControl.clock[opponent]/100;
  142.     if(me < him) increment += 2;
  143. /* if I am > 60 sec behind increment or if I am less and in last 2 min */
  144.     if((him - me) > 60 || (me<him && me < 120))increment++;
  145. /* if I am losing more time with each move add another */
  146.   if ( ((me - him) > 60) && tcompsum < topsum) increment--;
  147.   if ( tcompsum > topsum) increment +=2;
  148. /* if I am doing really well use more time per move */
  149.   else if (me > him && tcompsum < topsum) increment = -1;
  150.   if (me > (him+60) ) increment = -1;
  151.   TimeControl.moves[computer] += increment;
  152. /* but dont let moves go below MINMOVES */
  153.   if (TimeControl.moves[computer] < MINMOVES )TimeControl.moves[computer] = MINMOVES;
  154. }
  155.  
  156. /* hmm.... shouldn`t main be moved to the interface routines */
  157. int
  158. main (int argc, CHAR **argv)
  159. {
  160.   CHAR *xwin = 0;
  161.   CHAR *Lang = NULL;
  162. #if defined Think_C && defined Window_Events
  163.     if (StillDown()) /* fake command line by holding mouse btn */
  164.           argc = ccommand(&argv);
  165. #endif
  166.   gsrand (starttime = ((unsigned int) time ((time_t *) 0)));    /* init urand */
  167. #ifdef ttblsz
  168.   ttblsize = ttblsz;
  169.   rehash = -1;
  170. #endif /* ttblsz */
  171.   if (argc > 2)
  172.     {
  173.       if (argv[1][0] == '-' && argv[1][1] == 'L')
  174.     {
  175.       Lang = argv[2];
  176.       argv += 2;
  177.       argc -= 2;
  178.     }
  179.     }
  180.   InitConst (Lang);
  181.   ColorStr[0] = CP[118];
  182.   ColorStr[1] = CP[119];
  183.  
  184.   while (argc > 1 && ((argv[1][0] == '-') || (argv[1][0] == '+')))
  185.     {
  186.       switch (argv[1][1])
  187.     {
  188.     case 'a':
  189.       ahead = ((argv[1][0] == '-') ? false : true);
  190.       break;
  191.     case 'b':
  192.       argv++;
  193.       argc--;
  194.       if (argc > 1)
  195.         {
  196.           bookfile = argv[1];
  197. #ifdef notdef
  198. #ifdef BINBOOK
  199.           binbookfile = NULL;
  200. #endif
  201. #endif
  202.         }
  203.       break;
  204. #ifdef BINBOOK
  205.     case 'B':
  206.       argv++;
  207.       argc--;
  208.       if (argc > 1)
  209.         binbookfile = argv[1];
  210.       break;
  211. #endif
  212. #ifdef CACHE
  213.     case 'C':
  214.         argc--;
  215.                 argv++;
  216.                 if(argc > 1)ETABLE = atoi(argv[1]);
  217.         break;
  218. #endif
  219.         case 'N': /* tom@izf.tno.nl */
  220.           flag.nonull = ((argv[1][0] == '-') ? true : false);
  221.           break;
  222.         case 'V': /* tom@izf.tno.nl */
  223.           flag.verydeep = ((argv[1][0] == '-') ? false : true);
  224.           break;
  225.         case 'D': /* tom@izf.tno.nl */
  226.           flag.deepnull = ((argv[1][0] == '-') ? false : true);
  227.           break;
  228.         case 'p': /* tom@izf.tno.nl */
  229.           flag.pvs = ((argv[1][0] == '-') ? false : true);
  230.           break;
  231.         case 'F': /* tom@izf.tno.nl */
  232.           flag.noscore = ((argv[1][0] == '-') ? false : true);
  233.           break;
  234.         case 'e': /* tom@izf.tno.nl */
  235.           flag.neweval = ((argv[1][0] == '-') ? false : true);
  236.           break;
  237.         case 'E': /* tom@izf.tno.nl */
  238.           flag.threat = ((argv[1][0] == '-') ? false : true);
  239.           break;
  240.     case 'h':
  241.       hash = ((argv[1][0] == '-') ? false : true);
  242.       break;
  243.     case 's':
  244.       argc--;
  245.       argv++;
  246.       if (argc > 1)
  247.         strcpy (savefile, argv[1]);
  248.       break;
  249.     case 'l':
  250.       argc--;
  251.       argv++;
  252.       if (argc > 1)
  253.         strcpy (listfile, argv[1]);
  254.       break;
  255. #ifndef GDBM
  256.     case 'S':
  257.       argc--;
  258.       argv++;
  259.       if(argc > 1)booksize = atoi(argv[1]);
  260.       break;
  261. #endif
  262.     case 'P':
  263.       argc--;
  264.       argv++;
  265.       if(argc > 1)bookmaxply = atoi(argv[1]);
  266.       break;
  267.  
  268. #if ttblsz
  269.     case 'r':
  270.       if (argc > 2)
  271.         rehash = atoi (argv[2]);
  272.       argc--;
  273.       argv++;
  274.       if (rehash > MAXrehash)
  275.         rehash = MAXrehash;
  276.       break;
  277.     case 'T':
  278.       if (argc > 2)
  279.         ttblsize = atoi (argv[2]);
  280.       argc--;
  281.       argv++;
  282.       if ((ttblsize <= MINTTABLE)) ttblsize = (MINTTABLE)+1;
  283.       break;
  284.     case 'H':
  285.       if (argc > 2)
  286.         HashDepth = atoi (argv[2]);
  287.       argc--;
  288.       argv++;
  289.       break;
  290. #ifdef NEWAGE
  291.     case 'A':
  292.       if (argc > 2)
  293.         newage = atoi (argv[2]);
  294.       argc--;
  295.       argv++;
  296.       break;
  297. #endif
  298. #ifdef HASHFILE
  299.     case 't':        /* create or test persistent transposition */
  300.       TestHashFile();
  301.       return 0;
  302.     case 'c':        /* create or test persistent transposition table */
  303.         {
  304.                 long filesz = 0L;
  305.                         filesz = atol (argv[2]);
  306.                 if (filesz < 0)
  307.                         filesz = 0;
  308.                 if (filesz < 24)
  309.                         filesz = (1 << filesz);
  310.                 CreateHashFile(filesz);
  311.                 return (0);
  312.                 }
  313. #endif /* HASHFILE */
  314. #endif /* ttblsz */
  315.     case 'x':
  316.       xwin = &argv[1][2];
  317.       break;
  318.     case 'v':
  319.       fprintf (stderr, CP[102], version, patchlevel);
  320.       exit (1);
  321.     default:
  322.       fprintf (stderr, CP[113]);
  323.       exit (1);
  324.     }
  325.       argv++;
  326.       argc--;
  327.     }
  328.   XC = 0;
  329.   MaxResponseTime = 0;
  330. #if defined CHESSTOOL || defined XBOARD
  331.   signal (SIGTERM, TerminateChess);
  332.   XCmoves[0] = 40;
  333.   XCminutes[0] = 5;
  334.   XCseconds[0] = 0;
  335.   XCadd[0] = 0;
  336.   XC = 1;
  337.   TCflag = true;
  338. #else
  339.   TCflag = false;
  340. #endif
  341.   if (argc == 2)
  342.     {
  343.       CHAR *p;
  344.  
  345.       MaxResponseTime = 100L * strtol (argv[1], &p, 10);
  346.       if (*p == ':')
  347.     MaxResponseTime = 60L * MaxResponseTime +
  348.       100L * strtol (++p, (CHAR **) NULL, 10);
  349.       XCadd[0] = 0;
  350.       XCmoves[0] = 0;
  351.       XCminutes[0] = 0;
  352.       XCseconds[0] = 0;
  353.     }
  354.   if (argc >= 3)
  355.     {
  356.       CHAR *p;
  357.       if (argc > 9)
  358.     {
  359.       printf ("%s\n", CP[220]);
  360.       exit (1);
  361.     }
  362.       XCmoves[0] = atoi (argv[1]);
  363.       XCminutes[0] = (SHORT)strtol (argv[2], &p, 10);
  364.       if (*p == ':')
  365.     XCseconds[0] = (SHORT)strtol (p + 1, (CHAR **) NULL, 10);
  366.       else
  367.     XCseconds[0] = 0;
  368.       XC = 1;
  369.       argc -= 3;
  370.       argv += 3;
  371.       while (argc > 1)
  372.     {
  373.       XCmoves[XC] = atoi (argv[0]);
  374.       XCminutes[XC] = (SHORT)strtol (argv[1], &p, 10);
  375.       if (*p == ':')
  376.         XCseconds[XC] = (SHORT)strtol (p + 1, (CHAR **) NULL, 10);
  377.       else
  378.         XCseconds[XC] = 0;
  379.       if (XCmoves[XC] && (XCminutes[XC] || XCseconds[XC]))
  380.         XC++;
  381.       else
  382.         {
  383.           printf (CP[220]);
  384.           exit (1);
  385.         }
  386.       argc -= 2;
  387.       argv += 2;
  388.     }
  389.       if (argc)
  390.     {
  391.       printf ("%s\n", CP[220]);
  392.       exit (1);
  393.     }
  394.     }
  395. #ifdef Think_C    /* Allocate memory for "huge" static arrays */
  396.   Initialize_mem();
  397. #endif
  398.   Initialize ();
  399.   Initialize_dist ();
  400.   Initialize_moves ();
  401.   NewGame ();
  402.  
  403.   flag.easy = ahead;
  404.   flag.hash = hash;
  405.   if (xwin)
  406.     xwndw = atoi (xwin);
  407.  
  408.   OpenHashFile(); /* Open persistent transposition table */
  409.   while (!(flag.quit))
  410.     {
  411.       oppptr = (oppptr + 1) % MINGAMEIN;
  412.       if (flag.bothsides && !flag.mate)
  413.     SelectMove (opponent, 1);
  414.       else
  415.     InputCommand ();
  416.       if (opponent == black)
  417.     if (flag.gamein || TCadd)
  418.       {
  419.         TimeCalc ();
  420.       }
  421.     else if (TimeControl.moves[opponent] == 0 && TCflag)
  422.       {
  423.         SetTimeControl ();
  424.       }
  425.  
  426.       compptr = (compptr + 1) % MINGAMEIN;
  427.       if (!(flag.quit || flag.mate || flag.force))
  428.     {
  429.       SelectMove (computer, 1);
  430.       if (computer == black)
  431.         if (flag.gamein)
  432.           {
  433.         TimeCalc ();
  434.           }
  435.         else if (TimeControl.moves[computer] == 0 && TCflag)
  436.           {
  437.         SetTimeControl ();
  438.           }
  439.     }
  440.     }
  441.         CloseHashFile();
  442.  
  443.   ExitChess ();
  444.   return (0);
  445. }
  446.