2 * util.c - C source for GNU SHOGI
4 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6 * GNU SHOGI is based on GNU CHESS
8 * Copyright (c) 1988,1989,1990 John Stanback
9 * Copyright (c) 1992 Free Software Foundation
11 * This file is part of GNU SHOGI.
13 * GNU Shogi is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 1, or (at your option)
18 * GNU Shogi is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with GNU Shogi; see the file COPYING. If not, write to
25 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32 unsigned int TTadd = 0;
38 parse (FILE * fd, short unsigned int *mv, short int side, char *opening)
40 register int c, i, r1, r2, c1, c2;
44 while ((c = getc (fd)) == ' ' || c == '\n') ;
54 if (c == '\n' || c == EOF)
61 while (c != '?' && c != ' ' && c != '\t' && c != '\n' && c != EOF)
62 s[++i] = (char) (c = getc (fd));
66 if (s[0] == '!' || s[0] == ';' || i < 3)
68 while (c != '\n' && c != EOF)
76 *mv = (locn (r1, c1) << 8) | locn (r2, c2);
78 { /* Bad move, not for the program to play */
79 *mv |= 0x8000; /* Flag it ! */
87 * The field of a hashtable is computed as follows:
88 * if sq is on board (< NO_SQUARES) the field gets the value
89 * of the piece on the square sq;
90 * if sq is off board (>= NO_SQUARES) it is a catched figure,
91 * and the field gets the number of catched pieces for
100 register short i = sq;
101 if ( i < NO_SQUARES ) {
102 return ( (color[i] == white) ? (0x80 | board[i]) : board[i] );
105 assert(i!=NO_SQUARES || (Captured[black][0]==0 && Captured[white][0]==0));
108 return ( (Captured[black][i] << 4) | Captured[white][i] );
118 BDpiece(unsigned char p)
120 unsigned short piece = p & 0x7f;
121 if ( piece == no_piece )
131 BDpromoted(unsigned char p)
133 unsigned short piece = p & 0x7f;
134 if ( is_promoted[piece] )
141 ShowBD(unsigned char bd[])
144 for ( i = 0; i < PTBLBDSIZE; i++)
146 if ( i < NO_SQUARES )
148 printf("%c%c(%c%c) ",
149 BDpromoted(bd[i]),BDpiece(bd[i]),
150 BDpromoted(CB(i)),BDpiece(CB(i)));
151 if ( i % NO_COLS == NO_COLS - 1 )
155 printf("%2x(%2x) ",bd[i],CB(i));
170 void ShowPTBL (struct hashentry *ptbl)
172 movealgbr(ptbl->mv,mvstr[0]);
173 printf("hk=%lx hb=%lx ptbl=%lx bd=%lx mv=%s d=%d f=%d s=%d\n",
174 hashkey, hashbd, ptbl,
175 ptbl->hashbd, mvstr[0], ptbl->depth, ptbl->flags, ptbl->score);
181 ProbeTTable (short int side,
189 * Look for the current board position in the transposition table.
193 register struct hashentry far *ptbl;
194 register /*unsigned*/ short i = 0; /*to match new type of rehash --tpm*/
197 /* printf("FOR hk=%lx hb=%lx d=%d\n",hashkey,hashbd,depth); */
200 ptbl = &ttable[side][hashkey % ttblsize];
204 if ((ptbl->depth) == 0)
206 if (ptbl->hashbd == hashbd)
213 /* rehash max rehash times */
214 if (((short)(ptbl->depth) >= (short) depth))
217 for (i = 0; i < PTBLBDSIZE; i++)
219 if (ptbl->bd[i] != CB (i))
223 ShowMessage (CP[199]); /*ttable collision detected*/
225 printf("hashkey = 0x%x, hashbd = 0x%x\n", hashkey, hashbd);
230 #endif /* HASHTEST */
233 PV = SwagHt = ptbl->mv;
234 #if !defined BAREBONES
237 if (ptbl->flags & truescore)
239 *score = ptbl->score;
240 /* adjust *score so moves to mate is from root */
241 if (*score > SCORE_LIMIT)
243 else if (*score < -SCORE_LIMIT)
245 *beta = -2*(SCORE_LIMIT+1000);
247 else if (ptbl->flags & lowerbound)
249 if (ptbl->score > *alpha)
250 *alpha = ptbl->score - 1;
253 /* printf("GET "); ShowPTBL(ptbl); */
262 PutInTTable (short int side,
268 short unsigned int mv)
271 * Store the current board position in the transposition table.
275 register struct hashentry far *ptbl;
276 register /*unsigned*/ short i = 0; /*to match new type of rehash --tpm*/
278 ptbl = &ttable[side][hashkey % ttblsize];
282 if ((ptbl->depth) == 0 || ptbl->hashbd == hashbd)
299 /* adjust score so moves to mate is from this ply */
300 if (score > SCORE_LIMIT)
302 else if (score < -SCORE_LIMIT)
304 ptbl->hashbd = hashbd;
305 ptbl->depth = (unsigned char) depth;
312 algbr (mv >> 8, mv & 0xff, 0);
313 printf ("-add-> d=%d s=%d p=%d a=%d b=%d %s\n", depth, score, ply, alpha, beta, mvstr);
318 ptbl->flags = lowerbound;
319 ptbl->score = beta + 1;
322 ptbl->flags = truescore;
325 for (i = 0; i < PTBLBDSIZE; i++)
327 ptbl->bd[i] = CB (i);
329 #endif /* HASHTEST */
332 /* printf("PUT "); ShowPTBL(ptbl); */
340 static struct hashentry far *ttageb, *ttagew;
347 register struct hashentry far *w, *b;
348 for ( b=ttable[black], w=ttable[white]; b < &ttable[black][ttblsize]; w++, b++)
353 ttageb = ttable[black];
354 ttagew = ttable[white];
355 register unsigned int a;
356 for (a = 0; a < ttblsize + (unsigned int)rehash; a++)
358 (ttable[black])[a].depth = 0;
359 (ttable[white])[a].depth = 0;
362 array_zero(ttable[black],(ttblsize+rehash));
363 array_zero(ttable[white],(ttblsize+rehash));
365 printf("TTable zeroed\n");
368 array_zero(etab[0],sizeof(struct etable)*(size_t)ETABLE);
369 array_zero(etab[1],sizeof(struct etable)*(size_t)ETABLE);
375 int Fbdcmp(unsigned char *a,unsigned char *b)
378 for(i = 0; i < PTBLBDSIZE; i++)
379 if(a[i] != b[i]) return false;
383 ProbeFTable (short int side,
391 * Look for the current board position in the persistent transposition table.
395 register short int i;
396 register unsigned long hashix;
397 struct fileentry new, t;
399 hashix = ((side == black) ? (hashkey & 0xFFFFFFFE) : (hashkey | 1)) % filesz;
401 for (i = 0; i < PTBLBDSIZE; i++)
404 for (i = 0; i < frehash; i++)
407 sizeof (struct fileentry) * ((hashix + 2 * i) % (filesz)),
409 fread (&t, sizeof (struct fileentry), 1, hashfile);
411 if(!Fbdcmp(t.bd, new.bd)) continue;
412 if (((short int) t.depth >= depth)
413 && (new.flags == (unsigned short)(t.flags & (kingcastle | queencastle))))
415 #if !defined BAREBONES
418 PV = (t.f << 8) | t.t;
419 *score = (t.sh << 8) | t.sl;
420 /* adjust *score so moves to mate is from root */
421 if (*score > SCORE_LIMIT)
423 else if (*score < -SCORE_LIMIT)
425 if (t.flags & truescore)
427 *beta = -((SCORE_LIMIT+1000)*2);
429 else if (t.flags & lowerbound)
434 else if (t.flags & upperbound)
446 PutInFTable (short int side,
452 short unsigned int f,
453 short unsigned int t)
456 * Store the current board position in the persistent transposition table.
460 register unsigned short i;
461 register unsigned long hashix;
462 struct fileentry new, tmp;
464 hashix = ((side == black) ? (hashkey & 0xFFFFFFFE) : (hashkey | 1)) % filesz;
465 for (i = 0; i < PTBLBDSIZE; i++)
467 new.f = (unsigned char) f;
468 new.t = (unsigned char) t;
470 new.flags = upperbound;
472 new.flags = ((score > beta) ? lowerbound : truescore);
473 new.depth = (unsigned char) depth;
474 /* adjust *score so moves to mate is from root */
475 if (score > SCORE_LIMIT)
477 else if (score < -SCORE_LIMIT)
481 new.sh = (unsigned char) (score >> 8);
482 new.sl = (unsigned char) (score & 0xFF);
484 for (i = 0; i < frehash; i++)
487 sizeof (struct fileentry) * ((hashix + 2 * i) % (filesz)),
489 if ( !fread (&tmp, sizeof (struct fileentry), 1, hashfile) )
490 {perror("hashfile");exit(1);}
491 if (tmp.depth && !Fbdcmp(tmp.bd,new.bd))continue;
492 if (tmp.depth == depth) break;
493 if (!tmp.depth || (short) tmp.depth < depth)
496 sizeof (struct fileentry) * ((hashix + 2 * i) % (filesz)),
504 fwrite (&new, sizeof (struct fileentry), 1, hashfile);
505 #if !defined BAREBONES
513 #endif /* HASHFILE */
521 array_zero (rpthash, sizeof (rpthash));
532 PutInEETable (short int side,int score)
535 * Store the current eval position in the transposition table.
539 register struct etable far *ptbl;
540 ptbl = &(*etab[side])[hashkey % (ETABLE)];
541 ptbl->ehashbd = hashbd;
542 ptbl->escore[black] = pscore[black];
543 ptbl->escore[white] = pscore[white];
544 ptbl->hung[black] = hung[black];
545 ptbl->hung[white] = hung[white];
547 #if !defined SAVE_SSCORE
548 array_copy (svalue, &(ptbl->sscore), sizeof (svalue));
550 #if !defined BAREBONES
558 CheckEETable (short int side)
560 /* Get an evaluation from the transposition table */
562 register struct etable far *ptbl;
563 ptbl = &(*etab[side])[hashkey % (ETABLE)];
564 if (hashbd == ptbl->ehashbd)
573 ProbeEETable (short int side, short int *score)
575 /* Get an evaluation from the transposition table */
577 register struct etable far *ptbl;
578 ptbl = &(*etab[side])[hashkey % (ETABLE)];
579 if (hashbd == ptbl->ehashbd)
581 pscore[black] = ptbl->escore[black];
582 pscore[white] = ptbl->escore[white];
583 #if defined SAVE_SSCORE
584 array_zero (svalue, sizeof(svalue));
586 array_copy (&(ptbl->sscore), svalue, sizeof (svalue));
588 *score = ptbl->score;
589 hung[black] = ptbl->hung[black];
590 hung[white] = ptbl->hung[white];
591 #if !defined BAREBONES