~ubuntu-branches/ubuntu/precise/gcompris/precise

« back to all changes in this revision

Viewing changes to src/chess_computer-activity/gnuchess/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2010-06-27 22:51:30 UTC
  • mfrom: (1.1.16 upstream) (5.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100627225130-mf7h4m5r8m7bd9fb
Tags: 9.3-1
* New upstream release.
* Drop GTK_DISABLE_DEPRECATED patch, useless for now.
* Provide RELEASE_NOTE_9.3.txt downloaded from sourceforge.
* New voice package for Asturian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GNU Chess 5.0 - main.c - entry point
 
2
   Copyright (c) 1999-2002 Free Software Foundation, Inc.
 
3
 
 
4
   GNU Chess is based on the two research programs 
 
5
   Cobalt by Chua Kong-Sian and Gazebo by Stuart Cracraft.
 
6
 
 
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)
 
10
   any later version.
 
11
 
 
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.
 
16
 
 
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.
 
21
 
 
22
   Contact Info: 
 
23
     bug-gnu-chess@gnu.org
 
24
     cracraft@ai.mit.edu, cracraft@stanfordalumni.org, cracraft@earthlink.net
 
25
*/
 
26
 
 
27
#include <config.h>
 
28
#include <time.h>
 
29
#include <string.h>
 
30
#include <stdio.h>
 
31
#include <stdlib.h>
 
32
#include <getopt.h>
 
33
#include <errno.h>                                                              
 
34
 
 
35
#include "common.h"
 
36
#include "book.h"
 
37
 
 
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];
 
45
BitBoard Ray[64][8];
 
46
BitBoard FromToRay[64][64];
 
47
BitBoard RankBit[8];
 
48
BitBoard FileBit[8];
 
49
BitBoard Ataks[2][7];
 
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];
 
57
BitBoard pinned;
 
58
BitBoard rings[4];
 
59
BitBoard boxes[2];
 
60
BitBoard stonewall[2];
 
61
BitBoard pieces[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];
 
72
int RootPV;
 
73
GameRec Game[MAXGAMEDEPTH];
 
74
int GameCnt;
 
75
int RealGameCnt;
 
76
short RealSide;
 
77
int computer;
 
78
unsigned int flags;
 
79
unsigned int preanalyze_flags;
 
80
int cboard[64];
 
81
int Mvboard[64];
 
82
Board board;
 
83
HashType hashcode[2][7][64];
 
84
HashType ephash[64];
 
85
HashType WKCastlehash;
 
86
HashType WQCastlehash;
 
87
HashType BKCastlehash;
 
88
HashType BQCastlehash;
 
89
HashType Sidehash;
 
90
HashType HashKey;
 
91
HashType PawnHashKey;
 
92
HashSlot *HashTab[2];
 
93
PawnSlot *PawnTab[2];
 
94
int Idepth;
 
95
int SxDec;
 
96
int Game50;
 
97
int lazyscore[2];
 
98
int maxposnscore[2];
 
99
int rootscore;
 
100
int lastrootscore;
 
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;
 
123
unsigned HashSize;
 
124
unsigned long TTHashMask;
 
125
unsigned long PHashMask;
 
126
char SANmv[SANSZ];
 
127
unsigned long history[2][4096];
 
128
int killer1[MAXPLYDEPTH];
 
129
int killer2[MAXPLYDEPTH];
 
130
int ChkCnt[MAXPLYDEPTH];
 
131
int ThrtCnt[MAXPLYDEPTH];
 
132
char id[32];
 
133
char solution[64];
 
134
double ElapsedTime;
 
135
Timer StartTime;
 
136
float SearchTime;
 
137
int SearchDepth;
 
138
int MoveLimit[2];
 
139
float TimeLimit[2];
 
140
int TCMove;
 
141
int TCinc;
 
142
float TCTime;
 
143
int castled[2];
 
144
int hunged[2];
 
145
int phase;
 
146
int Hashmv[MAXPLYDEPTH];
 
