# Chequered Grid Problem

Hey there,

I was just wandering if anyone knows of a method or work around for creating a chequered board no matter what the index is.

For instance if i create my grid at an odd index it is fine, but even is not. I need a way to ignore this and just work either way.

``````for (int x = 0; x < levelSize.x; x++)
{
for (int y = 0; y < levelSize.y; y++)
{
matIndex++;
if (matIndex > 1)
matIndex = 0;
Vector3 blockPos = new Vector3(-levelSize.x / 2 + 0.5f + x, 0, -levelSize.y / 2 + 0.5f + y);
GameObject newBlock = Instantiate(block, blockPos, Quaternion.identity) as GameObject;
newBlock.GetComponent<Renderer>().material = materials[matIndex];
Debug.Log(matIndex);
yield return new WaitForSeconds(0.1f);
}
}
``````

Now i know it works like this.

Loop index :: 0 1 2 3 4 5 6
my index :: 0 1 0 1 0 1 0

Loop index :: 7 8 9 10 11 12
my index :: 1 0 1 0 1 0

This works great 0 = white, 1 = Black

But if i go ahead and change it to this.

Loop Index :: 0 1 2 3 4 5 6 7
my index :: 0 1 0 1 0 1 0 1

Loop index :: 8 9 10 11 12 13 14 etc
my index :: 0 1 0 1 0 1 0

But as you can see this is a problem as now it is creating the same Black white pattern for the next part of the grid.

Any help here would be great, I feel like a dumbass for not being able to work this out.

Cheers

John

If you’re basing it around a full grid’s generation at a time (without gaps in it or, rather, where gaps aren’t skipped over), then a grid can be assembled with alternating values by knowing only a width ahead of time.

For ease, I’ll base it more on your current implementation:

``````for (int x = 0; x < levelSize.x; x++)
{
for (int y = 0; y < levelSize.y; y++)
{
matIndex = (x + y) % 2;
}
}
``````

Wait a second, wasn’t that really simple? Yes. Yes it was.

the modulo operator (%) will give you the remainder of a division. In other words, `12 / 10 = 1.2` works for decimals, but not for integers. For integers, `12 / 10 = 1` instead. When you have only 1, what about the remaining two? That’s where the modulo operator fills in that gap. `12 % 10 = 2` because that’s the remainder after division occurs.

How does this apply to the x and y? Well, I’m getting the modulus of 2 in order to get only 0 and 1 as values out of it. With that in mind, adding x and y together then gives you a grid:

``````10101010 -- x = 0-7, y = 3
01010101 -- x = 0-7, y = 2
10101010 -- x = 0-7, y = 1
01010101 -- x = 0-7, y = 0
``````

For reference for a linear generation, the same can be processed with a 1-dimension loop based around:

``````for(int i = 0; i < width * height; i++)
{
panelType = ((i % width) + (i / width)) % 2;
}
``````

The total size of the loop comes from width and height together, but each pass along the width is based on the width itself, without need to factor in the height for the contents of the loop.

I hope this works for you :

``````using UnityEngine;
using System.Collections;

public class GenerateCheckeredGrid : MonoBehaviour {

public GameObject objBlack;
public GameObject objWhite;
public int counter = 0;
GameObject objTemp;

void Start () {
GenGrid();

}

void GenGrid()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (counter % 2 == 0)
{
objTemp = (GameObject)Instantiate(objWhite);
Vector3 pos = new Vector3(i, j, 0);
objTemp.transform.position = pos;
}
else
{
objTemp = (GameObject)Instantiate(objBlack);
Vector3 pos = new Vector3(i, j, 0);
objTemp.transform.position = pos;
}
counter++;
}
counter--;
}
}
}
``````

I took the different GameObjects approach, you can also do the material thing  