Filling Grid with 25 spaces randomly

Hi,

I’m trying to build one of those braingames where the player has to memorize a few circles on a Grid. Let’s say the player has to memorize 4 circles.
So i have a Grid setup with 25 positions, all these positions have an UISprite and a box collider attached to them. I have all these positions in an array which i shuffle:

for(var i = GM.Grid.Length - 1; i > 0; i--)
{
	var r = Random.Range(0, i);
	var tmp = GM.Grid[i];
	GM.Grid[i] = GM.Grid[r];
	GM.Grid[r] = tmp;
}

Next I set the circles (in this case level = 4):

for(var i = 0; i < level; i++)
 {
	 GM.setGGridItem(i);      //sets a Green Circle on the appropriate position
 } 
 yield WaitForSeconds(0.5);
GM.wipeGrid();                   //Empties the grid

Now I want the player to point out on which position the circles where briefly shown. The thing is, with my array shuffled and thus my touch positions are screwed up. For instance item01 which is Grid position 0x0 before shuffling the array is now grid position 3x4.

Can anyone help me out with this one because i can’t seem to figure it out :frowning:

I’m trying to recreate something like this:
1300050--60361--$iPhone53-169x300.png

You will have to swap the positions of the sprites when you shuffle

@Mister-E2 as you can see in the shuffle code i posted, i’m already swapping positions of the sprites.

Ok…so what does your array contain? UISprites or positions?. By swap the positions I mean the actual screen position.

GM.Grid*.position = GM.Grid[r].position*
GM.Grid[r].position = tmp.position

@Mister-E2 my array contains the UISprites at the correct position in the grid. So I swap the UISprites in the Array

Mister-E2 is right.

Basically your shuffling only changes there logical position. (Grid Array)
Now you need to synchronize there world/screen position. (Each Item stores it’s world position as well)

And when you change there logical position you also need to update there world position so that it fits with there logical position.
Please ask if you need further clarification of what I mean with world/logical.

@joshimoo

Can you give me some further clarification? I just can’t seem to wrap my head around this one…

this is how i did this in my project:

void CappedRanArray(int AmountToDraw = 20)
{

System.Random RandomObj = new System.Random (RandomSeed);

List TileList = new List(); //list all tiles as numbers
{
int n = 0;//local scope

for (int x = 0; x < GridArray.GetLength(0); x++) {
for (int y = 0; y < GridArray.GetLength(1); y++) {
TileList.Add (n);
n++;
}
}
}

List OutTileList = new List();//will contain picked tile numbers

for (int n = 0; n < AmountToDraw; n++){
int RandomNumber = (int)(RandomObj.NextDouble()*(TileList.Count-1)); //get a random number
OutTileList.Add (TileList[RandomNumber]); //add that one to the out list

TileList.RemoveAt(RandomNumber);//remove from old list to prevent duplicates
}

while (OutTileList.Count > 0)//Finnally Draw Data
{
int TileNumber = OutTileList[0];
int x = TileNumber % GridArray.GetLength(0);
int y = TileNumber / GridArray.GetLength(0);

GridArray [x,y] = 1;

OutTileList.RemoveAt(0);
}

}

so in my code GridArray is a 2d grid that has 1 for filled tiles, 0 for empty, so public int[,] GridArray = new int[GridXSize, GridYSize];

this code fills exactly the amount I tell it I use it to simulate a limited amount of board game items, i don’t know how you’re storing your items, for 2d, 2d arrays are good. So it creates a list of the total number of tiles, picks random items from the list while removing them, then it uses the result numbers at the end there, it gets x from the modulo of the result, and y from the divisor, the only confusing part (the other approach would be a list of xy coords and pick from those)

What you are doing with the following lines:

var tmp = GM.Grid[i];

    GM.Grid[i] = GM.Grid[r];

    GM.Grid[r] = tmp;

is swapping memory locations. So the position in the array ( i ) which originally pointed at the UISprite that was there is now pointing at the UISprite at the ( r ) position. You presumably already understand this as you had written the code.

This is the logical position which joshimoo mentioned. i.e. where they are positioned in the block of memory which your array takes up.

Now, say one of the UISprites you are trying to shuffle had a screen position of (200,50). That position is the sprites phyiscal position, in view of the camera. It has nothing to do with it’s logical position within the array (i.e. the index you access the UISprite from the array ).

Therefore, when you swap them with your original code, you must also swap the physical position of both.

I get a sense your misunderstanding is coming from the thinking that “swapping the entire object will swap all the variables values”. But that is not how it works. Each object in your array contains all the data about itself. This means when you swap the UISprites, they will keep all their original values, because all you are actually doing is changing where the entire object exists in memory.

Think of it like this:

2 cars:
Car1: has 2 children and 2 adults
Car2: has 1 child and 2 adults inside.
Each car is in a parking space side by side (like an Array). Now, if you “swap” there logical position, so Car1 is now in the position that Car2 was in and vice versa. You realise that their occupants have not changed, they are the same car, with the same type of people.
So if you wanted Car1 to have the same number of children as car 2 and you wanted Car2 to have the same has Car1, you can see that you will then have to swap them.

I hope that explained it well

Thx Mister-E2 I think I get it now but I still don’t really know how to code this :frowning: can you post some examples of code in JS on how to achieve this?