147
short RootPieces;
 
148
short RootPawns;
 
149
short RootMaterial;
 
150
short RootAlpha;
 
151
short RootBeta;
 
152
short pickphase[MAXPLYDEPTH];
 
153
short InChk[MAXPLYDEPTH];
 
154
short KingThrt[2][MAXPLYDEPTH];
 
155
short threatmv;
 
156
uint8_t threatply;
 
157
short KingSafety[2];
 
158
short pscore[64];
 
159
short bookmode;
 
160
short bookfirstlast;
 
161
 
 
162
char *progname;
 
163
FILE *ofp;
 
164
int myrating, opprating, suddendeath, TCionc;
 
165
char name[50];
 
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? */
 
174
 
 
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 };
 
179
char algbr[64][3] =
 
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" 
 
188
};
 
189
char algbrfile[9] = "abcdefgh";
 
190
char algbrrank[9] = "12345678";
 
191
  
 
192
char notation[8] = { " PNBRQK" };
 
193
char lnotation[8] = { " pnbrqk" };
 
194
 
 
195
short Shift00[64] =
 
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
 
204
};
 
205
 
 
206
int r90[64] =
 
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 };
 
215
 
 
216
short Shift90[64] =
 
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
 
225
};
 
226
 
 
227
int r45[64] =
 
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 };
 
236
 
 
237
short Shift45[64] =
 
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 };
 
246
 
 
247
int Mask45[64] =
 
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 };
 
256
 
 
257
int r315[64] =
 
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 };
 
266
 
 
267
short Shift315[64] =
 
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 };
 
276
 
 
277
int Mask315[64] =
 
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 };
 
286
 
 
287
int rank6[2] = { 5, 2 };
 
288
int rank7[2] = { 6, 1 };
 
289
int rank8[2] = { 7, 0 };
 
290
 
 
291
int main (int argc, char *argv[])
 
292
{
 
293
  int i;
 
294
 
 
295
  /*
 
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.
 
299
   */
 
300
 
 
301
  int c;
 
302
  int opt_help = 0, opt_version = 0, opt_post = 0, opt_xboard = 0, opt_hash = 0, opt_easy = 0, opt_manual = 0;
 
303
  char *endptr;
 
304
 
 
305
  progname = argv[0]; /* Save in global for cmd_usage */
 
306
 
 
307
  while (1)
 
308
  {
 
309
    static struct option long_options[] =
 
310
    {
 
311
        {"hashsize", 1, 0, 's'},
 
312
        {"version", 0, 0, 'v'},
 
313
        {"help", 0, 0, 'h'},
 
314
        {"xboard", 0, 0, 'x'},
 
315
        {"post", 0, 0, 'p'},
 
316
        {"easy", 0, 0, 'e'},
 
317
        {"manual", 0, 0, 'm'},
 
318
        {0, 0, 0, 0}
 
319
    };
 
320
 
 
321
    /* getopt_long stores the option index here. */ 
 
322
 
 
323
    int option_index = 0;
 
324
 
 
325
    c = getopt_long (argc, argv, "ehmpvxs:",
 
326
             long_options, &option_index);
 
327
 
 
328
    /* Detect the end of the options. */
 
329
   if (c == -1)
 
330
     break;
 
331
 
 
332
   /* 
 
333
    * Options with a straight flag, could use getoopt_long
 
334
    * flag setting but this is more "obvious" and easier to
 
335
    * modify.
 
336
    */
 
337
   switch (c)
 
338
     {
 
339
     case 'v':
 
340
       opt_version = 1;
 
341
       break;
 
342
     case 'h':
 
343
       opt_help = 1;
 
344
       break;
 
345
     case 'x':
 
346
       opt_xboard = 1;
 
347
       break;
 
348
     case 'p':
 
349
       opt_post = 1;
 
350
       break;
 
351
     case 'e':
 
352
       opt_easy = 1;
 
353
       break;
 
354
     case 'm':
 
355
       opt_manual = 1;
 
356
       break;
 
357
     case 's':    
 
358
       if  ( optarg == NULL ){ /* we have error such as two -s */
 
359
         opt_help = 1;
 
360
         break;
 
361
       }
 
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");
 
366
         return(1);
 
367
       }
 
368
       break;
 
369
     case '?': /* On error give help - getopt does a basic message. */
 
370
       opt_help = 1;
 
371
       break;
 
372
     default:
 
373
       puts ("Option Processing Failed\n");
 
374
       abort();
 
375
     }
 
376
  } /* end of getopt_long style parsing */
 
