I have an array:
private int[,] _blocks = new int[6, 4];
It represents a set of blocks which is 6 deep horizontally and 4 deep vertically. Graphically it looks like this:
I need a function that would take in a number, from 1 to 24 and return the array element that matches. So for number 14, I'd get back _blocks[1, 2];
I've created a simplistic function:
private int GetBlockByPosition(int position)
{
int count = 0;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 4; j++)
{
if (++count == position)
return _blocks[i, j];
}
}
return -1;
}
But this seems very wasteful and smells bad. Is there a more elegant and faster way?