1. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
10

## Array 3d problem

Hello everyone,
I have an 2d array that represent a tic tac toe board.
And empty box is just "" ;
My current game board is saved in ar1 which is 2d string array.
I want to make an array of game boards which is array of 2d array = 3d array.
So I guess it would be like that:

Java Code:
```String[][][]ar3 = new String[80][9][9]; // array of game boards

for(int k=0;k<ar3.length;k++)// filling the array with the current game board
{
ar3[k] = ar1;
}```
Yea I want 80 boards and the game would be 9x9.
Till now everything is fine .. but now I would like to look on the game board(ar1) and make every possible move on the ar3.
So for every possible move I got a board on ar3.
For that I would create an array that would contain the empty indexes on the board which is every possible move on ar2:

Java Code:
```int[][]ar2 = new int[81][2]; // contains blank boxes indexes
int line = 0;

for(int k=0;k<SIZE;k++) //finds blank boxes indexes and adding to the array
for(int j=0;j<SIZE;j++,line++)
{
if(ar1[k][j].equals(""))
{
ar2[line][0] = k;
ar2[line][1] = j;
}
else
{
ar2[line][0] = -1;
ar2[line][1] = -1;
}

}```
As you can see in case that the box has something else then "" which is X or O then I put -1
This code is doing what I want but here comes the problem now I will try to generate all the possible moves which stored in ar2 in ar3:
Java Code:
```String[][][]ar3 = new String[80][9][9]; // array of game boards

for(int k=0;k<ar3.length;k++)// filling the array with the current game board
{
ar3[k] = ar1;
}
for(int k=0;k<ar3.length;k++)// making a move
{
int i1 = ar2[k][0];
int i2 = ar2[k][1];
if(!(i1 == -1 || i2 == -1))
if(num%2==0)
ar3[k][i1][i2] = "X";
else
ar3[k][i1][i2] = "O";
}```
I have no idea why instead of making a single move for each board , for each index in ar3 it's making all of the moves for all of the boards .. for example (I will demonstrate on a 3x3 board)
^ means empty
The board before looks like this:

^ ^ ^
^ X ^
^ ^ ^
but after the "move" i'm trying to make (let's say 0,0)
all of the boards looks like this:

O ^ ^
^ X ^
^ ^ ^

Instead just of the first 1... and then I'm doing the same thing with diffrent indexes for the second board (ar3[1]) but it affects all of the boards.. (ar3[0-k]) so eventually I got 80 boards which are the same.
Any one got an idea?
Why it changes all of the boards?instead just the one on the K index?

Thanks!

2. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
300
Rep Power
9

## Re: Array 3d problem

What I suspect is a common error of confusing immutable and mutable types:
ar3[k] = ar1;

You assign a field to the ar3 - take a look at your board after the change - ar1 is changed and all of the ar3 will be too because they actually reference it!
You need to copy every single cell by itself. What you want is a method:

Java Code:
```	public static void copyarray(String[][] sArrayDest, String[][] sArraySrc)
{
for (int i=0; i<sArraySrc.length; i++)
for (int t=0; t<sArraySrc[i].length; t++)
{
sArrayDest[i][t]	= sArraySrc[i][t];
}
}```
Last edited by Sierra; 12-11-2012 at 12:43 AM.

3. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
10

## Re: Array 3d problem

Thank you so much!