1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#include <string.h>
#include <assert.h>
#include "response.h"
// examples from uci_min.txt
// "option name Nullmove type check default true\n"
// "option name Selectivity type spin default 2 min 0 max 4\n"
// "option name Style type combo default Normal var Solid var Normal var Risky\n"
// "option name NalimovPath type string default c:\\n"
// "option name Clear Hash type button\n"
void print_setting(option_setting_t setting) {
printf("option name %s type ", setting.option_name);
// accidently did the yoda style if statement, i am keeping it
if (OPTION_SPIN == setting.type) {
option_spin_setting_t data = setting.data.spin;
printf(
"spin default %d min %d max %d\n",
data.default_value,
data.min,
data.max
);
} else if (OPTION_COMBO == setting.type) {
option_combo_setting_t data = setting.data.combo;
printf("combo default %s", data.combination[data.default_index]);
for (int i = 0; i < data.count; i++) {
printf(" var %s", data.combination[i]);
}
printf("\n");
} else if (OPTION_CHECK == setting.type) {
printf("check default %d\n", setting.data.check_default);
} else if (OPTION_STRING == setting.type) {
printf("string default %s\n", setting.data.string_default);
} else if (OPTION_BUTTON == setting.type) {
printf("button\n");
} else { assert(0); }
}
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(uci_state *state, ucicmd cmd) {
if (current_state == STATE_INITIAL) {
return handle_initial(state, cmd);
}
if (current_state == STATE_IDLE) {
return handle_idle(state, cmd);
}
if (current_state == STATE_SYNC) {
return handle_sync(state, cmd);
}
if (current_state == STATE_PING) {
return handle_ping(state, cmd);
}
if (current_state == STATE_ACTIVE) {
return handle_active(state, cmd);
}
return handle_halt(state, cmd);
}
void handle_initial(uci_state *state, ucicmd cmd) {
if (cmd.empty || strcmp(cmd.root, "uci") != 0) return;
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;
}
void handle_idle(uci_state *state, 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(uci_state *state, ucicmd cmd) {
// when i come up with some info to give:
// printf("info\n");
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");
//
// if (TODO) {
// printf("bestmove\n");
// current_state = STATE_IDLE;
// }
printf("readyok\n");
current_state = STATE_ACTIVE;
}
void handle_active(uci_state *state, 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(uci_state *state, 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;
}
|