void TNode::CreateMoveTree()
{
CreateNextRowOfMoves();
int numChildren = GetNumChildren();
for (int i=0; i<numChildren; i++)
{
GetChild(i)->CreateNextRowOfMoves();
int numChildren2 = GetChild(i)->GetNumChildren();
for (int j=0; j<numChildren2; j++)
{
GetChild(i)->GetChild(j)->CreateNextRowOfMoves();
int numChildren3 = GetChild(i)->GetChild(j)->GetNumChildren();
for (int k=0; k<numChildren3; k++)
{
GetChild(i)->GetChild(j)->GetChild(k)->CreateNextRowOfMoves();
}
}
}
}
which, using my tree class, creates a tree of valid moves 4 branches deep. I know there must be a iterative recursive solution that does this better, and then I'd be able to pass in a parameter for how many branches deep I want to go too. Making this go a couple of branches deeper the way it is now would be horrible both to write and to read.
Has anyone got tips on how they usually approach coming up with iterative solutions? I just can't seem to see how to approach this.
That's what I meant, damn, got the words mixed up. Thanks for the reply coder777, wouldn't that cause an endless loop though? The probelm I have really is that I'm not sure how to do it recursively because I don't know how to store how many branches have been done so far.
CreateNextRowOfMoves() creates children, the code doesnt fill an already existing tree, it takes one node and adds four more rows of moves. If CreateMoveTree() calls CreateNextRowOfMoves() then it will keep making children until it crashes.
The problem was to get a tree 4 levels deep...Tbh, I'd say that the iterative solution is better since you don't need to pass extra variables or recurse.