【问题标题】:four connect ai Alpha beta minmax四连接 ai Alpha beta minmax
【发布时间】:2017-03-26 10:22:26
【问题描述】:

您好,首先我的英语非常好,所以当有些事情无法理解时,请见谅。我为井字游戏编写了一个 MinMax 算法,它工作得非常好。所以我尝试了一个四连接的 MinMax 算法,遗憾的是它不能像我想要的那样工作。然后我在谷歌上找到了Alpha beta MinMax,当我终于理解它时,我尝试了它,但它是一场灾难^^。这是我为 4 连接所做的 MinMax,有人可以给我建议如何实现 alpha 和 beta 吗?

private int computerzug(Color[][] board, int depth, Color spielerFarbe) 
{

    if(getGameLogic().hasWon(board, spielerFarbe))
    {
        System.out.println("hy");
        return -10 -depth;
    }
    if(getGameLogic().hasDraw(board))
    {
        return 0;
    }
    if(depth==6)
    {
        return 0;
    }
    int max = Integer.MIN_VALUE;
    int index = 0;
    for(int i =0;i<board[0].length;i++)
    {
        if(board[0][i].equals(Color.WHITE))
        {
            Color[][] board1 = new Color[board.length][board[0].length];
            board1 = copy(board);
            board1[getRow(board, i)][i] = spielerFarbe;
            int moval = -computerzug(board1, depth+1, (spielerFarbe.equals(Color.BLUE)?Color.RED:Color.BLUE));
            if(moval> max)
            {
                max = moval;
                index = i;
            }
        }
    }
    if(depth==0)
    {
        col = index;
        row = getRow(this.board, index);
    }
    return max;
}   

我正在使用二维颜色阵列来模拟电路板。

【问题讨论】:

  • 我也很欣赏一些关于 Java 或算法的一般建议 :) 我总是乐于学习新东西

标签: java algorithm alpha-beta-pruning


【解决方案1】:

假设您的代码适用于 minimax,alpha beta 变体应如下所示(我无法测试):

private int computerzug(Color[][] board, int depth, Color spielerFarbe, int alpha, int beta) 
{

    if(getGameLogic().hasWon(board, spielerFarbe))
    {
        System.out.println("hy");
        return -10 -depth;
    }
    if(getGameLogic().hasDraw(board))
    {
        return 0;
    }
    if(depth==6)
    {
        return 0;
    }
    int max = Integer.MIN_VALUE;
    int index = 0;
    for(int i =0;i<board[0].length;i++)
    {
        if(board[0][i].equals(Color.WHITE))
        {
            Color[][] board1 = new Color[board.length][board[0].length];
            board1 = copy(board);
            board1[getRow(board, i)][i] = spielerFarbe;
            int moval = -computerzug(board1, depth+1, (spielerFarbe.equals(Color.BLUE)?Color.RED:Color.BLUE), -beta, -alpha);
            if( moval >= beta )
                return moval;  // fail-soft beta-cutoff
            if( moval > max ) {
                max = moval;
                index = i;
                if( moval > alpha )
                    alpha = moval;
            }           
        }
    }
    if(depth==0)
    {
        col = index;
        row = getRow(this.board, index);
    }
    return max;
} 

对于初始调用,您使用非常高的 alpha 值和非常低(负)的 beta 值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多