4 * ----------------------------------------------------------------------
5 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
7 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
9 * GNU SHOGI is based on GNU CHESS
11 * Copyright (c) 1988, 1989, 1990 John Stanback
12 * Copyright (c) 1992 Free Software Foundation
14 * This file is part of GNU SHOGI.
16 * GNU Shogi is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 3 of the License,
19 * or (at your option) any later version.
21 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with GNU Shogi; see the file COPYING. If not, see
28 * <http://www.gnu.org/licenses/>.
29 * ----------------------------------------------------------------------
35 unsigned int TTadd = 0;
41 parse(FILE * fd, unsigned short *mv, char *opening)
43 int c, i, r1, r2, c1, c2;
47 while (((c = getc(fd)) == ' ') || (c == '\n'));
60 if ((c == '\n') || (c == EOF))
69 while ((c != '?') && (c != ' ')
70 && (c != '\t') && (c != '\n') && (c != EOF))
72 s[++i] = (char) (c = getc(fd));
80 if ((s[0] == '!') || (s[0] == ';') || (i < 3))
82 while ((c != '\n') && (c != EOF))
92 *mv = (locn(r1, c1) << 8) | locn(r2, c2);
96 /* Bad move, not for the program to play */
97 *mv |= 0x8000; /* Flag it ! */
106 * The field of a hashtable is computed as follows:
107 * if sq is on board (< NO_SQUARES) the field gets the value
108 * of the piece on the square sq;
109 * if sq is off board (>= NO_SQUARES) it is a catched figure,
110 * and the field gets the number of catched pieces for
121 return ((color[i] == white) ? (0x80 | board[i]) : board[i]);
126 return ((Captured[black][i] << 4) | Captured[white][i]);
136 * Look for the current board position in the transposition table.
140 ProbeTTable (short side,
147 struct hashentry *ptbl;
148 /*unsigned*/ short i = 0; /* to match new type of rehash --tpm */
150 ptbl = &ttable[side][hashkey % ttblsize];
154 if ((ptbl->depth) == 0)
157 if (ptbl->hashbd == hashbd)
166 /* rehash max rehash times */
168 if (((short)(ptbl->depth) >= (short) depth))
171 for (i = 0; i < PTBLBDSIZE; i++)
173 if (ptbl->bd[i] != CB(i))
179 ShowMessage("ttable collision detected");
181 printf("hashkey = 0x%x, hashbd = 0x%x\n",
188 #endif /* HASHTEST */
191 PV = SwagHt = ptbl->mv;
195 if (ptbl->flags & truescore)
197 *score = ptbl->score;
198 /* adjust *score so moves to mate is from root */
200 if (*score > SCORE_LIMIT)
202 else if (*score < -SCORE_LIMIT)
205 *beta = -2 * (SCORE_LIMIT + 1000);
207 else if (ptbl->flags & lowerbound)
209 if (ptbl->score > *alpha)
210 *alpha = ptbl->score - 1;
222 * Store the current board position in the transposition table.
226 PutInTTable(short side,
233 struct hashentry *ptbl;
234 /*unsigned*/ short i = 0; /* to match new type of rehash --tpm */
236 ptbl = &ttable[side][hashkey % ttblsize];
240 if ((ptbl->depth) == 0 || ptbl->hashbd == hashbd)
257 /* adjust score so moves to mate is from this ply */
259 if (score > SCORE_LIMIT)
261 else if (score < -SCORE_LIMIT)
264 ptbl->hashbd = hashbd;
265 ptbl->depth = (unsigned char) depth;
271 ptbl->flags = lowerbound;
272 ptbl->score = beta + 1;
276 ptbl->flags = truescore;
280 for (i = 0; i < PTBLBDSIZE; i++)
282 #endif /* HASHTEST */
292 array_zero(ttable[black], (ttblsize + rehash));
293 array_zero(ttable[white], (ttblsize + rehash));
296 array_zero(etab[0], sizeof(struct etable)*(size_t)ETABLE);
297 array_zero(etab[1], sizeof(struct etable)*(size_t)ETABLE);
308 Fbdcmp(unsigned char *a, unsigned char *b)
312 for (i = 0; i < PTBLBDSIZE; i++)
324 * Look for the current board position in the persistent transposition table.
328 ProbeFTable(short side,
336 unsigned long hashix;
337 struct fileentry new, t;
339 hashix = ((side == black) ? (hashkey & 0xFFFFFFFE)
340 : (hashkey | 1)) % filesz;
342 for (i = 0; i < PTBLBDSIZE; i++)
347 for (i = 0; i < frehash; i++)
350 sizeof(struct fileentry) * ((hashix + 2 * i) % (filesz)),
352 fread(&t, sizeof(struct fileentry), 1, hashfile);
357 if (!Fbdcmp(t.bd, new.bd))
360 if (((short) t.depth >= depth)
361 && (new.flags == (unsigned short)(t.flags
362 & (kingcastle | queencastle))))
366 PV = (t.f << 8) | t.t;
367 *score = (t.sh << 8) | t.sl;
369 /* adjust *score so moves to mate is from root */
370 if (*score > SCORE_LIMIT)
372 else if (*score < -SCORE_LIMIT)
375 if (t.flags & truescore)
377 *beta = -((SCORE_LIMIT + 1000)*2);
379 else if (t.flags & lowerbound)
384 else if (t.flags & upperbound)
400 * Store the current board position in the persistent transposition table.
404 PutInFTable(short side,
414 unsigned long hashix;
415 struct fileentry new, tmp;
417 hashix = ((side == black) ? (hashkey & 0xFFFFFFFE)
418 : (hashkey | 1)) % filesz;
420 for (i = 0; i < PTBLBDSIZE; i++)
423 new.f = (unsigned char) f;
424 new.t = (unsigned char) t;
427 new.flags = upperbound;
429 new.flags = ((score > beta) ? lowerbound : truescore);
431 new.depth = (unsigned char) depth;
433 /* adjust *score so moves to mate is from root */
434 if (score > SCORE_LIMIT)
436 else if (score < -SCORE_LIMIT)
440 new.sh = (unsigned char) (score >> 8);
441 new.sl = (unsigned char) (score & 0xFF);
443 for (i = 0; i < frehash; i++)
446 sizeof(struct fileentry) * ((hashix + 2 * i) % (filesz)),
449 if (!fread(&tmp, sizeof(struct fileentry), 1, hashfile) )
455 if (tmp.depth && !Fbdcmp(tmp.bd, new.bd))
458 if (tmp.depth == depth)
461 if (!tmp.depth || ((short) tmp.depth < depth))
464 sizeof(struct fileentry) * ((hashix + 2 * i) % (filesz)),
467 fwrite(&new, sizeof(struct fileentry), 1, hashfile);
475 #endif /* HASHFILE */
485 array_zero(rpthash, sizeof(rpthash));
495 * Store the current eval position in the transposition table.
499 PutInEETable(short side, int score)
503 ptbl = &(*etab[side])[hashkey % (ETABLE)];
504 ptbl->ehashbd = hashbd;
505 ptbl->escore[black] = pscore[black];
506 ptbl->escore[white] = pscore[white];
507 ptbl->hung[black] = hung[black];
508 ptbl->hung[white] = hung[white];
511 #if !defined SAVE_SSCORE
512 array_copy(svalue, &(ptbl->sscore), sizeof(svalue));
522 /* Get an evaluation from the transposition table */
525 CheckEETable(short side)
529 ptbl = &(*etab[side])[hashkey % (ETABLE)];
531 if (hashbd == ptbl->ehashbd)
539 /* Get an evaluation from the transposition table */
542 ProbeEETable(short side, short *score)
546 ptbl = &(*etab[side])[hashkey % (ETABLE)];
548 if (hashbd == ptbl->ehashbd)
550 pscore[black] = ptbl->escore[black];
551 pscore[white] = ptbl->escore[white];
553 #if defined SAVE_SSCORE
554 array_zero(svalue, sizeof(svalue));
556 array_copy(&(ptbl->sscore), svalue, sizeof(svalue));
559 *score = ptbl->score;
560 hung[black] = ptbl->hung[black];
561 hung[white] = ptbl->hung[white];