Move input and procesing time, otim to separate routine
[hachu.git] / hachu.c
diff --git a/hachu.c b/hachu.c
index 854c9e7..2f4a767 100644 (file)
--- a/hachu.c
+++ b/hachu.c
@@ -2091,7 +2091,7 @@ MapFromScratch(attacks);
   postThinking--; repCnt = 0; tlim1 = tlim2 = 1e8; msp = 0;\r
   Search(-INF-1, INF+1, 0, 1, sup1 & ~PROMOTE, sup2);\r
   postThinking++;\r
-  listStart = retFirst; listEnd = retMSP;\r
+  listStart = retFirst; listEnd = msp = retMSP;\r
 }\r
 \r
 MOVE\r
@@ -2116,7 +2116,7 @@ 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
-  ListMoves();\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
@@ -2157,7 +2157,7 @@ Highlight(char *coords)
   char b[BSIZE], buf[2000], *q;\r
   for(i=0; i<bsize; i++) b[i] = 0;\r
   ReadSquare(coords, &sqr);\r
-  ListMoves();\r
+//  ListMoves();\r
   for(i=listStart; i<listEnd; i++) {\r
     if(sqr == (moveStack[i]>>SQLEN & SQUARE)) {\r
       int t = moveStack[i] & SQUARE;\r
@@ -2244,6 +2244,25 @@ 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
+        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
+        return;\r
+      }\r
+    }\r
+\r
     main()\r
     {\r
       int engineSide=NONE;                     // side played by engine\r
@@ -2259,6 +2278,8 @@ printf("# setup done");fflush(stdout);
 \r
         fflush(stdout);                 // make sure everything is printed before we do something that might take time\r
 \r
+        if(listEnd == 0) ListMoves();   // always maintain a list of legal moves in root position\r
+\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
@@ -2285,6 +2306,7 @@ pboard(board);
             stm = MakeMove2(stm, move);  // assumes MakeMove returns new side to move\r
             gameMove[moveNr++] = move;  // remember game\r
             printf("move %s\n", MoveToText(move, 1));\r
+            listEnd = 0;\r
           }\r
         }\r
 \r
@@ -2304,22 +2326,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
@@ -2341,36 +2355,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
@@ -2386,7 +2382,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
@@ -2396,11 +2392,28 @@ 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
+        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