简体   繁体   English

Minimax 算法是否按应有的方式实现了树?

[英]Minimax algorithm is the tree implemented as it should be?

I want to create a minimax search which has a depth of 3, each node will have two childs, does the code properly represent that, is this the correct implementation, or am I doing something wrong?我想创建一个深度为 3 的极小极大搜索,每个节点将有两个子节点,代码是否正确表示,这是正确的实现,还是我做错了什么?

    static int miniMaxAlgorithm(int node, int depth, bool maxPlayer)
    {
        int eval;
        int maxEval;
        int minEval;

        if (depth == 0)
        {            
             return node; // static evaluation to be returned          
        }

        if (maxPlayer)
        {

            maxEval = int.MaxValue;

            for (int i = 0; i < 2; i++)
            {
                eval = miniMaxAlgorithm(i, depth - 1, maxPlayer = false);
                maxEval = Math.Max(maxEval, eval);
                Console.WriteLine(maxEval);
                Console.WriteLine("{0}", depth);
            }
         
            return maxEval;
        }

        else
        {
            minEval = int.MinValue;

            for (int i = 0; i < 2; i++)
            {
                eval = miniMaxAlgorithm(i, depth - 1, maxPlayer = true);
                minEval = Math.Min(minEval, eval);
                Console.WriteLine(minEval);
                Console.WriteLine("{0}", depth);
            }
            return minEval;
        }

    }

It looks alright but unfinished to me.它看起来不错,但对我来说还没有完成。 What are you planning to do with it?你打算用它做什么? If you are playing a game for example then you want to make the move right before the例如,如果您正在玩游戏,那么您希望在

eval = miniMaxAlgorithm(i, depth - 1, maxPlayer = false);

And then undo the move right after that.然后在那之后立即撤消移动。

If you want to return the best move you also need to make some adjustments.如果您想返回最佳移动,您还需要进行一些调整。 But based on the pseudo code for Minimax found here the foundation of your implementation looks correct.但是根据此处找到的 Minimax 伪代码,您的实现基础看起来是正确的。

However, for easier readability, I would consider the Negamax function instead which might also make things easier down the road.然而,为了更容易阅读,我会考虑使用Negamax函数,这也可能使事情变得更容易。 Depending on what you plan to do with this.取决于您打算对此做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM