src/chess/pgn.c

changeset 132
5762f2b5f87a
parent 130
3fc6b1d6cbe9
--- a/src/chess/pgn.c	Sun Jun 07 14:55:20 2026 +0200
+++ b/src/chess/pgn.c	Sun Jun 07 16:56:22 2026 +0200
@@ -362,141 +362,3 @@
 
     free(moveblk);
 }
-
-static size_t fen_pieces(char *str, GameState *gamestate) {
-    size_t i = 0;
-    for (int row = 7 ; row >= 0 ; row--) {
-        unsigned int skip = 0;
-        for (int file = 0 ; file < 8 ; file++) {
-            if (gamestate->board[row][file]) {
-                if (skip > 0) {
-                    str[i++] = '0'+skip;
-                    skip = 0;
-                }
-                switch (gamestate->board[row][file] & ~ENPASSANT_THREAT) {
-                case WHITE|KING: str[i++] = 'K'; break;
-                case WHITE|QUEEN: str[i++] = 'Q'; break;
-                case WHITE|BISHOP: str[i++] = 'B'; break;
-                case WHITE|KNIGHT: str[i++] = 'N'; break;
-                case WHITE|ROOK: str[i++] = 'R'; break;
-                case WHITE|PAWN: str[i++] = 'P'; break;
-                case BLACK|KING: str[i++] = 'k'; break;
-                case BLACK|QUEEN: str[i++] = 'q'; break;
-                case BLACK|BISHOP: str[i++] = 'b'; break;
-                case BLACK|KNIGHT: str[i++] = 'n'; break;
-                case BLACK|ROOK: str[i++] = 'r'; break;
-                case BLACK|PAWN: str[i++] = 'p'; break;
-                }
-            } else {
-                skip++;
-            }
-        }
-        if (skip > 0) {
-            str[i++] = '0'+skip;
-        }
-        if (row > 0) {
-            str[i++] = '/';
-        }
-    }
-    
-    return i;
-}
-
-static size_t fen_color(char *str, GameState *gamestate) {
-    uint8_t color = opponent_color(gamestate->movecount > 0 ?
-        (last_move(gamestate).piece & COLOR_MASK) : BLACK);
-    
-    str[0] = color == WHITE ? 'w' : 'b';
-    
-    return 1;
-}
-
-static bool fen_castling_chkmoved(GameState *gamestate,
-    uint8_t row, uint8_t file) {
-
-    for (unsigned i = 0 ; i < gamestate->movecount ; i++) {
-        if (gamestate->moves[i].fromfile == file
-            && gamestate->moves[i].fromrow == row) {
-            return true;
-        }
-    }
-    
-    return false;
-}
-
-static size_t fen_castling(char *str, GameState *gamestate) {
-    bool K, Q, k, q;
-    
-    if (fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('e'))) {
-        K = Q = false;
-    } else {
-        K = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('h'));
-        Q = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('a'));
-    }
-    if (fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('e'))) {
-        k = q = false;
-    } else {
-        k = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('h'));
-        q = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('a'));
-    }
-    
-    size_t i = 0;
-    if (K) str[i++] = 'K';
-    if (Q) str[i++] = 'Q';
-    if (k) str[i++] = 'k';
-    if (q) str[i++] = 'q';
-    if (!i) str[i++] = '-';
-    
-    return i;
-}
-
-static size_t fen_enpassant(char *str, GameState *gamestate) {
-    
-    str[0] = '-'; str[1] = '\0';
-    
-    for (int file = 0 ; file < 8 ; file++) {
-        if (gamestate->board[3][file] & ENPASSANT_THREAT) {
-            str[0] = filechr(file);
-            str[1] = rowchr(2);
-        }
-        if (gamestate->board[4][file] & ENPASSANT_THREAT) {
-            str[0] = filechr(file);
-            str[1] = rowchr(5);
-        }
-    }
-    
-    return str[0] == '-' ? 1 : 2;
-}
-
-static size_t fen_halfmove(char *str, GameState *gamestate) {
-    unsigned int hm = 0;
-    for (unsigned int i = 0; i < gamestate->movecount; i++) {
-        if (gamestate->moves[i].capture
-            || (gamestate->moves[i].piece & PIECE_MASK) == PAWN) {
-            hm = 0;
-        } else {
-            hm++;
-        }
-    }
-
-    return sprintf(str, "%u", hm);
-}
-
-static size_t fen_movenr(char *str, GameState *gamestate) {
-    return sprintf(str, "%u", gamestate->movecount);
-}
-
-void compute_fen(char *str, GameState *gamestate) {
-    str += fen_pieces(str, gamestate);
-    *str = ' '; str++;
-    str += fen_color(str, gamestate);
-    *str = ' '; str++;
-    str += fen_castling(str, gamestate);
-    *str = ' '; str++;
-    str += fen_enpassant(str, gamestate);
-    *str = ' '; str++;
-    str += fen_halfmove(str, gamestate);
-    *str = ' '; str++;
-    str += fen_movenr(str, gamestate);
-    str[0] = '\0';
-}

mercurial