I've got this code which is part of my first attempt at a partial minimax tree search, but when I call size() on the vector 'children_' which is a member of my 'TNode' class, it skips the line, and the line after. What could cause this to happen?
What's even stranger is that it behaves differently on different iterations, sometimes it skips the second for loop, before which 'numChildren' is initialised to garbage, and after which it is the number I expected it to be, even though it skipped the call to size()! And by this point it is useless anyway as the for loop was not entered.
Here's the code. It's my Max() function, but the Min() function has exactly the same problem.
int TNode::Max(int searchDepth)
{
if ( searchDepth == 0 || board_.GameOver() )
{
score_ = CalculateScore();
return score_;
}
// add next possible moves to tree
char nextColour = getNextColour();
for (int col=0; col<NUM_COLS; col++)
{
if ( !board_.ColumnIsFull(column_) )
AddChild(depth_+1,col, nextColour);
}
int numChildren = children_.size(); // this line...
int max = std::numeric_limits<int>::min(); // and this line are always skipped
for (int i=0; i<numChildren; i++)
{
board_.Insert(children_.at(i)->column_,nextColour);
int current = children_.at(i)->Min(searchDepth-1);
board_.UndoLastMove();
if ( current > max )
max = current;
}
score_ = max;
return max;
}