Whats wrong? Trying to build a tree
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, Childre开发者_Python百科n =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.
you need to recurse over MakeTree. Try using the yield return syntax and an IEnumerable, it will make your life easier
精彩评论