From de4f20f28afe23ecfc546ad242e5bb44710996cc Mon Sep 17 00:00:00 2001 From: tslil clingman Date: Tue, 26 Jan 2021 17:42:53 -0500 Subject: Storing best moves! --- include/negamax.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'include/negamax.c') diff --git a/include/negamax.c b/include/negamax.c index f6a7525..7f05040 100644 --- a/include/negamax.c +++ b/include/negamax.c @@ -131,6 +131,7 @@ negamax(const uint8_t cur_depth, float alpha, float beta, uint64_t hash = zobrist_compute(); tt_entry_t *entry = tt_seek(hash); + // CAUTION: >= breaks search stability if (entry != NULL && entry->depth == cur_depth) { if (entry->flag == TT_EXACT) { return entry->value; @@ -146,15 +147,16 @@ negamax(const uint8_t cur_depth, float alpha, float beta, if ((list = action_list_generate()) == NULL) return alpha; // should never happen! + if (entry != NULL) { + action_move_to_front(entry->action, list); + } - /* - * action_node_t *best = NULL; - */ - + action_t best_action; float value = -infty; + for (action_node_t *node=list->head; node!=NULL; node=node->next) { - action_take(node); + action_take(node->action); // Compute the value of the node float node_value; if (ply >= 2*board_size - 2 && (w = check_win()) < 0xFF) { @@ -168,14 +170,15 @@ negamax(const uint8_t cur_depth, float alpha, float beta, } else { node_value = colour * cnn1986_evaluate_black_win(); } - action_undo(node); + action_undo(node->action); negamax_display_progress(cur_depth, list->length); if (node_value > value) { value = node_value; + best_action = node->action; if (cur_depth == negamax_search_depth) - action_to_ptn(node, negamax_ptn); + action_to_ptn(node->action, negamax_ptn); } alpha = fmax(value, alpha); @@ -189,12 +192,12 @@ negamax(const uint8_t cur_depth, float alpha, float beta, else if (value <= alpha) flag = TT_UPPERBOUND; if (entry == NULL) { - tt_insert(hash, flag, cur_depth, value); + tt_insert(hash, flag, cur_depth, value, best_action); } else { entry->flag = flag; entry->value = value; entry->depth = cur_depth; - // entry->action = best; + entry->action = best_action; } return value; -- cgit v1.3.1