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 strcat(option_line,"option name");
126 strcat(option_line," Polyglot");
128 sprintf(option_string," %s",opt->name);
129 strcat(option_line,option_string);
130 sprintf(option_string," type %s",opt->type);
131 strcat(option_line,option_string);
132 if(strcmp(opt->type,"button")){
133 sprintf(option_string," default %s",opt->default_);
134 strcat(option_line,option_string);
136 if(!strcmp(opt->type,"spin")){
137 sprintf(option_string," min %s",opt->min);
138 strcat(option_line,option_string);
140 if(!strcmp(opt->type,"spin")){
141 sprintf(option_string," max %s",opt->max);
142 strcat(option_line,option_string);
144 for(j=0;j<opt->var_nb;j++){
145 sprintf(option_string," var %s",opt->var[j]);
146 strcat(option_line,option_string);
150 // send_uci_options()
152 static void send_uci_options() {
155 char option_line[StringSize]="";
156 gui_send(GUI,"id name %s", Uci->name);
157 gui_send(GUI,"id author %s", Uci->author);
158 for(i=0;i<Uci->option_nb;i++){
159 format_uci_option_line(option_line,Uci->option+i);
160 gui_send(GUI,"%s",option_line);
164 format_uci_option_line(option_line,p);
165 gui_send(GUI,"%s",option_line);
169 gui_send(GUI,"uciok");
176 static void parse_setoption(const char string[]) {
181 string_copy=my_strdup(string);
182 if(match(string_copy,"setoption name * value *")){
185 if(match(name, "Polyglot *")){
187 polyglot_set_option(pg_name,value);
189 engine_send(Engine,"%s",string);
192 engine_send(Engine,"%s",string);
198 // uci2uci_gui_step()
200 void uci2uci_gui_step(char string[]) {
203 }else if(match(string,"uci")){
206 }else if(match(string,"setoption *")){
207 parse_setoption(string);
209 }else if(match(string,"position *")){
210 parse_position(string);
212 }else if(match(string,"go *")){
214 board_from_fen(UCIboard,StartFen);
218 if(!strstr(string,"infinite")){
219 move=book_move(UCIboard,option_get_bool("BookRandom"));
220 if (move != MoveNone && move_is_legal(move,UCIboard)) {
221 if(strstr(string,"ponder")){
225 send_book_move(move);
230 }else if(match(string,"ponderhit") || match(string,"stop")){
231 if(SavedMove!=MoveNone){
232 send_book_move(SavedMove);
236 }else if(match(string,"quit")){
237 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
240 engine_send(Engine,"%s",string);
243 void uci2uci_engine_step(char string[]) {
244 gui_send(GUI,string);
247 // end of uci2uci.cpp