0

嗨,所以我最近开始用 java 编程,我给自己设定了一个任务,为我制作的井字游戏制作 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) 通过创建一个新状态来返回该状态的所有后续状态,该新状态包含旧动作和 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:32 if (currentState.getNull() == 0)
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;
}

如果有人可以提供帮助,谢谢您:)

4

1 回答 1

0

你的问题在这里:

scoreTemp.add(evaluate(aiPlayer, minmax (!max, state)));

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

于 2016-09-03T21:58:47.263 回答