Fix setting oldBest
[hachu.git] / hachu.c
diff --git a/hachu.c b/hachu.c
index c9350ae..918ef49 100644 (file)
--- a/hachu.c
+++ b/hachu.c
@@ -16,8 +16,9 @@
 #define PATH level==0 || path[0] == 0xc4028 &&  (level==1 /*|| path[1] == 0x75967 && (level == 2 || path[2] == 0x3400b && (level == 3))*/)\r
 //define PATH 0\r
 \r
-#define HASH\r
-#define KILLERS\r
+#define XHASH\r
+#define XKILLERS\r
+#define XNULLMOVE\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 \r
 #ifdef WIN32 \r
 #    include <windows.h>\r
+     int InputWaiting()\r
+     {  // checks for waiting input in pipe\r
+       static int pipe, init;\r
+       static HANDLE inp;\r
+       DWORD cnt;\r
+\r
+       if(!init) inp = GetStdHandle(STD_INPUT_HANDLE);\r
+       if(!PeekNamedPipe(inp, NULL, 0, NULL, &cnt, NULL)) return 1;\r
+       return cnt;\r
+    }\r
 #else\r
 #    include <sys/time.h>\r
+#    include <sys/ioctl.h>\r
+     int InputWaiting()\r
+     {\r
+       int cnt;\r
+       if(ioctl(0, FIONREAD, &cnt)) return 1;\r
+       return cnt;\r
+     }\r
      int GetTickCount() // with thanks to Tord\r
      { struct timeval t;\r
        gettimeofday(&t, NULL);\r
@@ -113,7 +131,8 @@ typedef struct {
 char *array, fenArray[4000], *reason;\r
 int bWidth, bHeight, bsize, zone, currentVariant, chuFlag, tenFlag, chessFlag, repDraws;\r
 int stm, xstm, hashKeyH, hashKeyL, framePtr, msp, nonCapts, rootEval, retMSP, retFirst, retDep, pvPtr, level, cnt50, mobilityScore;\r
-int nodes, startTime, tlim1, tlim2, repCnt, comp;\r
+int nodes, startTime, tlim1, tlim2, tlim3, repCnt, comp, abortFlag;\r
+Move ponderMove;\r
 Move retMove, moveStack[10000], path[100], repStack[300], pv[1000], repeatMove[300], killer[100][2];\r
 \r
       int maxDepth;                            // used by search\r
@@ -1481,6 +1500,7 @@ GenCapts(int sqr, int victimValue)
                    NewCapture(x, SPECIAL + 8*(i-1&7) + (i+1&7) + victimValue - SORTKEY(attacker), p[attacker].promoFlag);\r
                  v = kStep[i+1];\r
                  if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])\r
+\r
                    NewCapture(x, SPECIAL + 8*(i+1&7) + (i-1&7) + victimValue - SORTKEY(attacker), p[attacker].promoFlag);\r
                }\r
              } else { // primary victim on first ring\r
@@ -1564,6 +1584,8 @@ FireSet (UndoInfo *tb)
     if(p[i].pos != ABSENT) tb->fireMask |= fireFlags[i-2];\r
 }\r
 \r
+void TerminationCheck();\r
+\r
 #define QSdepth 0\r
 \r
 int\r
