home *** CD-ROM | disk | FTP | other *** search
/ Dream 44 / Amiga_Dream_44.iso / RiscPc / jeux / ArcBoard004.arc / !GNUChessX / src / c / GnuChess < prev    next >
Text File  |  1996-01-10  |  11KB  |  451 lines

  1. /*
  2.  * main.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992-1995 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.  
  24. /* Changed Dec 1995 (SPD) to work-around !EasyC bug */
  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.         
  163. #if defined Think_C && defined Window_Events
  164.     if (StillDown()) /* fake command line by holding mouse btn */
  165.           argc = ccommand(&argv);
  166. #endif
  167.   gsrand (starttime = ((unsigned int) time ((time_t *) 0)));    /* init urand */
  168. #ifdef ttblsz
  169.   ttblsize = ttblsz;
  170.   rehash = -1;
  171. #endif /* ttblsz */
  172.   if (argc > 2)
  173.     {
  174.       if (argv[1][0] == '-' && argv[1][1] == 'L')
  175.     {
  176.       Lang = argv[2];
  177.       argv += 2;
  178.       argc -= 2;
  179.     }
  180.     }
  181.   InitConst (Lang);
  182.   ColorStr[0] = CP[118];
  183.   ColorStr[1] = CP[119];
  184.  
  185.   while (argc > 1 && ((argv[1][0] == '-') || (argv[1][0] == '+')))
  186.     {
  187.       switch (argv[1][1])
  188.     {
  189.     case 'a':
  190.       ahead = ((argv[1][0] == '-') ? false : true);
  191.       break;
  192.     case 'b':
  193.       argv++;
  194.       argc--;
  195.       if (argc > 1)
  196.         {
  197.           bookfile = argv[1];
  198. #ifdef notdef
  199. #ifdef BINBOOK
  200.           binbookfile = NULL;
  201. #endif
  202. #endif
  203.         }
  204.       break;
  205. #ifdef BINBOOK
  206.     case 'B':
  207.       argv++;
  208.       argc--;
  209.       if (argc > 1)
  210.         binbookfile = argv[1];
  211.       break;
  212. #endif
  213. #ifdef CACHE
  214.     case 'C':
  215.         argc--;
  216.                 argv++;
  217.                 if(argc > 1)ETABLE = atoi(argv[1]);
  218.         break;
  219. #endif
  220.         case 'N': /* tom@izf.tno.nl */
  221.           flag.nonull = ((argv[1][0] == '-') ? true : false);
  222.           break;
  223.         case 'V': /* tom@izf.tno.nl */
  224.           flag.verydeep = ((argv[1][0] == '-') ? false : true);
  225.           break;
  226.         case 'D': /* tom@izf.tno.nl */
  227.           flag.deepnull = ((argv[1][0] == '-') ? false : true);
  228.           break;
  229.         case 'p': /* tom@izf.tno.nl */
  230.           flag.pvs = ((argv[1][0] == '-') ? false : true);
  231.           break;
  232.         case 'F': /* tom@izf.tno.nl */
  233.           flag.noscore = ((argv[1][0] == '-') ? false : true);
  234.           break;
  235.         case 'e': /* tom@izf.tno.nl */
  236.           flag.neweval = ((argv[1][0] == '-') ? false : true);
  237.           break;
  238.         case 'E': /* tom@izf.tno.nl */
  239.           flag.threat = ((argv[1][0] == '-') ? false : true);
  240.           break;
  241.     case 'h':
  242.       hash = ((argv[1][0] == '-') ? false : true);
  243.       break;
  244.     case 's':
  245.       argc--;
  246.       argv++;
  247.       if (argc > 1)
  248.         strcpy (savefile, argv[1]);
  249.       break;
  250.     case 'l':
  251.       argc--;
  252.       argv++;
  253.       if (argc > 1)
  254.         strcpy (listfile, argv[1]);
  255.       break;
  256. #ifndef GDBM
  257.     case 'S':
  258.       argc--;
  259.       argv++;
  260.       if(argc > 1)booksize = atoi(argv[1]);
  261.       break;
  262. #endif
  263.     case 'P':
  264.       argc--;
  265.       argv++;
  266.       if(argc > 1)bookmaxply = atoi(argv[1]);
  267.       break;
  268.  
  269. #if ttblsz
  270.     case 'r':
  271.       if (argc > 2)
  272.         rehash = atoi (argv[2]);
  273.       argc--;
  274.       argv++;
  275.       if (rehash > MAXrehash)
  276.         rehash = MAXrehash;
  277.       break;
  278.     case 'T':
  279.       if (argc > 2)
  280.         ttblsize = atoi (argv[2]);
  281.       argc--;
  282.       argv++;
  283.       if ((ttblsize <= MINTTABLE)) ttblsize = (MINTTABLE)+1;
  284.       break;
  285.     case 'H':
  286.       if (argc > 2)
  287.         HashDepth = atoi (argv[2]);
  288.       argc--;
  289.       argv++;
  290.       break;
  291. #ifdef NEWAGE
  292.     case 'A':
  293.       if (argc > 2)
  294.         newage = atoi (argv[2]);
  295.       argc--;
  296.       argv++;
  297.       break;
  298. #endif
  299. #ifdef HASHFILE
  300.     case 't':        /* create or test persistent transposition */
  301.       TestHashFile();
  302.       return 0;
  303.     case 'c':        /* create or test persistent transposition table */
  304.         {
  305.                 long filesz = 0L;
  306.                         filesz = atol (argv[2]);
  307.                 if (filesz < 0)
  308.                         filesz = 0;
  309.                 if (filesz < 24)
  310.                         filesz = (1 << filesz);
  311.                 CreateHashFile(filesz);
  312.                 return (0);
  313.                 }
  314. #endif /* HASHFILE */
  315. #endif /* ttblsz */
  316.     case 'x':
  317.       xwin = &argv[1][2];
  318.       break;
  319.     case 'v':
  320.       fprintf (stderr, CP[102], version, patchlevel);
  321.       exit (1);
  322.     default:
  323.       fprintf (stderr, CP[113]);
  324.       exit (1);
  325.     }
  326.       argv++;
  327.       argc--;
  328.     }
  329.   XC = 0;
  330.   MaxResponseTime = 0;
  331. #if defined CHESSTOOL || defined XBOARD
  332.   signal (SIGTERM, TerminateChess);
  333.   XCmoves[0] = 40;
  334.   XCminutes[0] = 5;
  335.   XCseconds[0] = 0;
  336.   XCadd[0] = 0;
  337.   XC = 1;
  338.   TCflag = true;
  339. #else
  340.   TCflag = false;
  341. #endif
  342.   if (argc == 2)
  343.     {
  344.       CHAR *p;
  345.  
  346.       MaxResponseTime = 100L * strtol (argv[1], &p, 10);
  347.       if (*p == ':')
  348.     MaxResponseTime = 60L * MaxResponseTime +
  349.       100L * strtol (++p, (CHAR **) NULL, 10);
  350.       XCadd[0] = 0;
  351.       XCmoves[0] = 0;
  352.       XCminutes[0] = 0;
  353.       XCseconds[0] = 0;
  354.     }
  355.   if (argc >= 3)
  356.     {
  357.       CHAR *p;
  358.       if (argc > 9)
  359.     {
  360.       printf ("%s\n", CP[220]);
  361.       exit (1);
  362.     }
  363.       XCmoves[0] = atoi (argv[1]);
  364.       XCminutes[0] = (SHORT)strtol (argv[2], &p, 10);
  365.       if (*p == ':')
  366.     XCseconds[0] = (SHORT)strtol (p + 1, (CHAR **) NULL, 10);
  367.       else
  368.     XCseconds[0] = 0;
  369.       XC = 1;
  370.       argc -= 3;
  371.       argv += 3;
  372.       while (argc > 1)
  373.     {
  374.       XCmoves[XC] = atoi (argv[0]);
  375.       XCminutes[XC] = (SHORT)strtol (argv[1], &p, 10);
  376.       if (*p == ':')
  377.         XCseconds[XC] = (SHORT)strtol (p + 1, (CHAR **) NULL, 10);
  378.       else
  379.         XCseconds[XC] = 0;
  380.       if (XCmoves[XC] && (XCminutes[XC] || XCseconds[XC]))
  381.         XC++;
  382.       else
  383.         {
  384.           printf (CP[220]);
  385.           exit (1);
  386.         }
  387.       argc -= 2;
  388.       argv += 2;
  389.     }
  390.       if (argc)
  391.     {
  392.       printf ("%s\n", CP[220]);
  393.       exit (1);
  394.     }
  395.     }
  396. #ifdef Think_C    /* Allocate memory for "huge" static arrays */
  397.   Initialize_mem();
  398. #endif
  399.   Initialize ();
  400.   Initialize_dist ();
  401.   Initialize_moves ();
  402.   NewGame ();
  403.  
  404.   flag.easy = ahead;
  405.   flag.hash = hash;
  406.   if (xwin)
  407.     xwndw = atoi (xwin);
  408.  
  409.   OpenHashFile(); /* Open persistent transposition table */
  410.   while (!(flag.quit))
  411.     {
  412.       oppptr = (oppptr + 1) % MINGAMEIN;
  413.       if (flag.bothsides && !flag.mate)
  414.     SelectMove (opponent, 1);
  415.       else
  416.     InputCommand ();
  417.       if (opponent == black)
  418.     if (flag.gamein || TCadd)
  419.       {
  420.         TimeCalc ();
  421.       }
  422.     else if (TimeControl.moves[opponent] == 0 && TCflag)
  423.       {
  424.         SetTimeControl ();
  425.       }
  426.  
  427.       compptr = (compptr + 1) % MINGAMEIN;
  428. #ifdef __arm__  /* Work-around for !EasyC bug */
  429.       if (!flag.quit && !flag.mate && !flag.force)
  430. #else
  431.       if (!(flag.quit || flag.mate || flag.force))
  432. #endif
  433.     {
  434.       SelectMove (computer, 1);
  435.       if (computer == black)
  436.         if (flag.gamein)
  437.           {
  438.         TimeCalc ();
  439.           }
  440.         else if (TimeControl.moves[computer] == 0 && TCflag)
  441.           {
  442.         SetTimeControl ();
  443.           }
  444.     }
  445.     }
  446.         CloseHashFile();
  447.  
  448.   ExitChess ();
  449.   return (0);
  450. }
  451.