2014-07-05 86 views
-3

我試圖創建min-max算法的特定版本,但不幸的是它似乎並沒有像預期的那樣工作。粘貼在下面的函數應該爲tictactoe生成所有可能的結果(場景),並用非負面結果填充列表。不幸的是,它似乎並沒有工作 - 它似乎突然停止(沒有崩潰 - 它仍然有效,但沒有效果)。它甚至不會自行循環。 下面的代碼:「for」循環掛在遞歸算法

void ai::generate(scenario&situation, int who) 
{ 
    if(situation.total_moves<9) 
    { 
     if(who==-1) 
     { 
      int i; 
      for(i=0;i<9;i++) 
      { 
       scenario tmp=situation; 
       if(!tmp.current_field.game[i]) 
       { 
        tmp.ai_moves.add(i); 
        tmp.current_field.game[i]=1; 
        tmp.total_moves++; 
        if(tmp.current_field.result()==1) 
         possibilities.add(tmp); 
        if(tmp.current_field.result()!=-1) 
         generate(tmp,1); 
       } 
      } 
      return; 
     } 
     if(who==1) 
     { 
      int i; 
      for(i=0;i<9;i++) 
      { 
       scenario tmp=situation;//works fine 
       if(!situation.current_field.game[i])//it's an int 
       { 
        tmp.player_moves.add(i); 
        tmp.current_field.game[i]=2; 
        tmp.total_moves++; 
        if(tmp.current_field.result()==1) 
         possibilities.add(tmp); 
        if(tmp.current_field.result()!=-1) 
         generate(tmp,-1); 
       }//if the code following "if" gets skipped, it hangs 
      } 
      return; 
     } 
    } 
    if((!situation.current_field.result())&&(situation.total_moves==8)) 
     possibilities.add(situation); 
    return; 
} 

而且,你問之前:這裏使用的其他類的成員函數似乎很好地工作。

+0

如果它只是「突然停止」,那麼它不會掛在身上,與標題相矛盾。明確實際與預期的結果。 –

+6

注意:SO不是羣發調試服務,而是Q&A站點。如果你的問題對別人沒用,那麼它不屬於這裏;因此你應該改善你的問題,以便它對別人有用。調試它,直到你遇到一種你無法解釋的行爲,準確地詢問爲什麼會發生這種情況,而你認爲​​別的事情應該發生,我們將解釋發生了什麼。 –

+0

tmp.current_field.game [i] = 2; 這是故意分配給2的嗎? – Veritas

回答

0

下面的AI將構建一個可以讓AI獲勝的移動圖/樹(前提是您正確實現了缺少的方法,這很重要)。如果沒有任何動作離開(玩家足夠聰明,最終成爲領帶),那麼AI必須做一些隨機動作(你可以很容易地編輯,這樣人工智能至少會嘗試以平局結束,但我認爲更多有趣的AI是極好的,但會利用玩家的任何錯誤來擊敗它)。

void ai::tic_tac_toe_prediction(scenario & situation, int who_move_first){ 
    int moves_left = 8; 
    for(int i = 0; i<9 ; i++){ 

     static const int arr[] = {1,2,3,4,5,6,7,8,9}; //possible moves 
     std::vector<int> movesVec(arr, arr + sizeof(arr)/sizeof(arr[0])); 

     situation.generateRootNode(movesVec[i],who_move_first); 
     situation.setCurrentNode(situation.getRoot(i)); 

     movesVec.erase(movesVec.begin()+1); 

     if(!generate(situation,(who_move_first+1)%2,movesVec)) 
      situation.deleteRoot(i); //no winning move predicted 
    } 
} 

bool ai::generate(scenario &situation, int who, const std::vector<int> & moves){ 
    bool any_good_move = false; 
    for(int i =0; i<moves.size(); i++){ 
     std::vector<int> movesLeft = moves; 

     situation.addChildToCurrent_and_MakeChildCurrent(movesLeft[i],who); 

     movesLeft.erase(movesLeft.begin()+i); 

     if(situation.AI_wins()){ 
      situation.makeParentCurrent(); 
      any_good_move = true; 
     }else if (situation.Player_wins()){ 
      situation.deleteCurrent_and_MakeParentCurrent(); 
     }else if(generate(situation,(who+1)%2,movesLeft)){ 
      situation.makeParentCurrent(); 
      any_good_move = true; 
     }else 
      situation.deleteCurrent_and_MakeParentCurrent(); 
    } 

    return any_good_move; 
}