I'm currently implementing a MiniMax Algorithm with Alpha Beta Pruning for a Tic Tac Toe game.
My algorithm takes in an empty board and, at the end has that board contain the same state as the current board, along with the next move made. Then, I simply make *this
(the current board) equal to the returned board.
However, for some reason, my algorithm is getting stuck in an infinite loop. Here's my miniMax function:
int board::miniMax(int alpha, int beta, board & childWithMaximum)
{
if (checkDone())
return boardScore();
vector<board> children = getChildren();
while (!children.empty())
{
board curr = children.back();
board dummyBoard;
int score = curr.miniMax(alpha, beta, dummyBoard);
if (computerTurn && (beta > score)) {
beta = score;
childWithMaximum = *this;
if (alpha >= beta)
break;
} else if (alpha < score) {
alpha = score;
childWithMaximum = *this;
if (alpha >= beta)
break;
}
}
return computerTurn ? alpha : beta;
}
I've done some print-statement debugging, and it appears that this getChildren()
helper function is working. I had it print out a couple of children and there were other board states in the tree:
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;
}
However, my miniMax()
function is not making the return board equal to the next move.