title: C++ minimax function
body: <p>I have searched Google and Stackoverflow for this question, but I still don't understand how a minimax function works.</p>
  
  <p>I found the wikipedia entry has a pseudocode version of the function:</p>
  
  <pre><code>function integer minimax(node, depth)
      if node is a terminal node or depth &lt;= 0:
          return the heuristic value of node
      α = -∞
      for child in node:                       # evaluation is identical for both players 
          α = max(α, -minimax(child, depth-1))
      return α
  </code></pre>
  
  <p>Several other minimax functions I found with Google are basically the same thing; I'm trying to implement this in C++, and this is what I have come up with so far:</p>
  
  <pre><code>double miniMax(Board eval, int iterations)
  {
      //I evaluate the board from both players' point of view and subtract the difference
      if(iterations == 0)
          return boardEval(eval, playerNumber) - boardEval(eval, opponentSide());
  
      /*Here, playerTurn tells the findPossibleMoves function whose turn it is;
      I mean, how do you generate a list of possible moves if you don't even know
      whose turn it's supposed to be? But the problem is, I don't see where I can
      get playerTurn from, as there are only 2 parameters in all the examples of
      minimax I've seen*/
      vector&lt;int&gt; moves = eval.findPossibleMoves(playerTurn);
  
      //I'm assuming -∞ in the wikipedia article means a very low number?
      int result = -999999999;
  
      //Now I run this loop to evaluate each possible move
      /*Also, the Lua example in the wiki article has
        alpha = node.player==1 and math.max(alpha,score) or math.min(alpha,score)
        Is alpha a boolean there?!*/
      for(int i = 0; i * 2 &lt; moves.size(); i++)
      {
          //I make a copy of the board...
          Board temp = eval;
  
          /*and make the next possible move... once again playerTurn crops up, and I
          don't know where I can get that variable from*/
          temp.putPiece(moves[i * 2], moves[i * 2 + 1], playerTurn);
  
          /*So do I create a function max that returns the bigger of two doubles?*/
          result = max(result, -miniMax(temp, iterations - 1));
      }
  
      return result;
      /*So now I've returned the maximum score from all possible moves within a certain
      # of moves; so how do I know which move to make? I have the score; how do I know
      which sequence of moves that score belongs to?*/
  }
  </code></pre>
  
  <p>As you can see, I'm pretty confused about this minimax function. Please at the very least give me some hints to help me with this.</p>
  
  <p>Thanks! :)</p>