tags:

views:

99

answers:

1

Calling the function MakeTree(4, gameboard) does not work properly, it only prints out the first validMove-Nodes. What am I doing wrong?

private Move MakeTree(int depth, Board b)
    {

        Move Tree = GenerateValidMoves(b, b.MyLocation);
        if (depth == 0) return Tree;
        foreach (TreeNode<Move> Child in Tree.Children)
        {
            Board temp = Board.ApplyMove(b, Child.Value);
            Child.Children.Add(MakeTree(depth-1, temp));
        }
        return Tree;
    }

Gives me following Output:

[S 1|1, D: 2|1 (East)] Depth=1, Children =1
[] Depth=2, Children =0
[S 1|1, D: 1|2 (South)] Depth=1, Children =1
[]Depth=2, Children =0

Displaying each generated Tree inline gives me following output:

[S 1|1, D: 2|1 (East)] Depth=1, Children =0
[S 1|1, D: 1|2 (South)] Depth=1, Children =0
[S 2|1, D: 3|1 (East)] Depth=1, Children =0
[S 2|1, D: 2|2 (South)] Depth=1, Children =0
[S 3|1, D: 4|1 (East)] Depth=1, Children =0
[S 3|1, D: 3|2 (South)] Depth=1, Children =0
[S 4|1, D: 5|1 (East)] Depth=1, Children =0
[S 4|1, D: 4|2 (South)] Depth=1, Children =0
[S 4|1, D: 5|1 (East)] Depth=1, Children =0
...

So apparently it only visits the first generated node.

A: 

you need to recurse over MakeTree. Try using the yield return syntax and an IEnumerable, it will make your life easier

Steve
Can you please elaborate a little bit?
BeatMe