void fen_book_get_move(const struct book *book, const struct position *position, size_t size, move m[size]) { char fen[FEN_BUFFER_LENGTH]; const char **entry; const char *str; m[0] = 0; if (book == NULL || position == NULL) { return; } (void) position_print_fen(position, fen, 0, white); entry = bsearch(fen, book->fen_book.entries, book->fen_book.count, sizeof book->fen_book.entries[0], cmp_entry_key); if (entry == NULL) { (void) position_print_fen(position, fen, 0, black); entry = bsearch(fen, book->fen_book.entries, book->fen_book.count, sizeof book->fen_book.entries[0], cmp_entry_key); if (entry == NULL) { return; } } str = position_read_fen(NULL, *entry, NULL, NULL); while ((size > 0) && ((str = next_token(str)) != NULL)) { fen_read_move(*entry, str, m); ++m; --size; } *m = 0; }
static void test_move_str(void) { struct position pos; enum player turn; int ep_index; char str[1024]; const char *end; move m; end = position_read_fen(&pos, positions[0].FEN, &ep_index, &turn); assert(end != NULL); assert(ep_index == 0); assert(turn == black); assert(end == (positions[0].FEN + strlen(positions[0].FEN))); end = position_print_fen(&pos, str, 0, black); assert(strcmp(str, positions[0].FEN) == 0); assert(end == (str + strlen(positions[0].FEN))); assert(read_move(&pos, "h8h7", &m, black) == 0); assert(m == create_move_g(sq_h1, sq_h2, king, 0, false)); print_move(&pos, m, str, mn_coordinate, black); assert(strcmp(str, "h8h7") == 0); assert(read_move(&pos, "h8g8", &m, black) == 0); assert(m == create_move_g(sq_h1, sq_g1, king, 0, false)); print_move(&pos, m, str, mn_coordinate, black); assert(strcmp(str, "h8g8") == 0); assert(read_move(&pos, "b5b4", &m, black) == 0); assert(m == create_move_g(sq_b4, sq_b5, pawn, 0, false)); print_move(&pos, m, str, mn_coordinate, black); assert(strcmp(str, "b5b4") == 0); assert(read_move(&pos, "b5a4", &m, black) == 0); assert(m == create_move_g(sq_b4, sq_a5, pawn, pawn, false)); print_move(&pos, m, str, mn_coordinate, black); assert(strcmp(str, "b5a4") == 0); assert(read_move(&pos, "b5c4", &m, black) != 0); assert(read_move(&pos, "Kh7", &m, black) == 0); assert(m == create_move_g(sq_h1, sq_h2, king, 0, false)); print_move(&pos, m, str, mn_san, black); assert(strcmp(str, "Kh7") == 0); assert(read_move(&pos, "Kg8", &m, black) == 0); assert(m == create_move_g(sq_h1, sq_g1, king, 0, false)); print_move(&pos, m, str, mn_san, black); assert(strcmp(str, "Kg8") == 0); assert(read_move(&pos, "b4", &m, black) == 0); assert(m == create_move_g(sq_b4, sq_b5, pawn, 0, false)); print_move(&pos, m, str, mn_san, black); assert(strcmp(str, "b4") == 0); assert(read_move(&pos, "bxa4", &m, black) == 0); assert(m == create_move_g(sq_b4, sq_a5, pawn, pawn, false)); print_move(&pos, m, str, mn_san, black); assert(strcmp(str, "bxa4") == 0); assert(read_move(&pos, "bxc4", &m, black) != 0); assert(read_move(&pos, "o-o", &m, black) != 0); assert(read_move(&pos, "o-o-o", &m, black) != 0); end = position_read_fen(&pos, positions[1].FEN, &ep_index, &turn); assert(end != NULL); assert(ep_index == 0); assert(turn == white); assert(end == (positions[1].FEN + strlen(positions[1].FEN))); end = position_print_fen(&pos, str, 0, white); assert(strcmp(str, positions[1].FEN) == 0); assert(end == (str + strlen(positions[1].FEN))); assert(read_move(&pos, "o-o", &m, white) != 0); assert(read_move(&pos, "o-o-o", &m, white) != 0); end = position_read_fen(&pos, positions[2].FEN, &ep_index, &turn); assert(end != NULL); assert(ep_index == sq_d5); assert(turn == white); assert(end == (positions[2].FEN + strlen(positions[2].FEN))); end = position_print_fen(&pos, str, sq_d5, white); assert(strcmp(str, positions[2].FEN) == 0); assert(end == (str + strlen(positions[2].FEN))); assert(read_move(&pos, "o-o", &m, white) == 0); assert(m == mcastle_king_side); assert(read_move(&pos, "O-O", &m, white) == 0); assert(m == mcastle_king_side); print_move(&pos, m, str, mn_coordinate, white); assert(strcmp(str, "e1g1") == 0); print_move(&pos, m, str, mn_san, white); assert(strcmp(str, "O-O") == 0); assert(read_move(&pos, "o-o-o", &m, white) != 0); assert(read_move(&pos, "e5d6", &m, white) == 0); assert(m == create_move_t(sq_e5, sq_d6, mt_en_passant, pawn, pawn, false)); print_move(&pos, m, str, mn_coordinate, white); assert(strcmp(str, "e5d6") == 0); assert(read_move(&pos, "exd6", &m, white) == 0); assert(m == create_move_t(sq_e5, sq_d6, mt_en_passant, pawn, pawn, false)); print_move(&pos, m, str, mn_san, white); assert(strcmp(str, "exd6e.p.") == 0); end = position_read_fen(&pos, positions[3].FEN, &ep_index, &turn); assert(end != NULL); assert(ep_index == 0); assert(turn == white); assert(end == (positions[3].FEN + strlen(positions[3].FEN))); end = position_print_fen(&pos, str, 0, white); assert(strcmp(str, positions[3].FEN) == 0); assert(end == (str + strlen(positions[3].FEN))); assert(read_move(&pos, "o-o", &m, white) == 0); assert(m == mcastle_king_side_check); assert(read_move(&pos, "O-O", &m, white) == 0); assert(m == mcastle_king_side_check); print_move(&pos, m, str, mn_coordinate, white); assert(strcmp(str, "e1g1") == 0); print_move(&pos, m, str, mn_san, white); assert(strcmp(str, "O-O") == 0); assert(read_move(&pos, "c1b2", &m, white) == 0); assert(m == create_move_g(sq_c1, sq_b2, bishop, 0, true)); assert(read_move(&pos, "Bb2", &m, white) == 0); assert(m == create_move_g(sq_c1, sq_b2, bishop, 0, true)); assert(read_move(&pos, "Bb2+", &m, white) == 0); assert(m == create_move_g(sq_c1, sq_b2, bishop, 0, true)); print_move(&pos, m, str, mn_san, white); assert(strcmp(str, "Bb2+") == 0); end = position_read_fen(&pos, positions[4].FEN, &ep_index, &turn); assert(end != NULL); assert(ep_index == 0); assert(turn == white); assert(end == (positions[4].FEN + strlen(positions[4].FEN))); end = position_print_fen(&pos, str, 0, white); assert(strcmp(str, positions[4].FEN) == 0); assert(end == (str + strlen(positions[4].FEN))); assert(read_move(&pos, "c7b8q", &m, white) == 0); assert(m == create_move_t(sq_c7, sq_b8, mt_promotion, queen, knight, false)); assert(read_move(&pos, "c7b8Q", &m, white) == 0); assert(m == create_move_t(sq_c7, sq_b8, mt_promotion, queen, knight, false)); assert(read_move(&pos, "c7b8n", &m, white) == 0); assert(m == create_move_t(sq_c7, sq_b8, mt_promotion, knight, knight, false)); assert(read_move(&pos, "c7b8r", &m, white) == 0); assert(m == create_move_t(sq_c7, sq_b8, mt_promotion, rook, knight, false)); assert(read_move(&pos, "c7b8b", &m, white) == 0); assert(m == create_move_t(sq_c7, sq_b8, mt_promotion, bishop, knight, false)); assert(read_move(&pos, "c7d8q", &m, white) == 0); assert(m == create_move_t(sq_c7, sq_d8, mt_promotion, queen, queen, true)); assert(read_move(&pos, "c7d8Q", &m, white) == 0); assert(m == create_move_t(sq_c7, sq_d8, mt_promotion, queen, queen, true)); assert(read_move(&pos, "cxd8=Q", &m, white) == 0); assert(m == create_move_t(sq_c7, sq_d8, mt_promotion, queen, queen, true)); assert(read_move(&pos, "cxd8=Q+", &m, white) == 0); assert(m == create_move_t(sq_c7, sq_d8, mt_promotion, queen, queen, true)); assert(read_move(&pos, "cxd8=R+", &m, white) == 0); assert(m == create_move_t(sq_c7, sq_d8, mt_promotion, rook, queen, true)); end = position_read_fen(&pos, "rnbqkbnr/ppppp2p/5p2/6p1/8/4P3/PPPP1PPP/RNBQKBNR w KQkq -", NULL, NULL); assert(end != NULL); assert(read_move(&pos, "d1h5", &m, white) == 0); assert(m == create_move_g(sq_d1, sq_h5, queen, 0, true)); assert(read_move(&pos, "Qh5", &m, white) == 0); assert(m == create_move_g(sq_d1, sq_h5, queen, 0, true)); assert(read_move(&pos, "Qh5+", &m, white) == 0); assert(m == create_move_g(sq_d1, sq_h5, queen, 0, true)); assert(read_move(&pos, "Qh5#", &m, white) == 0); assert(m == create_move_g(sq_d1, sq_h5, queen, 0, true)); print_move(&pos, m, str, mn_san, white); assert(strcmp(str, "Qh5#") == 0); }