1
at TicTacToe.minvalue(TicTacToe.java:184)
    at TicTacToe.maxvalue(TicTacToe.java:197)
    at TicTacToe.minvalue(TicTacToe.java:184)
    at TicTacToe.maxvalue(TicTacToe.java:197)
    at TicTacToe.minvalue(TicTacToe.java:184)
    at TicTacToe.maxvalue(TicTacToe.java:197)
    at TicTacToe.minvalue(TicTacToe.java:184)
    at TicTacToe.maxvalue(TicTacToe.java:197)
    at TicTacToe.minvalue(TicTacToe.java:184)
    at TicTacToe.maxvalue(TicTacToe.java:197)
    at TicTacToe.minvalue(TicTacToe.java:184)

I cannot see the error since it's so long. Here are the functions maxValue()and minValue()

 private int minvalue(char [] brd){
    int m = (int) Integer.MAX_VALUE;
    int v;
    for(int a : actions(brd)){
        char [] nextbrd = result(brd, a, human);
        if(status(nextbrd)=='C') v = maxvalue(nextbrd); //line 184
        else v = utility(nextbrd);
        m = Math.min(m, v);

    }
    return m;
}

private int maxvalue(char [] brd){
    int m = (int) Integer.MAX_VALUE;
    int v;
    for(int a : actions(brd)){
        char [] nextbrd = result(brd, a, agent);
        if(status(nextbrd)=='C') v = minvalue(nextbrd); //line 189
        else v = utility(nextbrd);
        if(v==Math.max(m, v))   nextAction = a;
        m = Math.max(m, v);

    }
    return m;
}

utility function() just returns 1 if the winner is the agent. -1 otherwise. status returns 'C' if the game is not yet over. I dont know what's wrong.

UPDATE: I changed the m in the maxValue() function to MIN_VALUE but I still get the same error.

1 Answers1

1

Once your code reach the maxvalue method you will get an StackOverflow error as your nextAction value will never change again. The condition to change nextAction is shown as

if(v==Math.max(m, v))   nextAction = a;

But at the beginning you have initialized

int m = (int) Integer.MAX_VALUE;

So the if statement will never be true as per your code. The following correction looks the obvious approach to avoid StackOverflow error.

   private int maxvalue(char [] brd){
    int m = (int) Integer.MIN_VALUE;
    int v;
    for(int a : actions(brd)){
        char [] nextbrd = result(brd, a, agent);
        if(status(nextbrd)=='C') v = minvalue(nextbrd); //line 189
        else v = utility(nextbrd);
        if(v==Math.max(m, v))   nextAction = a;
        m = Math.max(m, v);

    }
    return m;
}
Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56