在Shell中编写了TicTacToe,计算运行不正常

pvabu6sv  于 2022-11-16  发布在  Shell
关注(0)|答案(1)|浏览(70)

计算机总是在第一个空的地方玩,我不知道为什么。当我在第一个地方玩的时候,计算机在第二个地方玩,依此类推。如果极小极大函数总是返回1,不会改变任何事情。发生了什么?
注意:我的棋盘是1比9。如果电脑赢了,check_win给予我10,如果人类赢了,-10,如果平局,0,如果比赛没有结束“假”。

#!/bin/bash

best_move(){
    local best_score=-1000
    for (( i=1; i<=${#board[@]}; i++ ))
    do
        if [[ ${board[$i]} =~ $re_isnumber ]]; then
            board[$i]=$computer
            local score=$(minimax 0 "false")
            if [ $score -gt $best_score ]; then
                best_score=$score
                local move=$i
            fi
            board[$i]=$i
            
        fi
    done
    board[$move]=$computer
    current_player=$human
}

minimax(){
    result=$(check_winner)
    if [[ $result == 10 ]]; then
        echo result
        return
    fi

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

    if [[ $result == 0 ]]; then
        echo $result
        return
    fi
    
    if [[ $3 == "true" ]]; then
        maximize
    else
        minimize
    fi
}

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

minimize(){
    local best_score=800
    for (( i=0; i<${#board[@]}; i++ ))
    do
        if [[ ${board[$i]} =~ $re_isnumber ]]; then
            board[$i]=$human
            local score=$(minimax $(($2+1)) "true")
            if [ $score -lt $best_score ]; then
                best_score=$score
            fi
            board[$i]=$i
            
        fi
    done
    echo $best_score
}
ee7vknir

ee7vknir1#

如果你的程序返回了第一个位置,那是因为它没有找到比这更好的移动。我不会为你调试整个程序,但它这样做的最常见的原因是因为你发送了错误的评估分数。我不熟悉你的语言,然而,如果你从check_赢家得到了某个结果,你似乎总是会返回一个10/-10的分数。返回的分数需要取决于轮到谁。如果“O”赢了并且是“O”转,你需要发回赢分10。如果“O”赢了并且是“X”转,你需要发回输分-10。

相关问题