2

I am implementing a simple TicTacToe game just to try out the Minimax with Alpha Beta pruning algorithm and I got stucked... The problem I'm facing is: The AI is not responding correctly to the player last move and I can't figure out where is my mistake. Here is the 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 i = moves.begin(); i != 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 i = moves.begin(); i != 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;
}

The variable 'bestMove' is a class member and the initial call is:

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

I would very much appreciate some help.

Poncho
  • 31
  • 5
  • There might be a mistake in the method where you actually choose a move, where you make use of the calculated bm value (please post the code for that too) – xXliolauXx Jul 14 '15 at 14:51

0 Answers0