@@ -1572,7 +1594,7 @@ Search (int alpha, int beta, int difEval, int depth, int oldPromo, int promoSupp
   int i, j, k, firstMove, oldMSP = msp, curMove, sorted, bad, phase, king, iterDep, replyDep, nextVictim, to, defer, dubious, bestMoveNr;\r
   int resDep;\r
   int myPV = pvPtr;\r
-  int score, bestScore, curEval, iterAlpha;\r
+  int score, bestScore, oldBest, curEval, iterAlpha;\r
   Move move, nullMove;\r
   UndoInfo tb;\r
 #ifdef HASH\r
@@ -1597,7 +1619,7 @@ if(PATH) /*pboard(board),pmap(attacks, BLACK),*/printf("search(%d) {%d,%d} eval=
   alpha -= (alpha < curEval);\r
   beta  -= (beta <= curEval);\r
 \r
-  nodes++;\r
+  if(!(nodes++ & 4095)) TerminationCheck();\r
   pv[pvPtr++] = 0; // start empty PV, directly behind PV of parent\r
 \r
 \r
@@ -1619,9 +1641,10 @@ if(PATH) /*pboard(board),pmap(attacks, BLACK),*/printf("search(%d) {%d,%d} eval=
   }\r
 #endif\r
 \r
-  replyDep = (depth < 1 ? depth : iterDep < 1 ? 1 : iterDep);\r
-  while(++iterDep <= depth) {\r
+  replyDep = (depth < 1 ? depth-1 : iterDep);\r
+  while(++iterDep <= depth || iterDep == 1) {\r
 if(flag && depth>= 0) printf("iter %d:%d\n", depth,iterDep),fflush(stdout);\r
+    oldBest = bestScore;\r
     iterAlpha = alpha; bestScore = -INF; bestMoveNr = 0; resDep = 60;\r
     for(curMove = firstMove; ; curMove++) { // loop over moves\r
 if(flag && depth>= 0) printf("phase=%d: first/curr/last = %d / %d / %d\n", phase, firstMove, curMove, msp);fflush(stdout);\r
@@ -1633,10 +1656,10 @@ if(flag && depth>= 0) printf("phase=%d: first/curr/last = %d / %d / %d\n", phase
              bestScore = curEval; resDep = QSdepth;\r
              if(bestScore >= beta || depth < -1) goto cutoff;\r
            }\r
-#if 0\r
-           if(curEval >= beta) {\r
+#ifdef NULLMOVE\r
+           else if(curEval >= beta) {\r
              stm ^= WHITE;\r
-             score = -Search(-beta, -iterAlpha, -difEval, depth-3, promoSuppress & SQUARE, ABSENT);\r
+             score = -Search(-beta, 1-beta, -difEval, depth > 3 ? depth-3 : 0, promoSuppress & SQUARE, ABSENT);\r
              stm ^= WHITE;\r
              if(score >= beta) { msp = oldMSP; retDep += 3; return score + (score < curEval); }\r
            }\r
@@ -1778,6 +1801,11 @@ level--;
     repetition:\r
       UnMake(&tb);\r
       xstm = stm; stm ^= WHITE;\r
+      if(abortFlag > 0) { // unwind search\r
+printf("# abort (%d) @ %d\n", abortFlag, level);\r
+        if(curMove == firstMove) bestScore = oldBest, bestMoveNr = firstMove; // none searched yet\r
+        goto leave;\r
+      }\r
 #if 1\r
 if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curMove, moveStack[curMove], MoveToText(moveStack[curMove], 0), score, bestScore);\r
 \r
@@ -1827,7 +1855,8 @@ if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curM
        printf("\n");\r
         fflush(stdout);\r
       }\r
-      if(GetTickCount() - startTime > tlim1) break; // do not start iteration we can (most likely) not finish\r
+printf("# s=%d t=%d 1=%d 3=%d f=%d\n",startTime,GetTickCount(),tlim1,tlim3,abortFlag);\r
+      if(!(abortFlag & 1) && GetTickCount() - startTime > tlim1) break; // do not start iteration we can (most likely) not finish\r
     }\r
     replyDep = iterDep;\r
 #ifdef HASH\r
@@ -1841,6 +1870,7 @@ if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curM
     } else hashTable[index].move[hit] = 0;\r
 #endif\r
   } // next depth\r
+leave:\r
   retMSP = msp;\r
   retFirst = firstMove;\r
   msp = oldMSP; // pop move list\r
@@ -1940,7 +1970,7 @@ typedef Move MOVE;
     void SetMemorySize(int n);              // if n is different from last time, resize all tables to make memory usage below n MB\r
     char *MoveToText(MOVE move, int m);     // converts the move from your internal format to text like e2e2, e1g1, a7a8q.\r
     MOVE ParseMove(char *moveText);         // converts a long-algebraic text move to your internal move format\r
-    int  SearchBestMove(int stm, int timeLeft, int mps, int timeControl, int inc, int timePerMove, MOVE *move, MOVE *ponderMove);\r
+    int  SearchBestMove(MOVE *move, MOVE *ponderMove);\r
     void PonderUntilInput(int stm);         // Search current position for stm, deepening forever until there is input.\r
 \r
 UndoInfo undoInfo;\r
@@ -2078,6 +2108,21 @@ ReadSquare (char *p, int *sqr)
   return 2 + (r + ONE > 9);\r
 }\r
 \r
