1 view

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 best move to the user. I am trying hard to return the best move, but cannot decide when to choose the best move. Help, anyone?

moveT MiniMax(stateT state)

{

moveT best move;

max_move(state,bestMove);

return the best move;

}

int max_move(stateT state,int & bestMove)

{

int v = -10000;

if(GameIsOver(state))

{

return EvaluateStaticPosition(state);

}

vector<moveT> moveList;

GenerateMoveList(state, moveList);

int nMoves = moveList.size();

for(int i = 0 ; i < nMoves ; i++)

{

moveT move = moveList[i];

MakeMove(state, move);

int curValue = min_move(state,bestMove);

if(curValue > v)

{

v = curValue;

bestMove = move;

}

RetractMove(state, move);

}

return v;

}

int min_move(stateT state, int &bestMove)

{

int v = 10000;

if(GameIsOver(state))

{

return EvaluateStaticPosition(state);

}

vector<moveT> moveList;

GenerateMoveList(state, moveList);

int nMoves = moveList.size();

for(int i = 0 ; i < nMoves; i++)

{

moveT move = moveList[i];

MakeMove(state, move);

int curValue = max_move(state,depth+1,bestMove);

if(curValue < v)

{

curValue = v;

}

RetractMove(state, move);

}

return v;

}

P.S.: There is another 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.

Update: The whole code can be found here: http://ideone.com/XPswCl

by (108k points)

We can take advantage of the fact that tic-tac-toe is a zero-sum game. This means that at the end of the game, the sum of the scores of the players will equal zero. For a two-player game, this means that one player's score will always be the negative of the other players. This is convenient for us, since minimizing the other player's score is then identical to maximizing one's own score. So instead of one player maximizing his score and one player minimizing the other player's score, we can just have both players attempt to maximize their own score.

Change EvaluateStaticPosition back to its original form, so that it gives a score based on how good the board state is for the current player.

int EvaluateStaticPosition(stateT state)

{

if(CheckForWin(state, state.whoseTurn))

{

return WINNING_POSITION;

}

if(CheckForWin(state, Opponent(state.whoseTurn)))

{

return LOSING_POSITION;

}

return NEUTRAL_POSITION;

}

Delete MinMove, since we only care about maximizing. Rewrite MaxMove so that it chooses the move that gives the opponent the worst possible score. The score for the best move is the negative of the other player's worst score.

int MaxMove(stateT state, moveT &bestMove)

{

if(GameIsOver(state))

{

return EvaluateStaticPosition(state);

}

vector<moveT> moveList;

GenerateMoveList(state, moveList);

int nMoves = moveList.size();

int v = -1000;

for(int i = 0 ;i<nMoves; i++)

{

moveT move = moveList[i];

MakeMove(state, move);

moveT opponentsBestMove;

int curRating = -MaxMove(state, opponentsBestMove);

if (curRating > v)

{

v = curRating;

bestMove = move;

}

RetractMove(state, move);

}

return v;

}

Since MaxMove is used for both the players, we no longer need to differentiate among players in the MiniMax function.

moveT MiniMax(stateT state)

{

moveT bestMove;

int i = 0;

i = MaxMove(state, bestMove);

cout<<"i is "<<i<<endl;

return bestMove;

}

I did intentionally put bestMove = move in the MinMove function, inside the curRating < v block. That is the logical place for it; if a move's rating is the lowest found so far, then that move is the best move. At least until another even lower rating is found.

Minimax is a decision rule used in Artificial Intelligence so if you wish to learn more about Minimax Decision Rule then visit this Artificial Intelligence Course.