static char* FEN_print_board(const struct position *pos, char *str, enum player turn) { int empty_count; for (int rank = rank_8; ; rank += RSOUTH) { empty_count = 0; for (int file = file_a; is_valid_file(file); file += EAST) { int i = ind(rank, file); if (turn == black) i = flip_i(i); enum piece p = position_piece_at(pos, i); enum player player = position_player_at(pos, i); if (p != nonpiece) { if (empty_count > 0) { *str++ = '0' + (char)empty_count; empty_count = 0; } if (turn == black) player = opponent_of(player); *str++ = square_to_char(p, player); } else { ++empty_count; } } if (empty_count > 0) *str++ = '0' + empty_count; if (rank == rank_1) return str; else *str++ = '/'; } }
static void test_coordinates(void) { assert(is_coordinate("g6")); assert(!is_coordinate("g9")); assert(!is_coordinate("g0")); assert(!is_coordinate("6")); assert(is_coordinate("g6 lorem ipsum")); assert(!is_coordinate("lorem ipsum")); assert(str_to_index("g6", white) == ind(rank_6, file_g)); assert(str_to_index("g6", black) == flip_i(ind(rank_6, file_g))); }