src/strategies/minimax.c
// Returns and integer indicating the best mark you can get with a specific board state
// by testing the next n turns.
// This AI maximize and its enemy minimize.
Returns and integer indicating the best mark you can get with a specific board state
by testing the next n turns.
This AI maximize and its enemy minimize.
Returns and integer indicating the best mark you can get with a specific board state by testing the next n turns . This AI maximize and its enemy minimize .
struct strat_board minimaxAB(struct strat_board *b, int p, int alpha, int beta, int n) {
    if (n == 0) {
        b->mark = board_value(b, p);
        return *b;
    } else {
        struct strat_board *successors = malloc(sizeof(struct strat_board));
        int successors_nb = find_successors(b, &successors, p);
        if (p == get_current_player(&b->board)) {
            int max = alpha;
            int best_board_id = 0;
            struct strat_board tested_board;
            int end = 0;
            int i = 0;
            int v;
            while ((i < successors_nb) && (end == 0)) {
                tested_board = minimaxAB(&successors[i], (p + 1) % 2, max, beta, n - 1);
                v = tested_board.mark;
                if (v > max) {
                    max = v;
                    best_board_id = i;
                }
                if (max >= beta) {
                    end = 1;
                }
                i++;
            }
            tested_board = successors[best_board_id];
            free(successors);
            return tested_board;
        } else {
            int min = beta;
            int best_board_id = 0;
            struct strat_board tested_board;
            int end = 0;
            int i = 0;
            int v;
            while ((i < successors_nb) && (end == 0)) {
                tested_board = minimaxAB(&successors[i], (p + 1) % 2, alpha, min, n - 1);
                v = tested_board.mark;
                if (v < min) {
                    min = v;
                    best_board_id = i;
                }
                if (min <= alpha) {
                    end = 1;
                }
                i++;
            }
            tested_board = successors[best_board_id];
            free(successors);
            return tested_board;
        }
    }
}
struct strat_board minimaxAB ( struct strat_board * b , int p , int alpha , int beta , int n ) { if ( n == 0 ) { b -> mark = board_value ( b , p ) ; return * b ; } else { struct strat_board * successors = malloc ( sizeof ( struct strat_board ) ) ; int successors_nb = find_successors ( b , & successors , p ) ; if ( p == get_current_player ( & b -> board ) ) { int max = alpha ; int best_board_id = 0 ; struct strat_board tested_board ; int end = 0 ; int i = 0 ; int v ; while ( ( i < successors_nb ) && ( end == 0 ) ) { tested_board = minimaxAB ( & successors [ i ] , ( p + 1 ) % 2 , max , beta , n - 1 ) ; v = tested_board . mark ; if ( v > max ) { max = v ; best_board_id = i ; } if ( max >= beta ) { end = 1 ; } i ++ ; } tested_board = successors [ best_board_id ] ; free ( successors ) ; return tested_board ; } else { int min = beta ; int best_board_id = 0 ; struct strat_board tested_board ; int end = 0 ; int i = 0 ; int v ; while ( ( i < successors_nb ) && ( end == 0 ) ) { tested_board = minimaxAB ( & successors [ i ] , ( p + 1 ) % 2 , alpha , min , n - 1 ) ; v = tested_board . mark ; if ( v < min ) { min = v ; best_board_id = i ; } if ( min <= alpha ) { end = 1 ; } i ++ ; } tested_board = successors [ best_board_id ] ; free ( successors ) ; return tested_board ; } } }
Returns and integer indicating the best mark you can get with a specific board state
by testing the next n turns.
Returns and integer indicating the best mark you can get with a specific board state by testing the next n turns .
// find all the possible moves from this state of the board. // printf("Successors number : %d\n", successors_nb);