用bash编写minimax算法

时间:2019-05-29 05:56:15

标签: bash unix minimax

我为这个模糊的问题表示歉意,我完全迷失了方向,不知道目前出了什么问题。

我最近一直在尝试学习bash脚本,因此决定写一个带有minimax和alpha beta修剪的tictactoe。因为我还是UNIX的新手,所以我以Python方式编写了脚本。当我尝试运行minimax函数时,我没有收到任何错误,但没有输出任何内容,也没有正常退出(必须按CTRL C)。

如果当前板获胜,则该功能能够告诉它获胜并返回我需要的东西。但是当不赢时,它就无法下降到下一个水平。

#!/bin/bash

declare -a board=( A1 " o" " x" " o" " o" " x" C1 C2 C3 )
shopt -s extglob


#draws the grid
draw(){
grid="| "${board[0]}" | ${board[1]} | ${board[2]} |
----------------
| ${board[3]} | ${board[4]} | ${board[5]} |
----------------
| ${board[6]} | ${board[7]} | ${board[8]} |" 

echo "$grid"
}

#test if board has a winning combination for either player
wintest(){
local testboard=("$@")
for p in " o" " x"
do
#test for vertical win
for i in $(seq 0 2) 
    do
        if [ "$p" == "${testboard[$i]}" -a "$p" == "${testboard[$(($i+3))]}" -a "$p" == "${testboard[$(($i+6))]}" ]; then
        echo 1
        fi
    done
#test for horizontal win
for i in 0 3 6 
    do  
        if [ "$p" == "${testboard[$i]}" -a "$p" == "${testboard[$i+1]}" -a "$p" == "${testboard[$i+2]}" ]; then
        echo 1
        fi
    done
#diagonal win   
if [ "$p" == "${testboard[0]}" -a "$p" == "${testboard[4]}" -a "$p" == "${testboard[8]}" ]; then
echo 1
fi
if [ "$p" == "${testboard[2]}" -a "$p" == "${testboard[4]}" -a "$p" == "${testboard[6]}" ]; then
echo 1
fi
done
}

#minimax function. parameters: player depth alpha beta board
minimax(){
if [ $1 -eq 1 ]; then
local player="1"
local p=" o"

elif [ $1 -eq 2 ]; then 
local player="2"
local p=" x"
fi

local depth=$2
local alpha=$3
local beta=$4

shift 4

#create array amove containing available moves
local boardc=("$@") 
local amove=("${boardc[@]/+(" x"|" o")}")
for i in "${!amove[@]}"; do 
if [[ -z "${amove[$i]}" ]]; then
unset amove[$i]
fi
done

#determine if anybody has won in the current board
#$player=1 is the maximing player. wintest is called on the start of the turn,
#if a winning combination exist, it belongs to the previous player

local winstat=$(wintest "${boardc[@]}")
if [ -n "$winstat" ]; then
    if [ "${player}" == 2 ]; then   
    echo "$depth"

    else  
    echo "-$depth"
    fi

else
    if [ "${player}" == 1 ]; then
    local bestval=-1000
        for i in "${amove[@]}"
        do      
            local boardcm="${boardc[@]}"    
            boardcm=("${boardcm[@]/"$i"/"$p"}")
            local value="$( minimax 2 $(( $depth+1 )) $alpha $beta "${boardcm[@]}" )"
            bestval=$(( "$bestval" > "$value" ? $bestval : "$value" ))
            alpha=$(( "$bestval" > "$alpha" ? "$bestval" : "$alpha" ))
            if [ "$beta" -le "$alpha" ] 
            then 
            break
            fi
        done
        echo "$bestval"

    elif [ "${player}" == 2 ]; then
    local bestval=1000
        for i in "${amove[@]}"
        do      
            local boardcm="${boardc[@]}"    
            boardcm=("${boardcm[@]/"$i"/"$p"}")
            local value="$( minimax 1 $(( $depth+1 )) $alpha $beta "${boardcm[@]}" )"
            bestval=$(( "$bestval" < "$value" ? "$bestval" : "$value" ))
            beta=$(( "$bestval" < "$beta" ? "$bestval" : "$beta" ))
            if [ "$beta" -le "$alpha" ]; then 
            break
            fi
        done
        echo "$bestval"
    fi
fi
}

理论上,minimax函数应返回一个单一值,该值是在当前板状态下可以确保的最高(或最低值,具体取决于玩家)。该功能将一直工作到递归发生的部分。

很抱歉,我无法在问题中更详细地介绍我,我真的不知道为什么该功能不起作用

0 个答案:

没有答案