有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java四连接ai Alpha beta minmax

你好,首先我的英语很好,所以当有些事情不能理解时请原谅。我为tic-tac-toe编写了一个MinMax算法,效果非常好。所以我试着用一个最小-最大算法来计算四个连接,可惜它不能像我想要的那样工作。然后我在谷歌上找到了Alpha-beta-MinMax,当我最终理解它时,我尝试了一下,但这是一场灾难。这是我的最小值我为4 connect做了什么有人能给我一个建议如何实现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;
}   

我正在使用2D颜色阵列来模拟电路板


共 (1) 个答案

  1. # 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使用非常低的(负)值