2016-09-03 100 views
0

嗨,所以我最近開始在java編程,我已經爲我的一個井字遊戲AI製作了一個任務 但是,minmax算法拋出堆棧溢出錯誤,我不能在錯誤或程序中看到問題所在。minimax algorim堆棧溢出錯誤

這裏的程序:

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

極大極小被稱爲在:

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

三江源如果有人可以幫助:)

+0

你的遞歸在哪裏?哪些行涉及到SO異常? –

+1

請注意你的'State.getNull()'和'State.getSuccessorStates(Player)'代碼' –

+0

這看起來像一個可能的地方:'scoresTemp.add(evaluate(aiPlayer,minmax(!max,state)));' –

回答

0

你的問題是在這裏:

scoresTemp.add(!評價(aiPlayer,MINMAX(最大值,狀態)));

當您調用minmax方法時,會創建一堆耗盡內存的數據(java允許使用一定數量的計算機內存)。 然後,您再次在minmax中調用minmax,使其創建更多數據,並且無限期地發生,直到沒有剩餘內存並且Java拋出StackOverflow異常。