minimax.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:ultimate-tic-tac-toe 作者: stoimenoff 项目源码 文件源码
def minimax(macroboard, depth, maximizing=True):
    """
    Perform a minimax search on a game moves tree.
    If optional parameter maximizing is True, assume that maximizing player
    is on turn. If is False - assume minimizing player. True, by default.
    """
    if macroboard.state != State.IN_PROGRESS or depth <= 0:
        return score(macroboard) * (maximizing * 2 - 1)

    if maximizing:
        bestscore = - math.inf
    else:
        bestscore = math.inf

    moves = macroboard.available_moves
    depth = balance_depth(depth, len(moves))
    for px, py in moves:
        child = deepcopy(macroboard)
        child.make_move(px, py)
        move_score = minimax(child, depth - 1, not maximizing)
        if maximizing:
            bestscore = max(bestscore, move_score)
        else:
            bestscore = min(bestscore, move_score)
    return bestscore
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号