20 // const char * StartFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w HAha - 0 1";
21 const char * StartFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
25 static const bool Strict = FALSE;
29 #define skip_white_space() \
31 if (c != ' ' && c!='\t') my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos); \
32 while(c==' ' || c=='\t') c=string[++pos];
39 bool board_from_fen(board_t * board, const char string[]) {
46 int king_pos[ColourNb];
53 king_pos[White] = SquareNone;
54 king_pos[Black] = SquareNone;
61 for (rank = 7; rank >= 0; rank--) {
63 for (file = 0; file < 8;) {
65 sq = square_make(file,rank);
67 if (c >= '1' && c <= '8') { // empty square(s)
70 if (file + len > 8) my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos);
72 for (i = 0; i < len; i++) {
73 board->square[sq++] = Empty;
79 piece = piece_from_char(c);
80 if (piece == PieceNone256) my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos);
82 if (piece_is_king(piece)) king_pos[piece_colour(piece)] = sq;
84 board->square[sq++] = piece;
92 if (c != '/') my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos);
109 my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos);
119 board->castle[White][SideH] = SquareNone;
120 board->castle[White][SideA] = SquareNone;
121 board->castle[Black][SideH] = SquareNone;
122 board->castle[Black][SideA] = SquareNone;
124 if (c == '-') { // no castling rights
130 // TODO: filter out illegal rights
136 } else if (c == 'K') {
138 for (sq = H1; sq > king_pos[White]; sq--) {
139 if (board->square[sq] == WhiteRook256) {
140 board->castle[White][SideH] = sq;
145 } else if (c == 'Q') {
147 for (sq = A1; sq < king_pos[White]; sq++) {
148 if (board->square[sq] == WhiteRook256) {
149 board->castle[White][SideA] = sq;
154 } else if (c == 'k') {
156 for (sq = H8; sq > king_pos[Black]; sq--) {
157 if (board->square[sq] == BlackRook256) {
158 board->castle[Black][SideH] = sq;
163 } else if (c == 'q') {
165 for (sq = A8; sq < king_pos[Black]; sq++) {
166 if (board->square[sq] == BlackRook256) {
167 board->castle[Black][SideA] = sq;
172 } else if (c >= 'A' && c <= 'H') {
174 // white castling right
176 sq = square_make(file_from_char(tolower(c)),Rank1);
178 if (sq > king_pos[White]) { // h side
179 board->castle[White][SideH] = sq;
181 board->castle[White][SideA] = sq;
184 } else if (c >= 'a' && c <= 'h') {
186 // black castling right
188 sq = square_make(file_from_char(tolower(c)),Rank8);
190 if (sq > king_pos[Black]) { // h side
191 board->castle[Black][SideH] = sq;
193 board->castle[Black][SideA] = sq;
198 my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos);
210 if (c == '-') { // no en-passant
217 if (c < 'a' || c > 'h') my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos);
218 file = file_from_char(c);
221 if (c < '1' || c > '8') my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos);
222 rank = rank_from_char(c);
225 sq = square_make(file,rank);
228 board->ep_square = sq;
233 board->move_nb = 0; // HACK, in case of broken syntax
236 if (!Strict) goto update;
237 my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos);
242 if (!Strict) goto update;
243 my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos);
246 board->ply_nb = atoi(&string[pos]);
247 do c = string[++pos]; while (isdigit(c));
254 if (!Strict) goto update;
255 my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos);
260 if (!Strict) goto update;
261 my_fatal("board_from_fen(): bad FEN (pos=%d)\n",pos);
264 board->move_nb = atoi(&string[pos]) - 1;
265 do c = string[++pos]; while (isdigit(c));
270 board_init_list(board);
277 bool board_to_fen(const board_t * board, char string[], int size) {
286 ASSERT(board_is_ok(board));
287 ASSERT(string!=NULL);
292 if (size < 92) return FALSE;
298 for (rank = 7; rank >= 0; rank--) {
300 for (file = 0; file < 8;) {
302 sq = square_make(file,rank);
303 piece = board->square[sq];
304 ASSERT(piece==Empty||piece_is_ok(piece));
306 if (piece == Empty) {
309 for (; file < 8 && board->square[square_make(file,rank)] == Empty; file++) {
313 ASSERT(len>=1&&len<=8);
318 c = piece_to_char(piece);
328 string[pos-1] = ' '; // HACK: remove the last '/'
332 string[pos++] = (colour_is_white(board->turn)) ? 'w' : 'b';
339 if (option_get_bool(Option,"Chess960")) {
343 if (board->castle[White][SideH] != SquareNone) {
344 string[pos++] = toupper(file_to_char(square_file(board->castle[White][SideH])));
347 if (board->castle[White][SideA] != SquareNone) {
348 string[pos++] = toupper(file_to_char(square_file(board->castle[White][SideA])));
351 if (board->castle[Black][SideH] != SquareNone) {
352 string[pos++] = tolower(file_to_char(square_file(board->castle[Black][SideH])));
355 if (board->castle[Black][SideA] != SquareNone) {
356 string[pos++] = tolower(file_to_char(square_file(board->castle[Black][SideA])));
363 if (board->castle[White][SideH] != SquareNone) string[pos++] = 'K';
364 if (board->castle[White][SideA] != SquareNone) string[pos++] = 'Q';
365 if (board->castle[Black][SideH] != SquareNone) string[pos++] = 'k';
366 if (board->castle[Black][SideA] != SquareNone) string[pos++] = 'q';
369 if (pos == old_pos) string[pos++] = '-';
375 if (board->ep_square == SquareNone) {
378 if (!square_to_string(board->ep_square,&string[pos],3)) return FALSE;
384 // halfmove clock and fullmove number
386 sprintf(&string[pos],"%d %d",board->ply_nb,board->move_nb+1);