src/chess/fen.c

changeset 216
d0c3d3016650
parent 215
d20887f0e8b3
equal deleted inserted replaced
215:d20887f0e8b3 216:d0c3d3016650
75 static size_t fen_color(char *str, GameState *gamestate) { 75 static size_t fen_color(char *str, GameState *gamestate) {
76 str[0] = current_color(gamestate) == WHITE ? 'w' : 'b'; 76 str[0] = current_color(gamestate) == WHITE ? 'w' : 'b';
77 return 1; 77 return 1;
78 } 78 }
79 79
80 static bool fen_castling_chkmoved(GameState *gamestate,
81 Rank rank, File file) {
82
83 for (unsigned i = 0 ; i < gamestate->movecount ; i++) {
84 if (gamestate->moves[i].fromfile == file
85 && gamestate->moves[i].fromrank == rank) {
86 return true;
87 }
88 }
89
90 return false;
91 }
92
93 static size_t fen_castling(char *str, GameState *gamestate) { 80 static size_t fen_castling(char *str, GameState *gamestate) {
94 bool K, Q, k, q;
95
96 if (fen_castling_chkmoved(gamestate, rankidx('1'), fileidx('e'))) {
97 K = Q = false;
98 } else {
99 K = !fen_castling_chkmoved(gamestate, rankidx('1'), fileidx('h'));
100 Q = !fen_castling_chkmoved(gamestate, rankidx('1'), fileidx('a'));
101 }
102 if (fen_castling_chkmoved(gamestate, rankidx('8'), fileidx('e'))) {
103 k = q = false;
104 } else {
105 k = !fen_castling_chkmoved(gamestate, rankidx('8'), fileidx('h'));
106 q = !fen_castling_chkmoved(gamestate, rankidx('8'), fileidx('a'));
107 }
108
109 size_t i = 0; 81 size_t i = 0;
110 if (K) str[i++] = 'K'; 82 if (!gamestate->castling.K) str[i++] = 'K';
111 if (Q) str[i++] = 'Q'; 83 if (!gamestate->castling.Q) str[i++] = 'Q';
112 if (k) str[i++] = 'k'; 84 if (!gamestate->castling.k) str[i++] = 'k';
113 if (q) str[i++] = 'q'; 85 if (!gamestate->castling.q) str[i++] = 'q';
114 if (!i) str[i++] = '-'; 86 if (!i) str[i++] = '-';
115 87
116 return i; 88 return i;
117 } 89 }
118 90

mercurial