diff options
Diffstat (limited to 'include/negamax.c')
| -rw-r--r-- | include/negamax.c | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/include/negamax.c b/include/negamax.c index 16eba9a..43fa5f6 100644 --- a/include/negamax.c +++ b/include/negamax.c @@ -102,7 +102,7 @@ negamax_generate(void) { tt_init(); float result = negamax(negamax_search_depth, -safe_infty, safe_infty, - (ply&1)?1.0:-1.0); + (ply & 1) ? +1.0 : -1.0); tt_free(); return result; @@ -113,49 +113,47 @@ static enum WIN_TYPE w; static float negamax(const uint8_t cur_depth, float alpha, float beta, const float colour) { + + if (cur_depth == 0) return colour*cnn1986_evaluate_black_win(); + float value = -infty; action_list_t *list = action_list_generate(); + if (list == NULL) return value; // ??? - for (action_list_t *node=list; node!=NULL; node=node->next) { - negamax_display_progress(cur_depth); - + for (action_node_t *node=list->head; node!=NULL; node=node->next) { action_take(node); // Somebody won? - if (ply >= 2*board_size - 3 && (w = check_win()) < 0xFF) { - if (w == WIN_ROAD_BLACK || w == WIN_FLAT_BLACK) { - value = colour*infty; - if (value > 0) { - // Always take the win for ourselves - action_undo(node); - if (cur_depth == negamax_search_depth) - action_to_ptn(node, negamax_ptn); - goto prune; - } - } else if (w == WIN_DRAW) value = 0; // Draw is fixed at neutral + if (ply >= 2*board_size - 2 && (w = check_win()) < 0xFF) { + if (w == WIN_ROAD_BLACK || w == WIN_FLAT_BLACK) value = colour*infty; + else if (w == WIN_DRAW) value = 0; // Draw is fixed at neutral else value = -colour*infty; - } else if (cur_depth > 1) { - // Recurse away from the leaves - value = fmax(value, -negamax(cur_depth - 1, -beta, -alpha, -colour)); + if (value > 0) { + // Always take the win for ourselves + action_undo(node); + if (cur_depth == negamax_search_depth) { + action_to_ptn(node, negamax_ptn); + } + goto prune; + } } else { - // Evaluate a leaf - value = fmax(value, colour * cnn1986_evaluate_black_win()); + value = fmax(value, -negamax(cur_depth - 1, -beta, -alpha, -colour)); } action_undo(node); + negamax_display_progress(cur_depth, list->length); + if (value > alpha) { alpha = value; if (cur_depth == negamax_search_depth) action_to_ptn(node, negamax_ptn); + if (alpha >= beta) break; } - if (alpha >= beta) break; } - - action_list_free(list); - prune: + action_list_free(list); return value; } |
