- Home /
how to fill elements of adjacents cells of a 4x4 table?
hi! we are making a 4x4 table and need to insert the data in its adjacent cell. we are able to get the adjacent cells but when we are putting the element on each cell, not all cells have content. there is only 1-3 cells with element. how can we solve this problem? thanks in advance :)
here is our code.
//first random position System.Random rnd = new System.Random(); int initA = rnd.Next(1, 4); int initB = rnd.Next(1, 4); Debug.Log("[" + initA + "," + initB + "]");
// getting neighbors and adding elements
int[,] adder = {{-1,-1},{-1,0},{-1,1},
{0,-1}, {0,1},
{1,-1},{1,0},{1,1}};
int neighborRow = initA, neighborCol = initB;
Debug.Log("adder Length: " + adder.Length + " tilegrid length: "+ tileGrid.Length);
for (int i = 0; i < binDigits.Length; i++)
{
int curRow = neighborRow, curCol = neighborCol;
neighborRow = neighborRow+ adder[i, 0];
neighborCol = neighborCol + adder[i, 1];
Debug.Log(i);
while (neighborRow < 0 || neighborCol < 0 || neighborRow >= 4 || neighborCol >= 4 || isFilled[neighborRow, neighborCol] != false)
{
int curRnd = Random.Range(0, 8);
neighborRow = curRow + adder[curRnd, 0];
neighborCol = curCol + adder[curRnd, 1];
}
Debug.Log("neighbors: [" + neighborRow + "," + neighborCol + "]");
isFilled[neighborRow, neighborCol] = true;
Debug.Log("isFilled? " + isFilled[neighborRow, neighborCol]);
tileGrid[neighborRow, neighborCol] = binDigits[i].ToString();
Debug.Log("Content{ " + tileGrid[neighborRow, neighborCol] + " }");
}
hard to say, whats binDigits? how many times is logging the i value? the code is hard to understand, whats that while for? can you explain what are you trying to achieve in each part?
Answer by nyxara143 · Feb 22, 2019 at 02:30 AM
binDigits is an array containing the elements to be put in the tileGrid. Thanks!
and your issue is that not all the bindigits are being put in the tileGrid?
Your answer
![](https://koobas.hobune.stream/wayback/20220612192146im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# 2D GameObject array, why are these two pieces of code not identical? 1 Answer
Using Vector2 as 2D Array Index 1 Answer
Error while spawning multiple objects 1 Answer
Problem with controlling a 2D array to display strings on GUI. 2 Answers
"Fatal error! CheckDisalowAllowcation" when making a huge 2d array 2 Answers