static void test_chars(void) { assert(char_to_file('a') == file_a); assert(char_to_file('b') == file_b); assert(char_to_file('h') == file_h); assert(char_to_file('A') == file_a); assert(char_to_file('B') == file_b); assert(char_to_file('H') == file_h); assert(char_to_rank('1', white) == rank_1); assert(char_to_rank('2', white) == rank_2); assert(char_to_rank('3', white) == rank_3); assert(char_to_rank('4', white) == rank_4); assert(char_to_rank('5', white) == rank_5); assert(char_to_rank('8', white) == rank_8); assert(char_to_rank('1', black) == rank_8); assert(char_to_rank('2', black) == rank_7); assert(char_to_rank('3', black) == rank_6); assert(char_to_rank('4', black) == rank_5); assert(char_to_rank('5', black) == rank_4); assert(char_to_rank('8', black) == rank_1); assert(index_to_file_ch(0) == 'h'); assert(index_to_file_ch(1) == 'g'); assert(index_to_file_ch(7) == 'a'); assert(index_to_file_ch(63) == 'a'); assert(index_to_rank_ch(0, white) == '8'); assert(index_to_rank_ch(1, white) == '8'); assert(index_to_rank_ch(8 + 7, white) == '7'); assert(index_to_rank_ch(63, white) == '1'); assert(index_to_rank_ch(0, black) == '1'); assert(index_to_rank_ch(1, black) == '1'); assert(index_to_rank_ch(8 + 7, black) == '2'); assert(index_to_rank_ch(63, black) == '8'); assert(piece_to_char(queen) == 'q'); assert(square_to_char(queen, white) == 'Q'); assert(square_to_char(queen, black) == 'q'); assert(is_file('f')); assert(is_file('F')); assert(!is_file('4')); assert(!is_file('i')); assert(!is_file(' ')); assert(is_rank('1')); assert(is_rank('6')); assert(!is_rank('9')); assert(!is_rank('0')); assert(!is_rank('a')); assert(!is_rank(' ')); }
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++ = '/'; } }