Transpoistion Tables are the next item on todo-list, but i cant get them working "correct". Maybe someone could help me.
I try to store in AlphaBeta-search the score and the move with flag for fail high and fail low and exact score in a Transposition Table. Zobrist-Hashing seems to work allright (after adding castle rights).
But in which case do i update alpha respectively beta score or use the stored move from TT?
I am even not sure if i store the flag right.
Code: Select all
Score alphabeta_qs_mvvlva_tt(Bitboard *board, Boardindex *bindex, int som, int depth, Move lastmove, Score alpha, Score beta) {
Move moves[218];
Move bestmove = 0;
int i,movecounter = 0;
int flag = 0;
Score score = ALPHA;
U64 hash = compute_hash(board, som);
struct tt *tt;
NODECOUNT++;
score = eval(board, bindex, som);
tt = &TT[hash>>core_bit];
if (tt->hash == hash && tt->depth >= max_depth-depth+PLY) {
if (tt->flag != 0) {
moves[movecounter] = tt->bestmove;
movecounter++;
}
if (tt->flag == 1) {
alpha = MAX(alpha, tt->score);
}
if (tt->flag ==-1) {
beta = MIN(beta, tt->score);
}
}
if (depth <= 0 && kingincheck(board,som))
depth++;
if (depth <= 0)
return alphabeta_qsearch_mvvlva_tt(board, bindex, som, depth, lastmove, alpha, beta);
movecounter = genmoves(board, bindex, moves, movecounter, som, lastmove);
if (movecounter <= 0)
return score;
qsort(moves, movecounter, sizeof(*moves), mvv_lva);
MOVECOUNT+= movecounter;
for (i=0; i < movecounter; i++) {
domove(board, bindex, moves[i], som);
score = -alphabeta_qs_mvvlva_tt(board, bindex, !som, depth-1, moves[i], -beta, -alpha);
if (score >= beta) {
undomove(board, bindex, moves[i], som);
beta_cut++;
bestmove = moves[i];
flag = 1;
save_to_tt(hash, bestmove, score, flag, max_depth-depth+PLY);
return score;
}
if (score > alpha) {
alpha_cut++;
alpha = score;
bestmove = moves[i];
flag = -1;
}
undomove(board, bindex, moves[i], som);
}
save_to_tt(hash, bestmove, alpha, flag, max_depth-depth+PLY);
return alpha;