- Home /
As I compare 4 objects in an array?
I want to compare the objects around it: [X+1,Y][X-1,Y][X,Y-1][X,Y+1]
But the index like [X-1,Y]
of the objetcs in the edges of the matrix is out of index.
for (int i = 0; i <= array.Length; i++)
{
for (int j = 0; j <= array.Length; j++)
{
if ((array[i, j].typeOfBlock!= array[i + 1, j].typeOfBlock) &&
(array[i, j].typeOfBlock != array[i - 1, j].typeOfBlock) &&
(array[i, j].typeOfBlock != array[i, j + 1].typeOfBlock) &&
(array[i, j].typeOfBlock != array[i, j - 1].typeOfBlock)
{
array[i, j].obj.renderer.material.mainTexture =
ChooseTexture(array[i, j], 0);
}
}
}
I tried to put if(i-1 >0) but keep throwing me an error.
I did things such as:
"if ((i + 1 <= 7) && (i - 1 >= 0) && (j + 1 <= 7) && (j - 1 >= 0))"
This last one works, but I let a lot of comparisons out this way. I did other things to try control the index but without success. how should I do this correctly?
I do not know why the code is not displaying properly, so click in edit to see it
Answer by Bunny83 · Feb 20, 2013 at 02:44 PM
The easiest way is to do 4 seperate checks
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
var type = array[i, j].typeOfBlock;
if (i+1 < array.GetLength(0) && type == array[i + 1, j].typeOfBlock)
continue;
if (i-1 >= 0 && type == array[i - 1, j].typeOfBlock)
continue;
if (j+1 < array.GetLength(1) && type == array[i, j + 1].typeOfBlock)
continue;
if (j-1 >= 0 && type == array[i, j - 1].typeOfBlock)
continue;
array[i, j].obj.renderer.material.mainTexture =
ChooseTexture(array[i, j], 0);
}
}
Thank you for the help, I didn't know about this continue keyword. But now throw an null error.
is this the same thing that you have done?
var type = array[i, j].typeOfBlock;
if (i + 1 <= 7 && type != array[i + 1, j].typeOfBlock)
{
if (i - 1 >= 0 && type == array[i - 1, j].typeOfBlock)
{
if (j + 1 <= 7 && type == array[i, j + 1].typeOfBlock)
{
if (j - 1 >= 0 && type == array[i, j - 1].typeOfBlock)
{
array[i, j].obj.renderer.material.mainTexture =
ChooseTexture(array[i, j], 0);
}
}
}
}
@hherzog: No, it's not the same. In your case no border piece can be precessed successfully because if one of your if statments "detects" and edge (so it can't check the actual type) it automatically fails. In my case i only check the type if there is a tile which can be checked. If there's no tile (because the index is out of bounds) this "side" is simply ignored.
So effectively if you just imagine the very first tile. It doesn't have one above (y-1) and doesn't have one on the left(x-1), but if the right tile(x+1) and the one below(y+1) matches it will execute your code.
Btw. I just copied the code in the first place, but you should keep in $$anonymous$$d that a multidimensional array has multiple length values, one for each dimension. You shouldn't use Length for them. There's a function GetLength(dimension) which will return the length of the given dimension.
I'll edit my answer...
Just to clarify this. In my case i used "negative-logic". So my conditions test for cases when we don't want to execute the code.
You used positive-logic. So your conditions should evaluate to true when you want execute the code.
Your version should look like this:
var type = array[i, j].typeOfBlock;
if (i + 1 > 7 || type != array[i + 1, j].typeOfBlock)
{
if (i - 1 < 0 || type == array[i - 1, j].typeOfBlock)
{
if (j + 1 > 7 || type == array[i, j + 1].typeOfBlock)
{
if (j - 1 < 0 || type == array[i, j - 1].typeOfBlock)
{
array[i, j].obj.renderer.material.mainTexture =
ChooseTexture(array[i, j], 0);
}
}
}
}
This might look a bit strange, but this is actually the same as my version. Basically each if statement is responsible for checking one side. If we can't check a certain side we want to assume that the check was successful. That's why either the index is out of bounds or the checked type is correct. If the index is out of bounds the "if" is already true (because of the "or") so the second condition, the actual check, isn't evaluated and that's what we want.
In general my version (which uses "early exits") is usually the one that can be understood best. The cascaded version is also better than one gigantic if statement but the disadvantage is that due to indention it grows to the right so it get's difficult to read / needs scrolling. It's also easy to get confused where a block begins and where it ends.
As one big "if":
if ((i + 1 > 7 || type != array[i + 1, j].typeOfBlock)
&& (i - 1 < 0 || type == array[i - 1, j].typeOfBlock)
&& (j + 1 > 7 || type == array[i, j + 1].typeOfBlock)
&& (j - 1 < 0 || type == array[i, j - 1].typeOfBlock))
{
//[...]
ps I'll hope that "7" is your highest possible index and not the count. The count should be 8
That was pretty enlightening,thank you very much.
I was thinking that I would need a lot of code just because with my code I couldn't handle it the border objects, and just a few "||" it would solve the problem
Answer by flaviusxvii · Feb 20, 2013 at 02:17 PM
In the past, I've limited my loop to just the "inside elements" so they're free to compare against the "border elements". So my loops would look something like:
for(int i = 1; i < arr.Length - 1; i++) {
for(int j = 1; j < arr.Length - 1; j++) {
// Here you can safely use arr[i-1][j-1].. things like that.
}
}
foreach? foreach doesn't work well with multi-dimensional arrays. Also you need to know the current i and j value or you can't compare them. See this SO question
Your answer
Follow this Question
Related Questions
how to create array with grid layout group 1 Answer
Rotate non square Color array by 90 degrees 1 Answer
Checking to see if a loaded level is equal to a member of an integer array 1 Answer
Need help with C# code. both gameObject and col.gameObject are getting destroyed. here is the code. 2 Answers
Check if pressed key exists inside an array of KeyCodes 2 Answers