I've been struggling with implementing a miniMax algorithm for a Tic tac toe AI for a couple of days now. Right now, the problem I'm getting is that I get an empty board in the 'returnBoard' input when I call the minimax() function. I know that my algorithm is going through a series of boards, because I've printed out children as I've got them and I see that the computer is making moves and giving scores to boards. Any suggestions?
void board::getBestMove()
{
board returnBoard;
miniMax(INT_MIN + 1, INT_MAX -1, returnBoard);
*this = returnBoard;
}
int board::miniMax(int alpha, int beta, board & childWithMaximum)
{
if (checkDone())
return boardScore();
vector<board> children = getChildren();
for (int i = 0; i < children.size(); ++i)
{
board curr = children[i];
//children.pop_back();
board dummyBoard;
int score = curr.miniMax(alpha, beta, dummyBoard);
if (computerTurn)
{
if (beta > score)
{
beta = score;
childWithMaximum = curr;
if (alpha >= beta) break;
}
}
else
{
if (alpha < score)
{
alpha = score;
childWithMaximum = curr;
if (alpha >= beta) break;
}
}
}
return computerTurn? alpha : beta;
}
vector<board> board::getChildren()
{
vector<board> children;
for (int i = 0; i < 3; ++i)
{ for (int j = 0; j < 3; ++j)
{
if (getPosition(i, j) == '*') //move not made here
{
board moveMade(*this);
moveMade.setPosition(i, j);
children.push_back(moveMade);
}
}
}
return children;
}
Here are the entire runnable contents.
*this = returnedBoard;
This is probably your problem. You should never assign to *this
.