示例#1
0
int g3_move(const struct connect4 *game, int secondsleft) {
    // Seed random with our magic number
    srand(42);

    int specialCase = g3_handleSpecialCase(game);
    if (specialCase != -1) {
        //puts("Moving based on special case");
        return specialCase;
    }

    /* // for testing
    int col;

    scanf("%d", &col);
    return col;
*/

    int *possibleMoves = get_possible_moves(game);

    int nextMove;
    if (secondsleft > 25) {
        //puts("Slow move");
        // Allocate our root
        g3_MCnode *root = calloc(1, sizeof(g3_MCnode));

        // Search the tree
        g3_mcts(game, root);

        nextMove = g3_bestMove(root->scores, possibleMoves, game);

        // Clean up
        g3_freeMCTree(root);
    } else {
        //puts("Fast move");
        nextMove = g3_fastMove(game, possibleMoves);
    }

    free(possibleMoves);

    return nextMove;
}
bool BoardC4::play_random_move(Token player) {
	if (played_count<size) {
		Moves possible_moves=get_possible_moves(player);

		int selected=rand()/(RAND_MAX + 1.0) * possible_moves.size();
		Moves::const_iterator selected_iter=possible_moves.begin();
		while (selected>0) {
			selected--;
			selected_iter++;
		}
		play_move(**selected_iter);

		//play_move(*selected);
		//Move *selected=possible_moves[rand()%possible_moves.size()];
		//play_move(*selected);

		for (Moves::iterator iter=possible_moves.begin(); iter!=possible_moves.end(); iter++) delete *iter;

		return true;
	} else {
		//std::cout<<"board full"<<std::endl;
		return false;
	}
}
示例#3
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;
}