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

Shell实现TicTacToeAI异常:总是落子第一个空位问题排查

井字棋AI总是选第一个空位的问题分析与修复

核心问题1:best_move中move变量的更新逻辑缺陷

在best_move函数里,你仅在score > best_score时更新move变量:

if [ $score -gt $best_score ]; then
    best_score=$score
    local move=$i
fi

当所有空位的评估分数相等时(比如你测试的minimax始终返回1),只有第一个满足条件的空位会被赋值给move,后续空位的分数等于当前best_score,不会触发更新,导致AI永远选择第一个空位。

修复方案:将判断条件改为score >= best_score,这样分数相等时也会更新move(最终会选中最后一个分数相等的空位,避免固定选第一个):

if [ $score -ge $best_score ]; then
    best_score=$score
    local move=$i
fi

同时建议初始化move为有效位置(比如local move=1),避免循环无匹配项时move未定义。

核心问题2:minimax函数参数传递完全错误

  1. 参数位置不匹配:minimax中判断是否为最大化玩家用的是第三个参数$3,但best_move调用时只传了两个参数:minimax 0 "false",导致$3为空,永远执行minimize分支,AI的评估逻辑完全失效。
    正确调用应传递第三个参数(电脑回合属于最大化玩家):

    local score=$(minimax 0 "true")
    

    同时把minimax里的分支判断改为检查第二个参数(统一参数顺序为minimax <depth> <is_maximizing>):

    if [[ $2 == "true" ]]; then
        maximize
    else
        minimize
    fi
    
  2. maximize/minimize调用minimax时参数缺失:这两个函数被minimax调用时没有传递深度参数,导致$2为空,$(($2+1))被错误解析为$((0+1)),深度计算逻辑混乱。需要从minimax传递深度参数:
    比如minimax调用maximize时:

    maximize $1
    

    然后修改maximize函数定义:

    maximize(){
        local depth=$1
        local best_score=-800
        for (( i=1; i<=${#board[@]}; i++ ))
        do
            if [[ ${board[$i]} =~ $re_isnumber ]]; then
                board[$i]=$computer
                local score=$(minimax $((depth+1)) "false")
                if [ $score -gt $best_score ]; then
                    best_score=$score
                fi
                board[$i]=$i
            fi
        done
        echo $best_score
    }
    

核心问题3:minimax返回值错误

在判断电脑获胜的分支中,你echo的是字符串result而非变量$result:

if [[ $result == 10 ]]; then
    echo result
    return
fi

这会导致返回"result"字符串而非数字10,后续分数比较会出现逻辑异常或报错。修复为:

if [[ $result == 10 ]]; then
    echo $result
    return
fi

核心问题4:数组索引不统一

棋盘位置为1-9,但maximize和minimize的循环从0开始:

for (( i=0; i<${#board[@]}; i++ ))

如果board数组是从索引1到9初始化的,索引0属于无效位置,会导致AI尝试在无效位置落子,评估逻辑错误。应统一循环从1开始:

for (( i=1; i<=${#board[@]}; i++ ))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 04:55:31