minimax算法如何返回实际移动?

时间:2016-02-25 12:33:35

标签: c# unity3d minimax tree-search

我正在研究minimax algoritm,我有一个问题来比较每个节点并且无法返回最佳节点。一旦我可以比较它,它仍然选择节点的底部,有人可以帮我修改我的代码吗?

这是我的代码:

public Node minimax(ArrayList newBoard, int depth, Player p, Node n)
{
    int value = 0;

    if (depth == 0 || StatePosition.PlayerWins(p, newBoard))
    {
        n.Score = StatePosition.getScore(newBoard);  //the heuristic value of node
        Debug.Log(n.Pos + " - " + n.Des + " " + n.MinMax + " " + n.Score);
        return n; //out n
    }

    else
    {

        Children(p, newBoard, n);    //generate possible moves

        foreach (Node anak in n.Child)
        {    //foreach moves

            ArrayList Board = (ArrayList)newBoard.Clone();  //make a clone of board
            Debug.Log(anak.Pos + " " + anak.Des + " move applied");

            applyMove(Board, anak.Pos, anak.Des, p); //try current move

            Node b = minimax((ArrayList)Board.Clone(), depth - 1, p == Player.H ? Player.C : Player.H, anak);

            if (anak.MinMax == MiniMaxValue.MAX)
            {
                value = -1000;
                if (value < anak.Score)
                {
                    n = anak;
                }
            }
            else
            {
                value = 1000;
                if (value > anak.Score)
                {
                    n = anak;
                }
            }
        }
    }
    return n;
}

0 个答案:

没有答案