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, short side, 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,
234 struct hashentry *ptbl;
235 /*unsigned*/ short i = 0; /* to match new type of rehash --tpm */
237 ptbl = &ttable[side][hashkey % ttblsize];
241 if ((ptbl->depth) == 0 || ptbl->hashbd == hashbd)
258 /* adjust score so moves to mate is from this ply */
260 if (score > SCORE_LIMIT)
262 else if (score < -SCORE_LIMIT)
265 ptbl->hashbd = hashbd;
266 ptbl->depth = (unsigned char) depth;
272 ptbl->flags = lowerbound;
273 ptbl->score = beta + 1;
277 ptbl->flags = truescore;
281 for (i = 0; i < PTBLBDSIZE; i++)
283 #endif /* HASHTEST */
293 array_zero(ttable[black], (ttblsize + rehash));
294 array_zero(ttable[white], (ttblsize + rehash));
297 array_zero(etab[0], sizeof(struct etable)*(size_t)ETABLE);
298 array_zero(etab[1], sizeof(struct etable)*(size_t)ETABLE);
309 Fbdcmp(unsigned char *a, unsigned char *b)
313 for (i = 0; i < PTBLBDSIZE; i++)
325 * Look for the current board position in the persistent transposition table.
329 ProbeFTable(short side,
337 unsigned long hashix;
338 struct fileentry new, t;
340 hashix = ((side == black) ? (hashkey & 0xFFFFFFFE)
341 : (hashkey | 1)) % filesz;
343 for (i = 0; i < PTBLBDSIZE; i++)
348 for (i = 0; i < frehash; i++)
351 sizeof(struct fileentry) * ((hashix + 2 * i) % (filesz)),
353 fread(&t, sizeof(struct fileentry), 1, hashfile);
358 if (!Fbdcmp(t.bd, new.bd))
361 if (((short) t.depth >= depth)
362 && (new.flags == (unsigned short)(t.flags
363 & (kingcastle | queencastle))))
367 PV = (t.f << 8) | t.t;
368 *score = (t.sh << 8) | t.sl;
370 /* adjust *score so moves to mate is from root */
371 if (*score > SCORE_LIMIT)
373 else if (*score < -SCORE_LIMIT)
376 if (t.flags & truescore)
378 *beta = -((SCORE_LIMIT + 1000)*2);
380 else if (t.flags & lowerbound)
385 else if (t.flags & upperbound)
401 * Store the current board position in the persistent transposition table.
405 PutInFTable(short side,
415 unsigned long hashix;
416 struct fileentry new, tmp;
418 hashix = ((side == black) ? (hashkey & 0xFFFFFFFE)
419 : (hashkey | 1)) % filesz;
421 for (i = 0; i < PTBLBDSIZE; i++)
424 new.f = (unsigned char) f;
425 new.t = (unsigned char) t;
428 new.flags = upperbound;
430 new.flags = ((score > beta) ? lowerbound : truescore);
432 new.depth = (unsigned char) depth;
434 /* adjust *score so moves to mate is from root */
435 if (score > SCORE_LIMIT)
437 else if (score < -SCORE_LIMIT)
441 new.sh = (unsigned char) (score >> 8);
442 new.sl = (unsigned char) (score & 0xFF);
444 for (i = 0; i < frehash; i++)
447 sizeof(struct fileentry) * ((hashix + 2 * i) % (filesz)),
450 if (!fread(&tmp, sizeof(struct fileentry), 1, hashfile) )
456 if (tmp.depth && !Fbdcmp(tmp.bd, new.bd))
459 if (tmp.depth == depth)
462 if (!tmp.depth || ((short) tmp.depth < depth))
465 sizeof(struct fileentry) * ((hashix + 2 * i) % (filesz)),
468 fwrite(&new, sizeof(struct fileentry), 1, hashfile);
476 #endif /* HASHFILE */
486 array_zero(rpthash, sizeof(rpthash));
496 * Store the current eval position in the transposition table.
500 PutInEETable(short side, int score)
504 ptbl = &(*etab[side])[hashkey % (ETABLE)];
505 ptbl->ehashbd = hashbd;
506 ptbl->escore[black] = pscore[black];
507 ptbl->escore[white] = pscore[white];
508 ptbl->hung[black] = hung[black];
509 ptbl->hung[white] = hung[white];
512 #if !defined SAVE_SSCORE
513 array_copy(svalue, &(ptbl->sscore), sizeof(svalue));
523 /* Get an evaluation from the transposition table */
526 CheckEETable(short side)
530 ptbl = &(*etab[side])[hashkey % (ETABLE)];
532 if (hashbd == ptbl->ehashbd)
540 /* Get an evaluation from the transposition table */
543 ProbeEETable(short side, short *score)
547 ptbl = &(*etab[side])[hashkey % (ETABLE)];
549 if (hashbd == ptbl->ehashbd)
551 pscore[black] = ptbl->escore[black];
552 pscore[white] = ptbl->escore[white];
554 #if defined SAVE_SSCORE
555 array_zero(svalue, sizeof(svalue));
557 array_copy(&(ptbl->sscore), svalue, sizeof(svalue));
560 *score = ptbl->score;
561 hung[black] = ptbl->hung[black];
562 hung[white] = ptbl->hung[white];