0

Hello first of all my English inst very good so excuse me when some things aren't understandable. I wrote a MinMax algorithm for tic-tac-toe and it worked really fine. So I tried myself on a MinMax algorithm for four connect sadly it doesn't work like I want. Then I found on google the Alpha beta MinMax and when I finally understood it I tried it out but it was a catastrophe ^^. Here is my MinMax what I did for 4 connect can somebody give me an advice how I can implement alpha and 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;
}   

I am working with a 2D Arrays of Color to simulate the board.

1 Answers1

0

Assuming that your code is working for minimax, the alpha beta variant should look like this (I could not test it):

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;
} 

for the initial call, you use a very high value for alpha and a very low (negative) value for beta

ZzetT
  • 568
  • 4
  • 16