4 * ----------------------------------------------------------------------
5 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
8 * GNU SHOGI is based on GNU CHESS
10 * Copyright (c) 1988, 1989, 1990 John Stanback
11 * Copyright (c) 1992 Free Software Foundation
13 * This file is part of GNU SHOGI.
15 * GNU Shogi is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 3 of the License,
18 * or (at your option) any later version.
20 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * You should have received a copy of the GNU General Public License along
26 * with GNU Shogi; see the file COPYING. If not, see
27 * <http://www.gnu.org/licenses/>.
28 * ----------------------------------------------------------------------
34 unsigned int TTadd
= 0;
40 parse(FILE * fd
, unsigned short *mv
, char *opening
)
42 int c
, i
, r1
, r2
, c1
, c2
;
46 while (((c
= getc(fd
)) == ' ') || (c
== '\n'));
59 if ((c
== '\n') || (c
== EOF
))
68 while ((c
!= '?') && (c
!= ' ')
69 && (c
!= '\t') && (c
!= '\n') && (c
!= EOF
))
71 s
[++i
] = (char) (c
= getc(fd
));
79 if ((s
[0] == '!') || (s
[0] == ';') || (i
< 3))
81 while ((c
!= '\n') && (c
!= EOF
))
91 *mv
= (locn(r1
, c1
) << 8) | locn(r2
, c2
);
95 /* Bad move, not for the program to play */
96 *mv
|= 0x8000; /* Flag it ! */
105 * The field of a hashtable is computed as follows:
106 * if sq is on board (< NO_SQUARES) the field gets the value
107 * of the piece on the square sq;
108 * if sq is off board (>= NO_SQUARES) it is a catched figure,
109 * and the field gets the number of catched pieces for
120 return ((color
[i
] == white
) ? (0x80 | board
[i
]) : board
[i
]);
125 return ((Captured
[black
][i
] << 4) | Captured
[white
][i
]);
135 * Look for the current board position in the transposition table.
139 ProbeTTable (short side
,
146 struct hashentry
*ptbl
;
147 /*unsigned*/ short i
= 0; /* to match new type of rehash --tpm */
149 ptbl
= &ttable
[side
][hashkey
% ttblsize
];
153 if ((ptbl
->depth
) == 0)
156 if (ptbl
->hashbd
== hashbd
)
165 /* rehash max rehash times */
167 if (((short)(ptbl
->depth
) >= (short) depth
))
170 for (i
= 0; i
< PTBLBDSIZE
; i
++)
172 if (ptbl
->bd
[i
] != CB(i
))
178 ShowMessage("ttable collision detected");
180 printf("hashkey = 0x%x, hashbd = 0x%x\n",
187 #endif /* HASHTEST */
190 PV
= SwagHt
= ptbl
->mv
;
194 if (ptbl
->flags
& truescore
)
196 *score
= ptbl
->score
;
197 /* adjust *score so moves to mate is from root */
199 if (*score
> SCORE_LIMIT
)
201 else if (*score
< -SCORE_LIMIT
)
204 *beta
= -2 * (SCORE_LIMIT
+ 1000);
206 else if (ptbl
->flags
& lowerbound
)
208 if (ptbl
->score
> *alpha
)
209 *alpha
= ptbl
->score
- 1;
221 * Store the current board position in the transposition table.
225 PutInTTable(short side
,
232 struct hashentry
*ptbl
;
233 /*unsigned*/ short i
= 0; /* to match new type of rehash --tpm */
235 ptbl
= &ttable
[side
][hashkey
% ttblsize
];
239 if ((ptbl
->depth
) == 0 || ptbl
->hashbd
== hashbd
)
256 /* adjust score so moves to mate is from this ply */
258 if (score
> SCORE_LIMIT
)
260 else if (score
< -SCORE_LIMIT
)
263 ptbl
->hashbd
= hashbd
;
264 ptbl
->depth
= (unsigned char) depth
;
270 ptbl
->flags
= lowerbound
;
271 ptbl
->score
= beta
+ 1;
275 ptbl
->flags
= truescore
;
279 for (i
= 0; i
< PTBLBDSIZE
; i
++)
281 #endif /* HASHTEST */
291 array_zero(ttable
[black
], (ttblsize
+ rehash
));
292 array_zero(ttable
[white
], (ttblsize
+ rehash
));
295 array_zero(etab
[0], sizeof(struct etable
)*(size_t)ETABLE
);
296 array_zero(etab
[1], sizeof(struct etable
)*(size_t)ETABLE
);
307 Fbdcmp(unsigned char *a
, unsigned char *b
)
311 for (i
= 0; i
< PTBLBDSIZE
; i
++)
323 * Look for the current board position in the persistent transposition table.
327 ProbeFTable(short side
,
335 unsigned long hashix
;
336 struct fileentry
new, t
;
338 hashix
= ((side
== black
) ? (hashkey
& 0xFFFFFFFE)
339 : (hashkey
| 1)) % filesz
;
341 for (i
= 0; i
< PTBLBDSIZE
; i
++)
346 for (i
= 0; i
< frehash
; i
++)
349 sizeof(struct fileentry
) * ((hashix
+ 2 * i
) % (filesz
)),
351 fread(&t
, sizeof(struct fileentry
), 1, hashfile
);
356 if (!Fbdcmp(t
.bd
, new.bd
))
359 if (((short) t
.depth
>= depth
)
360 && (new.flags
== (unsigned short)(t
.flags
361 & (kingcastle
| queencastle
))))
365 PV
= (t
.f
<< 8) | t
.t
;
366 *score
= (t
.sh
<< 8) | t
.sl
;
368 /* adjust *score so moves to mate is from root */
369 if (*score
> SCORE_LIMIT
)
371 else if (*score
< -SCORE_LIMIT
)
374 if (t
.flags
& truescore
)
376 *beta
= -((SCORE_LIMIT
+ 1000)*2);
378 else if (t
.flags
& lowerbound
)
383 else if (t
.flags
& upperbound
)
399 * Store the current board position in the persistent transposition table.
403 PutInFTable(short side
,
413 unsigned long hashix
;
414 struct fileentry
new, tmp
;
416 hashix
= ((side
== black
) ? (hashkey
& 0xFFFFFFFE)
417 : (hashkey
| 1)) % filesz
;
419 for (i
= 0; i
< PTBLBDSIZE
; i
++)
422 new.f
= (unsigned char) f
;
423 new.t
= (unsigned char) t
;
426 new.flags
= upperbound
;
428 new.flags
= ((score
> beta
) ? lowerbound
: truescore
);
430 new.depth
= (unsigned char) depth
;
432 /* adjust *score so moves to mate is from root */
433 if (score
> SCORE_LIMIT
)
435 else if (score
< -SCORE_LIMIT
)
439 new.sh
= (unsigned char) (score
>> 8);
440 new.sl
= (unsigned char) (score
& 0xFF);
442 for (i
= 0; i
< frehash
; i
++)
445 sizeof(struct fileentry
) * ((hashix
+ 2 * i
) % (filesz
)),
448 if (!fread(&tmp
, sizeof(struct fileentry
), 1, hashfile
) )
454 if (tmp
.depth
&& !Fbdcmp(tmp
.bd
, new.bd
))
457 if (tmp
.depth
== depth
)
460 if (!tmp
.depth
|| ((short) tmp
.depth
< depth
))
463 sizeof(struct fileentry
) * ((hashix
+ 2 * i
) % (filesz
)),
466 fwrite(&new, sizeof(struct fileentry
), 1, hashfile
);
474 #endif /* HASHFILE */
484 array_zero(rpthash
, sizeof(rpthash
));
494 * Store the current eval position in the transposition table.
498 PutInEETable(short side
, int score
)
502 ptbl
= &(*etab
[side
])[hashkey
% (ETABLE
)];
503 ptbl
->ehashbd
= hashbd
;
504 ptbl
->escore
[black
] = pscore
[black
];
505 ptbl
->escore
[white
] = pscore
[white
];
506 ptbl
->hung
[black
] = hung
[black
];
507 ptbl
->hung
[white
] = hung
[white
];
510 #if !defined SAVE_SSCORE
511 array_copy(svalue
, &(ptbl
->sscore
), sizeof(svalue
));
521 /* Get an evaluation from the transposition table */
524 CheckEETable(short side
)
528 ptbl
= &(*etab
[side
])[hashkey
% (ETABLE
)];
530 if (hashbd
== ptbl
->ehashbd
)
538 /* Get an evaluation from the transposition table */
541 ProbeEETable(short side
, short *score
)
545 ptbl
= &(*etab
[side
])[hashkey
% (ETABLE
)];
547 if (hashbd
== ptbl
->ehashbd
)
549 pscore
[black
] = ptbl
->escore
[black
];
550 pscore
[white
] = ptbl
->escore
[white
];
552 #if defined SAVE_SSCORE
553 array_zero(svalue
, sizeof(svalue
));
555 array_copy(&(ptbl
->sscore
), svalue
, sizeof(svalue
));
558 *score
= ptbl
->score
;
559 hung
[black
] = ptbl
->hung
[black
];
560 hung
[white
] = ptbl
->hung
[white
];