src/chess/fen.c

changeset 194
619f07c95894
parent 163
2a6d83f4677e
child 195
27d02ccb0cef
--- a/src/chess/fen.c	Tue Aug 25 18:35:18 2026 +0200
+++ b/src/chess/fen.c	Tue Aug 25 18:59:18 2026 +0200
@@ -34,15 +34,16 @@
 
 static size_t fen_pieces(char *str, GameState *gamestate) {
     size_t i = 0;
-    for (int row = 7 ; row >= 0 ; row--) {
+    Rank rank = 7;
+    do {
         unsigned int skip = 0;
-        for (int file = 0 ; file < 8 ; file++) {
-            if (gamestate->board[row][file]) {
+        for (File file = 0 ; file < 8 ; file++) {
+            if (gamestate->board[rank][file]) {
                 if (skip > 0) {
                     str[i++] = '0'+skip;
                     skip = 0;
                 }
-                switch (piece_at(gamestate, row, file)) {
+                switch (piece_at(gamestate, rank, file)) {
                 case WHITE|KING: str[i++] = 'K'; break;
                 case WHITE|QUEEN: str[i++] = 'Q'; break;
                 case WHITE|BISHOP: str[i++] = 'B'; break;
@@ -63,10 +64,10 @@
         if (skip > 0) {
             str[i++] = '0'+skip;
         }
-        if (row > 0) {
+        if (rank > 0) {
             str[i++] = '/';
         }
-    }
+    } while (rank-- > 0);
 
     return i;
 }
@@ -77,11 +78,11 @@
 }
 
 static bool fen_castling_chkmoved(GameState *gamestate,
-    uint8_t row, uint8_t file) {
+    Rank rank, File file) {
 
     for (unsigned i = 0 ; i < gamestate->movecount ; i++) {
         if (gamestate->moves[i].fromfile == file
-            && gamestate->moves[i].fromrow == row) {
+            && gamestate->moves[i].fromrank == rank) {
             return true;
         }
     }
@@ -92,17 +93,17 @@
 static size_t fen_castling(char *str, GameState *gamestate) {
     bool K, Q, k, q;
 
-    if (fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('e'))) {
+    if (fen_castling_chkmoved(gamestate, rankidx('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'));
+        K = !fen_castling_chkmoved(gamestate, rankidx('1'), fileidx('h'));
+        Q = !fen_castling_chkmoved(gamestate, rankidx('1'), fileidx('a'));
     }
-    if (fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('e'))) {
+    if (fen_castling_chkmoved(gamestate, rankidx('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'));
+        k = !fen_castling_chkmoved(gamestate, rankidx('8'), fileidx('h'));
+        q = !fen_castling_chkmoved(gamestate, rankidx('8'), fileidx('a'));
     }
 
     size_t i = 0;
@@ -122,11 +123,11 @@
     for (int file = 0 ; file < 8 ; file++) {
         if (enpassant_threat_exists(gamestate, 3, file)) {
             str[0] = filechr(file);
-            str[1] = rowchr(2);
+            str[1] = rankchr(2);
         }
         if (enpassant_threat_exists(gamestate, 4, file)) {
             str[0] = filechr(file);
-            str[1] = rowchr(5);
+            str[1] = rankchr(5);
         }
     }
 

mercurial