fixes castling not printed correctly to PGN

Wed, 29 Aug 2018 13:55:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 29 Aug 2018 13:55:18 +0200
changeset 64
4eda5df55f86
parent 63
611332453da0
child 65
dcc5bd2c56c0

fixes castling not printed correctly to PGN

src/chess/king.c file | annotate | diff | comparison | revisions
src/chess/pgn.c file | annotate | diff | comparison | revisions
src/chess/rules.c file | annotate | diff | comparison | revisions
src/client.c file | annotate | diff | comparison | revisions
src/server.c file | annotate | diff | comparison | revisions
--- a/src/chess/king.c	Wed Aug 29 13:45:13 2018 +0200
+++ b/src/chess/king.c	Wed Aug 29 13:55:18 2018 +0200
@@ -70,10 +70,10 @@
     
     uint8_t opponent_color = opponent_color(move->piece&COLOR_MASK);
     
-    // being in check does not "block" the king, so don't test it here
+    /* being in check does not "block" the king, so don't test it here */
     _Bool blocked = 0;
     
-    // just test, if castling move is blocked
+    /* just test, if castling move is blocked */
     if (abs(move->tofile - move->fromfile) == 2) {
         if (move->tofile == fileidx('c')) {
             blocked |= gamestate->board[move->torow][fileidx('b')];
--- a/src/chess/pgn.c	Wed Aug 29 13:45:13 2018 +0200
+++ b/src/chess/pgn.c	Wed Aug 29 13:55:18 2018 +0200
@@ -61,7 +61,7 @@
     char tagkey[32];
     char tagvalue[128];
     
-    // read tag pairs
+    /* read tag pairs */
     _Bool readmoves = 0;
     while (!readmoves) {
         while (isspace(c = fgetc(stream)));
@@ -99,7 +99,7 @@
         }
     }
     
-    // read moves
+    /* read moves */
     if (fgetc(stream) != '.') {
         return pgn_error_missing_dot;
     }
@@ -109,7 +109,7 @@
     uint8_t curcol = WHITE;
     
     while (readmoves) {
-        // move
+        /* move */
         while (isspace(c = fgetc(stream)));
         i = 0;
         do {
@@ -131,7 +131,7 @@
         // TODO: parse comments
         while (isspace(c = fgetc(stream)));
         
-        // end of game data encountered
+        /* end of game data encountered */
         if (c == EOF) {
             break;
         }
@@ -144,15 +144,15 @@
                 gamestate->remis = !gamestate->stalemate;
                 break;
             } else {
-                // oops, it was a move number, go back!
+                /* oops, it was a move number, go back! */
                 fseek(stream, -1, SEEK_CUR);
             }
         }
         
-        // we have eaten the next valuable byte, so go back
+        /* we have eaten the next valuable byte, so go back */
         fseek(stream, -1, SEEK_CUR);
         
-        // skip move number after black move
+        /* skip move number after black move */
         if (curcol == BLACK) {
             while (isdigit(c = fgetc(stream)));
             if (c != '.') {
@@ -169,7 +169,7 @@
     // TODO: tag pairs
     size_t bytes = 0;
     
-    // Result
+    /* Result */
     char *result;
     if (gamestate->stalemate || gamestate->remis) {
         result = "1/2-1/2";
@@ -185,7 +185,7 @@
     }
     fprintf(stream, "[Result \"%s\"]\n\n", result);
     
-    // moves
+    /* moves */
     int i = 1;
     for (MoveList *movelist = gamestate->movelist ;
         movelist ; movelist = movelist->next) {
@@ -198,7 +198,7 @@
         
         // TODO: move time and maybe other comments
         
-        // line break every 10 moves
+        /* line break every 10 moves */
         if (i % 20)  {
             fputc(' ', stream);
         } else {
--- a/src/chess/rules.c	Wed Aug 29 13:45:13 2018 +0200
+++ b/src/chess/rules.c	Wed Aug 29 13:55:18 2018 +0200
@@ -77,65 +77,69 @@
     /* at least 8 characters should be available, wipe them out */
     memset(string, 0, 8);
     
-    /* special formats for castling */
+    unsigned int idx;
     if ((move->piece&PIECE_MASK) == KING &&
             abs(move->tofile-move->fromfile) == 2) {
+        /* special formats for castling */
         if (move->tofile==fileidx('c')) {
             memcpy(string, "O-O-O", 5);
+            idx = 5;
         } else {
             memcpy(string, "O-O", 3);
-        }
-    }
-
-    /* start by notating the piece character */
-    string[0] = getpiecechr(move->piece);
-    int idx = string[0] ? 1 : 0;
-    
-    /* find out how many source information we do need */
-    uint8_t piece = move->piece & PIECE_MASK;
-    if (piece == PAWN) {
-        if (move->capture) {
-            string[idx++] = filechr(move->fromfile);
+            idx = 3;
         }
-    } else if (piece != KING) {
-        Move threats[16];
-        uint8_t threatcount;
-        get_real_threats(gamestate, move->torow, move->tofile,
-            move->piece&COLOR_MASK, threats, &threatcount);
-        if (threatcount > 1) {
-            int ambrows = 0, ambfiles = 0;
-            for (uint8_t i = 0 ; i < threatcount ; i++) {
-                if (threats[i].fromrow == move->fromrow) {
-                    ambrows++;
+    } else {
+        /* start by notating the piece character */
+        string[0] = getpiecechr(move->piece);
+        idx = string[0] ? 1 : 0;
+
+        /* find out how many source information we do need */
+        uint8_t piece = move->piece & PIECE_MASK;
+        if (piece == PAWN) {
+            if (move->capture) {
+                string[idx++] = filechr(move->fromfile);
+            }
+        } else if (piece != KING) {
+            /* resolve ambiguities, if any */
+            Move threats[16];
+            uint8_t threatcount;
+            get_real_threats(gamestate, move->torow, move->tofile,
+                move->piece&COLOR_MASK, threats, &threatcount);
+            if (threatcount > 1) {
+                int ambrows = 0, ambfiles = 0;
+                for (uint8_t i = 0 ; i < threatcount ; i++) {
+                    if (threats[i].fromrow == move->fromrow) {
+                        ambrows++;
+                    }
+                    if (threats[i].fromfile == move->fromfile) {
+                        ambfiles++;
+                    }
                 }
-                if (threats[i].fromfile == move->fromfile) {
-                    ambfiles++;
+                /* ambiguous row, name file */
+                if (ambrows > 1) {
+                    string[idx++] = filechr(move->fromfile);
+                }
+                /* ambiguous file, name row */
+                if (ambfiles > 1) {
+                    string[idx++] = filechr(move->fromrow);
                 }
             }
-            /* ambiguous row, name file */
-            if (ambrows > 1) {
-                string[idx++] = filechr(move->fromfile);
-            }
-            /* ambiguous file, name row */
-            if (ambfiles > 1) {
-                string[idx++] = filechr(move->fromrow);
-            }
+        }
+
+        /* capturing? */
+        if (move->capture) {
+            string[idx++] = 'x';
         }
-    }
-    
-    /* capturing? */
-    if (move->capture) {
-        string[idx++] = 'x';
-    }
-    
-    /* destination */
-    string[idx++] = filechr(move->tofile);
-    string[idx++] = rowchr(move->torow);
-    
-    /* promotion? */
-    if (move->promotion) {
-        string[idx++] = '=';
-        string[idx++] = getpiecechr(move->promotion);
+
+        /* destination */
+        string[idx++] = filechr(move->tofile);
+        string[idx++] = rowchr(move->torow);
+
+        /* promotion? */
+        if (move->promotion) {
+            string[idx++] = '=';
+            string[idx++] = getpiecechr(move->promotion);
+        }
     }
     
     /* check? */
@@ -456,7 +460,7 @@
     for (uint8_t r = 0 ; r < 8 ; r++) {
         for (uint8_t f = 0 ; f < 8 ; f++) {
             if ((gamestate->board[r][f] & COLOR_MASK) == color) {
-                // non-capturing move
+                /* non-capturing move */
                 memset(&(candidates[candidatecount]), 0, sizeof(Move));
                 candidates[candidatecount].piece = gamestate->board[r][f];
                 candidates[candidatecount].fromrow = r;
@@ -465,7 +469,7 @@
                 candidates[candidatecount].tofile = file;
                 candidatecount++;
 
-                // capturing move
+                /* capturing move */
                 memcpy(&(candidates[candidatecount]),
                     &(candidates[candidatecount-1]), sizeof(Move));
                 candidates[candidatecount].capture = 1;
@@ -572,7 +576,7 @@
         
         int reason = INVALID_POSITION;
         
-        // find threats for the specified position
+        /* find threats for the specified position */
         for (uint8_t i = 0 ; i < threatcount ; i++) {            
             if ((threats[i].piece & (PIECE_MASK | COLOR_MASK))
                     == move->piece &&
@@ -584,7 +588,7 @@
                 if (threat) {
                     return AMBIGUOUS_MOVE;
                 } else {
-                    // found threat is no real threat
+                    /* found threat is no real threat */
                     if (is_pinned(gamestate, &(threats[i]))) {
                         reason = incheck?KING_IN_CHECK:PIECE_PINNED;
                     } else {
@@ -594,7 +598,7 @@
             }
         }
         
-        // can't threaten specified position
+        /* can't threaten specified position */
         if (!threat) {
             return reason;
         }
--- a/src/client.c	Wed Aug 29 13:45:13 2018 +0200
+++ b/src/client.c	Wed Aug 29 13:55:18 2018 +0200
@@ -75,7 +75,7 @@
 
     uint8_t code = net_recieve_code(server.fd);
     if (code == NETCODE_GAMEINFO) {
-        // Start new game
+        /* Start new game */
         net_recieve_data(server.fd, &(settings->gameinfo), sizeof(GameInfo));
         dump_gameinfo(&(settings->gameinfo));
         if (prompt_yesno("Accept challenge")) {
--- a/src/server.c	Wed Aug 29 13:45:13 2018 +0200
+++ b/src/server.c	Wed Aug 29 13:55:18 2018 +0200
@@ -69,7 +69,7 @@
     GameState continuegame;
     gamestate_init(&continuegame);
     if (settings->continuepgn) {
-        // preload PGN data before handshake
+        /* preload PGN data before handshake */
         FILE *pgnfile = fopen(settings->continuepgn, "r");
         if (pgnfile) {
             int result = read_pgn(pgnfile, &continuegame,
@@ -106,7 +106,7 @@
 
     int fd = server.client->fd;
     if (settings->continuepgn) {
-        // Continue game, send PGN data
+        /* Continue game, send PGN data */
         uint16_t mc = 0;
         MoveList *movelist = continuegame.movelist;
         while (movelist) {
@@ -133,7 +133,7 @@
         net_send_data(fd, NETCODE_PGNDATA, pgndata, pgndata_size);
         free(pgndata);
     } else {
-        // Start new game
+        /* Start new game */
         net_send_data(fd, NETCODE_GAMEINFO,
             &(settings->gameinfo), sizeof(GameInfo));
     }

mercurial