C++ minimax function

蹲街弑〆低调 提交于 2019-12-01 10:46:25

That sample from Wikipedia is doing NegaMax with Alpha/Beta pruning.

You may be helped by getting the naming straight:

  • The basis is MiniMax, a literal implementation would involve 2 methods that take turns (mutually recursive), 1 for each side.

  • Lazy programmers turn this into NegaMax, one method with a strategically placed - operator.

  • Alpha/Beta pruning is keeping track of a Window of best moves (over multiple depths) to detect dead branches.

Your playerTurn is used to determine whose turn it is . In NegaMax you can derive this from the depth (iterations) being odd or even. But it would be easier to use 2 parameters (myColor, otherColor) and switch them at each level.

Your miniMax() function should remember the best move it found so far. So instead of this code:


  /*So do I create a function max that returns the bigger of two doubles?*/
  result = max(result, -miniMax(temp, iterations - 1));

You should do something like this:


  /*So do I create a function max that returns the bigger of two doubles?*/
  double score = -miniMax(temp, iterations - 1);
  if (score > result)
  {
     result = score;
     bestMove = i;
  }

Of course, you need a variable "bestMove" and a way to return the best move found to the caller.

Add the playerTurn variable as an argument to miniMax, and call miniMax which the current player's move initially and recursively.

Also, opponentSide needs to be a function of playerTurn.

A good place to start with game tree searching is the chess programming wiki. For your question about the move: I think it is most common to have two max-functions. The difference between the two max functions is that one returns only the score and the other returns the score and the best move. A recursive call order would be like following:

maxWithBestMoveReturn(...) --> min(...) --> max(...) --> min(...)

There are some good papers with pseudocode for the Alpha Beta algorithm:

To your question in the comment: and math.max(alpha,score) or math.min(alpha,score) Is alpha a boolean there?!

No alpha is a window bound in a alpha beta algorithm. The alpha value gets updated with a new value. Because alpha and beta are swapped with the recursive call of the negamax-Function the alpha variable refers to the beta variable in the next recursive call.

One note to the playerTurn variable: The minimax or alpha-beta algorithm doesn't need this information. So i would give the information -- who's next --, into the Board-Structure. The functions findPossibleMoves and boardEval get all information they need from the Board-Structure.

One note to the recursive break condition: If i understand your code right, then you only have the one with iterations == o. I think this means the algorithm has reached the desired depth. But what if there are no possible moves left befor the algorithm reaches this depth. Maybe you should write following:

vector<int> moves = findPossibleMoves(...);
if (!moves.size())
    return boardEval(...);

In your pseudocode, the node variable has to contain all the information about the current board position (or whatever). This information would include whose turn it is to move.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!