title: Return bestMove in minimax algorithm for tictactoe
body: <p>I have tried to code the minimax algorithm for tic-tac-toe given in Russel Norvig's book on Artificial Intelligence. It had everything except that the way to return the bestMove to the user. I am trying hard to return the bestMove, but cannot decide when to choose the bestMove. Help, anyone? </p>
  
  <pre><code>moveT MiniMax(stateT state)
  {
      moveT bestMove;
  
      max_move(state,bestMove);
  
      return bestMove;
  
  }
  
  int max_move(stateT state,int &amp; bestMove)
  {
      int v = -10000;
      if(GameIsOver(state))
      {
          return EvaluateStaticPosition(state);
  
      }
  
      vector&lt;moveT&gt; moveList;
      GenerateMoveList(state, moveList);
      int nMoves = moveList.size();
  
      for(int i = 0 ; i &lt; nMoves ; i++)
      {
          moveT move = moveList[i];
          MakeMove(state, move);
  
          int curValue = min_move(state,bestMove);
  
              if(curValue &gt; v)
              {
                v = curValue;
                bestMove = move;
              }
          RetractMove(state, move);
  
      }
  
      return v;
  
  }
  
  int min_move(stateT state, int &amp;bestMove)
  {
      int v = 10000;
      if(GameIsOver(state))
      {
        return EvaluateStaticPosition(state);
  
      }
      vector&lt;moveT&gt; moveList;
      GenerateMoveList(state, moveList);
  
      int nMoves = moveList.size();
  
      for(int i = 0 ; i &lt; nMoves; i++)
      {
          moveT move = moveList[i];
          MakeMove(state, move);
  
          int curValue = max_move(state,depth+1,bestMove);
  
              if(curValue &lt; v)
              {
                curValue = v;
              }
          RetractMove(state, move);
  
      }
      return v;
  }
  </code></pre>
  
  <p>P.S.: There are other pseudocode for finding the minmax value. However, they are focused on tic-tac-toe only, I am trying to extend it to other games. Thanks.</p>
  
  <p>Update : The whole code can be found here : <a href="http://ideone.com/XPswCl" rel="nofollow">http://ideone.com/XPswCl</a></p>