post #1 of 1
Thread Starter 
Please add "alpha beta pruning" in Minimax function so the program or game tic tac toe can efficiently works ..

I am providing you with the code "add alpha beta cut off in this" ..

These are the functions below in program tic tac toe ..

Also explain this code and at least explain it if you can write the code ...
Code:
// returns best move for the current computer player
    int MiniMax(char _board[9], player _player) {
        int best_val = -INFINITY, index = 0;
        std::list<int> move_list;
        char best_moves[9] = {0};
        generate_moves(_board, move_list);
        while(!move_list.empty()) {
            _board[move_list.front()] = _player.symbol;
            cSymbol = _player.symbol;
            int val = MinMove(_board, _player);
            if(val > best_val) {
                best_val = val;
                index = 0;
                best_moves[index] = move_list.front() + 1;
            } else if(val == best_val) {
                best_moves[++index] = move_list.front() + 1;
            }
            _board[move_list.front()] = 0;
            move_list.pop_front();
        }
        if(index > 0) {
            index = rand() % index;
        }
        return best_moves[index];
    }

// finds best move for 'min player'
int MinMove(char _board[9], player _player) {
    int pos_value = evaluate_position(_board, _player);
    if(pos_value != -1) {
        return pos_value;
    }
    int best_val = +INFINITY;
    std::list<int> move_list;
    generate_moves(_board, move_list);
    while(!move_list.empty()) {
        _player.symbol == 'X' ? cSymbol = 'O' : cSymbol = 'X';
        _board[move_list.front()] = cSymbol;
        int val = MaxMove(_board, _player);
        if(val < best_val) {
            best_val = val;
        }
        _board[move_list.front()] = 0;
        move_list.pop_front();
    }
    return best_val;
}

// finds best move for 'max player'
int MaxMove(char _board[9], player _player) {
    int pos_value = evaluate_position(_board, _player);
    if(pos_value != -1) {
        return pos_value;
    }
    int best_val = -INFINITY;
    std::list<int> move_list;
    generate_moves(_board, move_list);
    while(!move_list.empty()) {
        _player.symbol == 'X' ? cSymbol = 'X' : cSymbol = 'O';
        _board[move_list.front()] = cSymbol;
        int val = MinMove(_board, _player);
        if(val > best_val) {
            best_val = val;
        }
        _board[move_list.front()] = 0;
        move_list.pop_front();
    }
    return best_val;
}