I'm required to construct a three-dimensional array (a "universe"), but unlike traditional array navigation where you go to the end of the column, row, etc., increment and start at the beginning, I am required to move in reverse (like actually navigating a cube). The precise instructions:
- Move along a row (through the columns) until it comes to a side edge
- Move one layer deeper and then travel back along the row until it is back at a side edge
- Repeat these movements until the back edge is reached. Then move down to the next row.
- Move along a row (through the columns) until it comes to a side edge
- Move one layer forward and then travel back along the row until it is at a side edge
- Repeat the last 2 movements until the front edge is reached. Then move down to the next
row.
- Now repeat ALL of the above until the universe is exhausted
My code is as follows, though it may be hard to interpret. The loop structure follows the desired pattern perfectly until x=1, y=1, and z=0. What's happening here that causes this error? How can I fix it? Note: I cannot use any API classes.
public static void searchUniverse(int array[][][]){
int columnInverse=0, depthInverse=0, rowInverse=0, row=0, column=0, depth=0;
for (int x=0; x<array.length; x++){ //row 1 --> 1
for (int y=0; y<array[x].length; y++){ //depth 0 --> 1
depthInverse=array[x].length;
if (x%2==0)
depthInverse=2*y+1;
for (int z=0; z<array[y].length; z++){ //column 2 --> 0
columnInverse=array[y].length;
if (y%2==0 && x%2==0)
columnInverse=2*z+1;
System.out.println(x+" Row\t\t"+(x+1)+"\n"+y+" Depth\t\t"+(depthInverse-y)+"\n"+z+" Column\t"+(columnInverse-z)+"\n");
}
}
}
}