【问题标题】:Recursive population of tree树的递归种群
【发布时间】:2011-12-02 19:02:49
【问题描述】:

我一直在尝试在 java 中创建和填充树,然后使用 minimax 算法为 AI 找到最佳课程。

递归函数生成树:

public void gen(Node n, int depth){ 
    if(depth == 6){
        n = new Node();  
        n.depth = height;
    }
    else{
        n = new Node();
        n.depth = depth;            
        gen(n.e1, depth+1);
        gen(n.e2, depth+1);
        gen(n.e3, depth+1);
        gen(n.p1, depth+1);
        gen(n.p2, depth+1);
         gen(n.p3, depth+1);
        }
    }

用值填充树的函数:

public void score(Node node, char a){       
    //Assigning scores to states to find utility value
    //Changing state strings to reflect current state of nodes and phase
    if(node!=null && node.depth!=6){
           if(node.depth%2==1){
            //Player's turn
            node.state = node.state.substring(0, node.depth))+a+node.state.substring((node.depth+2));           
            score(node.e1, 'a');
            score(node.e2, 'b');
            score(node.e3, 'a');
            score(node.p1, 'b');
            score(node.p2, 'a');
            score(node.p3, 'b');
            }
            else if(node.depth%2==0){
            //AI's turn
            node.state = node.state.substring(0,(node.depth+4))+a+node.state.substring((node.depth+6));
            score(node.e1, 'a');
            score(node.e2, 'b');
            score(node.e3, 'a');
            score(node.p1, 'b');
            score(node.p2, 'a');
            score(node.p3, 'b');
            }
        }       
    }

通过打印内容来测试功能是否一切正常:

public void printTree(Node node){           
        if(node!=null){
            System.out.println(node.depth + " " + node.state);
            printTree(node.e1);
            printTree(node.e2);
            printTree(node.e3);
            printTree(node.p1);
            printTree(node.p2);
            printTree(node.p3);
        }
    }

还有,节点类本身: 最后一类节点 {
公共字符串状态 = "BCXXXCXXX";

//utility value
public int score;
public int oscore;
public int utility;
public int min;
public int max;
public int depth;

Node p1;
Node p2;
Node p3;    
Node e1;
Node e2;
Node e3;

public Node()
{

}

}

我运行打印功能,它打印 1 BxXXCXXX 我期望的第一个节点。我用一个空节点调用它,深度为 1。为什么它不生成(或打印)树的其余部分,深度为 6?

虽然我认为这可能无关紧要,但这段代码最终会在安卓游戏中使用。

【问题讨论】:

  • 对于极小极大算法,您根本不应该构建树。它应该隐含在递归模式中。
  • 您应该一次生成一条路径,就像您在递归 DFS 中所做的那样。

标签: java recursion tree minimax


【解决方案1】:

Java 按值传递Node,因此您的赋值n = new Node(); 无效。你的gen 函数应该返回它创建的节点,而不是把一个节点作为参数。

public Node gen(int depth){ 
    Node n = new Node();
    if (depth == 6){
        n.depth = height;
    } else {
        n.depth = depth;            
        n.e1 = gen(depth+1);
        n.e2 = gen(depth+1);
        n.e3 = gen(depth+1);
        n.p1 = gen(depth+1);
        n.p2 = gen(depth+1);
        n.p3 = gen(depth+1);
    }
    return n;
}

【讨论】:

  • 输出确实发生了变化,但我无法确认这是否成功,因为我的打印功能现在显示一个无限循环。我是 Java 新手,所以我也做错了吗?
  • @zigzag90 这是出乎意料的。你能分享一下打印输出的前 30..40 行吗?
  • 抱歉,我使用的是旧版本的功能。这次效果很好。我现在尝试将它添加到整个项目中,谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-29
  • 2014-08-12
  • 1970-01-01
相关资源
最近更新 更多