# Setting up an 2d grid in C#

Here is the basic situation. I want to set up a 2d grid format for a tactical RPG so I have a coordinate system. My intent is to input into a 2d 10x12 array that I have passing fine a position with in that array, for example player is located at 3,3. From there I take the movement range of the player, in this example 6 (the value will always be a number divisible by 2) and draw a diamond shape around the player based off that number.

Anyone who has played a tactical RPG is familiar with this system.

My trouble is coming in when trying to figure out the best way to build a temporary array that I am going to use to change the tiles textures to make it so the player can see this movement area.

I can set up an array just fine that has the right amount of length in the 1 position of the array.

For example:

for (int i = 0; i < moveDistance; i++)

{

tempArray*= new int[k]{?};*
}
This will obviously draw the array fine for the position.
What I need is for k to increase from 1 to my moveDistance-1 and back down to 1.
So my arrays would look something like:
tempArray =  {yPosition};
tempArray =  {(yPosition-1),yPosition,(yPosition+1)};
tempArray =  {(yPosition-2),(yPosition-1),yPosition,(yPosition+1),(yPosition+2);
tempArray =  {(yPosition-1),yPosition,(yPosition+1)};
tempArray =  {yPosition};
For further detail if our player was sitting at position 3,3 my temp array would look like:
tempArray =  {3};
tempArray =  {2,3,4};
tempArray =  {1,2,3,4,5};
tempArray =  {2,3,4};
tempArray =  {3};
I realize this will just return the Y coordinates for the movement. But it is a start for me.
Sorry for being so long winded. I can visualize and understand the logic behind how to do this I just don’t know how really to set up the conditional case where K will increase by K+2 until it reaches moveDistance-1 and then start going K-2.

You seem to already be on the right track so I’m going to assume that you don’t need that much help so instead of a complete answer I’m going to give this to you as a suggestion of where to start:

``````for (int x = 0; x <= moveDistance; x++) {
for (int y = 0; y <= moveDistance - x; y++) {
// let's assume that the function Position represents a point on your map
Position(x,y);
}
}
``````

By taking the current value of x from moveDistance, this limits the value that y can be, limiting the distance from (0, 0)

In the case that moveDistance is 2 this will return this following Positions:

(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0)

Things to consider:

• You’re going to want negative X values and negative Y values.
• You probably don’t want to list (-0, 0) and (0, 0) and (0, -0) since they are all the same thing. You can do this with an if statement.

It’s old post, but maybe it will be helpful for somebody. This code draws diamond on grid in specific position.

``````
int x = (int)position.x;
int y = (int)position.y;
int range = 5;
for( int i = 1; i <= range; i++ )
{
for( int j = 0; j < i; j++ )
{
if( j == 0 ) {
tab[ x - j, y + i - j ] = 1;
tab[ x - j, y - i + j ] = 1;
tab[ x + i - j, y + j ] = 1;
tab[ x - i + j, y + j ] = 1;
} else {
tab[ x - j, y + i - j ] = 1;
tab[ x - j, y - i + j ] = 1;
tab[ x + j, y + i - j ] = 1;
tab[ x + j, y - i + j ] = 1;
}
}
}

``````

I hope there are no bugs. If so, write it below where they are.