Warnings: be explicit about values we are using.
[gnushogi.git] / gnushogi / rawdsp.c
index 0a4e473..033d93b 100644 (file)
@@ -111,10 +111,15 @@ Raw_ShowLine(unsigned short *bstline)
 
 
 static void
-Raw_ShowMessage(char *s)
+Raw_ShowMessage(char *format, ...)
 {
-    if (!XSHOGI)
-        printf("%s\n", s);
+    if (XSHOGI)
+        return;
+    va_list ap;
+    va_start(ap, format);
+    vprintf(format, ap);
+    printf("\n");
+    va_end(ap);
 }
 
 
@@ -177,7 +182,7 @@ static void
 Raw_ShowPatternCount(short side, short n)
 {
     if (flag.post)
-        printf("%s matches %d pattern(s)\n", ColorStr[side], n);
+        printf("%s%s matches %d pattern(s)\n", xboard ? "# " : "" , ColorStr[side], n);
 }
 
 
@@ -190,7 +195,7 @@ Raw_ShowResponseTime(void)
 static void
 Raw_ShowResults(short score, unsigned short *bstline, char ch)
 {
-    if (flag.post  && !XSHOGI)
+    if (flag.post && (xboard || !XSHOGI))
     {
         ElapsedTime(2);
         printf("%2d%c %6d %4ld %8ld  ",
@@ -352,18 +357,21 @@ Raw_EditBoard(void)
     Book = BOOKFAIL;
     Raw_ClearScreen();
     Raw_UpdateDisplay(0, 0, 1, 0);
-    fputs(".   Exit to main\n", stdout);
-    fputs("#   Clear board\n", stdout);
-    fputs("c   Change sides\n", stdout);
-    fputs("enter piece & location: \n", stdout);
+    printf(".   Exit to main\n");
+    printf("#   Clear board\n");
+    printf("c   Change sides\n");
+    printf("enter piece & location:\n");
 
     a = black;
 
-    do
+    while(1)
     {
         scanf("%s", s);
         found = 0;
 
+        if (s[0] == '.')
+            break;
+
         if (s[0] == '#')
         {
             for (sq = 0; sq < NO_SQUARES; sq++)
@@ -373,10 +381,13 @@ Raw_EditBoard(void)
             }
 
             ClearCaptured();
+            continue;
         }
 
-        if (s[0] == 'c')
+        if (s[0] == 'c') {
             a = otherside[a];
+            continue;
+        }
 
         if (s[1] == '*')
         {
@@ -389,41 +400,39 @@ Raw_EditBoard(void)
                     break;
                 }
             }
-
-            c = -1;
-            r = -1;
+            if (!found)
+                printf("# Invalid piece type '%c'\n", s[0]);
+            continue;
         }
-        else
-        {
-            c = COL_NAME(s[1]);
-            r = ROW_NAME(s[2]);
+
+        c = COL_NUM(s[1]);
+        r = ROW_NUM(s[2]);
+
+        if ((c < 0) || (c >= NO_COLS) || (r < 0) || (r >= NO_ROWS)) {
+            printf("# Out-of-board position '%c%c'\n", s[1], s[2]);
+            continue;
         }
 
-        if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
-        {
-            sq = locn(r, c);
-            color[sq] = a;
-            board[sq] = no_piece;
+        sq = locn(r, c);
 
-            for (i = no_piece; i <= king; i++)
+        for (i = no_piece; i <= king; i++)
+        {
+            if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
             {
-                if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
-                {
-                    if (s[3] == '+')
-                        board[sq] = promoted[i];
-                    else
-                        board[sq] = i;
+                color[sq] = a;
+                if (s[3] == '+')
+                    board[sq] = promoted[i];
+                else
+                    board[sq] = i;
 
-                    found = 1;
-                    break;
-                }
+                found = 1;
+                break;
             }
-
-            if (found == 0)
-                color[sq] = neutral;
         }
+
+        if (!found)
+            printf("# Invalid piece type '%c'\n", s[0]);
     }
-    while (s[0] != '.');
 
     for (sq = 0; sq < NO_SQUARES; sq++)
         Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
@@ -517,12 +526,15 @@ Raw_OutputMove(void)
     if (mvstr[0][0] == '\0')
         goto nomove;
 
-    if (XSHOGI)
+    mycnt1++;
+    if (XSHOGI && xboard) /* xboard: print move in XBoard format, with 'move' prefix */
+        printf("move %s\n", mvstr[0]);
+    else if (XSHOGI)
         /* add remaining time in milliseconds to xshogi */
-        printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
+        printf("%d. ... %s %ld\n", mycnt1, mvstr[0],
                (TimeControl.clock[player] - et) * 10);
     else
-        printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
+        printf("%d. ... %s\n", mycnt1, mvstr[0]);
 
  nomove:
     if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
@@ -574,10 +586,10 @@ Raw_OutputMove(void)
         printf("%s mates!\n", ColorStr[computer]);
 #ifdef VERYBUGGY
     else if (!XSHOGI && (root->score < -SCORE_LIMIT))
-        printf("%s has a forced mate in %d moves!\n",
+        printf("%s%s has a forced mate in %d moves!\n", xboard ? "# " : "",
                ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
     else if (!XSHOGI && (root->score > SCORE_LIMIT))
-        printf("%s has a forced mate in %d moves!\n",
+        printf("%s%s has a forced mate in %d moves!\n", xboard ? "# " : "",
                ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
 #endif /* VERYBUGGY */
 }
@@ -690,56 +702,67 @@ Raw_SelectLevel(char *sx)
     strncpy(T, sx, NO_SQUARES);
     T[NO_SQUARES] = '\0';
 
-    /* if line empty, ask for input */
-    if (!T[0])
-    {
-        fputs("Enter #moves #minutes: ", stdout);
-        fgets(T, NO_SQUARES + 1, stdin);
-    }
-
-    /* skip blackspace */
-    for (p = T; *p == ' '; p++) ;
 
-    /* could be moves or a fischer clock */
-    if (*p == 'f')
-    {
-        /* its a fischer clock game */
-        char *q;
-        p++;
-        TCminutes = (short)strtol(p, &q, 10);
-        TCadd = (short)strtol(q, NULL, 10) *100;
-        TCseconds = 0;
-        TCmoves = 50;
-    }
-    else
-    {
-        /* regular game */
-        char *q;
-        TCadd = 0;
-        TCmoves = (short)strtol(p, &q, 10);
-        TCminutes = (short)strtol(q, &q, 10);
-
-        if (*q == ':')
-            TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
-        else
-            TCseconds = 0;
-
-#ifdef OPERATORTIME
-        fputs("Operator time (hundredths) = ", stdout);
-        scanf("%hd", &OperatorTime);
-#endif
-
-        if (TCmoves == 0)
+    if (!xboard) {
+        /* if line empty, ask for input */
+        if (!T[0])
         {
-            TCflag = false;
-            MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
-            TCminutes = TCseconds = 0;
+            fputs("Enter #moves #minutes: ", stdout);
+            fgets(T, NO_SQUARES + 1, stdin);
+        }
+    
+        /* skip blackspace */
+        for (p = T; *p == ' '; p++) ;
+    
+        /* could be moves or a fischer clock */
+        if (*p == 'f')
+        {
+            /* its a fischer clock game */
+            char *q;
+            p++;
+            TCminutes = (short)strtol(p, &q, 10);
+            TCadd = (short)strtol(q, NULL, 10) *100;
+            TCseconds = 0;
+            TCmoves = 50;
         }
         else
         {
-            TCflag = true;
-            MaxResponseTime = 0;
+            /* regular game */
+            char *q;
+            TCadd = 0;
+            TCmoves = (short)strtol(p, &q, 10);
+            TCminutes = (short)strtol(q, &q, 10);
+    
+            if (*q == ':')
+                TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
+            else
+                TCseconds = 0;
+    
+    #ifdef OPERATORTIME
+            fputs("Operator time (hundredths) = ", stdout);
+            scanf("%hd", &OperatorTime);
+    #endif
+    
+            if (TCmoves == 0)
+            {
+                TCflag = false;
+                MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
+                TCminutes = TCseconds = 0;
+            }
+            else
+            {
+                TCflag = true;
+                MaxResponseTime = 0;
+            }
         }
+    } else {
+        int min, sec=0, inc, mps;
+        /* parse regular "level MPS TC INC" command of WB protocol */
+        if (sscanf(sx, "%d %d %d", &mps, &min, &inc) != 3)
+            sscanf(sx, "%d %d:%d %d", &mps, &min, &sec, &inc);
+        TCminutes = min; TCseconds = sec;
+        TCadd = inc*100; TCmoves = mps ? mps : 50;
+        MaxResponseTime = 0; TCflag = true;
     }
 
     TimeControl.clock[black] = TimeControl.clock[white] = 0;