java - 不需要的对象覆盖

发布于 2024-08-31 23:21:14 字数 2917 浏览 6 评论 0原文

我正在尝试编写一个程序来解决逻辑轮难题。我构建了根节点,并尝试生成通过轮子的不同移动而生成的不同子节点。问题是,当我尝试生成子节点时,根节点被覆盖,一切都一团糟,我真的不知道为什么。 在这里你可以找到这个谜题逻辑轮。

我将轮子表示为3x3 阵列。下面是实现移动的代码:

public Node turn_right(Node aNode, int which_wheel)
{
    Node newNode = new Node(aNode.getYellow_wheel(),aNode.getBlue_wheel(),aNode.getGreen_wheel());

    int[][] yellow = new int[3][3];
    int[][] blue = new int[3][3];
    int[][] green = new int[3][3];

    if(which_wheel==0) //turn yellow wheel of this node to right
    {
        yellow[1][0] = newNode.getYellow_wheel()[0][0];
        yellow[2][0] = newNode.getYellow_wheel()[1][0];
        yellow[2][1] = newNode.getYellow_wheel()[2][0];
        yellow[2][2] = newNode.getYellow_wheel()[2][1];
        yellow[1][2] = newNode.getYellow_wheel()[2][2];
        yellow[0][2] = newNode.getYellow_wheel()[1][2];
        yellow[0][1] = newNode.getYellow_wheel()[0][2];
        yellow[0][0] = newNode.getYellow_wheel()[0][1];

        blue = newNode.getBlue_wheel();
        blue[1][0] = newNode.getYellow_wheel()[1][2];
        blue[2][0] = newNode.getYellow_wheel()[2][2];

        green = newNode.getGreen_wheel();

    }
    else if(which_wheel == 1)// turn blue wheel of this node to right
    {

        blue[1][0] = newNode.getBlue_wheel()[0][0];
        blue[2][0] = newNode.getBlue_wheel()[1][0];
        blue[2][1] = newNode.getBlue_wheel()[2][0];
        blue[2][2] = newNode.getBlue_wheel()[2][1];
        blue[1][2] = newNode.getBlue_wheel()[2][2];
        blue[0][2] = newNode.getBlue_wheel()[1][2];
        blue[0][1] = newNode.getBlue_wheel()[0][2];
        blue[0][0] = newNode.getBlue_wheel()[0][1];


        yellow = newNode.getYellow_wheel();
        yellow[0][2] = newNode.getBlue_wheel()[0][0];
        yellow[1][2] = newNode.getBlue_wheel()[1][0];

        green = newNode.getGreen_wheel();
        green[1][0] = newNode.getBlue_wheel()[1][2];
        green[2][0] = newNode.getBlue_wheel()[2][2];
    }
    else if (which_wheel == 2)//turn green wheel of this node to right
    {
        green[0][0] = newNode.getGreen_wheel()[0][1];
        green[0][1] = newNode.getGreen_wheel()[0][2];
        green[0][2] = newNode.getGreen_wheel()[1][2];
        green[1][2] = newNode.getGreen_wheel()[2][2];
        green[2][2] = newNode.getGreen_wheel()[2][1];
        green[2][1] = newNode.getGreen_wheel()[2][0];
        green[2][0] = newNode.getGreen_wheel()[1][0];
        green[1][0] = newNode.getGreen_wheel()[0][0];

        yellow = newNode.getYellow_wheel();

        blue = newNode.getBlue_wheel();
        blue[0][2] = newNode.getGreen_wheel()[0][0];
        blue[1][2] = newNode.getGreen_wheel()[1][0];
    }
    newNode= new Node(yellow,blue,green);
    return newNode;
}

还有另一个函数,就像这个函数,它执行相反的操作:它将轮子向左转动。我的问题是我不希望对象的 aNode 表被覆盖。

非常感谢。

I'm trying to make a program that solves the logic wheels puzzle. I construct the root node and I try to produce the different child-nodes that are produced by making different moves of the wheels. The problem is that while I try to produce the children, the root node is overwrited,and everything is messed-up and I really don't know why.
Here you can find the puzzle logic wheels.

I represent the wheels as 3x3 arrays. Here is the code that implements the moves:

