示例#1
0
文件: test_board.c 项目: 0x65/manul
/*
    Tests `make_move` and `undo_move` by making and undoing every single move for
    a set of fen positions, then making sure the resulting board is the same as the
    original board.
*/
int test_make_undo_move() {
    static const char* fen_positions[] = { "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
        "1B4n1/5k2/5P2/4p2N/P2QR3/p2pb2K/3Pb2R/8 w - - 0 1", "3b4/N2P1p2/1kP4p/p4n1K/2p5/1PPp2P1/1p6/8 w - - 0 1",
        "8/4p3/3R4/2P4R/P3Pp2/6kp/8/K7 w - - 0 1", "8/8/p3K3/k1N2B2/3P4/6Pp/5P1p/4R3 w - - 0 1",
        "K7/8/1P3r2/1P2b1p1/8/5p2/1Q2q2n/7k b - - 0 1", "1q1rr3/P1RN1P1p/Kp3p2/2P2p2/b4pP1/n4kb1/8/3Rn3 b - - 0 1",
        "8/2P5/Bp4Kn/q7/5P2/k7/8/B7 b - - 0 1", "8/8/1B6/8/P1b4K/8/8/1k6 b - - 0 1",
        "8/P2kp3/7B/1R3r2/1bK4p/Q4pp1/P6N/8 b - - 0 1" };

    board_t board[ELEMENTS_IN(fen_positions)], board_original[ELEMENTS_IN(fen_positions)];
    move_t move_list[256];

    for (int i = 0; i < ELEMENTS_IN(fen_positions); i++) {
        set_board_from_fen_string(&board[i], fen_positions[i]);
        memcpy(&board_original[i], &board[i], sizeof(board_t));
    }

    for (int i = 0; i < ELEMENTS_IN(fen_positions); i++) {
        move_t* end_of_list = generate_pseudolegal_moves(&board[i], move_list);

        for (int j = 0; move_list[j] != *end_of_list; j++) {
            make_move(&board[i], move_list[j]);
            undo_move(&board[i], move_list[j]);

            if (!compare_boards(&board[i], &board_original[i])) {
                fprintf(stderr, "test_make_undo_move -- Test \"%s\" failed: move %d.\n", fen_positions[i], j);
                return TEST_ERROR;
            }
        }
    }

    return TEST_SUCCESS;
}
示例#2
0
/*
    Makes a move_t from a supplied string (in long algebraic notation), or
    returns 0 if it's an invalid move.
*/
move_t move_from_text(const char* text, board_t* board) {
    int text_length = strlen(text);
    if (text_length != 4 && text_length != 5) return 0;

    move_t move = (((text[1]-'1')*8)+(tolower(text[0])-'a')) + ((((text[3]-'1')*8)+(tolower(text[2])-'a'))<<6);
    if (text_length == 5)
        if (board->to_move == WHITE)
            move |= piece_type_from_char(toupper(text[4]))<<23;
        else
            move |= piece_type_from_char(tolower(text[4]))<<23;

    move_t move_list[MAX_MOVES];
    move_t* end_of_move_list = generate_pseudolegal_moves(board, move_list);
    for (int i = 0; &move_list[i] != end_of_move_list; i++) {
        if ((move_list[i]&0x7800FFF) == move) {
            if (IS_CASTLE(move_list[i]) && !is_legal_castle(board, move_list[i])) return 0;
            make_move(board, move_list[i]);
            if (!is_king_in_check(board, !board->to_move)) {
                undo_move(board, move_list[i]);
                return move_list[i];
            }
       }
    }

    return 0;
}