You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python井字棋Minimax算法Bot最优落子异常问题求助

问题分析与解决方案

你的Minimax算法无法选择最优落子的核心原因是胜利判定逻辑错误:当前代码仅判断棋盘是否有赢家,却未区分赢家是Bot还是人类,且错误地用当前回合的maximizing参数来决定分数,导致Bot将自己的胜利判定为负分,从而避开了直接获胜的落子。

具体问题点

在你的minimax函数中,当Bot落子后已经赢了,此时进入递归调用的minimax函数中maximizing参数为False(代表接下来是人类回合),代码会返回-10,这相当于把Bot的胜利判定为糟糕的结果,自然不会选择这个落子。

修复步骤

1. 修改is_win函数,返回赢家编号

将原is_win函数从返回布尔值改为返回赢家的玩家编号(1代表Bot,2代表人类),无赢家则返回None:

def is_win(board):
    # 定义所有获胜模式
    win_patterns = [
        [0,1,2], [3,4,5], [6,7,8],  # 横向
        [0,3,6], [1,4,7], [2,5,8],  # 纵向
        [0,4,8], [2,4,6]             # 对角线
    ]
    for pattern in win_patterns:
        a, b, c = pattern
        if board[a] == board[b] == board[c] != 0:
            return board[a]  # 返回赢家的编号
    return None

2. 修正Minimax算法的终止条件

根据实际赢家判定分数,而非当前maximizing状态,同时加入深度加权(让Bot优先选择最快的胜利方式):

def minimax(board_list, depth, maximizing):
    temp_list = list(board_list)

    # 先判断是否有赢家
    winner = is_win(temp_list)
    if winner == 1:
        return 10 - depth  # Bot获胜,深度越小分数越高,优先快速胜利
    elif winner == 2:
        return depth - 10  # 人类获胜,深度越小分数越低,优先避免快速失败

    # 棋盘满了则平局
    if is_board_full(temp_list):
        return 0

    if maximizing:
        best_score = float('-inf')
        for i in range(len(temp_list)):
            if temp_list[i] == 0:
                temp_list[i] = 1
                score = minimax(temp_list, depth + 1, False)
                temp_list[i] = 0
                best_score = max(score, best_score)
        return best_score
    else:
        best_score = float('inf')
        for i in range(len(temp_list)):
            if temp_list[i] == 0:
                temp_list[i] = 2
                score = minimax(temp_list, depth + 1, True)
                temp_list[i] = 0
                best_score = min(score, best_score)
        return best_score

3. 验证逻辑

在你描述的场景中,当Bot选择8号格子后,is_win会返回1,minimax函数返回10-0=10,这是所有可选落子中的最高分数,因此Bot会优先选择这个直接获胜的位置。

内容的提问来源于stack exchange,提问作者Kasun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 20:13:30