src/chess/pgn.c

changeset 54
eef745ba3774
parent 50
41017d0a72c5
child 55
54ea19938d57
--- a/src/chess/pgn.c	Wed Aug 26 12:59:30 2015 +0200
+++ b/src/chess/pgn.c	Wed Aug 26 14:46:42 2015 +0200
@@ -194,3 +194,158 @@
     
     return bytes;
 }
+
+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->lastmove ?
+        (gamestate->lastmove->move.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) {
+    
+    MoveList *ml = gamestate->movelist;
+    while (ml) {
+        if (ml->move.fromfile == file && ml->move.fromrow == row) {
+            return 1;
+        }
+        ml = ml->next;
+    }
+    
+    return 0;
+}
+
+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 = 0;
+    } 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 = 0;
+    } 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 i = 0;
+    for (MoveList *move = gamestate->movelist ; move ; move = move->next) {
+        if (move->move.capture || (move->move.piece & PIECE_MASK) == PAWN) {
+            i = 0;
+        } else {
+            i++;
+        }
+    }
+    
+    char m[8];
+    size_t len = sprintf(m, "%u", i);
+    memcpy(str, m, len);
+    
+    return len;
+}
+
+static size_t fen_movenr(char *str, GameState *gamestate) {
+    
+    MoveList *move = gamestate->movelist;
+    unsigned int i = 1;
+    while (move) {
+        i++;
+        move = move->next;
+    }
+    
+    char m[8];
+    size_t len = sprintf(m, "%u", i);
+    memcpy(str, m, len);
+    
+    return len;
+}
+
+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