views:

50

answers:

2

Hello everyone!
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.

+1  A: 

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

splix
yes, that's my problem, I dont' worry so much about newNode, but about aNode. That's why I used newNode, but without success..Thank you for the answer, I'm going to try it immediately and post results.
gosling
Unfortunately, nothing changed. I don't get it. I've put it to print the root, then the first child(yellow left), then root again, then second child(yellow right),etc. In the third child(blue wheel left), while the parameter is the rootNode which is fine, it returns me sth as if I had used as rootNode the second children. The rootNode is printed with errors. After the fourth child root is printed again properly. I really don' t understand.
gosling
did you tried with `Node newNode = new Node(aNode.getYellow_wheel().clone(),aNode.getBlue_wheel().clone(),aNode.getGreen_wheel().clone());` ?
splix
A: 

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

gosling