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 // send_uci_options()
123 void send_uci_options() {
125 char option_line[StringSize]="";
126 char option_string[StringSize]="";
127 gui_send(GUI,"id name %s", Uci->name);
128 gui_send(GUI,"id author %s", Uci->author);
129 for(i=0;i<Uci->option_nb;i++){
130 strcat(option_line,"option");
131 if(strcmp(Uci->option[i].name,"<empty>")){
132 sprintf(option_string," name %s",Uci->option[i].name);
133 strcat(option_line,option_string);
135 if(strcmp(Uci->option[i].type,"<empty>")){
136 sprintf(option_string," type %s",Uci->option[i].type);
137 strcat(option_line,option_string);
139 if(strcmp(Uci->option[i].value,"<empty>")){
140 sprintf(option_string," default %s",Uci->option[i].value);
141 strcat(option_line,option_string);
143 if(strcmp(Uci->option[i].min,"<empty>")){
144 sprintf(option_string," min %s",Uci->option[i].min);
145 strcat(option_line,option_string);
147 if(strcmp(Uci->option[i].max,"<empty>")){
148 sprintf(option_string," max %s",Uci->option[i].max);
149 strcat(option_line,option_string);
151 if(strcmp(Uci->option[i].var,"<empty>")){
152 sprintf(option_string," var %s",Uci->option[i].var);
153 strcat(option_line,option_string);
155 gui_send(GUI,"%s",option_line);
156 strcpy(option_line,"");
158 gui_send(GUI,"uciok");
165 static void parse_setoption(const char string[]) {
167 char option[StringSize];
168 char name[StringSize];
169 char value[StringSize];
171 string_copy=my_strdup(string);
172 parse_open(parse,string_copy);
173 parse_add_keyword(parse,"setoption");
174 parse_add_keyword(parse,"name");
175 parse_add_keyword(parse,"value");
176 parse_get_word(parse,option,StringSize);
177 ASSERT(my_string_equal(option,"setoption"));
178 if(parse_get_word(parse,option,StringSize)){
179 if(my_string_equal(option,"name")){
180 parse_get_string(parse,name,StringSize);
181 if(parse_get_word(parse,option,StringSize)){
182 if(my_string_equal(option,"value")){
183 parse_get_string(parse,value,StringSize);
184 if(my_string_case_equal(name,PolyglotBookFile)){
185 my_log("POLYGLOT *** SETTING BOOK ***\n");
186 my_log("POLYGLOT BOOK \"%s\"\n",value);
191 my_log("POLYGLOT Unable to open book \"%s\"\n",value);
194 engine_send(Engine,"%s",string);
207 void uci_gui_step(char string[]) {
210 }else if(match(string,"uci")){
213 }else if(match(string,"setoption *")){
214 parse_setoption(string);
216 }else if(match(string,"position *")){
217 parse_position(string);
219 }else if(match(string,"go *")){
221 board_from_fen(UCIboard,StartFen);
225 if(!strstr(string,"infinite")){
226 move=book_move(UCIboard,option_get_bool("BookRandom"));
227 if (move != MoveNone && move_is_legal(move,UCIboard)) {
228 if(strstr(string,"ponder")){
232 send_book_move(move);
237 }else if(match(string,"ponderhit") || match(string,"stop")){
238 if(SavedMove!=MoveNone){
239 send_book_move(SavedMove);
243 }else if(match(string,"quit")){
244 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
247 engine_send(Engine,"%s",string);
250 void uci_engine_step(char string[]) {
251 gui_send(GUI,string);
254 // end of uci2uci.cpp