java - unwanted object overwriting
Posted
by gosling
on Stack Overflow
See other posts from Stack Overflow
or by gosling
Published on 2010-05-06T18:44:02Z
Indexed on
2010/05/06
18:48 UTC
Read the original article
Hit count: 287
java
|overwriting
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.
© Stack Overflow or respective owner