add repetition detection, root position count

- hist.c: add hist_next() - get next entry in history
- state_t: remove plycount, clock_50 -> ply50, add phase, repcount
- pos_t: add plycount, plyroot
- state_t moved to chessdefs.h
- perft-test: split perft from do_perft (todo for perft_alt)
- position.c: add pos_repcount()
- search.c: add is_draw()
- uci: set root position (after moves), adjust history moves repcount
This commit is contained in:
2024-08-04 09:37:58 +02:00
parent 01af1f5c49
commit 0ff41c408b
18 changed files with 183 additions and 55 deletions

View File

@@ -108,6 +108,7 @@ pos_t *pos_clear(pos_t *pos)
pos->king[WHITE] = SQUARE_NONE;
pos->king[BLACK] = SQUARE_NONE;
pos->prev = HIST_START;
return pos;
}
@@ -130,7 +131,7 @@ bool pos_cmp(const pos_t *pos1, const pos_t *pos2)
/* move_do/undo position state */
if (_cmpf(key) || _cmpf(en_passant) || _cmpf(castle) ||
_cmpf(clock_50) || _cmpf(plycount) || _cmpf(captured))
_cmpf(ply50) || _cmpf(plycount) || _cmpf(captured))
goto end;
if (_cmpf(checkers) || _cmpf(pinners) || _cmpf(blockers))
@@ -180,6 +181,36 @@ bitboard_t pos_checkers(const pos_t *pos, const color_t color)
return sq_attackers(pos, occ, pos->king[color], OPPONENT(color));
}
/**
* pos_repcount() - return position repetition count.
* @pos: &position to search
*
* Attention: positions before (and including) root position repcount is
* decreased by one. See do_moves() in uci.c.
*
* @return: The number of repetitions in history, zero otherwise.
*/
u8 pos_repcount(pos_t *pos)
{
int c50 = pos->ply50;
state_t *st = &pos->state;
hkey_t key = pos->key;
st = hist_prev4(st);
//printf("is rep: START=%p ", HIST_START);
//printf("state=%p diff=%zd c50=%d k1=%#lx k2=%#lx\n",
// st, st - HIST_START, c50, hash_short(pos->key), hash_short(st->key));
/* attention, what about root position ? Isn't it dangerous to compare with
* its key ? like: st->prev != HIST_START
*/
for (; c50 >= 0 && st != HIST_START; st = hist_prev2(st), c50 -= 2) {
if (key == st->key)
return st->repcount + 1;
}
return false;
}
/**
* pos_set_checkers_pinners_blockers() - calculate checkers, pinners and blockers.
* @pos: &position