我正在尝试制作解决逻辑轮拼图的程序。我构造了根节点,并尝试通过进行不同的轮子移动来生成不同的子节点。问题是,当我尝试生成子节点时,根节点被覆盖,一切都搞砸了,我真的不知道为什么。 在这里你可以找到拼图logic wheels.
我将轮子表示为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表。
非常感谢。
答案 0 :(得分:1)
.clone()
复制您不想覆盖的对象。
P.S。我理解你的问题,blue = newNode.getBlue_wheel();
的修改也会对newNode的蓝色轮子进行更改,是吗?
答案 1 :(得分:0)
嗯,我只需要这样做:
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 ......