function minimaxAlphaBeta(gameState, depth, alpha, beta, player, opponent, maxi_player) {
    const possibleMoves = gameState.getAllPossibleMoves();
    const possibleMovesSeparated = [];
    for (let i = 0; i < possibleMoves.length; i++) {
        for (let j = 0; j < possibleMoves[i].moves.moves.length; j++) {
            possibleMovesSeparated.push({"from": possibleMoves[i].coords, "to": possibleMoves[i].moves.moves[j]})
        }
    }
    const v = new Score(0, null);

    // Base case
    if (depth === 0 || possibleMovesSeparated.length === 0) {
        return heuristicFunction(gameState);
    } else {
        v.score = (player === maxi_player) ? Number.MIN_SAFE_INTEGER : Number.MAX_SAFE_INTEGER;
        v.move = possibleMovesSeparated[getRandomInt(possibleMovesSeparated.length)]; // Default move
        for (let i = 0; i < possibleMovesSeparated.length; i++) {
            let v_child = null;
            const child = cloneGameController(gameState);
            const move = possibleMovesSeparated[i];
            const state = child.handleMove(move.from, move.to);
            if (state === CHANGE_TURN) {
                child.changeTurn();
            }
            v_child = minimaxAlphaBeta(child, depth - 1, alpha, beta, opponent, player, maxi_player);
            if (player === maxi_player) {
                if (v_child.score > v.score) {
                    v.score = v_child.score;
                    v.move = move;
                }
                if (v.score > alpha) {
                    alpha = v.score;
                }
                if (beta <= alpha) {
                    return v;
                }
            } else {
                if (v_child.score < v.score) {
                    v.score = v_child.score;
                    v.move = move;
                }
                if (v.score < beta) {
                    beta = v.score;
                }
                if (beta <= alpha) {
                    return v;
                }
            }

        }
    }

    return v;
}
/**
 * Mini-max algorithm with alpha beta pruning for determining the next move.
 * @param gameState
 * @param depth
 * @param alpha
 * @param beta
 * @param player
 * @param opponent
 * @param maxi_player
 * @returns {Score}
 */
Mini-max algorithm with alpha beta pruning for determining the next move.
Mini - max algorithm with alpha beta pruning for determining the next move .
function minimaxAlphaBeta(gameState, depth, alpha, beta, player, opponent, maxi_player) {
    const possibleMoves = gameState.getAllPossibleMoves();
    const possibleMovesSeparated = [];
    for (let i = 0; i < possibleMoves.length; i++) {
        for (let j = 0; j < possibleMoves[i].moves.moves.length; j++) {
            possibleMovesSeparated.push({"from": possibleMoves[i].coords, "to": possibleMoves[i].moves.moves[j]})
        }
    }
    const v = new Score(0, null);
    if (depth === 0 || possibleMovesSeparated.length === 0) {
        return heuristicFunction(gameState);
    } else {
        v.score = (player === maxi_player) ? Number.MIN_SAFE_INTEGER : Number.MAX_SAFE_INTEGER;
        v.move = possibleMovesSeparated[getRandomInt(possibleMovesSeparated.length)]; 
        for (let i = 0; i < possibleMovesSeparated.length; i++) {
            let v_child = null;
            const child = cloneGameController(gameState);
            const move = possibleMovesSeparated[i];
            const state = child.handleMove(move.from, move.to);
            if (state === CHANGE_TURN) {
                child.changeTurn();
            }
            v_child = minimaxAlphaBeta(child, depth - 1, alpha, beta, opponent, player, maxi_player);
            if (player === maxi_player) {
                if (v_child.score > v.score) {
                    v.score = v_child.score;
                    v.move = move;
                }
                if (v.score > alpha) {
                    alpha = v.score;
                }
                if (beta <= alpha) {
                    return v;
                }
            } else {
                if (v_child.score < v.score) {
                    v.score = v_child.score;
                    v.move = move;
                }
                if (v.score < beta) {
                    beta = v.score;
                }
                if (beta <= alpha) {
                    return v;
                }
            }
        }
    }
    return v;
}
function minimaxAlphaBeta ( gameState , depth , alpha , beta , player , opponent , maxi_player ) { const possibleMoves = gameState . getAllPossibleMoves ( ) ; const possibleMovesSeparated = [ ] ; for ( let i = 0 ; i < possibleMoves . length ; i ++ ) { for ( let j = 0 ; j < possibleMoves [ i ] . moves . moves . length ; j ++ ) { possibleMovesSeparated . push ( { "from" : possibleMoves [ i ] . coords , "to" : possibleMoves [ i ] . moves . moves [ j ] } ) } } const v = new Score ( 0 , null ) ; if ( depth === 0 || possibleMovesSeparated . length === 0 ) { return heuristicFunction ( gameState ) ; } else { v . score = ( player === maxi_player ) ? Number . MIN_SAFE_INTEGER : Number . MAX_SAFE_INTEGER ; v . move = possibleMovesSeparated [ getRandomInt ( possibleMovesSeparated . length ) ] ; for ( let i = 0 ; i < possibleMovesSeparated . length ; i ++ ) { let v_child = null ; const child = cloneGameController ( gameState ) ; const move = possibleMovesSeparated [ i ] ; const state = child . handleMove ( move . from , move . to ) ; if ( state === CHANGE_TURN ) { child . changeTurn ( ) ; } v_child = minimaxAlphaBeta ( child , depth - 1 , alpha , beta , opponent , player , maxi_player ) ; if ( player === maxi_player ) { if ( v_child . score > v . score ) { v . score = v_child . score ; v . move = move ; } if ( v . score > alpha ) { alpha = v . score ; } if ( beta <= alpha ) { return v ; } } else { if ( v_child . score < v . score ) { v . score = v_child . score ; v . move = move ; } if ( v . score < beta ) { beta = v . score ; } if ( beta <= alpha ) { return v ; } } } } return v ; }
Mini-max algorithm with alpha beta pruning for determining the next move.
Mini - max algorithm with alpha beta pruning for determining the next move .
// Base case // Default move