377
 
 
378
  /* Initialize random number generator */
 
379
  srand((unsigned int) time(NULL));
 
380
  
 
381
  /* initialize control flags */
 
382
  flags = ULL(0);
 
383
 
 
384
  /* output for thinking */
 
385
  ofp = stdout;
 
386
 
 
387
  /* Handle old style command line options */
 
388
  if (argc > 1) {
 
389
    for (i = 0; i < argc; i++) {
 
390
      if (strcmp(argv[i],"xboard") == 0) {
 
391
        SET (flags, XBOARD);
 
392
      } else if (strcmp(argv[i],"post") == 0) {
 
393
        SET (flags, POST);
 
394
      }
 
395
    }
 
396
  }
 
397
  if (opt_xboard == 1)
 
398
        SET (flags, XBOARD);
 
399
  if (opt_post == 1)
 
400
        SET (flags, POST);      
 
401
  if (opt_manual ==1)
 
402
        SET (flags, MANUAL);
 
403
  cmd_version();
 
404
  
 
405
  /* If the version option was specified we can exit here */
 
406
  if (opt_version == 1)
 
407
        return(0);
 
408
  
 
409
  /* If a usage statement is required output it here */
 
410
  if (opt_help == 1){
 
411
    cmd_usage();
 
412
    return (1); /* Maybe an error if due to bad arguments. */
 
413
  }
 
414
 
 
415
  dbg_open(NULL);
 
416
 
 
417
  HashSize = 0 ; /* Set HashSize zero */
 
418
  if ( opt_hash != 0)
 
419
    CalcHashSize(opt_hash);
 
420
 
 
421
  Initialize ();
 
422
 
 
423
  if ( opt_easy == 0)
 
424
   SET (flags, HARD);
 
425
 
 
426
  if (argc > 1) {
 
427
    for (i = 0; i < argc; i++) {
 
428
      if (strcmp(argv[i],"xboard") == 0) {
 
429
        SET (flags, XBOARD);
 
430
      } else if (strcmp(argv[i],"post") == 0) {
 
431
        SET (flags, POST);
 
432
      } 
 
433
    }
 
434
  }
 
435
 
 
436
  bookmode = BOOKPREFER;
 
437
  bookfirstlast = 3;
 
438
 
 
439
  while (!(flags & QUIT)) {
 
440
    dbg_printf("Waiting for input...\n");
 
441
    wait_for_input();
 
442
    dbg_printf("Parsing input...\n");
 
443
    parse_input();
 
444
    dbg_printf("input_status = %d\n", input_status);
 
445
    if ((flags & THINK) && !(flags & MANUAL) && !(flags & ENDED)) {
 
446
      if (!(flags & XBOARD)) printf("Thinking...\n");
 
447
      Iterate ();
 
448
      CLEAR (flags, THINK);
 
449
    }
 
450
    RealGameCnt = GameCnt;
 
451
    RealSide = board.side;
 
452
    dbg_printf("Waking up input...\n");
 
453
    dbg_printf("input_status = %d\n", input_status);
 
454
    input_wakeup();
 
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) ) {
 
459
      ponder();
 
460
    }
 
461
  }
 
462
  
 
463
  CleanupInput();
 
464
 
 
465
  /*  Some cleaning up  */
 
466
  free (HashTab[0]);
 
467
  free (HashTab[1]);
 
468
 
 
469
  dbg_close();
 
470
  return (0);
 
471
}