diff options
| author | tslil clingman <tslil@posteo.de> | 2021-01-26 18:14:22 -0500 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-08-28 19:37:41 +0100 |
| commit | ef65e760347ff695654627934ac347fce4b63511 (patch) | |
| tree | 21e6bdcb22dff9c03d6037ec067fb5fe959f64be | |
| parent | de4f20f28afe23ecfc546ad242e5bb44710996cc (diff) | |
Small changes to build ct1986
| -rw-r--r-- | include/negamax.c | 43 | ||||
| -rw-r--r-- | src/ct1986.c | 13 | ||||
| -rw-r--r-- | src/ctaklm.c | 1 |
3 files changed, 26 insertions, 31 deletions
diff --git a/include/negamax.c b/include/negamax.c index 7f05040..061b629 100644 --- a/include/negamax.c +++ b/include/negamax.c @@ -93,20 +93,6 @@ negamax_free(void) { zobrist_free(); } -static enum WIN_TYPE w; - -static inline float -negamax_evaluate_terminal(const float colour) { - if (ply >= 2*board_size - 2 && (w = check_win()) < 0xFF) { - // Check win if far enough into the game - if (w == WIN_ROAD_BLACK || w == WIN_FLAT_BLACK) return colour*infty; - else if (w == WIN_DRAW) return 0; // Draw is fixed at neutral - else return -colour*infty; - } else { - return colour * cnn1986_evaluate_black_win(); - } -} - float negamax_generate(void) { // We need to start with something outside of [-∞,∞] because those @@ -123,6 +109,7 @@ negamax_generate(void) { } static enum TT_FLAG flag; +static enum WIN_TYPE w; static float negamax(const uint8_t cur_depth, float alpha, float beta, @@ -151,8 +138,9 @@ negamax(const uint8_t cur_depth, float alpha, float beta, action_move_to_front(entry->action, list); } - action_t best_action; - float value = -infty; + // TODO: what to do if this is never written to? + action_t best_action = list->head->action; + float best_value = -infty; for (action_node_t *node=list->head; node!=NULL; node=node->next) { @@ -162,8 +150,11 @@ negamax(const uint8_t cur_depth, float alpha, float beta, if (ply >= 2*board_size - 2 && (w = check_win()) < 0xFF) { node_value = -colour*infty; // Check win if far enough into the game - if (w == WIN_ROAD_BLACK || w == WIN_FLAT_BLACK) node_value = colour*infty; - else if (w == WIN_DRAW) node_value = 0; // Draw is neutral + if (w == WIN_ROAD_BLACK || w == WIN_FLAT_BLACK) { + node_value = colour*infty; + } else if (w == WIN_DRAW) { + node_value = 0; + } } else if (cur_depth > 1) { // If nobody won, or too early and not leaf, recurse node_value = -negamax(cur_depth - 1, -beta, -alpha, -colour); @@ -174,31 +165,31 @@ negamax(const uint8_t cur_depth, float alpha, float beta, negamax_display_progress(cur_depth, list->length); - if (node_value > value) { - value = node_value; + if (node_value > best_value) { + best_value = node_value; best_action = node->action; if (cur_depth == negamax_search_depth) action_to_ptn(node->action, negamax_ptn); } - alpha = fmax(value, alpha); + alpha = fmax(best_value, alpha); if (alpha >= beta) break; } action_list_free(list); flag = TT_EXACT; - if (value >= beta) flag = TT_LOWERBOUND; - else if (value <= alpha) flag = TT_UPPERBOUND; + if (best_value >= beta) flag = TT_LOWERBOUND; + else if (best_value <= alpha) flag = TT_UPPERBOUND; if (entry == NULL) { - tt_insert(hash, flag, cur_depth, value, best_action); + tt_insert(hash, flag, cur_depth, best_value, best_action); } else { entry->flag = flag; - entry->value = value; + entry->value = best_value; entry->depth = cur_depth; entry->action = best_action; } - return value; + return best_value; } diff --git a/src/ct1986.c b/src/ct1986.c index 2b0ffb5..ca22f15 100644 --- a/src/ct1986.c +++ b/src/ct1986.c @@ -145,10 +145,10 @@ main(int argc, char **argv) { if (lcd_begin() != EXIT_SUCCESS) return EXIT_FAILURE; - negamax_init_size(); - negamax_search_depth = 3; + negamax_search_depth = 3; new_game(5); + negamax_init(5); char *line = NULL; ssize_t read; @@ -168,9 +168,11 @@ main(int argc, char **argv) { human = 1; } } + free(line); + line = NULL; } else { playing = 0; - break; + human = 1; } } if (playing) { @@ -179,7 +181,10 @@ main(int argc, char **argv) { } } - cnn1986_cache_free(); + free(line); + free(gamelog); + negamax_free(); + lcd_end(); return EXIT_SUCCESS; } diff --git a/src/ctaklm.c b/src/ctaklm.c index 9b1c307..2e0e2b3 100644 --- a/src/ctaklm.c +++ b/src/ctaklm.c @@ -462,7 +462,6 @@ main(int argc, char **argv) { free(line); free(gamelog); - negamax_free(); return EXIT_SUCCESS; |
