(
    game, ai_depths, win_score, scoring=None, tt=None, verbose=True, **game_params
)
return result, depth, game.ai_move
Solves a game using iterative deepening, i.e. determines if by playing
    perfectly the first player can force a win, or whether it will always
    lose against a perfect opponent.


    This algorithm explores the game by using several times the Negamax
    algorithm, always starting at the initial state of the game, but
    taking increasing depth (in the list ai_depths) until the score of
    the initial condition indicates that the first player will certainly
    win or loose, in which case it stops.
    The use of transposition table leads to speed gain as the results
    of shallower searches are used to help exploring the deeper ones.

    Parameters
    -----------

    game
      An instance of a TwoPlayerGame

    ai_depths:
      List of AI depths to try (e.g. [5,6,7,8,9,10])


    win_score:
      Score above which a score means a win.

    scoring:
      Scoring function (see doc of class Negamax)

    tt:
      An optional transposition table to speed up computations.

    verbose:
      If set to ``True``, will print a summary of the best move
      after each depth tried.

    Returns
    --------

    (result, depth, move):
      As below

    result:
      Either 1 (certain victory of the first player) or -1
      (certain defeat) or 0 (either draw, or the search was not
      deep enough)

    depth:
      The minimal number of moves before victory (or defeat)

    move:
      Best move to play for the first player.

    Also returns with ``tt`` set.
      Will be None if ``use_tt`` was set to false, else will be a
      transposition table containing all the relevant situations to play
      a perfect game and can be used with ``AI_player(tt)``
Solves a game using iterative deepening, i.e. determines if by playing
    perfectly the first player can force a win, or whether it will always
    lose against a perfect opponent.
Solves a game using iterative deepening i . e . determines if by playing perfectly the first player can force a win or whether it will always lose against a perfect opponent .
def solve_with_iterative_deepening(
    game, ai_depths, win_score, scoring=None, tt=None, verbose=True, **game_params
):
    """
    Solves a game using iterative deepening, i.e. determines if by playing
    perfectly the first player can force a win, or whether it will always
    lose against a perfect opponent.


    This algorithm explores the game by using several times the Negamax
    algorithm, always starting at the initial state of the game, but
    taking increasing depth (in the list ai_depths) until the score of
    the initial condition indicates that the first player will certainly
    win or loose, in which case it stops.
    The use of transposition table leads to speed gain as the results
    of shallower searches are used to help exploring the deeper ones.

    Parameters
    -----------

    game
      An instance of a TwoPlayerGame

    ai_depths:
      List of AI depths to try (e.g. [5,6,7,8,9,10])


    win_score:
      Score above which a score means a win.

    scoring:
      Scoring function (see doc of class Negamax)

    tt:
      An optional transposition table to speed up computations.

    verbose:
      If set to ``True``, will print a summary of the best move
      after each depth tried.

    Returns
    --------

    (result, depth, move):
      As below

    result:
      Either 1 (certain victory of the first player) or -1
      (certain defeat) or 0 (either draw, or the search was not
      deep enough)

    depth:
      The minimal number of moves before victory (or defeat)

    move:
      Best move to play for the first player.

    Also returns with ``tt`` set.
      Will be None if ``use_tt`` was set to false, else will be a
      transposition table containing all the relevant situations to play
      a perfect game and can be used with ``AI_player(tt)``

    """
    if game.players is None:
        game.players = [AI_Player(None), AI_Player(None)]

    for depth in ai_depths:
        ai = Negamax(depth, scoring, tt=tt)
        ai(game)
        alpha = ai.alpha
        if verbose:
            print("d:%d, a:%d, m:%s" % (depth, alpha, str(game.ai_move)))
        if abs(alpha) >= win_score:
            break

    # 1:win, 0:draw, -1:defeat
    result = +1 if alpha >= win_score else (-1 if alpha <= -win_score else 0)

    return result, depth, game.ai_move
def solve_with_iterative_deepening ( game , ai_depths , win_score , scoring = None , tt = None , verbose = True , * * game_params ) : if game . players is None : game . players = [ AI_Player ( None ) , AI_Player ( None ) ] for depth in ai_depths : ai = Negamax ( depth , scoring , tt = tt ) ai ( game ) alpha = ai . alpha if verbose : print ( "d:%d, a:%d, m:%s" % ( depth , alpha , str ( game . ai_move ) ) ) if abs ( alpha ) >= win_score : break # 1:win, 0:draw, -1:defeat result = + 1 if alpha >= win_score else ( - 1 if alpha <= - win_score else 0 ) return result , depth , game . ai_move