/************************* UCI2WB by H.G.Muller ****************************/\r
\r
-#define VERSION "1.7"\r
+#define VERSION "1.10"\r
\r
#include <stdio.h>\r
#include <stdlib.h>\r
#define NONE 2\r
#define ANALYZE 3\r
\r
-char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, ponder, post, hasHash, c, sc='c', *suffix, *variants;\r
+char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, suspended, ponder, post, hasHash, c, sc='c', *suffix, *variants;\r
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; char currMove[20]; // for analyze mode\r
+int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500]; char currMove[20], moveMap[500][10]; // for analyze mode\r
\r
-FILE *toE, *fromE;\r
+FILE *toE, *fromE, *fromF;\r
int pid;\r
\r
#ifdef WIN32\r
void\r
StartSearch(char *ponder)\r
{ // send the 'go' command to engine. Suffix by ponder.\r
- int nr = moveNr + (ponder[0] != 0); // we ponder for one move ahead!\r
- fprintf(toE, "\ngo btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime);\r
- DPRINT( "\n# go btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime);\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
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
for(j=0; j<moveNr; j++) { fprintf(toE, " %s", move[j]); DPRINT(" %s", move[j]); }\r
}\r
\r
+void\r
+StartPonder()\r
+{\r
+ if(!move[moveNr][0]) return; // no ponder move\r
+ LoadPos(moveNr+1);\r
+ pondering = 1; lastDepth = 1;\r
+ StartSearch(" ponder");\r
+}\r
+\r
char *Convert(char *pv)\r
{ // convert Shogi coordinates to WB\r
char *p, *q, c;\r
m[4] = '=', m[5] = 0;\r
}\r
}\r
- if(m[0] == 'e' && (m[5] == 'a' || m[5] == 'h')) // seirawan castling + gating at Rook\r
- m[0] = m[5], m[2] = 'e'; // convert to RxK notation\r
+}\r
+\r
+int\r
+GetChar()\r
+{\r
+ int c;\r
+ if(fromF) {\r
+ if((c = fgetc(fromF)) != EOF) return c;\r
+ fclose(fromF); fromF = 0; printf("# end fake\n");\r
+ }\r
+ return fgetc(fromE);\r
}\r
\r
void *\r
{\r
char line[1024], command[256];\r
\r
+ if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n");\r
while(1) {\r
int i=0, x; char *p, dummy;\r
\r
fflush(stdout); fflush(toE);\r
- while((line[i] = x = fgetc(fromE)) != EOF && line[i] != '\n') i++;\r
+ while((line[i] = x = GetChar()) != EOF && line[i] != '\n') i++;\r
line[++i] = 0;\r
if(x == EOF) exit(0);\r
DPRINT("# engine said: %s", line), fflush(stdout);\r
- sscanf(line, "%s", command);\r
+ if(sscanf(line, "%s", command) != 1) continue;\r
if(!strcmp(command, "bestmove")) {\r
if(pause) { pondering = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore.\r
// move was a move to be played\r
sscanf(line, "bestmove %s", move[moveNr++]);\r
myTime -= (GetTickCount() - startTime)*1.02 + inc; // update own clock, so we can give correct wtime, btime with ponder\r
if(mps && ((moveNr+1)/2) % mps == 0) myTime += tc; if(sTime) myTime = sTime; // new session or move starts\r
+ stm = WHITE+BLACK - stm;\r
// first start a new ponder search, if pondering is on and we have a move to ponder on\r
if(p = strstr(line+9, "ponder")) {\r
+ sscanf(p+7, "%s", move[moveNr]);\r
if(computer != NONE && ponder) {\r
- sscanf(p+7, "%s", move[moveNr]);\r
DPRINT("# ponder on %s\n", move[moveNr]);\r
- LoadPos(moveNr+1); // load position\r
- // and set engine pondering\r
- pondering = 1; lastDepth = 1;\r
- StartSearch(" ponder");\r
+ StartPonder();\r
}\r
p[-1] = '\n'; *p = 0; // strip off ponder move\r
- }\r
+ } else move[moveNr][0] = 0;\r
Move4GUI(line+9);\r
printf("move %s\n", line+9); // send move to GUI\r
- if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == WHITE ? "1-0" : "0-1"); computer = NONE; }\r
- stm = WHITE+BLACK - stm;\r
+ if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == BLACK ? "1-0" : "0-1"); computer = NONE; }\r
}\r
else if(!strcmp(command, "info")) {\r
int d=0, s=0, t=0, n=0;\r
char *pv;\r
if(sscanf(line+5, "string times @ %c", &dummy) == 1) { printf("# %s", line+12); continue; }\r
+ if(collect && (pv = strstr(line+5, "currmove "))) {\r
+ if(p = strstr(line+5, "currmovenumber ")) {\r
+ n = atoi(p+15);\r
+ if(collect == 1 && n != 1) continue; // wait for move 1\r
+ if(collect + (n == 1) > 2) { // done collecting\r
+ if(inex && collect == 2) printf("%d 0 0 0 OK to exclude\n", lastDepth);\r
+ collect = 3; continue;\r
+ }\r
+ collect = 2; on[nr=n] = 1; sscanf(pv+9, "%s", moveMap[n]); continue; // store move\r
+ }\r
+ }\r
if(!post) continue;\r
if(sscanf(line+5, "string %c", &dummy) == 1) printf("%d 0 0 0 %s", lastDepth, line+12); else {\r
if(p = strstr(line+4, " depth ")) sscanf(p+7, "%d", &d), statDepth = d;\r
m[2] = 'a'+'0'+size - m[2];\r
m[3] = 'a'+'0'+size - m[3];\r
if(m[4] == '=') m[4] = 0; // no '=' in USI format!\r
- else if(m[4] != '\n') m[4] = '+'; // cater to WB 4.4 bug :-(\r
- }\r
- if(m[4] && (m[1] == '1' || m[1] == '8')) { // seirawan gating\r
- m[5] = m[0], m[6] = m[1], m[7] = '\0'; // copy from-square behind it, as gating square\r
- if(m[2] == 'e' && (m[0] == 'a' || m[0] == 'h'))\r
- m[2] = (m[0]+m[2]+1)>>1, m[0] = 'e'; // gating at Rook\r
+ else if(m[4]) m[4] = '+'; // cater to WB 4.4 bug :-(\r
}\r
}\r
\r
while(1) {\r
int i, x;\r
\r
- if(computer == stm || computer == ANALYZE) {\r
+ if((computer == stm || computer == ANALYZE) && !suspended) {\r
DPRINT("# start search\n");\r
LoadPos(moveNr); // load position\r
// and set engine thinking (note USI swaps colors!)\r
startTime = GetTickCount();\r
- if(computer == ANALYZE) fprintf(toE, "\ngo infinite\n"), printf("\ngo infinite\n");\r
- else StartSearch("");\r
+ if(computer == ANALYZE) {\r
+ fprintf(toE, "\ngo infinite"); DPRINT("\n# go infinite");\r
+ if(sm & 1) { // some moves are disabled\r
+ fprintf(toE, " searchmoves"); DPRINT(" searchmoves");\r
+ for(i=1; i<nr; i++) if(on[i]) { fprintf(toE, " %s", moveMap[i]); DPRINT(" %s", moveMap[i]); }\r
+ }\r
+ fprintf(toE, "\n"); DPRINT("\n");\r
+ // code for searchmoves goes here\r
+ } else StartSearch("");\r
}\r
nomove:\r
fflush(toE); fflush(stdout);\r
line[++i] = 0; if(x == EOF) { printf("# EOF\n"); exit(-1); }\r
sscanf(line, "%s", command);\r
if(!strcmp(command, "new")) {\r
- computer = BLACK; moveNr = 0; depth = -1;\r
+ computer = BLACK; moveNr = 0; depth = -1; move[0][0] = 0;\r
stm = WHITE; strcpy(iniPos, "position startpos");\r
if(memory != oldMem && hasHash) fprintf(toE, "setoption name %s value %d\n", hashOpt, memory);\r
oldMem = memory;\r
else if(!strcmp(command, "usermove")) {\r
sscanf(line, "usermove %s", command); // strips off linefeed\r
Move4Engine(command);\r
- stm = WHITE+BLACK - stm;\r
+ stm = WHITE+BLACK - stm; collect = (computer == ANALYZE); sm = 0;\r
// when pondering we either continue the ponder search as normal search, or abort it\r
if(pondering || computer == ANALYZE) {\r
if(pondering && !strcmp(command, move[moveNr])) { // ponder hit\r
}\r
else if(!strcmp(command, "protover")) {\r
if(!variants) variants = sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS;\r
- printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 done=0\n", variants);\r
+ printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 exclude=1 pause=1 done=0\n", variants);\r
printf("feature option=\"UCI2WB debug output -check %d\"\n", debug);\r
fprintf(toE, "u%ci\n", sc); fflush(toE); // this prompts UCI engine for options\r
Sync(PAUSE); // wait for uciok\r
*r = 0; sprintf(command, "%s%s %s %s", line+9, q+1, p, r+1);\r
} else strcpy(command, line+9);\r
sprintf(iniPos, "%s%sfen %s", iniPos[0]=='p' ? "position " : "", sc=='s' ? "s" : "", command);\r
- iniPos[strlen(iniPos)-1] = 0;\r
+ iniPos[strlen(iniPos)-1] = sm = 0; collect = (computer == ANALYZE);\r
}\r
else if(!strcmp(command, "variant")) {\r
if(!strcmp(line+8, "shogi\n")) size = 9, strcpy(iniPos, "position startpos");\r
}\r
else if(!strcmp(command, "undo") && (i=1) || !strcmp(command, "remove") && (i=2)) {\r
if(pondering || computer == ANALYZE) StopPonder(1);\r
- moveNr = moveNr > i ? moveNr - i : 0;\r
+ moveNr = moveNr > i ? moveNr - i : 0; collect = (computer == ANALYZE); sm = 0;\r
}\r
else if(!strcmp(command, ".")) {\r
printf("stat01: %d %d %d %d 100 %s\n", statTime, statNodes, statDepth, 100-currNr, currMove);\r
goto nomove;\r
}\r
+ else if(!strcmp(command+2, "clude") && collect > 2) { // include or exclude\r
+ int all = !strcmp(line+8, "all"), in = command[1] == 'n';\r
+ inex = 1; line[strlen(line)-1] = sm = 0; // strip LF and clear sm flag\r
+ for(i=1; i<nr; i++) { if(!strcmp(line+8, moveMap[i]) || all) on[i] = in; sm |= on[i]+1; } // sm: 2 = enabled, 1 = disabled\r
+ if(!(sm & 2)) goto nomove; // no moves enabled; continue current search\r
+ if(computer == ANALYZE) StopPonder(1); // abort old analysis\r
+ }\r
+ else if(!strcmp(command, "pause")) {\r
+ if(computer == stm) myTime -= GetTickCount() - startTime;\r
+ suspended = 1 + pondering; // remember if we were pondering, and stop search ignoring bestmove\r
+ StopPonder(pondering || computer == stm);\r
+ }\r
+ else if(!strcmp(command, "resume")) {\r
+ if(suspended == 2) StartPonder(); // restart interrupted ponder search\r
+ suspended = 0; // causes thinking to start in normal way if on move or analyzing\r
+ }\r
else if(!strcmp(command, "xboard")) ;\r
- else if(!strcmp(command, "analyze"))computer = ANALYZE;\r
+ else if(!strcmp(command, "analyze"))computer = ANALYZE, collect = 1, sm = 0;\r
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, "otim")) sscanf(line+4, "%d", &hisTime), hisTime *= 10;\r
else if(!strcmp(command, "post")) post = 1;\r
else if(!strcmp(command, "nopost")) post = 0;\r
- else if(!strcmp(command, "easy")) ponder = 0;\r
- else if(!strcmp(command, "hard")) ponder = 1;\r
+ else if(!strcmp(command, "easy")) ponder = 0, StopPonder(pondering);\r
+ else if(!strcmp(command, "hard")) ponder = 1, StartPonder();\r
else if(!strcmp(command, "ping")) pause = 1, fprintf(toE, "isready\n"), fflush(toE), Sync(PAUSE), printf("pong %s", line+5);\r
else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory);\r
else if(!strcmp(command, "cores")) sscanf(line, "cores %d", &cores);\r