working TT with perft

This commit is contained in:
2024-06-17 07:45:57 +02:00
parent 840202dc0e
commit f1657e9806
3 changed files with 102 additions and 86 deletions

View File

@@ -23,9 +23,10 @@
/**
* perft() - Perform perft on position
* @pos: &position to search
* @depth: Wanted depth.
* @ply: perft depth level.
* @pos: &position to search
* @depth: Wanted depth.
* @ply: current perft depth level (root = 1)
* @output: output total for 1st level moves.
*
* Run perft on a position. This function displays the available moves at @depth
* level for each possible first move, and the total of moves.
@@ -43,15 +44,15 @@
*/
u64 perft(pos_t *pos, int depth, int ply, bool output)
{
static movelist_t stack;
//int subnodes;
u64 subnodes, nodes = 0;
movelist_t movelist;
move_t *move, *last;
state_t state;
# ifdef PERFT_MOVE_HISTORY
static movelist_t stack;
if (ply == 1)
stack.nmoves = 0;
# endif
pos_set_checkers_pinners_blockers(pos);
@@ -62,38 +63,23 @@ u64 perft(pos_t *pos, int depth, int ply, bool output)
nodes++;
} else {
move_do(pos, *move, &state);
# ifdef PERFT_MOVE_HISTORY
stack.move[stack.nmoves++] = *move;
if (ply == 2 &&
//move_from(*move) == F7 &&
//move_to(*move) == F5 &&
move_from(stack.move[stack.nmoves-2]) == B2 &&
move_to(stack.move[stack.nmoves-2]) == B4 &&
move_from(stack.move[stack.nmoves-1]) == F7 &&
move_to(stack.move[stack.nmoves-1]) == F5
) {
//&& pos->board[F5] == B_PAWN) {
moves_print(&stack, 0);
pos_print(pos);
}
# endif
if (depth == 2) {
movelist_t movelist2;
pos_set_checkers_pinners_blockers(pos);
subnodes = pos_legal(pos, pos_gen_pseudo(pos, &movelist2))->nmoves;
} else {
hentry_t *entry;
//if (ply >= 4 && ply <= 8) {
if (ply == 4) {
if ((entry = tt_probe_perft(pos->key, depth))) {
subnodes = HASH_PERFT_VAL(entry->data);
printf("tt hit key=%lx ply=%d depth=%d nodes=%lu\n",
pos->key, ply, depth, subnodes);
} else {
subnodes = perft(pos, depth - 1, ply + 1, output);
tt_store_perft(pos->key, depth, subnodes);
}
} else if (ply >= 4) {
hentry_t *entry = tt_probe_perft(pos->key, depth);
if (entry != TT_MISS) {
subnodes = HASH_PERFT_VAL(entry->data);
} else {
subnodes = perft(pos, depth - 1, ply + 1, output);
tt_store_perft(pos->key, depth, subnodes);
}
} else {
subnodes = perft(pos, depth - 1, ply + 1, output);
}
if (output && ply == 1) {
char movestr[8];
@@ -101,7 +87,9 @@ u64 perft(pos_t *pos, int depth, int ply, bool output)
}
nodes += subnodes;
move_undo(pos, *move, &state);
# ifdef PERFT_MOVE_HISTORY
stack.nmoves--;
# endif
}
}
@@ -112,9 +100,10 @@ u64 perft(pos_t *pos, int depth, int ply, bool output)
/**
* perft_alt() - Perform perft on position, experimental version.
* @pos: &position to search
* @depth: Wanted depth.
* @ply: perft depth level.
* @pos: &position to search
* @depth: Wanted depth.
* @ply: current perft depth level (root = 1)
* @output: output total for 1st level moves.
*
* Run perft on a position. This function displays the available moves at @depth
* level for each possible first move, and the total of moves.
@@ -123,15 +112,12 @@ u64 perft(pos_t *pos, int depth, int ply, bool output)
*/
u64 perft_alt(pos_t *pos, int depth, int ply, bool output)
{
int subnodes;
u64 nodes = 0;
u64 subnodes, nodes = 0;
movelist_t movelist;
move_t *move, *last;
state_t state;
//movelist.nmoves = 0;
pos_set_checkers_pinners_blockers(pos);
state = pos->state;
pos_legal(pos, pos_gen_pseudo(pos, &movelist));
last = movelist.move + movelist.nmoves;
@@ -139,7 +125,7 @@ u64 perft_alt(pos_t *pos, int depth, int ply, bool output)
if (depth == 1) {
nodes++;
} else {
move_do_alt(pos, *move);
move_do_alt(pos, *move, &state);
if (depth == 2) {
movelist_t movelist2;
pos_set_checkers_pinners_blockers(pos);
@@ -149,11 +135,10 @@ u64 perft_alt(pos_t *pos, int depth, int ply, bool output)
}
if (output && ply == 1) {
char movestr[8];
printf("%s: %d\n", move_to_str(movestr, *move, 0), subnodes);
printf("%s: %lu\n", move_to_str(movestr, *move, 0), subnodes);
}
nodes += subnodes;
move_undo_alt(pos, *move);
pos->state = state;
move_undo_alt(pos, *move, &state);
}
}