Question

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.

Was it helpful?

Solution

The instructions in your while-loop never modify children, but will only stop if children.empty() is true. Therefore the loop inner is either never executed or executed infinitely.

Also here:

int score = curr.miniMax(alpha, beta, dummyBoard);

you call the function recursively with same parameters (except the third one which however is unused up to that point). Since the state of this, alpha and beta seems to be unchanged up to this point (except maybe if checkDone() or getPosition() changes it) this will also result in an infinite recursion.

Then, I simply make *this (the current board) equal to the returned board.

No, you only make other boards equal to *this. I do not see *this = anywhere in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top