public Node turn_right(Node aNode, int which_wheel)
{
    Node newNode = new Node(aNode.getYellow_wheel(),aNode.getBlue_wheel(),aNode.getGreen_wheel());

    int[][] yellow = new int[3][3];
    int[][] blue = new int[3][3];
    int[][] green = new int[3][3];

    if(which_wheel==0) //turn yellow wheel of this node to right
    {
        yellow[1][0] = newNode.getYellow_wheel()[0][0];
        yellow[2][0] = newNode.getYellow_wheel()[1][0];
        yellow[2][1] = newNode.getYellow_wheel()[2][0];
        yellow[2][2] = newNode.getYellow_wheel()[2][1];
        yellow[1][2] = newNode.getYellow_wheel()[2][2];
        yellow[0][2] = newNode.getYellow_wheel()[1][2];
        yellow[0][1] = newNode.getYellow_wheel()[0][2];
        yellow[0][0] = newNode.getYellow_wheel()[0][1];

        blue = newNode.getBlue_wheel();
        blue[1][0] = newNode.getYellow_wheel()[1][2];
        blue[2][0] = newNode.getYellow_wheel()[2][2];

        green = newNode.getGreen_wheel();

    }
    else if(which_wheel == 1)// turn blue wheel of this node to right
    {

        blue[1][0] = newNode.getBlue_wheel()[0][0];
        blue[2][0] = newNode.getBlue_wheel()[1][0];
        blue[2][1] = newNode.getBlue_wheel()[2][0];
        blue[2][2] = newNode.getBlue_wheel()[2][1];
        blue[1][2] = newNode.getBlue_wheel()[2][2];
        blue[0][2] = newNode.getBlue_wheel()[1][2];
        blue[0][1] = newNode.getBlue_wheel()[0][2];
        blue[0][0] = newNode.getBlue_wheel()[0][1];


        yellow = newNode.getYellow_wheel();
        yellow[0][2] = newNode.getBlue_wheel()[0][0];
        yellow[1][2] = newNode.getBlue_wheel()[1][0];

        green = newNode.getGreen_wheel();
        green[1][0] = newNode.getBlue_wheel()[1][2];
        green[2][0] = newNode.getBlue_wheel()[2][2];
    }
    else if (which_wheel == 2)//turn green wheel of this node to right
    {
        green[0][0] = newNode.getGreen_wheel()[0][1];
        green[0][1] = newNode.getGreen_wheel()[0][2];
        green[0][2] = newNode.getGreen_wheel()[1][2];
        green[1][2] = newNode.getGreen_wheel()[2][2];
        green[2][2] = newNode.getGreen_wheel()[2][1];
        green[2][1] = newNode.getGreen_wheel()[2][0];
        green[2][0] = newNode.getGreen_wheel()[1][0];
        green[1][0] = newNode.getGreen_wheel()[0][0];

        yellow = newNode.getYellow_wheel();

        blue = newNode.getBlue_wheel();
        blue[0][2] = newNode.getGreen_wheel()[0][0];
        blue[1][2] = newNode.getGreen_wheel()[1][0];
    }
    newNode= new Node(yellow,blue,green);
    return newNode;
}

There is another function, like this one that does the oposite:it turns the wheels to left. My problem is that I do not want object's aNode tables to be overwritten.

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无可置疑 2024-09-07 23:21:14

.clone() 复制您不想覆盖的对象。

PS,据我了解您的问题,对 blue = newNode.getBlue_wheel(); 的修改也会对 newNode 的蓝轮进行更改,是吗?

.clone() to copy object you not want to overwrite.

p.s. as i understand your problem that modifications of blue = newNode.getBlue_wheel(); also make changes on newNode's blue wheel, is it?

爱冒险 2024-09-07 23:21:14

好吧,我只需要做这样的事情:

