c++ - How to get the AI best move using minimax with alpha beta pruning -


i implementing simple tictactoe game try out minimax alpha beta pruning algorithm , got stucked... problem i'm facing is: ai not responding correctly player last move , can't figure out mistake. here minimax code:

int minimax( int depth, int alpha, int beta, bool maximizer ) {     std::vector<point> moves;     getpossiblemoves( moves );       if( depth == 0 || moves.size() == 0 ) {         return evaluateboard();     }     if( maximizer ) {         for( std::vector<point>::iterator = moves.begin(); != moves.end(); i++ ) {             board[( *i ).x][( *i ).y] = computer;             int score = minimax( depth - 1, alpha, beta, false );             if( score > alpha ) {                 alpha = score;                 bestmove = ( *i );             }             board[(*i).x][(*i).y] = empty;             if( alpha >= beta ) break;         }     } else {         for( std::vector<point>::iterator = moves.begin(); != moves.end(); i++ ) {             board[( *i ).x][( *i ).y] = human;             int score = minimax( depth - 1, alpha, beta, true );             if( score < beta ) {                 beta = score;                 bestmove = ( *i );             }             board[( *i ).x][( *i ).y] = empty;             if( alpha >= beta ) break;         }     }     return maximizer ? alpha : beta; } 

variable 'bestmove' class member , initial call is:

int bm = minimax( 4, -0xffffff, 0xffffff, true ); 

i appreciate help.


Comments