Print 'ponder' always immediately behind 'go'
[uci2wb.git] / UCI2WB.c
index b92e975..95fe411 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -34,6 +34,9 @@ char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, ponder
 int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug;\r
 int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500];\r
 char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20];\r
+char board[100];  // XQ board for UCCI\r
+char *nameWord = "name ", *valueWord = "value ", *wTime = "w", *bTime = "b", *wInc = "winc", *bInc = "binc"; // keywords that differ in UCCI\r
+int unit = 1;\r
 \r
 FILE *toE, *fromE, *fromF;\r
 int pid;\r
@@ -78,19 +81,64 @@ Sync (int action)
 }\r
 \r
 void\r
+FromFEN(char *fen)\r
+{      int i=0;\r
+       while(*fen) {\r
+           char c = *fen++;\r
+           if(c >= 'A') board[i++] = c; else\r
+           if(c == '/') i++; else\r
+           if(c == ' ') break; else\r
+           while(c-- > '0' && i < 99) board[i++] = 0;\r
+           if(i >= 99) break;\r
+       }\r
+}\r
+\r
+char *\r
+ToFEN(int stm)\r
+{\r
+       int i, n=0; static char fen[200]; char *p = fen;\r
+       for(i=0; i<99; i++) {\r
+           char c = board[i];\r
+           if(c >= 'A')  { if(n) *p++ = '0' + n; n = 0;  *p++ = c; } else n ++;\r
+           if(i%10 == 8) { if(n) *p++ = '0' + n; n = -1; *p++ = '/'; }\r
+       }\r
+       sprintf(p-1, " %c - - 0 1", stm);\r
+       return fen;\r
+}\r
+\r
+int\r
+Sqr(char *m, int j)\r
+{\r
+       int n = m[j] - 'a' + 10*('9' - m[j+1]);\r
+       if(n < 0) n = 0; else if(n > 99) n = 99; return n;\r
+}\r
+\r
+int\r
+Play(int nr)\r
+{\r
+       int i, last = -1;\r
+       FromFEN(iniPos + 4); // in XQ iniPos always has just "fen " prefix\r
+       for(i=0; i<nr; i++) {\r
+           int from=Sqr(move[i], 0), to=Sqr(move[i], 2);\r
+           if(board[to]) last = i;\r
+           board[to] = board[from]; board[from] = 0;\r
+       }\r
+       return last;\r
+}\r
+\r
+void\r
 StartSearch(char *ponder)\r
 {      // send the 'go' command to engine. Suffix by ponder.\r
        int x = (ponder[0] != 0);\r
        int nr = moveNr + x; // we ponder for one move ahead!\r
-       fprintf(toE, "\ngo btime %d wtime %d", stm == BLACK ^ x ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ x ^ sc=='s' ? myTime : hisTime);\r
-       DPRINT(    "\n# go btime %d wtime %d", stm == BLACK ^ x ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ x ^ sc=='s' ? myTime : hisTime);\r
+       fprintf(toE, "\ngo%s btime %d wtime %d", ponder, stm == BLACK ^ x ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ x ^ sc=='s' ? myTime : hisTime);\r
+       DPRINT(    "\n# go%s btime %d wtime %d", ponder, stm == BLACK ^ x ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ x ^ sc=='s' ? myTime : hisTime);\r
        if(sTime > 0) { fprintf(toE, " movetime %d", sTime); DPRINT(" movetime %d", sTime); } else\r
        if(mps) { fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2); DPRINT(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2); }\r
        if(inc && !suffix) { fprintf(toE, " winc %d binc %d", inc, inc); DPRINT(" winc %d binc %d", inc, inc); }\r
        if(depth > 0) { fprintf(toE, " depth %d", depth); DPRINT(" depth %d", depth); }\r
         if(suffix) { fprintf(toE, suffix, inc); DPRINT(suffix, inc); }\r
-       fprintf(toE, "%s\n", ponder);\r
-       DPRINT("%s\n", ponder);\r
+       fprintf(toE, "\n"); DPRINT("\n");\r
 }\r
 \r
 void\r
@@ -350,7 +398,7 @@ GUI2Engine()
            int sec = 0;\r
            sscanf(line, "level %d %d:%d %d", &mps, &tc, &sec, &inc) == 4 ||\r
            sscanf(line, "level %d %d %d", &mps, &tc, &inc);\r
-           tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0;\r
+           tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0; tc /= unit; inc /= unit;\r
        }\r
        else if(!strcmp(command, "option")) {\r
            char name[80], *p;\r
@@ -413,8 +461,8 @@ GUI2Engine()
        else if(!strcmp(command, "exit"))   computer = NONE, StopPonder(1);\r
        else if(!strcmp(command, "force"))  computer = NONE, StopPonder(pondering);\r
        else if(!strcmp(command, "go"))     computer = stm;\r
-       else if(!strcmp(command, "time"))   sscanf(line+4, "%d", &myTime),  myTime  *= 10;\r
-       else if(!strcmp(command, "otim"))   sscanf(line+4, "%d", &hisTime), hisTime *= 10;\r
+       else if(!strcmp(command, "time"))   sscanf(line+4, "%d", &myTime),  myTime  = (10*myTime)/unit;\r
+       else if(!strcmp(command, "otim"))   sscanf(line+4, "%d", &hisTime), hisTime = (10*hisTime)/unit;\r
        else if(!strcmp(command, "post"))   post = 1;\r
        else if(!strcmp(command, "nopost")) post = 0;\r
        else if(!strcmp(command, "easy") && !!*canPonder) ponder = 0, StopPonder(pondering), fprintf(toE, "setoption name %s value false\n", canPonder);\r
@@ -423,7 +471,7 @@ GUI2Engine()
        else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory);\r
        else if(!strcmp(command, "cores")&& !!*threadOpt) sscanf(line, "cores %d", &cores), fprintf(toE, "setoption name %s value %d\n", threadOpt, cores);\r
        else if(!strcmp(command, "sd"))     sscanf(line, "sd %d", &depth);\r
-       else if(!strcmp(command, "st"))     sscanf(line, "st %d", &sTime), sTime = 1000*sTime - 30, inc = 0;\r
+       else if(!strcmp(command, "st"))     sscanf(line, "st %d", &sTime), sTime = 1000*sTime - 30, inc = 0, sTime /= unit;\r
        else if(!strcmp(command, "quit"))   fprintf(toE, "quit\n"), fflush(toE), exit(0);\r
     }\r
 }\r