diff options
| author | tslil clingman <tslil@posteo.de> | 2023-01-15 21:31:00 +0100 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-08-28 19:37:41 +0100 |
| commit | 0223a9bec5535fced1a7698b55fd42155d9b0446 (patch) | |
| tree | e7a980454e65d88b56194eed733cabec29ef51b5 /src/cttei.c | |
| parent | ee216c008a188a9436fedb85c70ee5d1719733b1 (diff) | |
switch to explicit game state & important bug fix & clang format
Previously the code base assumed that there was a single, global game
state which was the implicit target of all actions taken. Looking
ahead at architectural improvements, this has now been (almost
entirely) made explicit and functions take tak_state_p where
necessary (and also where unnecessary).
Two important fixes to actions.c were made:
- Previously when generating the possible stack moves, stack height
overflows (> 15) were not taken into account and this resulted in the
tree search corrupting the board state. Now action search does not
list all legal actions, rather the subset of these encodeable by the
implementation.
- The check for crushing on a stack move was incorrect (too strict),
and this resulted in many legitimate moves being igonored.
Finally, in other changes, weights have also been improved by training
all games instead of some subset for chosen players, and clang-format
was run on the codebase.
Diffstat (limited to 'src/cttei.c')
| -rw-r--r-- | src/cttei.c | 96 |
1 files changed, 52 insertions, 44 deletions
diff --git a/src/cttei.c b/src/cttei.c index 534faef..f7fafa6 100644 --- a/src/cttei.c +++ b/src/cttei.c @@ -17,19 +17,18 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> +#include <negamax.h> #include <tak.h> #include <tps.h> -#include <negamax.h> // Set up output function for negamax -inline void -negamax_display_progress(const uint8_t cur_depth, - const uint8_t init_depth, - const uint32_t length) { +inline void negamax_display_progress(const uint8_t cur_depth, + const uint8_t init_depth, + const uint32_t length) { (void)(cur_depth); (void)(init_depth); (void)(length); @@ -38,68 +37,71 @@ negamax_display_progress(const uint8_t cur_depth, enum TEI_RETURN { TEI_QUIT, TEI_OK, TEI_FAILURE }; // expects ``(startpos|tps <tps>) moves <ptn>'' -static enum TEI_RETURN -parse_position_string(char *line) { +static enum TEI_RETURN parse_position_string(tak_state_p state, char *line) { // Find the word ``moves'' char *beg = strstr(line, "moves"); if (!strncasecmp(line, "tps", 3)) { // Split the string on the space before moves - if (beg) *(beg-1) = 0; + if (beg) + *(beg - 1) = 0; // so that we can load it as a TPS description - load_tps(line + 4); + load_tps(state, line + 4); } else if (!strncasecmp(line, "startpos", 8)) { - reset_state(board_size); + reset_state(state, 5); } else { return TEI_FAILURE; } - if (beg == NULL) return TEI_OK; + if (beg == NULL) + return TEI_OK; // parse the PTN sequence strtok(beg, " "); char *ptn = strtok(NULL, " "); while (ptn != NULL) { - if (do_ptn(ptn) != ACT_OK) return TEI_FAILURE; + if (do_ptn(state, ptn) != ACT_OK) + return TEI_FAILURE; ptn = strtok(NULL, " "); } return TEI_OK; } -static enum TEI_RETURN -handle_tei(char *line) { +static enum TEI_RETURN handle_tei(tak_state_p state, char *line) { if (!strcmp(line, "quit")) { return TEI_QUIT; - } if (!strcmp(line, "isready")) { + } + if (!strcmp(line, "isready")) { puts("readyok"); } else if (!strncmp(line, "setoption Depth value ", 22)) { negamax_search_depth = atoi(line + 23); } else if (!strncmp(line, "go", 2)) { // TODO: for now we ignore all of the parameters - float minimax = negamax_generate(); - enum ACT_RESULT r = do_ptn(negamax_ptn); - if (r != ACT_OK && r != GAME_END) return TEI_FAILURE; - printf("info score cp %f pv %s\nbestmove %s\n", - minimax, negamax_ptn, negamax_ptn); + float minimax = negamax_generate(state); + enum ACT_RESULT r = do_ptn(state, negamax_ptn); + if (r != ACT_OK && r != GAME_END) + return TEI_FAILURE; + printf("info score cp %f pv %s\nbestmove %s\n", minimax, negamax_ptn, + negamax_ptn); } else if (!strncmp(line, "position", 8)) { - return parse_position_string(line + 9); + return parse_position_string(state, line + 9); } else if (!strncmp(line, "teinewgame", 10)) { - if (line[11] != '5') return TEI_FAILURE; + if (line[11] != '5') + return TEI_FAILURE; const uint8_t size = atoi(line + 11); - if (size != board_size) { - negamax_free(); - reset_state(size); - negamax_init(size); + if (size != 5) { + return TEI_FAILURE; } else { - reset_state(size); + reset_state(state, 5); } } fflush(stdout); return TEI_OK; } -const char* license = "cttei, a TEI interface to the ct library & its computer opponent\n\ +const char *license = + "cttei, a TEI interface to the ct library & its computer opponent\n\ \n\ Copyright (C) 2021, tslil clingman\n\ \n\ @@ -119,44 +121,50 @@ int main(int argc, char **argv) { while ((read = getline(&line, &alloc_size, stdin))) { if (read > 0 && !strncmp("tei", line, 3)) { break; - } else return EXIT_FAILURE; + } else + return EXIT_FAILURE; } - if (line) free(line); + if (line) + free(line); line = NULL; // Identify ourselves, and send the options - puts("id name cttei_dense"); + puts("id name cttei"); puts("id author tslil clingman"); puts("option name Depth type spin default 4 min 2 max 6"); puts("teiok"); fflush(stdout); // Set default option + tak_state_p state = new_tak_state(5); negamax_search_depth = 4; negamax_init(5); - reset_state(5); + reset_state(state, 5); for (int playing = 1; playing;) { if ((read = getline(&line, &alloc_size, stdin)) > 0) { - line[read-1] = 0; - switch (handle_tei(line)) { - case TEI_FAILURE: return EXIT_FAILURE; - case TEI_QUIT: playing = 0; // fall-through - case TEI_OK: { - if (line) { - free(line); - line = NULL; + line[read - 1] = 0; + switch (handle_tei(state, line)) { + case TEI_FAILURE: + return EXIT_FAILURE; + case TEI_QUIT: + playing = 0; // fall-through + case TEI_OK: { + if (line) { + free(line); + line = NULL; + } + break; } - break; - } } } else { break; } } - if (line) free(line); + if (line) + free(line); negamax_free(); return EXIT_SUCCESS; |
