minimax algorim中的堆栈溢出错误

时间:2016-09-03 17:33:07

标签: java algorithm stack-overflow minimax minmax

嗨,所以我最近开始用java编程,我已经为自己设定了一个为我打造的tic tac toe游戏制作AI的任务 然而,minmax算法抛出了Stack Overflow错误,我无法在错误或程序中看到问题所在。

这是程序:

public State minmax(boolean max, State currentState)
{
    if (currentState.getNull() == 0) {
        return currentState;
    }
    else {
        State[] successorStates = currentState.getSuccessorStates(aiPlayer);

        ArrayList<Integer> scoresTemp = new ArrayList<>();

        for (State state : successorStates) {
            scoresTemp.add(evaluate(aiPlayer, minmax(!max, state)));
        }

        Integer[] scores = (Integer[]) scoresTemp.toArray();

        if (max) {
            State maxState = successorStates[0];
            int maxScore = evaluate(aiPlayer, maxState);
            for (int score : scores) {
                if (scores[0] > maxScore) {
                    maxScore = score;
                    maxState = successorStates[score];
                }
            }
            return maxState;
        }
        else
        {
            State minState = successorStates[0];
            int minScore = evaluate(aiPlayer, minState);
            for (int score : scores) {
                if (scores[0] > minScore) {
                    minScore = score;
                }
            }
            return minState;
        }
    }
}

它返回最佳移动状态。

getNull()返回可以播放的剩余空格量。

getSuccesorStates(Player)通过使新状态包含旧移动和播放器中的新移动来返回该状态的所有后续状态。

evaluate()返回值-1,0或1,具体取决于该状态下的获胜,平局或亏损。无返回0

编辑:

public int getNull()
{
    int amount = 0;

    for (int x =0; x<9; x++)
    {
        if (getAllCells()[x]==null)
        {
            amount++;
        }
    }

    return amount;
}

public State[] getSuccessorStates(Player player)
{
    State[] states = new State[getNull()];

    Player[][] stateCells = cells.clone();
    int[][] nullPositions = getNulls();

    for (int x=0; x<getNull(); x++)
    {
        stateCells[nullPositions[x][0]][nullPositions[x][1]] = player;
        states[x] = new State(player, stateCells);
        stateCells = cells.clone();
    }

    return states;
}

Caused by: java.lang.StackOverflowError
    at sample.AI.minmax(AI.java:23)
    at sample.AI.minmax(AI.java:32)
    at sample.AI.minmax(AI.java:32)
    .
    .
    .

23:if (currentState.getNull() == 0)
32:scoresTemp.add(evaluate(aiPlayer, minmax(!max, state)));

public Player[] getAllCells()
{
    Player[] cellList = new Player[9];
    for (int x = 0; x<3; x++)
    {
        for (int y = 0; y<3; y++)
        {
            cellList[y*3+x] = cells[x][y];
        }
    }
    return cellList;
}

minmax在:

中调用
public Ply getPly(State state)
{
    State bestState = minmax(true, state);
    State[] successorStates = state.getSuccessorStates(aiPlayer);
    ArrayList<State> states = new ArrayList<State>();
    for (int x=0; x<successorStates.length; x++)
    {
        states.add(successorStates[x]);
    }
    int[][] nulls = state.getNulls();

    Ply bestPly = new Ply(aiPlayer, nulls[states.indexOf(bestState)][0], nulls[states.indexOf(bestState)][1]);

    return bestPly;
}

如果有人可以提供帮助,谢谢你。)

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

scoresTemp.add(评估(aiPlayer, minmax (!max,state)));

当你调用minmax方法时,你会创建一堆耗尽内存的数据(java允许使用一定数量的计算机内存)。 然后你再次在minmax中调用minmax,使它创建更多的数据,并且这种情况无限发生,直到没有剩余内存并且Java抛出StackOverflow异常。