Alright, so i have a class that contains a 2d array. I make 2 instances of it with the same 2d array arrangement in both of them. If i change one of them the other changes as well for some reason. I can not figure out why both change. Shouldn't only the one instance change?
Here is the node class
import java.io.*;
public class GridNode
{
private int[][] state;
private GridNode firstChild;
private GridNode nextSibling;
// set state to newState and firstChild to newFirstChild and nextSibling to newNextSibling
public GridNode(int[][] newState, GridNode newFirstChild, GridNode newNextSibling)
{
state = newState;
firstChild = newFirstChild;
nextSibling = newNextSibling;
} // end GridNode constructor
// get current state
public int[][] getState()
{
return state;
} // end getState
public void setState(int[][] newState)
{
state = newState;
} // end setState
// set firstChild
public void setFirstChild(GridNode newFirstChild)
{
firstChild = newFirstChild;
} // end setFirstChild
// set nextSibling
public void setNextSibling(GridNode newNextSibling)
{
nextSibling = newNextSibling;
} // end setNextSibling
// get firstChild
public GridNode getFirstChild()
{
return firstChild;
} // end getFirstChild
// get nextSibling
public GridNode getNextSibling()
{
return nextSibling;
} // end getNextSibling
// print the current state
public void printNode()
{
for(int row = 0; row < 3; row++)
{
for(int col = 0; col < 3; col++)
System.out.print(state[row][col] + " ");
System.out.println();
} // end for
} // end printNode
// check to see if current state is equal to the goal state
public boolean isEqual(int[][] state, int[][] goalState)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (state[row][col] == goalState[row][col])
continue;
else
return false;
} // end for
} // end for
return true;
} // end isEqual
// calculates h1(n)
public int findHone(int[][] state, int[][] goalState)
{
int hOne = 0;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (state[row][col] != goalState[row][col] && state[row][col] != 0)
hOne++;
else
continue;
} // end for
} // end for
return hOne;
} // end findHone
// find f(n) = g(n) + h(n)
public int findF(int g, int h)
{
return g + h;
} // end findF
// check to see if it can move left
public boolean canMoveLeft(int[][] state)
{
int tempPos = -1;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (state[row][col] == 0)
tempPos = col;
} // end for
} // end for
if (tempPos == 0)
return false;
else
return true;
} // end canMoveLeft
// check to see if it can move up
public boolean canMoveUp(int[][] state)
{
int tempPos = -1;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (state[row][col] == 0)
tempPos = row;
} // end for
} // end for
if (tempPos == 0)
return false;
else
return true;
} // end canMoveUp
// check to see if it can move right
public boolean canMoveRight(int[][] state)
{
int tempPos = -1;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (state[row][col] == 0)
tempPos = col;
} // end for
} // end for
if (tempPos == 2)
return false;
else
return true;
} // end canMoveRight
// check to see if it can move down
public boolean canMoveDown(int[][] state)
{
int tempPos = -1;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (state[row][col] == 0)
tempPos = row;
} // end for
} // end for
if (tempPos == 2)
return false;
else
return true;
} // end canMoveDown
// move it left, the old one
public void moveLeft(int[][] state)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (state[row][col] == 0)
{
state[row][col] = state[row][col-1];
state[row][col-1] = 0;
//printNode();
return;
} // end if
} // end for
} // end for
} // end moveLeft
// move it Up
public void moveUp(int[][] state)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (state[row][col] == 0)
{
state[row][col] = state[row-1][col];
state[row-1][col] = 0;
// printNode(state);
return;
} // end if
} // end for
} // end for
} // end moveUp
// move it right
public void moveRight(int[][] state)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (state[row][col] == 0)
{
state[row][col] = state[row][col+1];
state[row][col+1] = 0;
// printNode(state);
return;
} // end if
} // end for
} // end for
} // end moveRight
// move it down
public void moveDown(int[][] state)
{
//int tempPos = -1;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (state[row][col] == 0)
{
state[row][col] = state[row+1][col];
state[row+1][col] = 0;
// printNode(state);
return;
} // end if
} // end for
} // end for
} // end moveDown
} // end GridNode
main where the 2 instances and everything is created
public class NewMain
{
//insert new value, check to see if swap is needed
/* static void realInsert(GridNode parent, GridNode newNode)
{
if (parent.getFirstChild() == null)
parent.setFirstChild(newNode);
else
realInsert(parent.getFirstChild().getNextSibling(), newNode);
if(parent.canMoveLeft(parent.getState()))
{
parent.moveLeft(parent.getState());
System.out.println("parent after move left:");
parent.printNode();
newNode.setState(parent.getState());
System.out.println();
System.out.println("newNode after setState is called");
newNode.printNode();
parent.moveRight(parent.getState());
System.out.println();
System.out.println("parent after move back happens");
parent.printNode();
System.out.println("one final print out of newNode");
newNode.printNode();
} // end if
*/
// } // end realInsert
public static void main(String[] args)
{
int[][] initialState = {{7, 6, 4},
{8, 5, 2},
{1, 0, 3}
};
GridNode parent = new GridNode(initialState, null, null);
GridNode child = new GridNode(parent.getState(), null, null);
parent.setFirstChild(child);
child.moveLeft(child.getState());
System.out.println("this is the child");
child.printNode();
System.out.println();
System.out.println("This is the parent");
parent.printNode();
System.out.println();
System.out.println("this is the initial state");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print(initialState[i][j] + " ");
}
System.out.println();
}
} // end main
} // end NewMain