2014-01-14 80 views
1

一個無限循環,我目前正在實施與Alpha Beta Pruning一個MiniMax Algorithm的井字遊戲。C++算法極小爲井字板

我的算法會在一個空的板,並在端部具有板包含相同的狀態作爲當前板,與由下移動一起。然後,我只需將*this(當前主板)與返回的主板相同。

然而,出於某種原因,我的算法陷入無限循環。這是我的極大極小功能:

int board::miniMax(int alpha, int beta, board & childWithMaximum) 
{ 
    if (checkDone()) 
     return boardScore(); 

    vector<board> children = getChildren(); 
    while (!children.empty()) 
    { 
     board curr = children.back(); 

     board dummyBoard; 
     int score = curr.miniMax(alpha, beta, dummyBoard); 

     if (computerTurn && (beta > score)) { 

      beta = score; 
      childWithMaximum = *this; 
      if (alpha >= beta) 
       break; 

     } else if (alpha < score) { 

      alpha = score; 
      childWithMaximum = *this; 
      if (alpha >= beta) 
       break; 
     } 
    } 
    return computerTurn ? alpha : beta; 
} 

我已經做了一些打印語句調試,看來這getChildren()的輔助功能正在工作。我有它打印出來幾個孩子,到處都是樹中的其他董事會狀態:

vector<board> board::getChildren() 
{ 
    vector<board> children; 

    for (int i = 0; i < 3; ++i) { 
     for (int j = 0; j < 3; ++j) { 
      if (getPosition(i, j) == '*') { 
       //move not made here 

       board moveMade(*this); 
       moveMade.setPosition(i, j);    
       children.push_back(moveMade); 
      } 
     } 
    } 

    return children; 
} 

但是,我miniMax()功能不使返回板等於下一步的行動。

回答

2

while -loop中的指示信息從不修改children,但只有在children.empty()爲真時纔會停止。因此,循環內部永遠不會執行或無限執行。

另外這裏:

int score = curr.miniMax(alpha, beta, dummyBoard); 

你遞歸調用的函數相同的參數(除了第三個它不過是未使用到這一點)。由於thisalphabeta狀態似乎是不變了這一點(除非也許如果checkDone()getPosition()改變它),這也將導致無限遞歸。

然後,我只是讓* this(當前板)等於返回的板。

不,您只能讓其他board s等於*this。我在代碼中的任何地方都看不到*this =

+0

也都子女應該有computerTurn =! parent.computerTurn ..所以它是MinMax算法而不是MinMin/MaxMax。沒有? – odedsh

+0

@odedsh我不知道,因爲我真的不理解的代碼,但是... – Nabla

+0

@odesh實際上SETPOSITION()中的getChildren()需要的照顧。 – gacharya