15 #include "move_legal.h"
24 static const int StringSize = 4096;
28 static board_t UCIboard[1];
29 static bool Init=true;
30 static int SavedMove=MoveNone;
34 static void parse_position(const char string[]) {
36 /* This is borrowed from Toga II. This code is quite hacky and will be
37 rewritten using the routines in parse.cpp.
43 char move_string[256];
49 string_copy=my_strdup(string);
51 fen = strstr(string_copy,"fen ");
52 moves = strstr(string_copy,"moves ");
56 if (fen != NULL) { // "fen" present
58 if (moves != NULL) { // "moves" present
60 moves[-1] = '\0'; // dirty, but so is UCI
63 board_from_fen(UCIboard,fen+4); // CHANGE ME
67 // HACK: assumes startpos
69 board_from_fen(UCIboard,StartFen);
74 if (moves != NULL) { // "moves" present
78 while (*ptr != '\0') {
80 while (*ptr == ' ') ptr++;
82 move_string[0] = *ptr++;
83 move_string[1] = *ptr++;
84 move_string[2] = *ptr++;
85 move_string[3] = *ptr++;
87 if (*ptr == '\0' || *ptr == ' ') {
88 move_string[4] = '\0';
90 move_string[4] = *ptr++;
91 move_string[5] = '\0';
93 move = move_from_can(move_string,UCIboard);
95 move_do(UCIboard,move);
105 static void send_book_move(int move){
106 char move_string[256];
107 my_log("POLYGLOT *BOOK MOVE*\n");
108 move_to_can(move,UCIboard,move_string,256);
110 gui_send(GUI,"info depth 1 time 0 nodes 0 nps 0 cpuload 0");
111 gui_send(GUI,"bestmove %s",move_string);
114 // format_uci_option_line()
116 static void format_uci_option_line(char * option_line,option_t *opt){
117 char option_string[StringSize];
119 strcpy(option_line,"");
120 strcat(option_line,"option name");
122 strcat(option_line," Polyglot");
124 sprintf(option_string," %s",opt->name);
125 strcat(option_line,option_string);
126 sprintf(option_string," type %s",opt->type);
127 strcat(option_line,option_string);
128 if(strcmp(opt->type,"button")){
129 sprintf(option_string," default %s",opt->default_);
130 strcat(option_line,option_string);
132 if(!strcmp(opt->type,"spin")){
133 sprintf(option_string," min %s",opt->min);
134 strcat(option_line,option_string);
136 if(!strcmp(opt->type,"spin")){
137 sprintf(option_string," max %s",opt->max);
138 strcat(option_line,option_string);
140 for(j=0;j<opt->var_nb;j++){
141 sprintf(option_string," var %s",opt->var[j]);
142 strcat(option_line,option_string);
146 // send_uci_options()
148 void send_uci_options() {
152 char option_line[StringSize]="";
153 gui_send(GUI,"id name %s", Uci->name);
154 gui_send(GUI,"id author %s", Uci->author);
155 for(i=0;i<Uci->option_nb;i++){
156 format_uci_option_line(option_line,Uci->option+i);
157 gui_send(GUI,"%s",option_line);
161 format_uci_option_line(option_line,p);
162 gui_send(GUI,"%s",option_line);
166 gui_send(GUI,"uciok");
173 static void parse_setoption(const char string[]) {
179 string_copy=my_strdup(string);
180 if(match(string_copy,"setoption name * value *")){
183 if(match(name, "Polyglot *")){
185 polyglot_set_option(pg_name,value);
187 engine_send(Engine,"%s",string);
190 engine_send(Engine,"%s",string);
198 void uci_gui_step(char string[]) {
201 }else if(match(string,"uci")){
204 }else if(match(string,"setoption *")){
205 parse_setoption(string);
207 }else if(match(string,"position *")){
208 parse_position(string);
210 }else if(match(string,"go *")){
212 board_from_fen(UCIboard,StartFen);
216 if(!strstr(string,"infinite")){
217 move=book_move(UCIboard,option_get_bool("BookRandom"));
218 if (move != MoveNone && move_is_legal(move,UCIboard)) {
219 if(strstr(string,"ponder")){
223 send_book_move(move);
228 }else if(match(string,"ponderhit") || match(string,"stop")){
229 if(SavedMove!=MoveNone){
230 send_book_move(SavedMove);
234 }else if(match(string,"quit")){
235 my_log("POLYGLOT *** \"quit\" from GUI ***\n");
238 engine_send(Engine,"%s",string);
241 void uci_engine_step(char string[]) {
242 gui_send(GUI,string);
245 // end of uci2uci.cpp