implemented FEN

Wed, 26 Aug 2015 14:46:42 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 26 Aug 2015 14:46:42 +0200
changeset 54
eef745ba3774
parent 53
78b580bfde33
child 55
54ea19938d57

implemented FEN

src/chess/pgn.c file | annotate | diff | comparison | revisions
src/chess/pgn.h file | annotate | diff | comparison | revisions
src/game.c file | annotate | diff | comparison | revisions
--- 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';
+}
--- a/src/chess/pgn.h	Wed Aug 26 12:59:30 2015 +0200
+++ b/src/chess/pgn.h	Wed Aug 26 14:46:42 2015 +0200
@@ -37,9 +37,10 @@
 #ifdef	__cplusplus
 extern "C" {
 #endif
-    
+
 int read_pgn(FILE *stream, GameState *gamestate, GameInfo *gameinfo);
 size_t write_pgn(FILE* stream, GameState *gamestate, GameInfo *gameinfo);
+void compute_fen(char *str, GameState *gamestate);
 
 #ifdef	__cplusplus
 }
--- a/src/game.c	Wed Aug 26 12:59:30 2015 +0200
+++ b/src/game.c	Wed Aug 26 14:46:42 2015 +0200
@@ -72,6 +72,10 @@
 }
 
 static void draw_board(GameState *gamestate, uint8_t perspective) {
+    char fen[90];
+    compute_fen(fen, gamestate);
+    mvaddstr(0, 0, fen);
+    
     for (uint8_t y = 0 ; y < 8 ; y++) {
         for (uint8_t x = 0 ; x < 8 ; x++) {
             uint8_t col = gamestate->board[y][x] & COLOR_MASK;
@@ -109,7 +113,7 @@
     
     /* move log */
     // TODO: introduce window to avoid bugs with a long move log
-    uint8_t logy = 0;
+    uint8_t logy = 1;
     const uint8_t logx = boardx + 30;
     int logi = 1;
     MoveList *logelem = gamestate->movelist;

mercurial