I'm attempting to create a 2d puzzle slider game. I created my own object called gamestate to store the parent gamestate and the new gamestate, as I plan on solving it using BFS. A sample array will look like
int[][] tArr = {{1,5,2},{3,4,0},{6,8,7}};
Which implies
[1, 5, 2, 3, 4, 0, 6, 8, 7]
To store this state I used the following for loop, which brings indexOutOfBounds exceptions
.
public class GameState {
public int[][] state; //state of the puzzle
public GameState parent; //parent in the game tree
public GameState() {
//initialize state to zeros, parent to null
state = new int[0][0];
parent = null;
}
public GameState(int[][] state) {
//initialize this.state to state, parent to null
this.state = state;
parent = null;
}
public GameState(int[][] state, GameState parent) {
//initialize this.state to state, this.parent to parent
this.state = new int[0][0];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
this.state[i][j] = state[i][j];
}
}
this.parent = parent;
}
Any ideas on how to fix this?