15 #include "move_legal.h"
24 #define StringSize 4096
28 static board_t UCIboard[1];
29 static bool Init=TRUE;
30 static int SavedMove=MoveNone;
34 static void send_uci_options();
38 static void parse_position(const char string[]) {
40 /* This is borrowed from Toga II. This code is quite hacky and will be
41 rewritten using the routines in parse.cpp.
47 char move_string[256];
53 string_copy=my_strdup(string);
55 fen = strstr(string_copy,"fen ");
56 moves = strstr(string_copy,"moves ");
60 if (fen != NULL) { // "fen" present
62 if (moves != NULL) { // "moves" present
64 moves[-1] = '\0'; // dirty, but so is UCI
67 board_from_fen(UCIboard,fen+4); // CHANGE ME
71 // HACK: assumes startpos
73 board_from_fen(UCIboard,StartFen);
78 if (moves != NULL) { // "moves" present
82 while (*ptr != '\0') {
84 while (*ptr == ' ') ptr++;
86 move_string[0] = *ptr++;
87 move_string[1] = *ptr++;
88 move_string[2] = *ptr++;
89 move_string[3] = *ptr++;
91 if (*ptr == '\0' || *ptr == ' ') {
92 move_string[4] = '\0';
94 move_string[4] = *ptr++;
95 move_string[5] = '\0';
97 move = move_from_can(move_string,UCIboard);
99 move_do(UCIboard,move);
109 static void send_book_move(int move){
110 char move_string[256];
111 my_log("POLYGLOT *BOOK MOVE*\n");
112 move_to_can(move,UCIboard,move_string,256);
114 gui_send(GUI,"info depth 1 time 0 nodes 0 nps 0 cpuload 0");
115 gui_send(GUI,"bestmove %s",move_string);
118 // format_uci_option_line()
120 static void format_uci_option_line(char * option_line,option_t *opt){
121 char option_string[StringSize];
123 strcpy(option_line,"");
124 // buffer overflow alert
125 strcat(option_line,"option name");
127 strcat(option_line," Polyglot");
129 sprintf(option_string," %s",opt->name);
130 strcat(option_line,option_string);
131 sprintf(option_string," type %s",opt->type);
132 strcat(option_line,option_string);
134 sprintf(option_string," default %s",opt->default_);
135 strcat(option_line,option_string);
138 sprintf(option_string," min %s",opt->min);
139 strcat(option_line,option_string);
142 sprintf(option_string," max %s",opt->max);
143 strcat(option_line,option_string);
145 for(j=0;j<opt->var_nb;j++){
146 sprintf(option_string," var %s",opt->var[j]);
147 strcat(option_line,option_string);
151 // send_uci_options()
153 static void send_uci_options() {
156 char option_line[StringSize]="";
157 gui_send(GUI,"id name %s", Uci->name);
158 gui_send(GUI,"id author %s", Uci->author);
159 option_start_iter(Uci->option);
160 while((opt=option_next(Uci->option))){
161 format_uci_option_line(option_line,opt);
162 gui_send(GUI,"%s",option_line);
164 option_start_iter(Option);
165 while((opt=option_next(Option))){
167 format_uci_option_line(option_line,opt);
168 gui_send(GUI,"%s",option_line);
171 gui_send(GUI,"uciok");
178 static void parse_setoption(const char string[]) {
183 string_copy=my_strdup(string);
184 if(match(string_copy,"setoption name * value *")){
187 if(match(name, "Polyglot *")){
189 polyglot_set_option(pg_name,value);
191 engine_send(Engine,"%s",string);
194 engine_send(Engine,"%s",string);
200 // uci2uci_gui_step()
202 void uci2uci_gui_step(char string[]) {
205 }else if(match(string,"uci")){
208 }else if(match(string,"setoption *")){
209 parse_setoption(string);
211 }else if(match(string,"position *")){
212 parse_position(string);
214 }else if(match(string,"go *")){
216 board_from_fen(UCIboard,StartFen);
220 if(!strstr(string,"infinite")){
221 move=book_move(UCIboard,option_get_bool(Option,"BookRandom"));
222 if (move != MoveNone && move_is_legal(move,UCIboard)) {
223 if(strstr(string,"ponder")){
227 send_book_move(move);
232 }else if(match(string,"ponderhit") || match(string,"stop")){
233 if(SavedMove!=MoveNone){
234 send_book_move(SavedMove);
238 }else if(match(string,"quit")){
239 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
242 engine_send(Engine,"%s",string);
245 void uci2uci_engine_step(char string[]) {
246 gui_send(GUI,string);
249 // end of uci2uci.cpp