一尘不染

C ++ minimax函数

algorithm

我已经在Google和Stackoverflow上搜索了此问题,但我仍然不了解minimax函数的工作原理。

我发现维基百科条目具有该功能的伪代码版本:

function integer minimax(node, depth)
    if node is a terminal node or depth <= 0:
        return the heuristic value of node
    α = -∞
    for child in node:                       # evaluation is identical for both players 
        α = max(α, -minimax(child, depth-1))
    return α

我在Google上发现的其他几个minimax函数基本上是同一件事。我正在尝试用C ++实现,这是我到目前为止提出的:

double miniMax(Board eval, int iterations)
{
    //I evaluate the board from both players' point of view and subtract the difference
    if(iterations == 0)
        return boardEval(eval, playerNumber) - boardEval(eval, opponentSide());

    /*Here, playerTurn tells the findPossibleMoves function whose turn it is;
    I mean, how do you generate a list of possible moves if you don't even know
    whose turn it's supposed to be? But the problem is, I don't see where I can
    get playerTurn from, as there are only 2 parameters in all the examples of
    minimax I've seen*/
    vector<int> moves = eval.findPossibleMoves(playerTurn);

    //I'm assuming -∞ in the wikipedia article means a very low number?
    int result = -999999999;

    //Now I run this loop to evaluate each possible move
    /*Also, the Lua example in the wiki article has
      alpha = node.player==1 and math.max(alpha,score) or math.min(alpha,score)
      Is alpha a boolean there?!*/
    for(int i = 0; i * 2 < moves.size(); i++)
    {
        //I make a copy of the board...
        Board temp = eval;

        /*and make the next possible move... once again playerTurn crops up, and I
        don't know where I can get that variable from*/
        temp.putPiece(moves[i * 2], moves[i * 2 + 1], playerTurn);

        /*So do I create a function max that returns the bigger of two doubles?*/
        result = max(result, -miniMax(temp, iterations - 1));
    }

    return result;
    /*So now I've returned the maximum score from all possible moves within a certain
    # of moves; so how do I know which move to make? I have the score; how do I know
    which sequence of moves that score belongs to?*/
}

如您所见,我对这个minimax函数感到很困惑。请至少给我一些提示,以帮助我解决这个问题。

谢谢!:)


阅读 248

收藏
2020-07-28

共1个答案

一尘不染

来自Wikipedia的样本正在 使用Alpha / Beta修剪 进行NegaMax 。

直接命名可能会有所帮助:

  • 基础是MiniMax,一个字面实现将涉及2个轮流(相互递归)的方法,每侧1个。

  • 懒惰的程序员将其转换为NegaMax,这是一种策略性地放置-运算符的方法。

  • Alpha / Beta修剪正在跟踪最佳移动窗口(跨越多个深度)以检测死枝。

playerTurn用于确定轮到谁。在NegaMax中,您可以从奇数或偶数的深度(迭代次数)中得出。但是使用2个参数(myColor,otherColor)并在每个级别进行切换会更容易。

2020-07-28