16 #include "move_legal.h"
25 static const int StringSize = 4096;
29 static board_t UCIboard[1];
30 static bool Init=true;
31 static int SavedMove=MoveNone;
36 #define strcasecmp lstrcmpi
41 static void parse_position(const char string[]) {
43 /* This is borrowed from Toga II. This code is quite hacky and will be
44 rewritten using the routines in parse.cpp.
50 char move_string[256];
56 string_copy=my_strdup(string);
58 fen = strstr(string_copy,"fen ");
59 moves = strstr(string_copy,"moves ");
63 if (fen != NULL) { // "fen" present
65 if (moves != NULL) { // "moves" present
67 moves[-1] = '\0'; // dirty, but so is UCI
70 board_from_fen(UCIboard,fen+4); // CHANGE ME
74 // HACK: assumes startpos
76 board_from_fen(UCIboard,StartFen);
81 if (moves != NULL) { // "moves" present
85 while (*ptr != '\0') {
87 while (*ptr == ' ') ptr++;
89 move_string[0] = *ptr++;
90 move_string[1] = *ptr++;
91 move_string[2] = *ptr++;
92 move_string[3] = *ptr++;
94 if (*ptr == '\0' || *ptr == ' ') {
95 move_string[4] = '\0';
97 move_string[4] = *ptr++;
98 move_string[5] = '\0';
100 move = move_from_can(move_string,UCIboard);
102 move_do(UCIboard,move);
112 static void send_book_move(int move){
113 char move_string[256];
114 my_log("POLYGLOT *BOOK MOVE*\n");
115 move_to_can(move,UCIboard,move_string,256);
117 gui_send(GUI,"info depth 1 time 0 nodes 0 nps 0 cpuload 0");
118 gui_send(GUI,"bestmove %s",move_string);
121 // format_uci_option_line()
123 static void format_uci_option_line(char * option_line,option_t *opt){
124 char option_string[StringSize];
126 strcpy(option_line,"");
127 strcat(option_line,"option name");
129 strcat(option_line," Polyglot");
131 sprintf(option_string," %s",opt->name);
132 strcat(option_line,option_string);
133 sprintf(option_string," type %s",opt->type);
134 strcat(option_line,option_string);
135 if(strcmp(opt->type,"button")){
136 sprintf(option_string," default %s",opt->default_);
137 strcat(option_line,option_string);
139 if(!strcmp(opt->type,"spin")){
140 sprintf(option_string," min %s",opt->min);
141 strcat(option_line,option_string);
143 if(!strcmp(opt->type,"spin")){
144 sprintf(option_string," max %s",opt->max);
145 strcat(option_line,option_string);
147 for(j=0;j<opt->var_nb;j++){
148 sprintf(option_string," var %s",opt->var[j]);
149 strcat(option_line,option_string);
153 // send_uci_options()
155 void send_uci_options() {
159 char option_line[StringSize]="";
160 gui_send(GUI,"id name %s", Uci->name);
161 gui_send(GUI,"id author %s", Uci->author);
162 for(i=0;i<Uci->option_nb;i++){
163 format_uci_option_line(option_line,Uci->option+i);
164 gui_send(GUI,"%s",option_line);
168 format_uci_option_line(option_line,p);
169 gui_send(GUI,"%s",option_line);
173 gui_send(GUI,"uciok");
180 static void parse_setoption(const char string[]) {
186 string_copy=my_strdup(string);
187 if(match(string_copy,"setoption name * value *")){
190 if(match(name, "Polyglot *")){
192 polyglot_set_option(pg_name,value);
194 engine_send(Engine,"%s",string);
197 engine_send(Engine,"%s",string);
205 void uci_gui_step(char string[]) {
208 }else if(match(string,"uci")){
211 }else if(match(string,"setoption *")){
212 parse_setoption(string);
214 }else if(match(string,"position *")){
215 parse_position(string);
217 }else if(match(string,"go *")){
219 board_from_fen(UCIboard,StartFen);
223 if(!strstr(string,"infinite")){
224 move=book_move(UCIboard,option_get_bool("BookRandom"));
225 if (move != MoveNone && move_is_legal(move,UCIboard)) {
226 if(strstr(string,"ponder")){
230 send_book_move(move);
235 }else if(match(string,"ponderhit") || match(string,"stop")){
236 if(SavedMove!=MoveNone){
237 send_book_move(SavedMove);
241 }else if(match(string,"quit")){
242 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
245 engine_send(Engine,"%s",string);
248 void uci_engine_step(char string[]) {
249 gui_send(GUI,string);
252 // end of uci2uci.cpp