src/chess/pgn.c

changeset 126
d58b2abdd330
parent 121
53f714ac783d
--- a/src/chess/pgn.c	Wed May 20 15:52:17 2026 +0200
+++ b/src/chess/pgn.c	Fri May 22 18:34:02 2026 +0200
@@ -41,7 +41,8 @@
     pgn_error_missing_brace,
     pgn_error_missing_dot,
     pgn_error_move_syntax,
-    pgn_error_move_semantics
+    pgn_error_move_semantics,
+    pgn_error_result_syntax,
 };
 
 static const char* pgn_error_strings[] = {
@@ -51,7 +52,8 @@
     "Tags must be enclosed in square brackets: '[Key \"Value\"]'.",
     "Move numbers must be terminated with a dot (e.g. '13.' - not '13').",
     "Move is syntactically incorrect.",
-    "Move is not valid according to chess rules."
+    "Move is not valid according to chess rules.",
+    "Result syntax is incorrect. Expected 1-0, 0-1, 1/2-1/2, or *.",
 };
 
 const char* pgn_error_str(int code) {
@@ -97,7 +99,9 @@
             return pgn_error_missing_bracket;
         }
 
+        // TODO: read clock info
         if (strcmp("Result", tagkey) == 0) {
+            // TODO: why are we parsing this? it is never used!
             memcpy(result, tagvalue, 8);
         }
     }
@@ -153,8 +157,20 @@
         }
         if (c == '1' || c == '0') {
             c = fgetc(stream);
+            // TODO: #842 - allow games to be continued if possible
             if (c == '-') {
-                gamestate->resign = !gamestate->checkmate;
+                if (!gamestate->checkmate) {
+                    // TODO: maybe we should not parse this here;
+                    //       there is an unused result string from the STR
+                    c = fgetc(stream);
+                    if (c == '1') {
+                        gamestate->wresign = true;
+                    } else if (c == '0') {
+                        gamestate->bresign = true;
+                    } else {
+                        return pgn_error_result_syntax;
+                    }
+                }
                 break;
             } else if (c == '/') {
                 gamestate->remis = !gamestate->stalemate;
@@ -248,7 +264,11 @@
     char *result;
     if (gamestate->stalemate || gamestate->remis) {
         result = "1/2-1/2";
-    } else if (gamestate->checkmate || gamestate->resign) {
+    } else if (gamestate->wresign) {
+        result = "0-1";
+    } else if (gamestate->bresign) {
+        result = "1-0";
+    } else if (gamestate->checkmate) {
         if (gamestate->movecount > 0) {
             result = (last_move(gamestate).piece & COLOR_MASK) == WHITE ?
                 "1-0" : "0-1";
@@ -259,6 +279,8 @@
         result = "*";
     }
     fprintf(stream, "[Result \"%s\"]\n\n", result);
+
+    // TODO: add optional clock info
     
     /* moves */
     size_t moveblkcap = 4096;

mercurial