src/chess/fen.c

changeset 133
c58ae152733e
parent 132
5762f2b5f87a
child 134
ce2d285d2ccb
equal deleted inserted replaced
132:5762f2b5f87a 133:c58ae152733e
70 70
71 return i; 71 return i;
72 } 72 }
73 73
74 static size_t fen_color(char *str, GameState *gamestate) { 74 static size_t fen_color(char *str, GameState *gamestate) {
75 uint8_t color = opponent_color(gamestate->movecount > 0 ? 75 str[0] = current_color(gamestate) == WHITE ? 'w' : 'b';
76 (last_move(gamestate).piece & COLOR_MASK) : BLACK);
77
78 str[0] = color == WHITE ? 'w' : 'b';
79
80 return 1; 76 return 1;
81 } 77 }
82 78
83 static bool fen_castling_chkmoved(GameState *gamestate, 79 static bool fen_castling_chkmoved(GameState *gamestate,
84 uint8_t row, uint8_t file) { 80 uint8_t row, uint8_t file) {
119 return i; 115 return i;
120 } 116 }
121 117
122 static size_t fen_enpassant(char *str, GameState *gamestate) { 118 static size_t fen_enpassant(char *str, GameState *gamestate) {
123 119
124 str[0] = '-'; str[1] = '\0'; 120 str[0] = '-';
125 121
126 for (int file = 0 ; file < 8 ; file++) { 122 for (int file = 0 ; file < 8 ; file++) {
127 if (gamestate->board[3][file] & ENPASSANT_THREAT) { 123 if (gamestate->board[3][file] & ENPASSANT_THREAT) {
128 str[0] = filechr(file); 124 str[0] = filechr(file);
129 str[1] = rowchr(2); 125 str[1] = rowchr(2);
153 149
154 static size_t fen_movenr(char *str, GameState *gamestate) { 150 static size_t fen_movenr(char *str, GameState *gamestate) {
155 return sprintf(str, "%u", gamestate->movecount); 151 return sprintf(str, "%u", gamestate->movecount);
156 } 152 }
157 153
154 static size_t fen_space(char *str) {
155 *str = ' ';
156 return 1;
157 }
158
158 void fen_compute(char *str, GameState *gamestate) { 159 void fen_compute(char *str, GameState *gamestate) {
159 str += fen_pieces(str, gamestate); 160 str += fen_pieces(str, gamestate);
160 *str = ' '; str++; 161 str += fen_space(str);
161 str += fen_color(str, gamestate); 162 str += fen_color(str, gamestate);
162 *str = ' '; str++; 163 str += fen_space(str);
163 str += fen_castling(str, gamestate); 164 str += fen_castling(str, gamestate);
164 *str = ' '; str++; 165 str += fen_space(str);
165 str += fen_enpassant(str, gamestate); 166 str += fen_enpassant(str, gamestate);
166 *str = ' '; str++; 167 str += fen_space(str);
167 str += fen_halfmove(str, gamestate); 168 str += fen_halfmove(str, gamestate);
168 *str = ' '; str++; 169 str += fen_space(str);
169 str += fen_movenr(str, gamestate); 170 str += fen_movenr(str, gamestate);
170 str[0] = '\0'; 171 *str = '\0';
171 } 172 }

mercurial