开发者

java - unwanted object overwriting

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];
    开发者_JS百科    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.


.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?


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