public Node turn_right(Node aNode, int which_wheel)
{
   Node newNode = (Node) aNode.clone();

    int[][] yellow = new int[3][3];
    int[][] blue = new int[3][3];
    int[][] green = new int[3][3];

    if(which_wheel==0) //turn yellow wheel of this node to right
    {
        yellow[1][0] = newNode.getYellow_wheel()[0][0];
        yellow[2][0] = newNode.getYellow_wheel()[1][0];
        yellow[2][1] = newNode.getYellow_wheel()[2][0];
        yellow[2][2] = newNode.getYellow_wheel()[2][1];
        yellow[1][2] = newNode.getYellow_wheel()[2][2];
        yellow[0][2] = newNode.getYellow_wheel()[1][2];
        yellow[0][1] = newNode.getYellow_wheel()[0][2];
        yellow[0][0] = newNode.getYellow_wheel()[0][1];

        blue[0][0] = newNode.getBlue_wheel()[0][0];
        blue[0][1] = newNode.getBlue_wheel()[0][1];
        blue[0][2] = newNode.getBlue_wheel()[0][2];
        blue[1][2] = newNode.getBlue_wheel()[1][2];
        blue[2][1] = newNode.getBlue_wheel()[2][1];
        blue[2][2] = newNode.getBlue_wheel()[2][2];
        blue[1][0] = newNode.getYellow_wheel()[1][2];
        blue[2][0] = newNode.getYellow_wheel()[2][2];

        green = newNode.getGreen_wheel();

    }
    else if(which_wheel == 1)// turn blue wheel of this node to right
    {

        blue[1][0] = newNode.getBlue_wheel()[0][0];
        blue[2][0] = newNode.getBlue_wheel()[1][0];
        blue[2][1] = newNode.getBlue_wheel()[2][0];
        blue[2][2] = newNode.getBlue_wheel()[2][1];
        blue[1][2] = newNode.getBlue_wheel()[2][2];
        blue[0][2] = newNode.getBlue_wheel()[1][2];
        blue[0][1] = newNode.getBlue_wheel()[0][2];
        blue[0][0] = newNode.getBlue_wheel()[0][1];


        yellow[0][0] = newNode.getYellow_wheel()[0][0];
        yellow[0][1] = newNode.getYellow_wheel()[0][1];
        yellow[1][0] = newNode.getYellow_wheel()[1][0];
        yellow[2][0] = newNode.getYellow_wheel()[2][0];
        yellow[2][1] = newNode.getYellow_wheel()[2][1];
        yellow[2][2] = newNode.getYellow_wheel()[2][2];
        yellow[0][2] = newNode.getBlue_wheel()[0][0];
        yellow[1][2] = newNode.getBlue_wheel()[1][0];

        green[0][0] = newNode.getGreen_wheel()[0][0];
        green[0][1] = newNode.getGreen_wheel()[0][1];
        green[0][2] = newNode.getGreen_wheel()[0][2];
        green[1][2] = newNode.getGreen_wheel()[1][2];
        green[2][1] = newNode.getGreen_wheel()[2][1];
        green[2][2] = newNode.getGreen_wheel()[2][2];
        green[1][0] = newNode.getBlue_wheel()[1][2];
        green[2][0] = newNode.getBlue_wheel()[2][2];
    }
    else if (which_wheel == 2)//turn green wheel of this node to right
    {
        green[0][0] = newNode.getGreen_wheel()[0][1];
        green[0][1] = newNode.getGreen_wheel()[0][2];
        green[0][2] = newNode.getGreen_wheel()[1][2];
        green[1][2] = newNode.getGreen_wheel()[2][2];
        green[2][2] = newNode.getGreen_wheel()[2][1];
        green[2][1] = newNode.getGreen_wheel()[2][0];
        green[2][0] = newNode.getGreen_wheel()[1][0];
        green[1][0] = newNode.getGreen_wheel()[0][0];

        yellow = newNode.getYellow_wheel();

         blue[0][0] = newNode.getBlue_wheel()[0][0];
        blue[0][1] = newNode.getBlue_wheel()[0][1];
        blue[1][0] = newNode.getBlue_wheel()[1][0];
        blue[2][0] = newNode.getBlue_wheel()[2][0];
        blue[2][1] = newNode.getBlue_wheel()[2][1];
        blue[2][2] = newNode.getBlue_wheel()[2][2];
        blue[0][2] = newNode.getGreen_wheel()[0][0];
        blue[1][2] = newNode.getGreen_wheel()[1][0];
    }
    newNode= new Node(yellow,blue,green);
    return newNode;
}

如果您使用 getter 函数并将结果分配给变量,然后尝试更改变量的值,它也会更改您使用其 getter 的对象的值。在我看来这很愚蠢。如果我想改变这样的值,我会使用setter......

Well I just had to make sth like this:

public Node turn_right(Node aNode, int which_wheel)
{
   Node newNode = (Node) aNode.clone();

    int[][] yellow = new int[3][3];
    int[][] blue = new int[3][3];
    int[][] green = new int[3][3];

    if(which_wheel==0) //turn yellow wheel of this node to right
    {
        yellow[1][0] = newNode.getYellow_wheel()[0][0];
        yellow[2][0] = newNode.getYellow_wheel()[1][0];
        yellow[2][1] = newNode.getYellow_wheel()[2][0];
        yellow[2][2] = newNode.getYellow_wheel()[2][1];
        yellow[1][2] = newNode.getYellow_wheel()[2][2];
        yellow[0][2] = newNode.getYellow_wheel()[1][2];
        yellow[0][1] = newNode.getYellow_wheel()[0][2];
        yellow[0][0] = newNode.getYellow_wheel()[0][1];

        blue[0][0] = newNode.getBlue_wheel()[0][0];
        blue[0][1] = newNode.getBlue_wheel()[0][1];
        blue[0][2] = newNode.getBlue_wheel()[0][2];
        blue[1][2] = newNode.getBlue_wheel()[1][2];
        blue[2][1] = newNode.getBlue_wheel()[2][1];
        blue[2][2] = newNode.getBlue_wheel()[2][2];
        blue[1][0] = newNode.getYellow_wheel()[1][2];
        blue[2][0] = newNode.getYellow_wheel()[2][2];

        green = newNode.getGreen_wheel();

    }
    else if(which_wheel == 1)// turn blue wheel of this node to right
    {

        blue[1][0] = newNode.getBlue_wheel()[0][0];
        blue[2][0] = newNode.getBlue_wheel()[1][0];
        blue[2][1] = newNode.getBlue_wheel()[2][0];
        blue[2][2] = newNode.getBlue_wheel()[2][1];
        blue[1][2] = newNode.getBlue_wheel()[2][2];
        blue[0][2] = newNode.getBlue_wheel()[1][2];
        blue[0][1] = newNode.getBlue_wheel()[0][2];
        blue[0][0] = newNode.getBlue_wheel()[0][1];


        yellow[0][0] = newNode.getYellow_wheel()[0][0];
        yellow[0][1] = newNode.getYellow_wheel()[0][1];
        yellow[1][0] = newNode.getYellow_wheel()[1][0];
        yellow[2][0] = newNode.getYellow_wheel()[2][0];
        yellow[2][1] = newNode.getYellow_wheel()[2][1];
        yellow[2][2] = newNode.getYellow_wheel()[2][2];
        yellow[0][2] = newNode.getBlue_wheel()[0][0];
        yellow[1][2] = newNode.getBlue_wheel()[1][0];

        green[0][0] = newNode.getGreen_wheel()[0][0];
        green[0][1] = newNode.getGreen_wheel()[0][1];
        green[0][2] = newNode.getGreen_wheel()[0][2];
        green[1][2] = newNode.getGreen_wheel()[1][2];
        green[2][1] = newNode.getGreen_wheel()[2][1];
        green[2][2] = newNode.getGreen_wheel()[2][2];
        green[1][0] = newNode.getBlue_wheel()[1][2];
        green[2][0] = newNode.getBlue_wheel()[2][2];
    }
    else if (which_wheel == 2)//turn green wheel of this node to right
    {
        green[0][0] = newNode.getGreen_wheel()[0][1];
        green[0][1] = newNode.getGreen_wheel()[0][2];
        green[0][2] = newNode.getGreen_wheel()[1][2];
        green[1][2] = newNode.getGreen_wheel()[2][2];
        green[2][2] = newNode.getGreen_wheel()[2][1];
        green[2][1] = newNode.getGreen_wheel()[2][0];
        green[2][0] = newNode.getGreen_wheel()[1][0];
        green[1][0] = newNode.getGreen_wheel()[0][0];

        yellow = newNode.getYellow_wheel();

         blue[0][0] = newNode.getBlue_wheel()[0][0];
        blue[0][1] = newNode.getBlue_wheel()[0][1];
        blue[1][0] = newNode.getBlue_wheel()[1][0];
        blue[2][0] = newNode.getBlue_wheel()[2][0];
        blue[2][1] = newNode.getBlue_wheel()[2][1];
        blue[2][2] = newNode.getBlue_wheel()[2][2];
        blue[0][2] = newNode.getGreen_wheel()[0][0];
        blue[1][2] = newNode.getGreen_wheel()[1][0];
    }
    newNode= new Node(yellow,blue,green);
    return newNode;
}

If you use a getter function and assign the result to a variable, and then try change the variable's value, it changes also the value of the object whose getter you used. That seems to me pretty stupid. If I wanted to change such a value, I would use a setter...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文