+int listStart, listEnd;\r
+char boardCopy[BSIZE];\r
+\r
+void\r
+ListMoves ()\r
+{ // create move list on move stack\r
+  int i;\r
+  for(i=0; i< BSIZE; i++) boardCopy[i] = !!board[i];\r
+MapFromScratch(attacks);\r
+  postThinking--; repCnt = 0; tlim1 = tlim2 = tlim3 = 1e8; abortFlag = msp = 0;\r
+  Search(-INF-1, INF+1, 0, 1, sup1 & ~PROMOTE, sup2);\r
+  postThinking++;\r
+  listStart = retFirst; listEnd = msp = retMSP;\r
+}\r
+\r
 MOVE\r
 ParseMove (char *moveText)\r
 {\r
@@ -2100,18 +2145,15 @@ ParseMove (char *moveText)
   ret = f<<SQLEN | t2;\r
   if(*moveText != '\n' && *moveText != '=') ret |= PROMOTE;\r
 printf("# suppress = %c%d\n", sup1%BW+'a', sup1/BW);\r
-MapFromScratch(attacks);\r
-  postThinking--; repCnt = 0;\r
-  Search(-INF-1, INF+1, 0, 1, sup1 & ~PROMOTE, sup2);\r
-  postThinking++;\r
-  for(i=retFirst; i<retMSP; i++) {\r
+//  ListMoves();\r
+  for(i=listStart; i<listEnd; i++) {\r
     if(moveStack[i] == INVALID) continue;\r
     if(c == '@' && (moveStack[i] & SQUARE) == (moveStack[i] >> SQLEN & SQUARE)) break; // any null move matches @@@@\r
     if((moveStack[i] & (PROMOTE | DEFER-1)) == ret) break;\r
     if((moveStack[i] & DEFER-1) == ret) deferred = i; // promoted version of entered non-promotion is legal\r
   }\r
-printf("# moveNr = %d in {%d,%d}\n", i, retFirst, retMSP);\r
-  if(i>=retMSP) {  // no exact match\r
+printf("# moveNr = %d in {%d,%d}\n", i, listStart, listEnd);\r
+  if(i>=listEnd) { // no exact match\r
     if(deferred) { // but maybe non-sensical deferral\r
       int flags = p[board[f]].promoFlag;\r
 printf("# deferral of %d\n", deferred);\r
@@ -2122,8 +2164,8 @@ printf("# deferral of %d\n", deferred);
        if(!(flags & promoBoard[f])) moveStack[i] |= DEFER; // came from outside zone, so essential deferral\r
       }\r
     }\r
-    if(i >= retMSP) {\r
-      for(i=retFirst; i<retMSP; i++) printf("# %d. %08x %08x %s\n", i-50, moveStack[i], ret, MoveToText(moveStack[i], 0));\r
+    if(i >= listEnd) {\r
+      for(i=listStart; i<listEnd; i++) printf("# %d. %08x %08x %s\n", i-50, moveStack[i], ret, MoveToText(moveStack[i], 0));\r
       reason = NULL;\r
       for(i=0; i<repCnt; i++) {if((repeatMove[i] & 0xFFFFFF) == ret) {\r
         if(repeatMove[i] & 1<<24) reason = (repeatMove[i] & 1<<25 ? "Distant capture of protected Lion" : "Counterstrike against Lion");\r
@@ -2134,7 +2176,7 @@ printf("# deferral of %d\n", deferred);
 }\r
     }\r
   }\r
-  return (i >= retMSP ? INVALID : moveStack[i]);\r
+  return (i >= listEnd ? INVALID : moveStack[i]);\r
 }\r
 \r
 void\r
@@ -2144,32 +2186,25 @@ Highlight(char *coords)
   char b[BSIZE], buf[2000], *q;\r
   for(i=0; i<bsize; i++) b[i] = 0;\r
   ReadSquare(coords, &sqr);\r
-MapFromScratch(attacks);\r
-//pmap(attacks, WHITE);\r
-//pmap(attacks, BLACK);\r
-//flag=1;\r
-  postThinking--; repCnt = 0;\r
-  Search(-INF-1, INF+1, 0, 1, sup1 & ~PROMOTE, sup2);\r
-  postThinking++;\r
-flag=0;\r
-  for(i=retFirst; i<retMSP; i++) {\r
+//  ListMoves();\r
+  for(i=listStart; i<listEnd; i++) {\r
     if(sqr == (moveStack[i]>>SQLEN & SQUARE)) {\r
       int t = moveStack[i] & SQUARE;\r
       if(t >= SPECIAL) continue;\r
-      b[t] = (board[t] == EMPTY ? 'Y' : 'R'); cnt++;\r
+      b[t] = (!boardCopy[t] ? 'Y' : 'R'); cnt++;\r
     }\r
   }\r
   if(!cnt) { // no moves from given square\r
     if(sqr != lastPut) return; // refrain from sending empty FEN\r
     // we lifted a piece for second leg of move\r
-    for(i=retFirst; i<retMSP; i++) {\r
+    for(i=listStart; i<listEnd; i++) {\r
       if(lastLift == (moveStack[i]>>SQLEN & SQUARE)) {\r
        int e, t = moveStack[i] & SQUARE;\r
        if(t < SPECIAL) continue; // only special moves\r
        e = lastLift + epList[t - SPECIAL]; // decode\r
        t = lastLift + toList[t - SPECIAL];\r
        if(e != sqr) continue;\r
-       b[t] = (board[t] == EMPTY ? 'Y' : 'R'); cnt++;\r
+       b[t] = (!boardCopy[t] ? 'Y' : 'R'); cnt++;\r
       }\r
     }\r
     if(!cnt) return;\r
@@ -2194,31 +2229,46 @@ flag=0;
   printf("highlight %s\n", buf);\r
 }\r
 \r
-int\r
-SearchBestMove (int stm, int timeLeft, int mps, int timeControl, int inc, int timePerMove, MOVE *move, MOVE *ponderMove)\r
+int timeLeft;                            // timeleft on engine's clock\r
+int mps, timeControl, inc, timePerMove;  // time-control parameters, to be used by Search\r
+char inBuf[8000], command[80], ponderMoveText[20];\r
+\r
+void\r
+SetSearchTimes (int timeLeft)\r
 {\r
-  int score, targetTime, movesLeft = BW*BH/4 + 20;\r
+  int targetTime, movesLeft = BW*BH/4 + 20;\r
   if(mps) movesLeft = mps - (moveNr>>1)%mps;\r
-  targetTime = timeLeft*10 / (movesLeft + 2) + 1000 * inc;\r
+  targetTime = (timeLeft - 1000*inc) / (movesLeft + 2) + 1000 * inc;\r
   if(moveNr < 30) targetTime *= 0.5 + moveNr/60.; // speedup in opening\r
-  if(timePerMove > 0) targetTime = timeLeft * 5;\r
-  startTime = GetTickCount();\r
+  if(timePerMove > 0) targetTime = 0.5*timeLeft, movesLeft = 1;\r
   tlim1 = 0.2*targetTime;\r
   tlim2 = 1.9*targetTime;\r
+  tlim3 = 5*timeLeft / (movesLeft + 4.1);\r
+}\r
+\r
+int\r
+SearchBestMove (MOVE *move, MOVE *ponderMove)\r
+{\r
+  int score;\r
+  startTime = GetTickCount();\r
   nodes = 0;\r
 MapFromScratch(attacks);\r
   retMove = INVALID; repCnt = 0;\r
   score = Search(-INF-1, INF+1, rootEval, maxDepth, sup1, sup2);\r
   *move = retMove;\r
-  *ponderMove = INVALID;\r
+  *ponderMove = pv[1];\r
   return score;\r
 }\r
 \r
+\r
 void\r
 PonderUntilInput (int stm)\r
 {\r
+MapFromScratch(attacks);\r
+  repCnt = 0; abortFlag = -1;\r
+  Search(-INF-1, INF+1, rootEval, maxDepth, sup1, sup2);\r
 }\r
-\r
\r
     int TakeBack(int n)\r
     { // reset the game and then replay it to the desired point\r
       int last, stm;\r
@@ -2238,25 +2288,59 @@ printf("# setup done");fflush(stdout);
       else printf("0-1%s\n", tail);\r
     }\r
 \r
+    void GetLine(int root)\r
+    {\r
+      int i, c;\r
+      while(1) {\r
+        // wait for input, and read it until we have collected a complete line\r
+        for(i = 0; (inBuf[i] = c = getchar()) != '\n'; i++) if(c == EOF || i>7997) exit(0);\r
+        inBuf[i+1] = 0;\r
+\r
+        // extract the first word\r
+        sscanf(inBuf, "%s", command);\r
+printf("# in (mode = %d,%d): %s\n", root, abortFlag, command);\r
+        if(!strcmp(command, "otim"))    { continue; } // do not start pondering after receiving time commands, as move will follow immediately\r
+        if(!strcmp(command, "time"))    { sscanf(inBuf, "time %d", &timeLeft); continue; }\r
+        if(!strcmp(command, "put"))     { ReadSquare(inBuf+4, &lastPut); continue; }  // ditto\r
+        if(!strcmp(command, "."))       { inBuf[0] = 0; return; } // ignore for now\r
+        if(!strcmp(command, "lift"))    { inBuf[0] = 0; Highlight(inBuf+5); return; } // treat here\r
+        abortFlag = 1;\r
+        return;\r
+      }\r
+    }\r
+\r
+    void\r
+    TerminationCheck()\r
+    {\r
+      if(abortFlag < 0) { // check for input\r
+        if(InputWaiting()) GetLine(0); // read & examine input command\r
+      } else {        // check for time\r
+        if(GetTickCount() - startTime > tlim3) abortFlag = 2;\r
+      }\r
+    }\r
+\r
     main()\r
     {\r
       int engineSide=NONE;                     // side played by engine\r
-      int timeLeft;                            // timeleft on engine's clock\r
-      int mps, timeControl, inc, timePerMove;  // time-control parameters, to be used by Search\r
-      MOVE move, ponderMove;\r
+      MOVE move;\r
       int i, score, curVarNr;\r
-      char inBuf[8000], command[80];\r
 \r
   Init(V_CHU); // Chu\r
+      listEnd = 1;\r
 \r
       while(1) { // infinite loop\r
 \r
         fflush(stdout);                 // make sure everything is printed before we do something that might take time\r
+        *inBuf = 0;\r
 \r
+        if(listEnd == 0) ListMoves();   // always maintain a list of legal moves in root position\r
+\r
+        abortFlag = 0;\r
         if(stm == engineSide) {         // if it is the engine's turn to move, set it thinking, and let it move\r
      \r
 pboard(board);\r
-          score = SearchBestMove(stm, timeLeft, mps, timeControl, inc, timePerMove, &move, &ponderMove);\r
+          SetSearchTimes(10*timeLeft);\r
+          score = SearchBestMove(&move, &ponderMove);\r
 \r
           if(move == INVALID) {         // game apparently ended\r
             int kcapt = 0, xstm = stm ^ WHITE, king, k = p[king=royal[xstm]].pos;\r
@@ -2277,8 +2361,10 @@ pboard(board);
             PrintResult(stm, score);\r
           } else {\r
             stm = MakeMove2(stm, move);  // assumes MakeMove returns new side to move\r
-            gameMove[moveNr++] = move;  // remember game\r
+            gameMove[moveNr++] = move;   // remember game\r
             printf("move %s\n", MoveToText(move, 1));\r
+            listEnd = 0;\r
+            continue;\r
           }\r
         }\r
 \r
@@ -2298,22 +2384,14 @@ pboard(board);
           }\r
         }\r
 \r
-      noPonder:\r
-        // wait for input, and read it until we have collected a complete line\r
-        for(i = 0; (inBuf[i] = getchar()) != '\n'; i++);\r
-        inBuf[i+1] = 0;\r
-\r
-        // extract the first word\r
-        sscanf(inBuf, "%s", command);\r
-printf("in: %s\n", command);\r
+        fflush(stdout);         // make sure everything is printed before we do something that might take time\r
+        if(!*inBuf) GetLine(1); // takes care of time and otim commands\r
 \r
         // recognize the command,and execute it\r
         if(!strcmp(command, "quit"))    { break; } // breaks out of infinite loop\r
         if(!strcmp(command, "force"))   { engineSide = NONE;    continue; }\r
         if(!strcmp(command, "analyze")) { engineSide = ANALYZE; continue; }\r
         if(!strcmp(command, "exit"))    { engineSide = NONE;    continue; }\r
-        if(!strcmp(command, "otim"))    { goto noPonder; } // do not start pondering after receiving time commands, as move will follow immediately\r
-        if(!strcmp(command, "time"))    { sscanf(inBuf, "time %d", &timeLeft); goto noPonder; }\r
         if(!strcmp(command, "level"))   {\r
           int min, sec=0;\r
           sscanf(inBuf, "level %d %d %d", &mps, &min, &inc) == 3 ||  // if this does not work, it must be min:sec format\r
@@ -2335,36 +2413,18 @@ printf("in: %s\n", command);
           if(sscanf(inBuf+7, "Contempt=%d", &contemptFactor) == 1) continue;\r
           continue;\r
         }\r
-        if(!strcmp(command, "variant")) {\r
-          for(i=0; i<7; i++) {\r
-            sscanf(inBuf+8, "%s", command);\r
-            if(!strcmp(variants[i].name, command)) {\r
-              Init(curVarNr = i); stm = Setup2(NULL); break;\r
-            }\r
-         }\r
-          continue;\r
-        }\r
         if(!strcmp(command, "sd"))      { sscanf(inBuf, "sd %d", &maxDepth);    continue; }\r
         if(!strcmp(command, "st"))      { sscanf(inBuf, "st %d", &timePerMove); continue; }\r
         if(!strcmp(command, "memory"))  { SetMemorySize(atoi(inBuf+7)); continue; }\r
         if(!strcmp(command, "ping"))    { printf("pong%s", inBuf+4); continue; }\r
     //  if(!strcmp(command, ""))        { sscanf(inBuf, " %d", &); continue; }\r
-        if(!strcmp(command, "new"))     {\r
-          engineSide = BLACK; Init(V_CHESS); stm = Setup2(NULL); maxDepth = MAXPLY; randomize = OFF; curVarNr = comp = 0;\r
-          continue;\r
-        }\r
-        if(!strcmp(command, "setboard")){ engineSide = NONE;  Init(curVarNr); stm = Setup2(inBuf+9); continue; }\r
         if(!strcmp(command, "easy"))    { ponder = OFF; continue; }\r
         if(!strcmp(command, "hard"))    { ponder = ON;  continue; }\r
-        if(!strcmp(command, "undo"))    { stm = TakeBack(1); continue; }\r
-        if(!strcmp(command, "remove"))  { stm = TakeBack(2); continue; }\r
         if(!strcmp(command, "go"))      { engineSide = stm;  continue; }\r
         if(!strcmp(command, "post"))    { postThinking = ON; continue; }\r
         if(!strcmp(command, "nopost"))  { postThinking = OFF;continue; }\r
         if(!strcmp(command, "random"))  { randomize = ON;    continue; }\r
         if(!strcmp(command, "hint"))    { if(ponderMove != INVALID) printf("Hint: %s\n", MoveToText(ponderMove, 0)); continue; }\r
-        if(!strcmp(command, "lift"))    { Highlight(inBuf+5); continue; }\r
-        if(!strcmp(command, "put"))     { ReadSquare(inBuf+4, &lastPut); continue; }\r
         if(!strcmp(command, "book"))    {  continue; }\r
        // non-standard commands\r
         if(!strcmp(command, "p"))       { pboard(board); continue; }\r
@@ -2380,7 +2440,7 @@ printf("in: %s\n", command);
         if(!strcmp(command, "accepted")){ continue; }\r
         if(!strcmp(command, "rejected")){ continue; }\r
         if(!strcmp(command, "result"))  { continue; }\r
-        if(!strcmp(command, "hover"))   {  continue; }\r
+        if(!strcmp(command, "hover"))   { continue; }\r
         if(!strcmp(command, ""))  {  continue; }\r
         if(!strcmp(command, "usermove")){\r
           int move = ParseMove(inBuf+9);\r
@@ -2390,11 +2450,29 @@ pboard(board);
             if(comp) PrintResult(stm, -INF); // against computer: claim\r
           } else {\r
             stm = MakeMove2(stm, move);\r
-            ponderMove = INVALID;\r
+            ponderMove = INVALID; listEnd = 0;\r
             gameMove[moveNr++] = move;  // remember game\r
           }\r
           continue;\r
         }\r
+        ponderMove = INVALID; // the following commands change the position, invalidating ponder move\r
+        listEnd = 0;\r
+        if(!strcmp(command, "new"))     {\r
+          engineSide = BLACK; Init(V_CHESS); stm = Setup2(NULL); maxDepth = MAXPLY; randomize = OFF; curVarNr = comp = 0;\r
+          continue;\r
+        }\r
+        if(!strcmp(command, "variant")) {\r
+          for(i=0; i<7; i++) {\r
+            sscanf(inBuf+8, "%s", command);\r
+            if(!strcmp(variants[i].name, command)) {\r
+              Init(curVarNr = i); stm = Setup2(NULL); break;\r
+            }\r
+         }\r
+          continue;\r
+        }\r
+        if(!strcmp(command, "setboard")){ engineSide = NONE;  Init(curVarNr); stm = Setup2(inBuf+9); continue; }\r
+        if(!strcmp(command, "undo"))    { stm = TakeBack(1); continue; }\r
+        if(!strcmp(command, "remove"))  { stm = TakeBack(2); continue; }\r
         printf("Error: unknown command\n");\r
       }\r
     }\r