1
/* GNU Chess 5.0 - main.c - entry point
2
Copyright (c) 1999-2002 Free Software Foundation, Inc.
4
GNU Chess is based on the two research programs
5
Cobalt by Chua Kong-Sian and Gazebo by Stuart Cracraft.
7
GNU Chess is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2, or (at your option)
12
GNU Chess is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with GNU Chess; see the file COPYING. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
Boston, MA 02111-1307, USA.
24
cracraft@ai.mit.edu, cracraft@stanfordalumni.org, cracraft@earthlink.net
38
short distance[64][64];
39
short taxicab[64][64];
40
unsigned char lzArray[65536];
41
BitBoard DistMap[64][8];
42
BitBoard BitPosArray[64];
43
BitBoard NotBitPosArray[64];
44
BitBoard MoveArray[8][64];
46
BitBoard FromToRay[64][64];
50
BitBoard PassedPawnMask[2][64];
51
BitBoard IsolaniMask[8];
52
BitBoard SquarePawnMask[2][64];
53
BitBoard Rook00Atak[64][256];
54
BitBoard Rook90Atak[64][256];
55
BitBoard Bishop45Atak[64][256];
56
BitBoard Bishop315Atak[64][256];
60
BitBoard stonewall[2];
62
BitBoard mask_kr_trapped_w[3];
63
BitBoard mask_kr_trapped_b[3];
64
BitBoard mask_qr_trapped_w[3];
65
BitBoard mask_qr_trapped_b[3];
66
BitBoard boardhalf[2];
67
BitBoard boardside[2];
68
short directions[64][64];
69
unsigned char BitCount[65536];
70
leaf Tree[MAXTREEDEPTH];
71
leaf *TreePtr[MAXPLYDEPTH];
73
GameRec Game[MAXGAMEDEPTH];
79
unsigned int preanalyze_flags;
83
HashType hashcode[2][7][64];
85
HashType WKCastlehash;
86
HashType WQCastlehash;
87
HashType BKCastlehash;
88
HashType BQCastlehash;
101
unsigned long GenCnt;
102
unsigned long NodeCnt;
103
unsigned long QuiesCnt;
104
unsigned long EvalCnt;
105
unsigned long EvalCall;
106
unsigned long ChkExtCnt;
107
unsigned long OneRepCnt;
108
unsigned long RcpExtCnt;
109
unsigned long PawnExtCnt;
110
unsigned long HorzExtCnt;
111
unsigned long ThrtExtCnt;
112
unsigned long KingExtCnt;
113
unsigned long NullCutCnt;
114
unsigned long FutlCutCnt;
115
unsigned long RazrCutCnt;
116
unsigned long TotalGetHashCnt;
117
unsigned long GoodGetHashCnt;
118
unsigned long TotalPutHashCnt;
119
unsigned long CollHashCnt;
120
unsigned long TotalPawnHashCnt;
121
unsigned long GoodPawnHashCnt;
122
unsigned long RepeatCnt;
124
unsigned long TTHashMask;
125
unsigned long PHashMask;
127
unsigned long history[2][4096];
128
int killer1[MAXPLYDEPTH];
129
int killer2[MAXPLYDEPTH];
130
int ChkCnt[MAXPLYDEPTH];
131
int ThrtCnt[MAXPLYDEPTH];
146
int Hashmv[MAXPLYDEPTH];
152
short pickphase[MAXPLYDEPTH];
153
short InChk[MAXPLYDEPTH];
154
short KingThrt[2][MAXPLYDEPTH];
164
int myrating, opprating, suddendeath, TCionc;
166
int computerplays; /* Side computer is playing */
167
int wasbookmove; /* True if last move was book move */
168
int nmovesfrombook; /* Number of moves since last book move */
169
int newpos, existpos; /* For book statistics */
170
float maxtime; /* Max time for the next searched move */
171
int n; /* Last mobility returned by CTL */
172
int ExchCnt[2]; /* How many exchanges? */
173
int bookloaded = 0; /* Is the book loaded already into memory? */
175
int slider[8] = { 0, 0, 0, 1, 1, 1, 0, 0 };
176
int Value[7] = { 0, ValueP, ValueN, ValueB, ValueR, ValueQ, ValueK};
177
int range[8] = { 0, 0, 0, 1, 1, 1, 0, 0 };
178
int ptype[2] = { pawn, bpawn };
180
{ "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1",
181
"a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
182
"a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
183
"a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
184
"a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
185
"a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
186
"a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
187
"a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8"
189
char algbrfile[9] = "abcdefgh";
190
char algbrrank[9] = "12345678";
192
char notation[8] = { " PNBRQK" };
193
char lnotation[8] = { " pnbrqk" };
196
{ 56, 56, 56, 56, 56, 56, 56, 56,
197
48, 48, 48, 48, 48, 48, 48, 48,
198
40, 40, 40, 40, 40, 40, 40, 40,
199
32, 32, 32, 32, 32, 32, 32, 32,
200
24, 24, 24, 24, 24, 24, 24, 24,
201
16, 16, 16, 16, 16, 16, 16, 16,
202
8, 8, 8, 8, 8, 8, 8, 8,
203
0, 0, 0, 0, 0, 0, 0, 0
207
{ A8, A7, A6, A5, A4, A3, A2, A1,
208
B8, B7, B6, B5, B4, B3, B2, B1,
209
C8, C7, C6, C5, C4, C3, C2, C1,
210
D8, D7, D6, D5, D4, D3, D2, D1,
211
E8, E7, E6, E5, E4, E3, E2, E1,
212
F8, F7, F6, F5, F4, F3, F2, F1,
213
G8, G7, G6, G5, G4, G3, G2, G1,
214
H8, H7, H6, H5, H4, H3, H2, H1 };
217
{ 0, 8, 16, 24, 32, 40, 48, 56,
218
0, 8, 16, 24, 32, 40, 48, 56,
219
0, 8, 16, 24, 32, 40, 48, 56,
220
0, 8, 16, 24, 32, 40, 48, 56,
221
0, 8, 16, 24, 32, 40, 48, 56,
222
0, 8, 16, 24, 32, 40, 48, 56,
223
0, 8, 16, 24, 32, 40, 48, 56,
224
0, 8, 16, 24, 32, 40, 48, 56
228
{ E4, F3, H2, C2, G1, D1, B1, A1,
229
E5, F4, G3, A3, D2, H1, E1, C1,
230
D6, F5, G4, H3, B3, E2, A2, F1,
231
B7, E6, G5, H4, A4, C3, F2, B2,
232
G7, C7, F6, H5, A5, B4, D3, G2,
233
C8, H7, D7, G6, A6, B5, C4, E3,
234
F8, D8, A8, E7, H6, B6, C5, D4,
235
H8, G8, E8, B8, F7, A7, C6, D5 };
238
{ 28, 36, 43, 49, 54, 58, 61, 63,
239
21, 28, 36, 43, 49, 54, 58, 61,
240
15, 21, 28, 36, 43, 49, 54, 58,
241
10, 15, 21, 28, 36, 43, 49, 54,
242
6, 10, 15, 21, 28, 36, 43, 49,
243
3, 6, 10, 15, 21, 28, 36, 43,
244
1, 3, 6, 10, 15, 21, 28, 36,
245
0, 1, 3, 6, 10, 15, 21, 28 };
248
{ 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01,
249
0x7F, 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03,
250
0x3F, 0x7F, 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07,
251
0x1F, 0x3F, 0x7F, 0xFF, 0x7F, 0x3F, 0x1F, 0x0F,
252
0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0x7F, 0x3F, 0x1F,
253
0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0x7F, 0x3F,
254
0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0x7F,
255
0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
258
{ A1, C1, F1, B2, G2, E3, D4, D5,
259
B1, E1, A2, F2, D3, C4, C5, C6,
260
D1, H1, E2, C3, B4, B5, B6, A7,
261
G1, D2, B3, A4, A5, A6, H6, F7,
262
C2, A3, H3, H4, H5, G6, E7, B8,
263
H2, G3, G4, G5, F6, D7, A8, E8,
264
F3, F4, F5, E6, C7, H7, D8, G8,
265
E4, E5, D6, B7, G7, C8, F8, H8 };
268
{ 63, 61, 58, 54, 49, 43, 36, 28,
269
61, 58, 54, 49, 43, 36, 28, 21,
270
58, 54, 49, 43, 36, 28, 21, 15,
271
54, 49, 43, 36, 28, 21, 15, 10,
272
49, 43, 36, 28, 21, 15, 10, 6,
273
43, 36, 28, 21, 15, 10, 6, 3,
274
36, 28, 21, 15, 10, 6, 3, 1,
275
28, 21, 15, 10, 6, 3, 1, 0 };
278
{ 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF,
279
0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0x7F,
280
0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0x7F, 0x3F,
281
0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0x7F, 0x3F, 0x1F,
282
0x1F, 0x3F, 0x7F, 0xFF, 0x7F, 0x3F, 0x1F, 0x0F,
283
0x3F, 0x7F, 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07,
284
0x7F, 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03,
285
0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 };
287
int rank6[2] = { 5, 2 };
288
int rank7[2] = { 6, 1 };
289
int rank8[2] = { 7, 0 };
291
int main (int argc, char *argv[])
296
* Parse command line arguments conforming with getopt_long syntax
297
* Note: we have to support "xboard" and "post" as bare strings
298
* for backward compatibility.
302
int opt_help = 0, opt_version = 0, opt_post = 0, opt_xboard = 0, opt_hash = 0, opt_easy = 0, opt_manual = 0;
305
progname = argv[0]; /* Save in global for cmd_usage */
309
static struct option long_options[] =
311
{"hashsize", 1, 0, 's'},
312
{"version", 0, 0, 'v'},
314
{"xboard", 0, 0, 'x'},
317
{"manual", 0, 0, 'm'},
321
/* getopt_long stores the option index here. */
323
int option_index = 0;
325
c = getopt_long (argc, argv, "ehmpvxs:",
326
long_options, &option_index);
328
/* Detect the end of the options. */
333
* Options with a straight flag, could use getoopt_long
334
* flag setting but this is more "obvious" and easier to
358
if ( optarg == NULL ){ /* we have error such as two -s */
362
errno = 0; /* zero error indicator */
363
opt_hash = strtol (optarg, &endptr, 10);
364
if ( errno != 0 || *endptr != '\0' ){
365
printf("Hashsize out of Range or Invalid\n");
369
case '?': /* On error give help - getopt does a basic message. */
373
puts ("Option Processing Failed\n");
376
} /* end of getopt_long style parsing */
378
/* Initialize random number generator */
379
srand((unsigned int) time(NULL));
381
/* initialize control flags */
384
/* output for thinking */
387
/* Handle old style command line options */
389
for (i = 0; i < argc; i++) {
390
if (strcmp(argv[i],"xboard") == 0) {
392
} else if (strcmp(argv[i],"post") == 0) {
405
/* If the version option was specified we can exit here */
406
if (opt_version == 1)
409
/* If a usage statement is required output it here */
412
return (1); /* Maybe an error if due to bad arguments. */
417
HashSize = 0 ; /* Set HashSize zero */
419
CalcHashSize(opt_hash);
427
for (i = 0; i < argc; i++) {
428
if (strcmp(argv[i],"xboard") == 0) {
430
} else if (strcmp(argv[i],"post") == 0) {
436
bookmode = BOOKPREFER;
439
while (!(flags & QUIT)) {
440
dbg_printf("Waiting for input...\n");
442
dbg_printf("Parsing input...\n");
444
dbg_printf("input_status = %d\n", input_status);
445
if ((flags & THINK) && !(flags & MANUAL) && !(flags & ENDED)) {
446
if (!(flags & XBOARD)) printf("Thinking...\n");
448
CLEAR (flags, THINK);
450
RealGameCnt = GameCnt;
451
RealSide = board.side;
452
dbg_printf("Waking up input...\n");
453
dbg_printf("input_status = %d\n", input_status);
455
dbg_printf("input_status = %d\n", input_status);
456
/* Ponder only after first move */
457
/* Ponder or (if pondering disabled) just wait for input */
458
if ((flags & HARD) && !(flags & QUIT) ) {
465
/* Some cleaning up */