Results 1 to 3 of 3
Thread: Array 3d problem
- 12-10-2012, 08:13 PM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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:
Yea I want 80 boards and the game would be 9x9.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; }
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:
As you can see in case that the box has something else then "" which is X or O then I put -1Java 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; } }
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:
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)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"; }
^ 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!
- 12-11-2012, 12:35 AM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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.
I like likes!.gif)
- 12-11-2012, 02:14 AM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Similar Threads
-
Array Problem
By Kendra-Sullivan in forum New To JavaReplies: 1Last Post: 11-22-2012, 05:56 AM -
[Problem] Enhanced for-loop with 2D arrays (or array in array)
By thewrongsyntax in forum New To JavaReplies: 0Last Post: 10-07-2012, 08:49 PM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Problem with Array Use
By Mike90 in forum New To JavaReplies: 1Last Post: 06-02-2010, 02:45 PM -
Array problem
By binarzt in forum New To JavaReplies: 5Last Post: 02-14-2010, 09:01 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks