4 * ----------------------------------------------------------------------
6 * Copyright (c) 2012 Free Software Foundation
8 * GNU SHOGI is based on GNU CHESS
10 * This file is part of GNU SHOGI.
12 * GNU Shogi is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 3 of the License,
15 * or (at your option) any later version.
17 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with GNU Shogi; see the file COPYING. If not, see
24 * <http://www.gnu.org/licenses/>.
25 * ----------------------------------------------------------------------
31 unsigned int TTadd = 0;
37 parse(FILE * fd, unsigned short *mv, short side, char *opening)
39 int c, i, r1, r2, c1, c2;
43 while (((c = getc(fd)) == ' ') || (c == '\n'));
56 if ((c == '\n') || (c == EOF))
65 while ((c != '?') && (c != ' ')
66 && (c != '\t') && (c != '\n') && (c != EOF))
68 s[++i] = (char) (c = getc(fd));
76 if ((s[0] == '!') || (s[0] == ';') || (i < 3))
78 while ((c != '\n') && (c != EOF))
88 *mv = (locn(r1, c1) << 8) | locn(r2, c2);
92 /* Bad move, not for the program to play */
93 *mv |= 0x8000; /* Flag it ! */
102 * The field of a hashtable is computed as follows:
103 * if sq is on board (< NO_SQUARES) the field gets the value
104 * of the piece on the square sq;
105 * if sq is off board (>= NO_SQUARES) it is a catched figure,
106 * and the field gets the number of catched pieces for
117 return ((color[i] == white) ? (0x80 | board[i]) : board[i]);
122 return ((Captured[black][i] << 4) | Captured[white][i]);
132 * Look for the current board position in the transposition table.
136 ProbeTTable (short side,
143 struct hashentry *ptbl;
144 /*unsigned*/ short i = 0; /* to match new type of rehash --tpm */
146 ptbl = &ttable[side][hashkey % ttblsize];
150 if ((ptbl->depth) == 0)
153 if (ptbl->hashbd == hashbd)
162 /* rehash max rehash times */
164 if (((short)(ptbl->depth) >= (short) depth))
167 for (i = 0; i < PTBLBDSIZE; i++)
169 if (ptbl->bd[i] != CB(i))
175 ShowMessage(CP[199]); /* ttable collision detected */
177 printf("hashkey = 0x%x, hashbd = 0x%x\n",
184 #endif /* HASHTEST */
187 PV = SwagHt = ptbl->mv;
191 if (ptbl->flags & truescore)
193 *score = ptbl->score;
194 /* adjust *score so moves to mate is from root */
196 if (*score > SCORE_LIMIT)
198 else if (*score < -SCORE_LIMIT)
201 *beta = -2 * (SCORE_LIMIT + 1000);
203 else if (ptbl->flags & lowerbound)
205 if (ptbl->score > *alpha)
206 *alpha = ptbl->score - 1;
218 * Store the current board position in the transposition table.
222 PutInTTable(short side,
230 struct hashentry *ptbl;
231 /*unsigned*/ short i = 0; /* to match new type of rehash --tpm */
233 ptbl = &ttable[side][hashkey % ttblsize];
237 if ((ptbl->depth) == 0 || ptbl->hashbd == hashbd)
254 /* adjust score so moves to mate is from this ply */
256 if (score > SCORE_LIMIT)
258 else if (score < -SCORE_LIMIT)
261 ptbl->hashbd = hashbd;
262 ptbl->depth = (unsigned char) depth;
268 ptbl->flags = lowerbound;
269 ptbl->score = beta + 1;
273 ptbl->flags = truescore;
277 for (i = 0; i < PTBLBDSIZE; i++)
279 #endif /* HASHTEST */
289 array_zero(ttable[black], (ttblsize + rehash));
290 array_zero(ttable[white], (ttblsize + rehash));
293 array_zero(etab[0], sizeof(struct etable)*(size_t)ETABLE);
294 array_zero(etab[1], sizeof(struct etable)*(size_t)ETABLE);
305 Fbdcmp(unsigned char *a, unsigned char *b)
309 for (i = 0; i < PTBLBDSIZE; i++)
321 * Look for the current board position in the persistent transposition table.
325 ProbeFTable(short side,
333 unsigned long hashix;
334 struct fileentry new, t;
336 hashix = ((side == black) ? (hashkey & 0xFFFFFFFE)
337 : (hashkey | 1)) % filesz;
339 for (i = 0; i < PTBLBDSIZE; i++)
344 for (i = 0; i < frehash; i++)
347 sizeof(struct fileentry) * ((hashix + 2 * i) % (filesz)),
349 fread(&t, sizeof(struct fileentry), 1, hashfile);
354 if (!Fbdcmp(t.bd, new.bd))
357 if (((short) t.depth >= depth)
358 && (new.flags == (unsigned short)(t.flags
359 & (kingcastle | queencastle))))
363 PV = (t.f << 8) | t.t;
364 *score = (t.sh << 8) | t.sl;
366 /* adjust *score so moves to mate is from root */
367 if (*score > SCORE_LIMIT)
369 else if (*score < -SCORE_LIMIT)
372 if (t.flags & truescore)
374 *beta = -((SCORE_LIMIT + 1000)*2);
376 else if (t.flags & lowerbound)
381 else if (t.flags & upperbound)
397 * Store the current board position in the persistent transposition table.
401 PutInFTable(short side,
411 unsigned long hashix;
412 struct fileentry new, tmp;
414 hashix = ((side == black) ? (hashkey & 0xFFFFFFFE)
415 : (hashkey | 1)) % filesz;
417 for (i = 0; i < PTBLBDSIZE; i++)
420 new.f = (unsigned char) f;
421 new.t = (unsigned char) t;
424 new.flags = upperbound;
426 new.flags = ((score > beta) ? lowerbound : truescore);
428 new.depth = (unsigned char) depth;
430 /* adjust *score so moves to mate is from root */
431 if (score > SCORE_LIMIT)
433 else if (score < -SCORE_LIMIT)
437 new.sh = (unsigned char) (score >> 8);
438 new.sl = (unsigned char) (score & 0xFF);
440 for (i = 0; i < frehash; i++)
443 sizeof(struct fileentry) * ((hashix + 2 * i) % (filesz)),
446 if (!fread(&tmp, sizeof(struct fileentry), 1, hashfile) )
452 if (tmp.depth && !Fbdcmp(tmp.bd, new.bd))
455 if (tmp.depth == depth)
458 if (!tmp.depth || ((short) tmp.depth < depth))
461 sizeof(struct fileentry) * ((hashix + 2 * i) % (filesz)),
464 fwrite(&new, sizeof(struct fileentry), 1, hashfile);
472 #endif /* HASHFILE */
482 array_zero(rpthash, sizeof(rpthash));
492 * Store the current eval position in the transposition table.
496 PutInEETable(short side, int score)
500 ptbl = &(*etab[side])[hashkey % (ETABLE)];
501 ptbl->ehashbd = hashbd;
502 ptbl->escore[black] = pscore[black];
503 ptbl->escore[white] = pscore[white];
504 ptbl->hung[black] = hung[black];
505 ptbl->hung[white] = hung[white];
508 #if !defined SAVE_SSCORE
509 array_copy(svalue, &(ptbl->sscore), sizeof(svalue));
519 /* Get an evaluation from the transposition table */
522 CheckEETable(short side)
526 ptbl = &(*etab[side])[hashkey % (ETABLE)];
528 if (hashbd == ptbl->ehashbd)
536 /* Get an evaluation from the transposition table */
539 ProbeEETable(short side, short *score)
543 ptbl = &(*etab[side])[hashkey % (ETABLE)];
545 if (hashbd == ptbl->ehashbd)
547 pscore[black] = ptbl->escore[black];
548 pscore[white] = ptbl->escore[white];
550 #if defined SAVE_SSCORE
551 array_zero(svalue, sizeof(svalue));
553 array_copy(&(ptbl->sscore), svalue, sizeof(svalue));
556 *score = ptbl->score;
557 hung[black] = ptbl->hung[black];
558 hung[white] = ptbl->hung[white];