Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:00:13+00:00 2026-06-15T16:00:13+00:00

I have an 2d array that represent a tic tac toe board. And empty

  • 0

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 9×9.
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 3×3 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:00:15+00:00Added an answer on June 15, 2026 at 4:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a Tic-Tac-Toe Game. I have added the 9 JButtons with
I have a simple one dimmensional array of integer values that represent a physical
In C#, I have an array of integers, that represent the primary keys of
I have a 2D array of integers that represent groupings (crystal grains) on a
I have an array of 128 booleans that represent bits. How can I convert
I have an array in PHP that is simply strings. These strings represent relationships
I have an array of latitude/longitude coordinate pairs that represent a polygon. I'm trying
I have an array of values (between -1.0 and 1.0) that represent intensity (Black
I have an array of Strings that represent Binary numbers (without leading zeroes) that
I have an array of strings in JS that represent dates on the format

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.