示例#1
0
文件: ai.c 项目: bfontaine/Reversi
ai* create_ai(board* b, char player_c) {
    
    ai* a = (ai*)malloc(sizeof(ai));

    future_move *fm = create_future_move(-1,-1);

    a->moves_len = 0;
    a->b = board_cp(b);
    a->player_c = player_c;
    a->current_player_c = FIRST_PLAYER;

    compute_moves(fm, a->b, player_c, MAX_DEEP, (player_c == FIRST_PLAYER));

    a->moves = fm->moves;
    a->moves_len = fm->moves_len;

    free(fm); fm = NULL;

    return a;
}
示例#2
0
文件: Pawn.cpp 项目: zxul767/Pawn
Pawn::Pawn ()
{
   compute_moves ();
}
示例#3
0
文件: Bishop.cpp 项目: zxul767/Pawn
Bishop::Bishop ()
{
   compute_moves ();
}
示例#4
0
文件: ai.c 项目: bfontaine/Reversi
int compute_moves(future_move* self, board* b, char player, int deep, short is_ia) {
    if (deep < 1) {
        return 0;
    }

    int i, moves_c_len = 0,
        col = -1,
        row = -1;
    

    if (self->points != 0) {
        /* if AI does not pass the turn */
        put_piece_by_colrow(b, self->col, self->row, player);
    }

    char **moves_c = NULL;
    moves_c = (char**)malloc(sizeof(char*)*MAX_POSSIBLE_MOVES);

    for (i=0; i<MAX_POSSIBLE_MOVES; i++) {
        moves_c[i] = NULL;
    }
    
    board* b2 = NULL;
    future_move* fm = NULL;

    moves_c_len = get_possible_moves(b, player, &moves_c);

    if (moves_c_len == 0) {
            /* pass */
            fm = create_future_move(-1,-1);
            ADD_FUTURE_MOVE(self, fm);
    } else {
        for (i=0; i<moves_c_len; i++) {
            convert_square(moves_c[i], &col, &row);

            /* create a future move with good points/weight and col/row */
            fm = create_future_move(col, row);
            fm->points = play(b, player, moves_c[i]); /* simule the move */
            put_piece(b, moves_c[i], EMPTY_C);  /* remove the piece */
            fm->weight = M_WEIGHT(*fm);

            ADD_FUTURE_MOVE(self, fm);
        }
    }

    for (i=0; i<(self->moves_len); i++) {
        b2 = board_cp(b);

        self->weight
            += (-1*(!is_ia))*compute_moves((self->moves)[i],
                                 b2,
                                 OTHER_PLAYER(player),
                                 deep-1,
                                 !is_ia);

        free(b2); b2 = NULL;
    }

    for (i=0; i<moves_c_len; i++) {
        if (moves_c[i] != NULL) {
            free(moves_c[i]); moves_c[i] = NULL;
        }
    }
    free(moves_c); moves_c = NULL;
    
    return self->weight;
}