Alpha beta修剪

时间:2012-03-30 15:15:53

标签: java minmax

我正在构建一个带有alpha beta的黑白棋游戏,我需要你在alpha beta中的帮助。 问题是计算机继续选择一个位于电路板低端的方块。我有一个可能的计算机移动列表(如下面的代码所示),这意味着计算机几乎总是选择该列表中的最后一个或最后一个移动,即使它不是最好的移动即可。我的评价功能很简单:黑色碎片减去白色碎片。 BTW:它在深度= 1时工作正常,但我需要它在深度= 3下工作。

public int AlphaBeta(int depth,int turn,TreeNode root,int alpha,int beta)
{
    if(depth==0)
        return evaluationFunc(turn,turns,root.board,root);
    else
    {
        buildSons(turn,root);
        TreeNode head =  generateList(root.sons);
        return executeCheckTheSons2(depth,turn,head,alpha,beta);
    }
}

public int executeCheckTheSons2(int depth,int turn,TreeNode head,int alpha,int beta)
    {
        int score;
        if(turn==1)
        {
            while(head!=null)
            {
                head.board=simulateTheMove(head.board,head.getX(),head.getY(),turn);
                score=AlphaBeta(depth-1,turn*-1,head,alpha,beta);
                if(score > alpha)
                {
                    alpha=score;
                    setMove(head);
                }
                if(alpha >= beta)
                    return alpha;
                head=head.next;
            }
                return alpha;
        }
        else
        {
            while(head!=null)
            {
                head.board=simulateTheMove(head.board,head.getX(),head.getY(),turn);
                score=AlphaBeta(depth-1,turn*-1,head,alpha,beta);
                if(score<beta)
                {
                    beta=score;
                    setMove(head);
                }
                if(alpha >= beta)
                    return beta;
                head=head.next;
            }
            return beta;
        }       
    }

    public void setMove(TreeNode root)
    {
        while(root.father.father!=null)
            root=root.father;
        pnt.setX(root.getX());
        pnt.setY(root.getY());
    }

1 个答案:

答案 0 :(得分:0)

我认为错误发生在setMove函数中。我想这是设置最终坐标来设置棋子的功能。此时,即使此分支的结果不是全局最佳结果,您也会在树中的每个深度调用此值。

例如,假设您正在考虑最后一次可能的行动。你从depth = 3开始,然后递归调用depth=2。当您输入executeCheckTheSons2时,您将从0开始评估可能的移动。其中一个动作可能会给你一个大于0的分数,所以你将调用setMove(head)并设置坐标以移动到最后一个可能的动作。当你现在从函数返回时,你会记录这个深度得分的分数,但从全球范围来看,这不是一个很好的举动。但是,setMove的最后一次调用仍处于活动状态,之后您不会更改它。

您应该将此调用移出executeCheckTheSons2并在某些上层函数中移动它。或者,将x和y坐标记录在executeCheckTheSons2函数的局部变量中,然后在从函数返回之前调用setMove

如果这个问题对您有用,请接受此作为答案。

相关问题