Search code examples
javaartificial-intelligenceminimax

Minimax Connect 4 AI trouble


I am making connect 4 AI, except the game continues until all 42 spaces are filled.
Score is kept by every 4 in a row gets 1 point.

public int[] Max_Value(GameBoard playBoard, int depth){
    GameBoard temp = new GameBoard(playBoard.playBoard);
    int h = 0, tempH = 999, tempCol=0;
    int myDepth = depth - 1;
    int[] tempH2 = new int[2];
    boolean noChildren = true;
    if(myDepth != -1){
        for(int i = 0; i < 7; i++){
            if(temp.isValidPlay(i)){
                count++;
                temp.playPiece(i);
                noChildren = false;
                tempH2 = Min_Value(temp, myDepth);
                if(tempH2[1] < tempH){
                    tempH=tempH2[1];
                    tempCol = i;
                }
                temp.removePiece(i);
            }
        }
    }   
    int[] x = new int[2];
    if(noChildren){
        h = temp.getHeuristic();
    }
    else{
        h = tempH;
        x[0]=tempCol;
    }
    x[1]=h;
    return x; 
}

public int[] Min_Value(GameBoard playBoard, int depth){
    GameBoard temp = new GameBoard(playBoard.playBoard);
    int h = 0, tempH = -999, tempCol=0;
    int myDepth = depth - 1;
    int[] tempH2 = new int[2];
    boolean noChildren = true;
    if(myDepth != -1){
        for(int i = 0; i < 7; i++){
            if(temp.isValidPlay(i)){
                count++;
                temp.playPiece(i);
                noChildren = false;
                tempH2 = Max_Value(temp, myDepth);
                if(tempH2[1] > tempH){
                    tempH=tempH2[1];
                    tempCol = i;
                }
                temp.removePiece(i);
            }
        }
    }   
    int[] x = new int[2];
    if(noChildren){
        h = temp.getHeuristic();
    }
    else{
        h = tempH;
        x[0]=tempCol;
    }
    x[1]=h;
    return x; 
}

I feel like I just stumbled through everything, and it feels like terrible code. However, I have never attempted anything like this before, and would appreciate any input. I can't tell where I am going wrong. My evaluation function just gives 1 point for each 4 in a row it can find for any given state. The main function calls the Min_Value function to start things off with a depth of 10.

I am attempting to return the column as well as the value of the heuristic. I hope I have provided enough information. Thanks for any insight.


Solution

  • Allright, after implementing the methods not shown (like evaluation, playmove, remove etc.) I was able to debug this. Assuming that these functions are implemented in some correct way in your version, the mistake is that you never actually call the evaluation function if depth is -1:

    You have this:

    [...]if(myDepth != -1)
    {/*restofthecode*/}[...]
    

    But what you need is something like this:

    [...]if(myDepth == -1)
    {
    return temp.getHeuristic();
    }
    /*restofthecode*/
    [...]
    

    That way, whenever you reach depth -1 (a leaf in your minimax tree), the board will be evaluated and the value returned (which is excactly what you need in minimax).

    Do this modification in both parts (min and max) and everything shuold be allright. If there are other problems, feel free to ask.