diff options
Diffstat (limited to 'src/uci/response.c')
| -rw-r--r-- | src/uci/response.c | 67 |
1 files changed, 53 insertions, 14 deletions
diff --git a/src/uci/response.c b/src/uci/response.c index 9b3118b..a7e53fd 100644 --- a/src/uci/response.c +++ b/src/uci/response.c @@ -1,7 +1,10 @@ #include <string.h> #include <assert.h> #include <stdlib.h> + #include "response.h" +#include "fen.h" +#include "bitboard.h" void apply_option(uci_state *state, char *name, char *buffer) { int settings_count = sizeof(state->option_settings)/sizeof(option_setting_t); @@ -33,7 +36,9 @@ void apply_option(uci_state *state, char *name, char *buffer) { } else { assert(0); } return; } - printf("invalid option\n"); + if (state->debug) { + printf("debug: invalid option\n"); + } } // examples from uci_min.txt @@ -79,10 +84,6 @@ enum { }; int current_state = STATE_INITIAL; -void todo() { - assert(0); -} - void handle_uci(uci_state *state, ucicmd cmd) { if (current_state == STATE_INITIAL) { return handle_initial(state, cmd); @@ -104,12 +105,14 @@ void handle_uci(uci_state *state, ucicmd cmd) { void handle_initial(uci_state *state, ucicmd cmd) { if (cmd.empty || strcmp(cmd.root, "uci") != 0) return; + // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 5 SECONDS printf("id name %s\n", state->name); printf("id author %s\n", state->author); int settings_count = sizeof(state->option_settings)/sizeof(option_setting_t); for (int i = 0; i < settings_count; i++) { print_setting(state->option_settings[i]); } + // --- printf("uciok\n"); current_state = STATE_IDLE; } @@ -136,35 +139,69 @@ void handle_idle(uci_state *state, ucicmd cmd) { *(--cursor) = 0; apply_option(state, name, buffer); } else if (strcmp(cmd.root, "ucinewgame") == 0) { + // clear game state here } else if (strcmp(cmd.root, "position") == 0) { + int k = 0; + char* mode = ucicmd_get_arg(cmd, 0); + if (strcmp(mode, "startpos") == 0) { + state->position = load_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + } + else if (strcmp(mode, "fen") == 0) { + state->position = load_fen(ucicmd_get_arg(cmd, k++)); + } else { assert(0); } + + if (cmd.args_count >= k) return; + assert(ucicmd_get_arg(cmd, k++), "moves") == 0); + struct uci_move *moves = malloc((cmd.args_count - k) * sizeof(struct uci_move)); + int l = 0; + for (; k < cmd.args_count; k++) { + char* move = ucicmd_get_arg(cmd, k); + moves[l++] = (struct uci_move) { + (move[0] - 'a') * 8 + (move[1] - '1'), + (move[2] - 'a') * 8 + (move[3] - '1') + }; + } + // SENT state->position & struct uci_move to ENGINE } else if (strcmp(cmd.root, "stop") == 0) { } else if (strcmp(cmd.root, "quit") == 0) { free(cmd.args); + + int settings_count = sizeof(state->option_settings)/sizeof(option_setting_t); + for (int i = 0; i < settings_count; i++) { + option_setting_t setting = state->option_settings[i]; + if (OPTION_STRING != setting.type) continue; + free(setting.value.string->data); + } + exit(0); } else if (strcmp(cmd.root, "isready") == 0) { current_state = STATE_SYNC; } else if (strcmp(cmd.root, "go") == 0) { current_state = STATE_ACTIVE; } else { - printf("Invalid command: %s\n", cmd.root); + printf("info violation\n"); } } void handle_sync(uci_state *state, ucicmd cmd) { - // when i come up with some info to give: - // printf("info\n"); + // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 5 SECONDS + + + + // --- printf("readyok\n"); current_state = STATE_IDLE; } void handle_ping(uci_state *state, ucicmd cmd) { - // when i come up with some info to give: - // printf("info\n"); - // + // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 1 SECONDS + // if (TODO) { // printf("bestmove\n"); // current_state = STATE_IDLE; // } + + // --- printf("readyok\n"); current_state = STATE_ACTIVE; } @@ -179,13 +216,15 @@ void handle_active(uci_state *state, ucicmd cmd) { } else if (strcmp(cmd.root, "stop") == 0) { current_state = STATE_HALT; } else { - printf("Invalid command: %s\n", cmd.root); + printf("info violation\n"); } } void handle_halt(uci_state *state, ucicmd cmd) { - // when i come up with some info to give: - // printf("info\n"); + // TIMEOUT NOTICE: THE FOLLOWING BLOCK SHOULDN'T TAKE MORE THAN 1 SECONDS + + + // --- printf("bestmove 0000\n"); // TODO: idk get it rn current_state = STATE_IDLE; } |
