summaryrefslogtreecommitdiff
path: root/src/uci/response.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/uci/response.c')
-rw-r--r--src/uci/response.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/uci/response.c b/src/uci/response.c
new file mode 100644
index 0000000..b27bf76
--- /dev/null
+++ b/src/uci/response.c
@@ -0,0 +1,103 @@
+#include <string.h>
+#include <assert.h>
+#include "response.h"
+
+enum {
+ STATE_INITIAL,
+ STATE_IDLE,
+ STATE_SYNC,
+ STATE_PING,
+ STATE_ACTIVE,
+ STATE_HALT,
+};
+int current_state = STATE_INITIAL;
+int debug = false;
+
+void todo() {
+ assert(0);
+}
+
+void handle_uci(ucicmd cmd) {
+ if (current_state == STATE_INITIAL) {
+ return handle_initial(cmd);
+ }
+ if (current_state == STATE_IDLE) {
+ return handle_idle(cmd);
+ }
+ if (current_state == STATE_SYNC) {
+ return handle_sync(cmd);
+ }
+ if (current_state == STATE_PING) {
+ return handle_ping(cmd);
+ }
+ if (current_state == STATE_ACTIVE) {
+ return handle_active(cmd);
+ }
+ return handle_halt(cmd);
+}
+
+void handle_initial(ucicmd cmd) {
+ if (cmd.empty || strcmp(cmd.root, "uci") != 0) return;
+ printf("uciok\n");
+ current_state = STATE_IDLE;
+}
+
+void handle_idle(ucicmd cmd) {
+ if (cmd.empty) return;
+ if (strcmp(cmd.root, "debug") == 0) {
+ debug = strcmp(ucicmd_get_arg(cmd, 0), "on") == 0;
+ } else if (strcmp(cmd.root, "setoption") == 0) {
+ } else if (strcmp(cmd.root, "ucinewgame") == 0) {
+ } else if (strcmp(cmd.root, "position") == 0) {
+ } else if (strcmp(cmd.root, "stop") == 0) {
+ } else if (strcmp(cmd.root, "quit") == 0) {
+ free(cmd.args);
+ 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);
+ }
+}
+
+void handle_sync(ucicmd cmd) {
+ // when i come up with some info to give:
+ // printf("info\n");
+ printf("readyok\n");
+ current_state = STATE_IDLE;
+}
+
+void handle_ping(ucicmd cmd) {
+ // when i come up with some info to give:
+ // printf("info\n");
+ //
+ // if (TODO) {
+ // printf("bestmove\n");
+ // current_state = STATE_IDLE;
+ // }
+ printf("readyok\n");
+ current_state = STATE_ACTIVE;
+}
+
+void handle_active(ucicmd cmd) {
+ if (cmd.empty) {
+ return;
+ }
+ // TODO: this section will call the engine to analysis
+ if (strcmp(cmd.root, "isready") == 0) {
+ current_state = STATE_PING;
+ } else if (strcmp(cmd.root, "stop") == 0) {
+ current_state = STATE_HALT;
+ } else {
+ printf("Invalid command: %s\n", cmd.root);
+ }
+}
+
+void handle_halt(ucicmd cmd) {
+ // when i come up with some info to give:
+ // printf("info\n");
+ printf("bestmove 0000\n"); // TODO: idk get it rn
+ current_state = STATE_IDLE;
+}