home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 3 / cd.iso / os2 / pmgnuchs / gnuchess.h < prev    next >
Text File  |  1994-04-13  |  20KB  |  601 lines

  1. /*
  2.  * gnuchess.h - Header file 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.  
  24. #include <stdio.h>
  25.  
  26. #define SEEK_SET 0
  27. #define SEEK_END 2
  28.  
  29. #include <ctype.h>
  30. #include <strings.h>
  31. #include <stdlib.h>
  32. #include <time.h>
  33. #include <malloc.h>
  34. #define RWA_ACC "r+b"
  35. #define WA_ACC "w+b"
  36. #define printz printf
  37. #define scanz scanf
  38.  
  39. #ifdef notdef
  40. #if defined(__STDC__) || defined(MSDOS)
  41. /* <stdio.h> */
  42.      extern int fclose (FILE *);
  43. #ifndef __ultrix /* work around bug in c89 compiler --t.mann */
  44.      extern int fscanf (FILE *, const char *, ...);
  45.      extern int fprintf (FILE *, const char *, ...);
  46. #endif /*__ultrix*/
  47.      extern int fflush (FILE *);
  48.  
  49. /* <stdlib.h> */
  50.      extern int abs (int);
  51.      extern int atoi (const char *);
  52.  
  53. /* <time.h> */
  54.      extern long int time (long int *);
  55.  
  56. /* <string.h> */
  57.      extern void *memset (void *, int, size_t);
  58. #endif
  59. #endif
  60.  
  61. /* Piece values */
  62. #define valueP 100
  63. #define valueN 350
  64. #define valueB 355
  65. #define valueR 550
  66. #define valueQ 1100
  67. #define valueK 1200
  68. /* masks into upper 8 bits of ataks array */
  69. #define ctlP 0x4000
  70. #define ctlN 0x2800
  71. #define ctlB 0x1800
  72. #define ctlR 0x0400
  73. #define ctlQ 0x0200
  74. #define ctlK 0x0100
  75. #define ctlBQ 0x1200
  76. #define ctlBN 0x0800
  77. #define ctlRQ 0x0600
  78. #define ctlNN 0x2000
  79. /* attack functions */
  80. #define Patak(c, u) (atak[c][u] > ctlP)
  81. #define Anyatak(c, u) (atak[c][u] > 0)
  82. /* distance function */
  83. #define taxicab(a,b) taxidata[a][b]
  84. /* hashtable flags */
  85. #define truescore 0x0001
  86. #define lowerbound 0x0002
  87. #define upperbound 0x0004
  88. #define kingcastle 0x0008
  89. #define queencastle 0x0010
  90. #define evalflag 0x0020
  91. /* king positions */
  92. #define wking PieceList[white][0]
  93. #define bking PieceList[black][0]
  94. #define EnemyKing PieceList[c2][0]
  95. /* constants */
  96. /* castle moves */
  97. #define BLACKCASTLE    0x3C3E
  98. #define WHITECASTLE    0x0406
  99. #define LONGBLACKCASTLE    0x3C3A
  100. #define LONGWHITECASTLE    0x0402
  101. /* truth values */
  102. #define false 0
  103. #define true 1
  104. /* colors */
  105. #define white 0
  106. #define black 1
  107. #define neutral 2
  108. /* piece code defines */
  109. #define no_piece 0
  110. #define pawn 1
  111. #define knight 2
  112. #define bishop 3
  113. #define rook 4
  114. #define queen 5
  115. #define king 6
  116. #define bpawn 7
  117. /* node flags */
  118. #define pmask 0x0007
  119. #define promote 0x0008
  120. #define cstlmask 0x0010
  121. #define epmask 0x0020
  122. #define exact 0x0040
  123. #define pwnthrt 0x0080
  124. #define check 0x0100
  125. #define capture 0x0200
  126. #define draw 0x0400
  127. #define book 0x1000
  128. /* move symbols */
  129. /*#define pxx (CP[2])
  130. #define qxx (CP[1]) */
  131. #define rxx (CP[4])
  132. #define cxx (CP[3])
  133. /* for everything that can't use the above */
  134. #define Qxx " pnbrqk"
  135. #define Pxx " PNBRQK"
  136. #define Cxx "abcdefgh"
  137. #define Rxx "12345678"
  138. /***************************************************************************/
  139. /***************** Table limits ********************************************/
  140. /*
  141.  * ttblsz can be a any size. Setting ttblsz 0 removes the transposition
  142.  * tables.
  143.  */
  144. #ifdef MSDOS
  145. #define vttblsz (8001)
  146. #else
  147. #define vttblsz (200001)
  148. #endif /* MSDOS */
  149.  
  150. #define ttblsz vttblsz
  151. #define TREE 1500        /* max number of tree entries */
  152. #define MAXDEPTH 35        /* max depth a search can be carried */
  153. #define MINDEPTH 2        /* min search depth =1 (no hint), >1 hint */
  154. #define MINTTABLE (8000)    /* min ttable size -1 */
  155. #define MAXMOVES 400        /* max number of half moves in a game */
  156. #if defined MSDOS
  157. #define BOOKSIZE 10000        /* Number of unique position/move combinations allowed */
  158. #else
  159. #define BOOKSIZE 250000        /* Number of unique position/move combinations allowed */
  160. #endif
  161. #define CPSIZE 235        /* size of lang file max */
  162. #define ETABLE (10001)        /* static eval cache */
  163. /***************** tuning paramaters **********************************************/
  164. #define MINGAMEIN 4
  165. #define MINMOVES 15
  166. #define CHKDEPTH 1        /* always look forward CHKDEPTH half-moves if in check */
  167. #define DEPTHBEYOND 11        /* Max to go beyond Sdepth */
  168. #define HASHDEPTH 4        /* depth above which to use HashFile */
  169. #define HASHMOVELIMIT 40    /* Use HashFile only for this many moves */
  170. #define PTVALUE 0            /* material value below which pawn threats at 5 & 3 are used */
  171. #define ZDEPTH 3        /* depth beyond which to check ZDELTA for extra time */
  172. #define ZDELTA 40        /* score delta per ply to cause extra time to be given */
  173. #define BESTDELTA 90
  174. /* about 1/2 second worth of nodes for your machine */
  175. #define ZNODES 3000        /* check the time every ZNODES positions */
  176. #define MAXTCCOUNTX  19    /* max number of time clicks per search to complete ply*/
  177. #define MAXTCCOUNTR 8        /* max number of time clicks per search extensions*/
  178. #define SDEPTHLIM Sdepth+2
  179. #define HISTORYLIM 4096        /* Max value of history killer */
  180. #define EWNDW 10        /* Eval window to force position scoring at depth greater than Sdepth + 2 */
  181. #define WAWNDW 90        /* alpha window when computer white*/
  182. #define WBWNDW 90        /* beta window when computer white*/
  183. #define BAWNDW 90        /* alpha window when computer black*/
  184. #define BBWNDW 90        /* beta window when computer black*/
  185. #define BXWNDW 90        /* window to force position scoring at lower */
  186. #define WXWNDW 90        /* window to force position scoring at lower */
  187. #ifdef SMALLRANDY
  188. #define DITHER 2        /* max amount random can alter a pos value */
  189. #else
  190. #define DITHER 5
  191. #endif
  192. #define BBONUS 2        /* points per stage value of B increases */
  193. #define RBONUS 6        /* points per stage value of R increases */
  194. #define KINGPOSLIMIT ( -1)    /* King positional scoring limit */
  195. #define KINGSAFETY  40
  196. #define MAXrehash (7)
  197. #define NULLMOVELIM 4000    /* below this total material on board don't use null move */
  198.  
  199. #if defined AG0
  200. #define WHITEAG0
  201. #define BLACKAG0
  202.  
  203. #elif defined AG1
  204. #define WHITEAG1
  205. #define BLACKAG1
  206.  
  207. #elif defined AG2
  208. #define WHITEAG2
  209. #define BLACKAG2
  210.  
  211. #elif defined AG3
  212. #define WHITEAG3
  213. #define BLACKAG3
  214.  
  215. #elif defined AGB
  216. #define WHITEAG2
  217. #define BLACKAG2
  218.  
  219. #elif defined AG4
  220. #define WHITEAG4
  221. #define BLACKAG4
  222. #endif
  223. /************************* parameters for Opening Book *********************************/
  224. #define BOOKFAIL 15        /* if no book move found for BOOKFAIL turns stop using book */
  225. #define BOOKMAXPLY 28        /* Max plys to keep in book database */
  226. #define BOOKPOCKET 64
  227. #define BOOKRAND 1000        /* used to select an opening move from a list */
  228. #define BOOKENDPCT 950        /* 5 % chance a BOOKEND will stop the book */
  229. #define DONTUSE -32768        /* flag move as don't use */
  230. /*************************** Book access defines ****************************************/
  231. #define SIDEMASK 0x1
  232. #define LASTMOVE 0x4000        /* means this is the last move of an opening */
  233. #define BADMOVE 0x8000        /* means this is a bad move in this position */
  234. /****************************************************************************************/
  235.      struct hashval
  236.      {
  237.        unsigned long key, bd;
  238.      };
  239.      struct hashentry
  240.      {
  241.        unsigned long hashbd;
  242.        unsigned char flags, depth;    /* char saves some space */
  243.        short score;
  244.        unsigned short mv;
  245. #ifdef HASHTEST
  246.        unsigned char bd[32];
  247. #endif /* HASHTEST */
  248.  
  249.      };
  250.  
  251. #ifdef HASHFILE
  252.     struct etable
  253.     { unsigned long ehashbd;
  254.         short int escore[2];
  255.         short int sscore[64];
  256.         short int score;
  257.         short int hung[2];
  258.     } ;
  259.  
  260. /*
  261.  * persistent transposition table. The size must be a power of 2. If you
  262.  * change the size, be sure to run gnuchess -c before anything else.
  263.  */
  264. #define frehash 6
  265. #ifdef MSDOS
  266. #define Deffilesz (1 << 11) -1
  267. #else
  268. #define Deffilesz (1 << 17) -1
  269. #endif /* MSDOS */
  270.      struct fileentry
  271.      {
  272.        unsigned char bd[32];
  273.        unsigned char f, t, flags, depth, sh, sl;
  274.      };
  275.  
  276. #endif /* HASHFILE */
  277.  
  278.  
  279.      struct leaf
  280.      {
  281.            short int f;
  282.     short int t;
  283.     short int  score;
  284.     short int  reply;
  285.     short int  width;
  286.            unsigned short flags;
  287.      };
  288.      struct GameRec
  289.      {
  290.        unsigned short gmove;    /* this move */
  291.        short score;        /* score after this move */
  292.        short depth;        /* search depth this move */
  293.        long time;               /* search time this move */
  294.        short piece;        /* piece captured */
  295.        short color;        /* color */
  296.        short flags;        /* move flags capture, promote, castle */
  297.        short Game50;        /* flag for repetition */
  298.        long nodes;        /* nodes searched for this move */
  299.        unsigned long hashkey, hashbd;    /* board key before this move */
  300.        short epssq;        /* epssquare before this move */
  301. #ifdef DEBUG40
  302.        unsigned short int d1[7];
  303. #endif
  304.      };
  305.      struct TimeControlRec
  306.      {
  307.        short moves[2];
  308.        long clock[2];
  309.      };
  310.  
  311.      struct flags
  312.      {
  313.        short mate;        /* the game is over */
  314.        short post;        /* show principle variation */
  315.        short quit;        /* quit/exit */
  316.        short regularstart;    /* did the game start from standard
  317.                  * initial board ? */
  318.        short reverse;        /* reverse board display */
  319.        short bothsides;        /* computer plays both sides */
  320.        short hash;        /* enable/disable transposition table */
  321.        short force;        /* enter moves */
  322.        short easy;        /* disable thinking on opponents time */
  323.        short beep;        /* enable/disable beep */
  324.        short timeout;        /* time to make a move */
  325.        short musttimeout;    /* time to make a move */
  326.        short back;        /* time to make a move */
  327.        short rcptr;        /* enable/disable recapture heuristics */
  328.        short rv;        /* reverse video */
  329.        short stars;        /* add stars to uxdsp screen */
  330.        short coords;        /* add coords to visual screen */
  331.        short shade;
  332.        short material;        /* draw on lack of material */
  333.        short illegal;        /* illegal position */
  334.        short onemove;        /* timing is onemove */
  335.        short gamein;        /* timing is gamein */
  336.      };
  337.  
  338. #ifdef DEBUG
  339.      extern FILE *debugfile;
  340.  
  341. #endif /* DEBUG */
  342. #ifdef HISTORY
  343.      extern unsigned short history[32768];
  344. #endif
  345.      extern char *ColorStr[2];
  346.      extern unsigned short int MV[MAXDEPTH];
  347.      extern int MSCORE;
  348.      extern int mycnt1, mycnt2;
  349.      extern short int ahead;
  350.      extern short int chesstool;
  351.      extern struct leaf Tree[], *root,rootnode;
  352.      extern char savefile[], listfile[];
  353.      extern short TrPnt[];
  354.      extern short board[], color[];
  355.      extern short PieceList[2][64], PawnCnt[2][8];
  356.      extern short castld[], Mvboard[];
  357.      extern short svalue[64];
  358.      extern struct flags flag;
  359.      extern short opponent, computer, INCscore;
  360.      extern short WAwindow, BAwindow, WBwindow, BBwindow;
  361.      extern short dither, player;
  362.      extern short xwndw, epsquare, contempt;
  363.      extern long ResponseTime, ExtraTime, MaxResponseTime, et, et0, time0, ft;
  364.      extern long reminus, replus;
  365.      extern unsigned long GenCnt, NodeCnt, ETnodes, EvalNodes, HashCnt, HashAdd, FHashCnt, FHashAdd,
  366.      HashCol, THashCol, filesz, hashmask, hashbase;
  367.      extern short HashDepth, HashMoveLimit;
  368.      extern struct GameRec GameList[];
  369.      extern short GameCnt, Game50;
  370.      extern short Sdepth, MaxSearchDepth;
  371.      extern int Book;
  372.      extern struct TimeControlRec TimeControl;
  373.      extern int TCadd;
  374.      extern short TCflag, TCmoves, TCminutes, TCseconds, OperatorTime;
  375.      extern int timecomp[MINGAMEIN], timeopp[MINGAMEIN];
  376.      extern int compptr,oppptr;
  377.      extern short XCmore, XCmoves[], XCminutes[], XCseconds[], XC;
  378.      extern const short otherside[];
  379.      extern const short Stboard[];
  380.      extern const short Stcolor[];
  381.      extern unsigned short hint;
  382.      extern short int TOflag;
  383.      extern short stage, stage2, Developed[];
  384.      extern short ChkFlag[], CptrFlag[], PawnThreat[];
  385.      extern short Pscore[], Tscore[];
  386.      extern /*unsigned*/ short rehash;  /* -1 is used as a flag --tpm */
  387.      extern unsigned int ttblsize;
  388.      extern short mtl[], pmtl[], hung[], emtl[];
  389.      extern short Pindex[];
  390.      extern short PieceCnt[];
  391.      extern short FROMsquare, TOsquare;
  392.      extern short HasKnight[], HasBishop[], HasRook[], HasQueen[];
  393.      extern const short qrook[];
  394.      extern const short krook[];
  395.      extern const short kingP[];
  396.      extern const short rank7[];
  397.      extern const short sweep[];
  398.      extern const short epmove1[], epmove2[];
  399.      extern unsigned short killr0[], killr1[];
  400.      extern unsigned short killr2[], killr3[];
  401.      extern unsigned short PV, SwagHt, Swag0, Swag1, Swag2, Swag3, Swag4, sidebit;
  402.      extern short killt[0x4000];
  403.      extern short mtl[2], pmtl[2], hung[2];
  404.      extern const short value[];
  405.      extern const short control[];
  406.      extern unsigned char nextpos[8][64][64];
  407.      extern unsigned char nextdir[8][64][64];
  408.      extern const short ptype[2][8];
  409.      extern FILE *hashfile;
  410.      extern unsigned int starttime;
  411.      extern short distdata[64][64], taxidata[64][64];
  412.      extern union U admin;
  413.      extern short unsigned bookmaxply;
  414.      extern int unsigned bookcount;
  415.      extern int unsigned booksize;
  416.      extern unsigned long hashkey, hashbd;
  417.      extern struct hashval hashcode[2][7][64];
  418.      extern char *CP[];
  419. #ifdef QUIETBACKGROUND
  420.      extern short background;
  421. #endif /* QUIETBACKGROUND */
  422.  
  423. #ifdef ttblsz
  424.      extern struct hashentry *ttable[2];
  425.  
  426. #endif
  427.  
  428. /*
  429.  * hashbd contains a 32 bit "signature" of the board position. hashkey
  430.  * contains a 16 bit code used to address the hash table. When a move is
  431.  * made, XOR'ing the hashcode of moved piece on the from and to squares with
  432.  * the hashbd and hashkey values keeps things current.
  433.  */
  434. #define UpdateHashbd(side, piece, f, t) \
  435. {\
  436.   if ((f) >= 0)\
  437.     {\
  438.       hashbd ^= hashcode[side][piece][f].bd;\
  439.       hashkey ^= hashcode[side][piece][f].key;\
  440.     }\
  441.   if ((t) >= 0)\
  442.     {\
  443.       hashbd ^= hashcode[side][piece][t].bd;\
  444.       hashkey ^= hashcode[side][piece][t].key;\
  445.     }\
  446. }
  447.  
  448.  
  449.      extern short rpthash[2][256];
  450.      extern char *DRAW;
  451.  
  452. #define distance(a,b) distdata[a][b]
  453. #define row(a) ((a) >> 3)
  454. #define column(a) ((a) & 7)
  455. #define locn(a,b) (((a) << 3) | (b))
  456.      extern short distdata[64][64];
  457.  
  458. /* init external functions */
  459.      extern void InitConst (char *lang);
  460.      extern void Initialize_dist (void);
  461.      extern void NewGame (HWND hWnd);
  462.      extern int parse (FILE * fd, short unsigned int *mv, short int side, char *opening);
  463.      extern void GetOpenings (HWND hWnd);
  464.      extern int OpeningBook (unsigned short int *hint, short int side);
  465.      extern void SelectMove ( short int side, short int iop);
  466.      extern int
  467.       search (short int side,
  468.            short int ply,
  469.            short int depth,
  470.            short int alpha,
  471.            short int beta,
  472.            short unsigned int *bstline,
  473.            short int *rpt);
  474. #if ttblsz
  475.      extern int
  476.       ProbeTTable (short int side,
  477.             short int depth,
  478.             short int ply,
  479.             short int *alpha,
  480.             short int *beta,
  481.             short int *score);
  482.      extern int
  483.       PutInTTable (short int side,
  484.             short int score,
  485.             short int depth,
  486.             short int ply,
  487.             short int alpha,
  488.             short int beta,
  489.             short unsigned int mv);
  490.      extern void ZeroTTable (void);
  491.      extern void ZeroRPT (void);
  492.      extern void Initialize_ttable (void);
  493.      extern unsigned int urand (void);
  494. #ifdef HASHFILE
  495.      extern void gsrand (unsigned int);
  496.      extern int
  497.       ProbeFTable (short int side,
  498.             short int depth,
  499.             short int ply,
  500.             short int *alpha,
  501.             short int *beta,
  502.             short int *score);
  503.      extern void
  504.       PutInFTable (short int side,
  505.             short int score,
  506.             short int depth,
  507.             short int ply,
  508.             short int alpha,
  509.             short int beta,
  510.             short unsigned int f,
  511.             short unsigned int t);
  512.  
  513. #endif /* HASHFILE */
  514. #endif /* ttblsz */
  515.      extern void Initialize_moves (void);
  516.      extern void MoveList (short int side, short int ply);
  517.      extern void CaptureList (short int side, short int ply);
  518.      extern int castle (short int side, short int kf, short int kt, short int iop);
  519.      extern void ataks (short int side, short int *a);
  520.      extern void
  521.       MakeMove (short int side,
  522.          struct leaf * node,
  523.          short int *tempb,
  524.          short int *tempc,
  525.          short int *tempsf,
  526.          short int *tempst,
  527.          short int *INCscore);
  528.      extern void
  529.       UnmakeMove (short int side,
  530.            struct leaf * node,
  531.            short int *tempb,
  532.            short int *tempc,
  533.            short int *tempsf,
  534.            short int *tempst);
  535.      extern void InitializeStats (void);
  536.      extern int
  537.       evaluate (short int side,
  538.          short int ply,
  539.          short int alpha,
  540.          short int beta,
  541.          short int INCscore,
  542.          short int *InChk);
  543.      extern short int ScorePosition (short int side);
  544.      extern void ExaminePosition (void);
  545.      extern void UpdateWeights (void);
  546.      extern void Initialize (void);
  547.      extern void InputCommand (void);
  548.      extern void ExitChess (void);
  549.      extern void ClrScreen (void);
  550.      extern void SetTimeControl (void);
  551.      extern void SelectLevel (void);
  552.      extern void
  553.       UpdateDisplay (HWND hWnd, short int f,
  554.               short int t,
  555.               short int flag,
  556.               short int iscastle);
  557.      extern void ElapsedTime (short int iop);
  558.      extern void ShowSidetoMove (void);
  559.      extern void SearchStartStuff (short int side);
  560.      extern void ShowDepth (char ch);
  561.      extern void TerminateSearch (int);
  562.      extern void
  563.       ShowResults (short int score,
  564.             short unsigned int *bstline,
  565.             char ch);
  566.      extern void PromptForMove (void);
  567.      extern void SetupBoard (void);
  568.      extern void algbr (short int f, short int t, short int flag);
  569.      extern void OutputMove (HWND hWnd);
  570.      extern void ShowCurrentMove (short int pnt, short int f, short int t);
  571.      extern void ListGame (void);
  572.      extern void ShowMessage (HWND hWnd, PSZ pszText);
  573.      extern void ClrScreen (void);
  574.      extern void gotoXY (short int x, short int y);
  575.      extern void ClrEoln (void);
  576.      extern void DrawPiece (HWND hWnd, short int sq);
  577.      extern void UpdateClocks (void);
  578.      extern void DoDebug (void);
  579.      extern void DoTable (short table[64]);
  580.      extern void ShowPostnValues (void);
  581.      extern void ChangeXwindow (void);
  582.      extern void SetContempt (void);
  583.      extern void ChangeHashDepth (void);
  584.      extern void ChangeBetaWindow (void);
  585.      extern void ChangeAlphaWindow (void);
  586.      extern void GiveHint (HWND hWnd);
  587.      extern void ShowPrompt (void);
  588.      extern void EditBoard (void);
  589.      extern void help (void);
  590.      extern void ChangeSearchDepth (void);
  591.      extern void skip (void);
  592.      extern void skipb (void);
  593.      extern void EnPassant (short int xside, short int f, short int t, short int iop);
  594.      extern void ShowNodeCnt (long int NodeCnt, LONG evrate);
  595.      extern void ShowLine (short unsigned int *bstline);
  596.      extern int pick (short int p1, short int p2);
  597.      extern int VerifyMove (HWND hWnd, char *s, short int inp, unsigned short *mv);
  598.      extern void AgeTT();
  599.      extern unsigned short TTage;
  600.      extern struct gdxadmin B;
  601.