Search code examples
javatic-tac-toe

Something going wrong with 3d array


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:

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:

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:

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!


Solution

  • I'm not sure I understand the code fully, so this is a wild guess, but:

    In the first excerpt you're giving all slots in ar3 a reference to the same object, ar1. This means that if you change ar1's contents, you'll see that change across all ar3 items because all those items are references to the same object. You could check to see whether this is your problem.

    Now, as a comment, I'd advise you to start doing two things that help me personally in these cases: The first is to give your identifiers as meaningful names as possible, so much that comments aren't really needed to understand what's what. The second is to take advantage of the fact that you're writing in a language like Java and try to write your code in a more object oriented fashion. For instance, if, instead of a 3D array, you have a normal array of (say) GameBoard objects, each of which contains its 2D information, it could have a very positive effect in making any "suspicious" code patterns (that don't correspond to your actual intention, that is) more apparent.